commit
8385e7e7c5
111
src/Config.php
Normal file
111
src/Config.php
Normal 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)));
|
||||
}
|
||||
}
|
@ -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
|
||||
|
41
tests/TaskwarriorTest.php
Normal file
41
tests/TaskwarriorTest.php
Normal 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'));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user