performance optimization

This commit is contained in:
DavidBadura 2015-09-19 10:45:05 +00:00
parent 8517e2dc58
commit 9885fd9c4b
3 changed files with 21 additions and 81 deletions

View File

@ -4,7 +4,6 @@ namespace DavidBadura\Taskwarrior;
use DavidBadura\Taskwarrior\Config\Context; use DavidBadura\Taskwarrior\Config\Context;
use DavidBadura\Taskwarrior\Config\Report; use DavidBadura\Taskwarrior\Config\Report;
use DavidBadura\Taskwarrior\Exception\ReferenceException;
use DavidBadura\Taskwarrior\Exception\TaskwarriorException; use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
use DavidBadura\Taskwarrior\Proxy\UuidContainer; use DavidBadura\Taskwarrior\Proxy\UuidContainer;
use DavidBadura\Taskwarrior\Query\QueryBuilder; use DavidBadura\Taskwarrior\Query\QueryBuilder;
@ -16,6 +15,7 @@ use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\JsonSerializationVisitor; use JMS\Serializer\JsonSerializationVisitor;
use JMS\Serializer\Naming\CamelCaseNamingStrategy; use JMS\Serializer\Naming\CamelCaseNamingStrategy;
use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; use JMS\Serializer\Naming\SerializedNameAnnotationStrategy;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Serializer; use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder; use JMS\Serializer\SerializerBuilder;
use ProxyManager\Factory\LazyLoadingValueHolderFactory; use ProxyManager\Factory\LazyLoadingValueHolderFactory;
@ -73,11 +73,11 @@ class TaskManager
throw new TaskwarriorException(implode(', ', $errors)); throw new TaskwarriorException(implode(', ', $errors));
} }
if (!$task->getUuid()) { $json = $this->serializeTask($task);
$this->add($task); $uuid = $this->taskwarrior->import($json);
} else {
$this->edit($task); $this->setValue($task, 'uuid', $uuid);
} $this->tasks[$uuid] = $task;
$this->refresh($task); $this->refresh($task);
} }
@ -389,52 +389,6 @@ class TaskManager
return $tasks[0]; return $tasks[0];
} }
/**
* @param Task $task
* @throws TaskwarriorException
*/
private function add(Task $task)
{
$json = $this->serializeTask($task);
$uuid = $this->taskwarrior->import($json);
$this->setValue($task, 'uuid', $uuid);
$this->tasks[$uuid] = $task;
}
/**
* @param Task $task
* @throws TaskwarriorException
*/
private function edit(Task $task)
{
$params = [
'description' => $task->getDescription(),
'project' => $task->getProject(),
'priority' => $task->getPriority(),
'tags' => $task->getTags(),
'due' => $this->transformDate($task->getDue()),
'wait' => $this->transformDate($task->getWait()),
'until' => $this->transformDate($task->getUntil())
];
if ($task->getRecurring()) {
$params['recur'] = $task->getRecurring()->getValue();
}
$params['depends'] = [];
foreach ($task->getDependencies() as $depend) {
if (!$depend->getUuid()) {
throw new ReferenceException("you can't save a task that has dependencies to tasks that have not been saved");
}
$params['depends'][] = $depend->getUuid();
}
$this->taskwarrior->modify($params, $task->getUuid());
}
/** /**
* @param Task $old * @param Task $old
* @param Task $new * @param Task $new
@ -478,22 +432,6 @@ class TaskManager
$refProp->setValue($task, $value); $refProp->setValue($task, $value);
} }
/**
* @param \DateTime $dateTime
* @return null|string
*/
private function transformDate(\DateTime $dateTime = null)
{
if (!$dateTime) {
return null;
}
$dateTime = clone $dateTime;
$dateTime->setTimezone(new \DateTimeZone('UTC'));
return $dateTime->format('Ymd\THis\Z');
}
/** /**
* @return Serializer * @return Serializer
*/ */

View File

@ -5,7 +5,6 @@ namespace DavidBadura\Taskwarrior;
use DavidBadura\Taskwarrior\Config\Config; use DavidBadura\Taskwarrior\Config\Config;
use DavidBadura\Taskwarrior\Exception\CommandException; use DavidBadura\Taskwarrior\Exception\CommandException;
use DavidBadura\Taskwarrior\Exception\TaskwarriorException; use DavidBadura\Taskwarrior\Exception\TaskwarriorException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process; use Symfony\Component\Process\Process;
use Webmozart\Assert\Assert; use Webmozart\Assert\Assert;
use Webmozart\PathUtil\Path; use Webmozart\PathUtil\Path;
@ -63,13 +62,12 @@ class Taskwarrior
'rc:' . $this->taskrc, 'rc:' . $this->taskrc,
'rc.data.location=' . $this->taskData, 'rc.data.location=' . $this->taskData,
'rc.json.array=true', 'rc.json.array=true',
'rc.json.depends.array=false',
'rc.confirmation=no', 'rc.confirmation=no',
), ),
$rcOptions $rcOptions
); );
if (version_compare($this->version(), '2.4.3') < 0) { if (version_compare($this->version(), '2.5.0.beta2') < 0) {
throw new TaskwarriorException(sprintf("Taskwarrior version %s isn't supported", $this->version())); throw new TaskwarriorException(sprintf("Taskwarrior version %s isn't supported", $this->version()));
} }
@ -196,14 +194,7 @@ class Taskwarrior
*/ */
public function import($json) public function import($json)
{ {
$fs = new Filesystem(); $output = $this->command('import', null, ['-'], $json);
$file = tempnam(sys_get_temp_dir(), 'task') . '.json';
$fs->dumpFile($file, $json);
$output = $this->command('import', null, [$file]);
$fs->remove($file);
if ($uuid = self::parseUuid($output)) { if ($uuid = self::parseUuid($output)) {
return $uuid; return $uuid;
@ -225,10 +216,11 @@ class Taskwarrior
* @param string $command * @param string $command
* @param string|string[] $filter * @param string|string[] $filter
* @param array $options * @param array $options
* @param string $input
* @return string * @return string
* @throws TaskwarriorException * @throws CommandException
*/ */
public function command($command, $filter = null, array $options = array()) public function command($command, $filter = null, array $options = array(), $input = null)
{ {
$parts = [$this->bin]; $parts = [$this->bin];
@ -251,6 +243,11 @@ class Taskwarrior
} }
$process = new Process($this->createCommandLine($parts)); $process = new Process($this->createCommandLine($parts));
if ($input) {
$process->setInput($input);
}
$process->run(); $process->run();
if (!$process->isSuccessful()) { if (!$process->isSuccessful()) {

View File

@ -422,6 +422,11 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase
{ {
$task1 = new Task(); $task1 = new Task();
$task1->setDescription('foo1'); $task1->setDescription('foo1');
$this->taskManager->save($task1);
$this->assertEquals(0, $task1->getUrgency());
$task1->setDue($this->createDateTime('1989-01-08 11:12:13')); $task1->setDue($this->createDateTime('1989-01-08 11:12:13'));
$this->taskManager->save($task1); $this->taskManager->save($task1);