Taskwarrior/src/Taskwarrior.php

284 lines
6.2 KiB
PHP
Raw Normal View History

2015-02-05 13:41:03 -06:00
<?php
namespace DavidBadura\Taskwarrior;
2015-04-06 16:13:02 -05:00
use DavidBadura\Taskwarrior\Exception\CommandException;
use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
2015-02-05 13:41:03 -06:00
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
);
}
/**
2015-04-03 09:11:02 -05:00
* @param string $filter
2015-02-05 16:00:30 -06:00
*/
2015-04-03 09:11:02 -05:00
public function delete($filter)
2015-02-05 16:00:30 -06:00
{
2015-04-03 09:11:02 -05:00
$this->command('delete', $filter);
2015-02-05 16:00:30 -06:00
}
2015-02-05 13:41:03 -06:00
/**
2015-04-03 09:11:02 -05:00
* @param string $filter
2015-02-05 13:41:03 -06:00
*/
2015-04-03 09:11:02 -05:00
public function done($filter)
2015-02-05 13:41:03 -06:00
{
2015-04-03 09:11:02 -05:00
$this->command('done', $filter);
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
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
*/
public function projects($filter = null)
{
$result = $this->command('_project', $filter);
2015-02-06 06:08:13 -06:00
2015-04-03 15:27:45 -05:00
return $this->parseResult($result);
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
*/
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
2015-04-03 17:23:26 -05:00
$tags = $this->parseResult($result);
return array_values(array_filter($tags, function ($value) {
return !in_array($value, ['next', 'nocal', 'nocolor', 'nonag']);
}));
2015-02-06 06:08:13 -06:00
}
2015-02-05 13:41:03 -06:00
/**
2015-04-03 15:27:45 -05:00
* @param string $json
2015-02-05 13:41:03 -06:00
* @return string
2015-04-06 16:13:02 -05:00
* @throws CommandException
2015-02-05 13:41:03 -06:00
* @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);
}
2015-04-03 15:27:45 -05:00
/*
* Local hack to allow utf8 chars
*/
$oldLocal = setlocale(LC_CTYPE, 0);
setlocale(LC_CTYPE, 'UTF8', 'en_US.UTF-8');
2015-02-05 13:41:03 -06:00
$process = $builder->getProcess();
2015-04-03 15:27:45 -05:00
setlocale(LC_CTYPE, $oldLocal);
2015-02-05 13:41:03 -06:00
$process->run();
if (!$process->isSuccessful()) {
2015-04-06 16:13:02 -05:00
throw new CommandException($process);
2015-02-05 13:41:03 -06:00
}
return $process->getOutput();
}
2015-02-06 06:08:13 -06:00
/**
* @return string
*/
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'];
}
}
2015-02-10 14:49:30 -06:00
if (array_key_exists('status', $params)) {
$options[] = 'status:' . $params['status'];
}
2015-02-06 17:22:17 -06:00
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
2015-04-03 15:27:45 -05:00
/**
* @param string $string
* @return array
*/
private function parseResult($string)
{
return array_filter(explode("\n", $string), 'strlen');
}
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
}