Merge pull request #12 from DavidBadura/config-reports

Config
This commit is contained in:
David Badura 2015-04-22 14:55:55 +02:00
commit 8385e7e7c5
3 changed files with 170 additions and 0 deletions

111
src/Config.php Normal file
View File

@ -0,0 +1,111 @@
<?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)));
}
}

View File

@ -22,6 +22,11 @@ class Taskwarrior
*/ */
private $version; private $version;
/**
* @var Config
*/
private $config;
/** /**
* @param string $taskrc * @param string $taskrc
* @param string $taskData * @param string $taskData
@ -192,6 +197,19 @@ class Taskwarrior
return $this->version = trim($this->command('_version')); 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 * @param $params
* @return array * @return array

41
tests/TaskwarriorTest.php Normal file
View File

@ -0,0 +1,41 @@
<?php
namespace DavidBadura\Taskwarrior\Test;
use DavidBadura\Taskwarrior\Taskwarrior;
use Symfony\Component\Filesystem\Filesystem;
/**
* @author David Badura <badura@simplethings.de>
*/
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('urgency.age.max'));
$this->assertEquals('365', $config->get('urgency.age.max'));
}
}