From 081e90ede27e05692140b230ca13493e6cce93fe Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Tue, 7 Apr 2015 21:23:48 +0000 Subject: [PATCH 1/4] add config --- src/Config.php | 81 +++++++++++++++++++++++++++++++++++++++ src/Taskwarrior.php | 18 +++++++++ tests/TaskwarriorTest.php | 41 ++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 src/Config.php create mode 100644 tests/TaskwarriorTest.php diff --git a/src/Config.php b/src/Config.php new file mode 100644 index 0000000..dbf5744 --- /dev/null +++ b/src/Config.php @@ -0,0 +1,81 @@ + + */ +class Config +{ + /** + * @var array + */ + private $config; + + /** + * @param $config + */ + public function __construct($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 toArray() + { + return $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); + + $config[$key] = $value; + } + + return $config; + } + + /** + * @param string $string + * @return self + */ + public static function create($string) + { + return new self(self::parse(($string))); + } +} \ No newline at end of file diff --git a/src/Taskwarrior.php b/src/Taskwarrior.php index f1d711b..3ae4e14 100644 --- a/src/Taskwarrior.php +++ b/src/Taskwarrior.php @@ -22,6 +22,11 @@ class Taskwarrior */ private $version; + /** + * @var Config + */ + private $config; + /** * @param string $taskrc * @param string $taskData @@ -192,6 +197,19 @@ class Taskwarrior return $this->version = trim($this->command('_version')); } + /** + * @return Config + * @throws CommandException + */ + public function config() + { + if (!$this->config) { + $this->config = Config::create($this->command('_show')); + } + + return $this->config; + } + /** * @param $params * @return array diff --git a/tests/TaskwarriorTest.php b/tests/TaskwarriorTest.php new file mode 100644 index 0000000..bff8a48 --- /dev/null +++ b/tests/TaskwarriorTest.php @@ -0,0 +1,41 @@ + + */ +class TaskwarriorTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var Taskwarrior + */ + protected $taskwarrior; + + public function setUp() + { + $this->tearDown(); + $this->taskwarrior = new Taskwarrior(__DIR__ . '/.taskrc', __DIR__ . '/.task'); + $this->taskwarrior->version(); // to initialise + } + + public function tearDown() + { + $fs = new Filesystem(); + $fs->remove(__DIR__ . '/.taskrc'); + $fs->remove(__DIR__ . '/.task'); + } + + + public function testConfig() + { + $config = $this->taskwarrior->config(); + + $this->assertInstanceOf('DavidBadura\Taskwarrior\Config', $config); + $this->assertTrue($config->has('alias._query')); + $this->assertEquals('export', $config->get('alias._query')); + } +} \ No newline at end of file From ec3976e39a1a0c50e18cae7c7d3a53315825c339 Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Tue, 7 Apr 2015 21:37:13 +0000 Subject: [PATCH 2/4] update config --- src/Config.php | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/Config.php b/src/Config.php index dbf5744..8b167fe 100644 --- a/src/Config.php +++ b/src/Config.php @@ -5,7 +5,7 @@ namespace DavidBadura\Taskwarrior; /** * @author David Badura */ -class Config +class Config implements \IteratorAggregate, \Countable { /** * @var array @@ -42,11 +42,35 @@ class Config /** * @return array */ - public function toArray() + 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 @@ -64,6 +88,12 @@ class Config list($key, $value) = explode('=', $line); + if ($value == 'no' || $value == 'off') { + $value = false; + } elseif ($value == 'yes' || $value == 'on') { + $value = true; + } + $config[$key] = $value; } From 72b047173ef225ca3b994efea32e49ee06d2492a Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Wed, 22 Apr 2015 10:11:11 +0000 Subject: [PATCH 3/4] fix array type hint --- src/Config.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Config.php b/src/Config.php index 8b167fe..5c0870e 100644 --- a/src/Config.php +++ b/src/Config.php @@ -13,9 +13,9 @@ class Config implements \IteratorAggregate, \Countable private $config; /** - * @param $config + * @param array $config */ - public function __construct($config) + public function __construct(array $config) { $this->config = $config; } From 076a160ebaaec8310f12565740a4d938cc1368d8 Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Wed, 22 Apr 2015 12:41:51 +0000 Subject: [PATCH 4/4] fix test --- tests/TaskwarriorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TaskwarriorTest.php b/tests/TaskwarriorTest.php index bff8a48..d61bb99 100644 --- a/tests/TaskwarriorTest.php +++ b/tests/TaskwarriorTest.php @@ -35,7 +35,7 @@ class TaskwarriorTest extends \PHPUnit_Framework_TestCase $config = $this->taskwarrior->config(); $this->assertInstanceOf('DavidBadura\Taskwarrior\Config', $config); - $this->assertTrue($config->has('alias._query')); - $this->assertEquals('export', $config->get('alias._query')); + $this->assertTrue($config->has('urgency.age.max')); + $this->assertEquals('365', $config->get('urgency.age.max')); } } \ No newline at end of file