From 081e90ede27e05692140b230ca13493e6cce93fe Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Tue, 7 Apr 2015 21:23:48 +0000 Subject: [PATCH] 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