add more configuration
This commit is contained in:
parent
72088ff6a8
commit
6268128cc9
111
src/Config.php
111
src/Config.php
@ -1,111 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior;
|
||||
|
||||
/**
|
||||
* @author David Badura <d.a.badura@gmail.com>
|
||||
*/
|
||||
class Config implements \IteratorAggregate, \Countable
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param mixed $default
|
||||
* @return array
|
||||
*/
|
||||
public function get($path, $default = null)
|
||||
{
|
||||
return array_key_exists($path, $this->config) ? $this->config[$path] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function has($key)
|
||||
{
|
||||
return array_key_exists($key, $this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function keys()
|
||||
{
|
||||
return array_keys($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \ArrayIterator
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return array
|
||||
*/
|
||||
public static function parse($string)
|
||||
{
|
||||
$config = [];
|
||||
$lines = explode("\n", $string);
|
||||
|
||||
foreach ($lines as $line) {
|
||||
|
||||
if (!trim($line)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
list($key, $value) = explode('=', $line);
|
||||
|
||||
if ($value == 'no' || $value == 'off') {
|
||||
$value = false;
|
||||
} elseif ($value == 'yes' || $value == 'on') {
|
||||
$value = true;
|
||||
}
|
||||
|
||||
$config[$key] = $value;
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return self
|
||||
*/
|
||||
public static function create($string)
|
||||
{
|
||||
return new self(self::parse(($string)));
|
||||
}
|
||||
}
|
334
src/Config/Config.php
Normal file
334
src/Config/Config.php
Normal file
@ -0,0 +1,334 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior\Config;
|
||||
|
||||
use DavidBadura\Taskwarrior\Exception\ConfigException;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
|
||||
/**
|
||||
* @author David Badura <d.a.badura@gmail.com>
|
||||
*/
|
||||
class Config implements \IteratorAggregate, \Countable
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var Uda[]
|
||||
*/
|
||||
private $udas = [];
|
||||
|
||||
/**
|
||||
* @var Context[]
|
||||
*/
|
||||
private $contexts = [];
|
||||
|
||||
/**
|
||||
* @var Report[]
|
||||
*/
|
||||
private $reports = [];
|
||||
|
||||
/**
|
||||
* @param array $config
|
||||
*/
|
||||
public function __construct(array $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
|
||||
$this->initContexts();
|
||||
$this->initReports();
|
||||
$this->initUdas();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $path
|
||||
* @param mixed $default
|
||||
* @return array
|
||||
*/
|
||||
public function get($path, $default = null)
|
||||
{
|
||||
return array_key_exists($path, $this->config) ? $this->config[$path] : $default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $key
|
||||
* @return bool
|
||||
*/
|
||||
public function has($key)
|
||||
{
|
||||
return array_key_exists($key, $this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function keys()
|
||||
{
|
||||
return array_keys($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function all()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \ArrayIterator
|
||||
*/
|
||||
public function getIterator()
|
||||
{
|
||||
return new \ArrayIterator($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function count()
|
||||
{
|
||||
return count($this->config);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Context
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function getContext($name)
|
||||
{
|
||||
if (!$this->hasContext($name)) {
|
||||
throw new ConfigException();
|
||||
}
|
||||
|
||||
return $this->contexts[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasContext($name)
|
||||
{
|
||||
return isset($this->contexts[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Context[]
|
||||
*/
|
||||
public function getContexts()
|
||||
{
|
||||
return $this->contexts;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Report
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function getReport($name)
|
||||
{
|
||||
if (!$this->hasReport($name)) {
|
||||
throw new ConfigException();
|
||||
}
|
||||
|
||||
return $this->reports[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasReport($name)
|
||||
{
|
||||
return isset($this->reports[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Report[]
|
||||
*/
|
||||
public function getReports()
|
||||
{
|
||||
return $this->reports;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return Uda
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function getUda($name)
|
||||
{
|
||||
if (!$this->hasUda($name)) {
|
||||
throw new ConfigException();
|
||||
}
|
||||
|
||||
return $this->udas[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function hasUda($name)
|
||||
{
|
||||
return isset($this->udas[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Uda[]
|
||||
*/
|
||||
public function getUdas()
|
||||
{
|
||||
return $this->udas;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function initContexts()
|
||||
{
|
||||
foreach ($this->config as $key => $value) {
|
||||
if (!preg_match('/context\.(\w+)/', $key, $matches)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$context = new Context();
|
||||
$context->name = $matches[1];
|
||||
$context->filter = $value;
|
||||
|
||||
$this->contexts[$context->name] = $context;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function initReports()
|
||||
{
|
||||
foreach ($this->config as $key => $value) {
|
||||
if (!preg_match('/report\.(\w+)\.(\w+)/', $key, $matches)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $matches[1];
|
||||
$attr = $matches[2];
|
||||
|
||||
if (!isset($this->reports[$name])) {
|
||||
$this->reports[$name] = new Report();
|
||||
$this->reports[$name]->name = $name;
|
||||
}
|
||||
|
||||
$report = $this->reports[$name];
|
||||
|
||||
switch ($attr) {
|
||||
case 'description':
|
||||
case 'filter':
|
||||
$report->$attr = $value;
|
||||
break;
|
||||
case 'columns':
|
||||
case 'labels':
|
||||
$report->$attr = explode(',', $value);
|
||||
break;
|
||||
case 'sort':
|
||||
$report->$attr = $this->parseOrder($value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
private function initUdas()
|
||||
{
|
||||
foreach ($this->config as $key => $value) {
|
||||
if (!preg_match('/uda\.(\w+)\.(\w+)/', $key, $matches)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$name = $matches[1];
|
||||
$attr = $matches[2];
|
||||
|
||||
if (!isset($this->udas[$name])) {
|
||||
$this->udas[$name] = new Uda();
|
||||
$this->udas[$name]->name = $name;
|
||||
}
|
||||
|
||||
$uda = $this->udas[$name];
|
||||
|
||||
switch ($attr) {
|
||||
case 'label':
|
||||
case 'type':
|
||||
$uda->$attr = $value;
|
||||
break;
|
||||
case 'values':
|
||||
$uda->$attr = explode(',', $value);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return array
|
||||
*/
|
||||
private function parseOrder($string)
|
||||
{
|
||||
$parts = explode(',', $string);
|
||||
$order = [];
|
||||
|
||||
foreach ($parts as $part) {
|
||||
$part = trim($part);
|
||||
|
||||
if (!$part) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$order[substr($part, 0, -1)] = substr($part, -1) == '+' ? Criteria::ASC : Criteria::DESC;
|
||||
}
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return array
|
||||
*/
|
||||
public static function parse($string)
|
||||
{
|
||||
$config = [];
|
||||
$lines = explode("\n", $string);
|
||||
|
||||
foreach ($lines as $line) {
|
||||
|
||||
if (!trim($line)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
list($key, $value) = explode('=', $line);
|
||||
|
||||
if ($value == 'no' || $value == 'off') {
|
||||
$value = false;
|
||||
} elseif ($value == 'yes' || $value == 'on') {
|
||||
$value = true;
|
||||
}
|
||||
|
||||
$config[$key] = $value;
|
||||
}
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $string
|
||||
* @return self
|
||||
*/
|
||||
public static function create($string)
|
||||
{
|
||||
return new self(self::parse(($string)));
|
||||
}
|
||||
}
|
19
src/Config/Context.php
Normal file
19
src/Config/Context.php
Normal file
@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior\Config;
|
||||
|
||||
/**
|
||||
* @author David Badura <d.a.badura@gmail.com>
|
||||
*/
|
||||
class Context
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $filter;
|
||||
}
|
39
src/Config/Report.php
Normal file
39
src/Config/Report.php
Normal file
@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior\Config;
|
||||
|
||||
/**
|
||||
* @author David Badura <d.a.badura@gmail.com>
|
||||
*/
|
||||
class Report
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $description = '';
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $filter = '';
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public $columns = [];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public $labels = [];
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public $sort = [];
|
||||
}
|
29
src/Config/Uda.php
Normal file
29
src/Config/Uda.php
Normal file
@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior\Config;
|
||||
|
||||
/**
|
||||
* @author David Badura <d.a.badura@gmail.com>
|
||||
*/
|
||||
class Uda
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
public $type;
|
||||
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
public $values;
|
||||
}
|
10
src/Exception/ConfigException.php
Normal file
10
src/Exception/ConfigException.php
Normal file
@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior\Exception;
|
||||
|
||||
/**
|
||||
* @author David Badura <d.a.badura@gmail.com>
|
||||
*/
|
||||
class ConfigException extends TaskwarriorException {
|
||||
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior;
|
||||
namespace DavidBadura\Taskwarrior\Query;
|
||||
|
||||
use DavidBadura\Taskwarrior\Task;
|
||||
use DavidBadura\Taskwarrior\TaskManager;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Criteria;
|
||||
|
@ -3,6 +3,7 @@
|
||||
namespace DavidBadura\Taskwarrior;
|
||||
|
||||
use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
|
||||
use DavidBadura\Taskwarrior\Query\QueryBuilder;
|
||||
use DavidBadura\Taskwarrior\Serializer\Handler\CarbonHandler;
|
||||
use DavidBadura\Taskwarrior\Serializer\Handler\RecurringHandler;
|
||||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
namespace DavidBadura\Taskwarrior;
|
||||
|
||||
use DavidBadura\Taskwarrior\Config\Config;
|
||||
use DavidBadura\Taskwarrior\Exception\CommandException;
|
||||
use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
|
||||
use Symfony\Component\Filesystem\Filesystem;
|
||||
|
96
tests/Config/ConfigTest.php
Normal file
96
tests/Config/ConfigTest.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace DavidBadura\Taskwarrior\Test;
|
||||
|
||||
use DavidBadura\Taskwarrior\Config\Config;
|
||||
|
||||
/**
|
||||
* @author David Badura <badura@simplethings.de>
|
||||
*/
|
||||
class ConfigTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
public function testParse()
|
||||
{
|
||||
$content = <<<CONFIG
|
||||
foo.bar=test
|
||||
baz=1
|
||||
on=on
|
||||
off=off
|
||||
yes=yes
|
||||
no=no
|
||||
CONFIG;
|
||||
|
||||
$config = Config::create($content);
|
||||
|
||||
$this->assertTrue($config->has('foo.bar'));
|
||||
$this->assertTrue($config->has('baz'));
|
||||
$this->assertFalse($config->has('bla'));
|
||||
|
||||
$this->assertEquals('test', $config->get('foo.bar'));
|
||||
$this->assertEquals(1, $config->get('baz'));
|
||||
$this->assertEquals(true, $config->get('on'));
|
||||
$this->assertEquals(false, $config->get('off'));
|
||||
$this->assertEquals(true, $config->get('yes'));
|
||||
$this->assertEquals(false, $config->get('no'));
|
||||
}
|
||||
|
||||
public function testContext()
|
||||
{
|
||||
$content = <<<CONFIG
|
||||
context.work=+work or +freelance
|
||||
CONFIG;
|
||||
|
||||
$config = Config::create($content);
|
||||
|
||||
$this->assertTrue($config->hasContext('work'));
|
||||
|
||||
$context = $config->getContext('work');
|
||||
|
||||
$this->assertEquals('work', $context->name);
|
||||
$this->assertEquals('+work or +freelance', $context->filter);
|
||||
}
|
||||
|
||||
public function testReport()
|
||||
{
|
||||
$content = <<<CONFIG
|
||||
report.active.columns=id,start,start.age
|
||||
report.active.description=Active tasks
|
||||
report.active.filter=status:pending and +ACTIVE
|
||||
report.active.labels=ID,Started,Active,Age
|
||||
report.active.sort=project+,start+
|
||||
CONFIG;
|
||||
|
||||
$config = Config::create($content);
|
||||
|
||||
$this->assertTrue($config->hasReport('active'));
|
||||
|
||||
$report = $config->getReport('active');
|
||||
|
||||
$this->assertEquals('active', $report->name);
|
||||
$this->assertEquals(['id', 'start', 'start.age'], $report->columns);
|
||||
$this->assertEquals('Active tasks', $report->description);
|
||||
$this->assertEquals('status:pending and +ACTIVE', $report->filter);
|
||||
$this->assertEquals(['ID', 'Started', 'Active', 'Age'], $report->labels);
|
||||
$this->assertEquals(['project' => 'ASC', 'start' => 'ASC'], $report->sort);
|
||||
}
|
||||
|
||||
public function testUda()
|
||||
{
|
||||
$content = <<<CONFIG
|
||||
uda.priority.label=Priority
|
||||
uda.priority.type=string
|
||||
uda.priority.values=H,M,L,
|
||||
CONFIG;
|
||||
|
||||
$config = Config::create($content);
|
||||
|
||||
$this->assertTrue($config->hasUda('priority'));
|
||||
|
||||
$uda = $config->getUda('priority');
|
||||
|
||||
$this->assertEquals('priority', $uda->name);
|
||||
$this->assertEquals('Priority', $uda->label);
|
||||
$this->assertEquals('string', $uda->type);
|
||||
$this->assertEquals(['H', 'M', 'L', ''], $uda->values);
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
|
||||
namespace DavidBadura\Taskwarrior\Test;
|
||||
|
||||
use DavidBadura\Taskwarrior\QueryBuilder;
|
||||
use DavidBadura\Taskwarrior\Query\QueryBuilder;
|
||||
|
||||
/**
|
||||
* @author David Badura <badura@simplethings.de>
|
||||
|
@ -33,7 +33,7 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
$config = $this->taskwarrior->config();
|
||||
|
||||
$this->assertInstanceOf('DavidBadura\Taskwarrior\Config', $config);
|
||||
$this->assertInstanceOf('DavidBadura\Taskwarrior\Config\Config', $config);
|
||||
$this->assertTrue($config->has('urgency.age.max'));
|
||||
$this->assertEquals('365', $config->get('urgency.age.max'));
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user