2015-02-05 13:41:03 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace DavidBadura\Taskwarrior;
|
|
|
|
|
|
|
|
use JMS\Serializer\SerializerBuilder;
|
|
|
|
use Symfony\Component\Filesystem\Filesystem;
|
2015-02-05 16:00:30 -06:00
|
|
|
use Symfony\Component\Process\ProcessBuilder;
|
2015-02-05 13:41:03 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author David Badura <d.a.badura@gmail.com>
|
|
|
|
*/
|
|
|
|
class Taskwarrior
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $rcOptions;
|
|
|
|
|
2015-02-08 05:23:14 -06:00
|
|
|
/**
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $version;
|
|
|
|
|
2015-02-05 13:41:03 -06:00
|
|
|
/**
|
|
|
|
* @param string $taskrc
|
|
|
|
* @param string $taskData
|
2015-02-06 17:22:17 -06:00
|
|
|
* @param array $rcOptions
|
2015-02-05 13:41:03 -06:00
|
|
|
*/
|
|
|
|
public function __construct($taskrc = '~/.taskrc', $taskData = '~/.task', $rcOptions = [])
|
|
|
|
{
|
|
|
|
$this->rcOptions = array_merge(
|
|
|
|
array(
|
|
|
|
'rc:' . $taskrc,
|
|
|
|
'rc.data.location=' . $taskData,
|
2015-02-05 15:28:00 -06:00
|
|
|
'rc.json.array=true',
|
2015-02-05 13:41:03 -06:00
|
|
|
'rc.confirmation=no',
|
|
|
|
),
|
|
|
|
$rcOptions
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $uuid
|
2015-02-05 16:00:30 -06:00
|
|
|
*/
|
2015-02-06 05:38:54 -06:00
|
|
|
public function delete($uuid)
|
2015-02-05 16:00:30 -06:00
|
|
|
{
|
2015-02-06 05:38:54 -06:00
|
|
|
$this->command('delete', $uuid);
|
2015-02-05 16:00:30 -06:00
|
|
|
}
|
|
|
|
|
2015-02-05 13:41:03 -06:00
|
|
|
/**
|
2015-02-06 05:38:54 -06:00
|
|
|
* @param string $uuid
|
2015-02-05 13:41:03 -06:00
|
|
|
*/
|
2015-02-06 05:38:54 -06:00
|
|
|
public function done($uuid)
|
2015-02-05 13:41:03 -06:00
|
|
|
{
|
2015-02-06 05:38:54 -06:00
|
|
|
$this->command('done', $uuid);
|
2015-02-05 13:41:03 -06:00
|
|
|
}
|
|
|
|
|
2015-02-06 06:08:13 -06:00
|
|
|
/**
|
|
|
|
* @param array $params
|
2015-02-06 17:22:17 -06:00
|
|
|
* @throws TaskwarriorException
|
2015-02-06 06:08:13 -06:00
|
|
|
*/
|
2015-02-06 17:22:17 -06:00
|
|
|
public function add(array $params)
|
2015-02-06 06:08:13 -06:00
|
|
|
{
|
2015-02-08 05:23:14 -06:00
|
|
|
$this->command('add', null, $this->getOptions($params));
|
2015-02-06 17:22:17 -06:00
|
|
|
}
|
2015-02-06 11:05:29 -06:00
|
|
|
|
2015-02-06 17:22:17 -06:00
|
|
|
/**
|
|
|
|
* @param array $params
|
|
|
|
* @param string|array $filter
|
|
|
|
*/
|
|
|
|
public function modify(array $params, $filter = null)
|
|
|
|
{
|
|
|
|
$this->command('modify', $filter, $this->getOptions($params));
|
|
|
|
}
|
2015-02-06 11:31:13 -06:00
|
|
|
|
2015-02-06 17:22:17 -06:00
|
|
|
/**
|
|
|
|
* @param null $filter
|
|
|
|
* @return array
|
|
|
|
* @throws TaskwarriorException
|
|
|
|
*/
|
|
|
|
public function projects($filter = null)
|
|
|
|
{
|
|
|
|
$result = $this->command('_project', $filter);
|
2015-02-06 06:08:13 -06:00
|
|
|
|
2015-02-06 17:22:17 -06:00
|
|
|
return array_filter(explode("\n", $result), 'strlen');
|
2015-02-06 06:08:13 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-06 17:22:17 -06:00
|
|
|
* @param null $filter
|
2015-02-06 06:08:13 -06:00
|
|
|
* @return array
|
|
|
|
* @throws TaskwarriorException
|
|
|
|
*/
|
2015-02-06 17:22:17 -06:00
|
|
|
public function tags($filter = null)
|
2015-02-06 06:08:13 -06:00
|
|
|
{
|
2015-02-06 17:22:17 -06:00
|
|
|
$result = $this->command('_tags', $filter);
|
2015-02-06 06:08:13 -06:00
|
|
|
|
|
|
|
return array_filter(explode("\n", $result), 'strlen');
|
|
|
|
}
|
|
|
|
|
2015-02-05 13:41:03 -06:00
|
|
|
/**
|
2015-02-06 17:22:17 -06:00
|
|
|
* @param string $json
|
2015-02-05 13:41:03 -06:00
|
|
|
* @return string
|
|
|
|
* @throws TaskwarriorException
|
|
|
|
*/
|
2015-02-06 05:38:54 -06:00
|
|
|
public function import($json)
|
2015-02-05 13:41:03 -06:00
|
|
|
{
|
|
|
|
$fs = new Filesystem();
|
|
|
|
|
|
|
|
$file = tempnam(sys_get_temp_dir(), 'task') . '.json';
|
|
|
|
$fs->dumpFile($file, $json);
|
|
|
|
|
|
|
|
$output = $this->command('import', $file);
|
|
|
|
|
2015-02-06 06:08:13 -06:00
|
|
|
$fs->remove($file);
|
|
|
|
|
2015-02-06 17:22:17 -06:00
|
|
|
if ($uuid = self::parseUuid($output)) {
|
|
|
|
return $uuid;
|
2015-02-05 13:41:03 -06:00
|
|
|
}
|
|
|
|
|
2015-02-06 17:22:17 -06:00
|
|
|
throw new TaskwarriorException();
|
2015-02-05 13:41:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-06 03:05:01 -06:00
|
|
|
* @param string|array $filter
|
2015-02-06 05:38:54 -06:00
|
|
|
* @return string
|
2015-02-05 13:41:03 -06:00
|
|
|
*/
|
2015-02-06 05:38:54 -06:00
|
|
|
public function export($filter = null)
|
2015-02-05 13:41:03 -06:00
|
|
|
{
|
2015-02-06 05:38:54 -06:00
|
|
|
return $this->command('export', $filter);
|
2015-02-05 13:41:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-06 17:22:17 -06:00
|
|
|
* @param string $command
|
2015-02-06 03:05:01 -06:00
|
|
|
* @param string|array $filter
|
2015-02-06 17:22:17 -06:00
|
|
|
* @param array $options
|
2015-02-05 13:41:03 -06:00
|
|
|
* @return string
|
|
|
|
* @throws TaskwarriorException
|
|
|
|
*/
|
2015-02-06 05:38:54 -06:00
|
|
|
public function command($command, $filter = null, array $options = array())
|
2015-02-05 13:41:03 -06:00
|
|
|
{
|
|
|
|
$builder = $this->createProcessBuilder();
|
|
|
|
|
2015-02-06 03:05:01 -06:00
|
|
|
if (!is_array($filter)) {
|
2015-02-06 17:22:17 -06:00
|
|
|
$filter = explode(' ', $filter);
|
2015-02-06 03:05:01 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ($filter as $param) {
|
|
|
|
if (empty($param)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$builder->add($param);
|
2015-02-05 13:41:03 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
$builder->add($command);
|
|
|
|
|
|
|
|
foreach ($options as $param) {
|
|
|
|
$builder->add($param);
|
|
|
|
}
|
|
|
|
|
|
|
|
$process = $builder->getProcess();
|
|
|
|
$process->run();
|
|
|
|
|
|
|
|
if (!$process->isSuccessful()) {
|
|
|
|
throw new TaskwarriorException(
|
|
|
|
$process->getErrorOutput(),
|
|
|
|
$process->getExitCode(),
|
|
|
|
$process->getCommandLine()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $process->getOutput();
|
|
|
|
}
|
|
|
|
|
2015-02-06 06:08:13 -06:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
* @throws TaskwarriorException
|
|
|
|
*/
|
|
|
|
public function version()
|
|
|
|
{
|
2015-02-08 05:23:14 -06:00
|
|
|
if ($this->version) {
|
|
|
|
return $this->version;
|
|
|
|
}
|
|
|
|
|
2015-02-08 07:13:03 -06:00
|
|
|
return $this->version = trim($this->command('_version'));
|
2015-02-06 06:08:13 -06:00
|
|
|
}
|
|
|
|
|
2015-02-06 17:22:17 -06:00
|
|
|
/**
|
|
|
|
* @param $params
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getOptions($params)
|
|
|
|
{
|
|
|
|
$options = [];
|
|
|
|
|
|
|
|
if (array_key_exists('due', $params)) {
|
|
|
|
$options[] = 'due:' . $params['due'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('wait', $params)) {
|
|
|
|
$options[] = 'wait:' . $params['wait'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('until', $params)) {
|
|
|
|
$options[] = 'until:' . $params['until'];
|
|
|
|
}
|
|
|
|
|
2015-02-08 12:32:51 -06:00
|
|
|
if (array_key_exists('recur', $params)) {
|
|
|
|
$options[] = 'recur:' . $params['recur'];
|
|
|
|
}
|
|
|
|
|
2015-02-06 17:22:17 -06:00
|
|
|
if (array_key_exists('project', $params)) {
|
|
|
|
$options[] = 'project:' . $params['project'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('priority', $params)) {
|
|
|
|
$options[] = 'priority:' . $params['priority'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('tags', $params)) {
|
|
|
|
if (is_array($params['tags'])) {
|
|
|
|
$options[] = 'tags:' . implode(',', $params['tags']);
|
|
|
|
} else {
|
|
|
|
$options[] = 'tags:' . $params['tags'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('description', $params)) {
|
|
|
|
$options[] = $params['description'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2015-02-05 13:41:03 -06:00
|
|
|
/**
|
|
|
|
* @return ProcessBuilder
|
|
|
|
*/
|
|
|
|
private function createProcessBuilder()
|
|
|
|
{
|
|
|
|
$builder = new ProcessBuilder();
|
|
|
|
|
|
|
|
foreach ($this->rcOptions as $option) {
|
|
|
|
$builder->add($option);
|
|
|
|
}
|
|
|
|
|
|
|
|
$builder->setPrefix('task');
|
|
|
|
$builder->setTimeout(360);
|
|
|
|
|
|
|
|
return $builder;
|
|
|
|
}
|
2015-02-06 17:22:17 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $string
|
|
|
|
* @return string|null
|
|
|
|
*/
|
|
|
|
public static function parseUuid($string)
|
|
|
|
{
|
|
|
|
if (preg_match('/([0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12})/', $string, $matches)) {
|
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
2015-02-05 13:41:03 -06:00
|
|
|
}
|