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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $taskrc
|
|
|
|
* @param string $taskData
|
2015-02-06 03:05:01 -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
|
|
|
|
* @param string|array $filter
|
|
|
|
*/
|
|
|
|
public function modify(array $params, $filter = null)
|
|
|
|
{
|
|
|
|
$options = [];
|
|
|
|
|
|
|
|
if (array_key_exists('due', $params)) {
|
|
|
|
$options[] = 'due:' . $params['due'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('project', $params)) {
|
|
|
|
$options[] = 'project:' . $params['project'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (array_key_exists('description', $params)) {
|
|
|
|
$options[] = $params['description'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->command('modify', $filter, $options);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
* @throws TaskwarriorException
|
|
|
|
*/
|
|
|
|
public function projects()
|
|
|
|
{
|
|
|
|
$result = $this->command('_project');
|
|
|
|
|
|
|
|
return array_filter(explode("\n", $result), 'strlen');
|
|
|
|
}
|
|
|
|
|
2015-02-05 13:41:03 -06:00
|
|
|
/**
|
|
|
|
* @param $json
|
|
|
|
* @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-05 13:41:03 -06:00
|
|
|
if (!preg_match('/([0-9a-f]{8}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{4}\-[0-9a-f]{12})/', $output, $matches)) {
|
|
|
|
throw new TaskwarriorException();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $matches[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
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 03:05:01 -06:00
|
|
|
* @param string $command
|
|
|
|
* @param string|array $filter
|
|
|
|
* @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)) {
|
|
|
|
$filter = [$filter];
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
return $this->command('_version');
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|