From 9885fd9c4b58f8282af6353cb3347a1c2785aeaf Mon Sep 17 00:00:00 2001 From: DavidBadura Date: Sat, 19 Sep 2015 10:45:05 +0000 Subject: [PATCH] performance optimization --- src/TaskManager.php | 74 ++++----------------------------------- src/Taskwarrior.php | 23 ++++++------ tests/TaskManagerTest.php | 5 +++ 3 files changed, 21 insertions(+), 81 deletions(-) diff --git a/src/TaskManager.php b/src/TaskManager.php index 1c5e06c..76cb8d1 100644 --- a/src/TaskManager.php +++ b/src/TaskManager.php @@ -4,7 +4,6 @@ namespace DavidBadura\Taskwarrior; use DavidBadura\Taskwarrior\Config\Context; use DavidBadura\Taskwarrior\Config\Report; -use DavidBadura\Taskwarrior\Exception\ReferenceException; use DavidBadura\Taskwarrior\Exception\TaskwarriorException; use DavidBadura\Taskwarrior\Proxy\UuidContainer; use DavidBadura\Taskwarrior\Query\QueryBuilder; @@ -16,6 +15,7 @@ use JMS\Serializer\Handler\HandlerRegistryInterface; use JMS\Serializer\JsonSerializationVisitor; use JMS\Serializer\Naming\CamelCaseNamingStrategy; use JMS\Serializer\Naming\SerializedNameAnnotationStrategy; +use JMS\Serializer\SerializationContext; use JMS\Serializer\Serializer; use JMS\Serializer\SerializerBuilder; use ProxyManager\Factory\LazyLoadingValueHolderFactory; @@ -73,11 +73,11 @@ class TaskManager throw new TaskwarriorException(implode(', ', $errors)); } - if (!$task->getUuid()) { - $this->add($task); - } else { - $this->edit($task); - } + $json = $this->serializeTask($task); + $uuid = $this->taskwarrior->import($json); + + $this->setValue($task, 'uuid', $uuid); + $this->tasks[$uuid] = $task; $this->refresh($task); } @@ -389,52 +389,6 @@ class TaskManager 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 $new @@ -478,22 +432,6 @@ class TaskManager $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 */ diff --git a/src/Taskwarrior.php b/src/Taskwarrior.php index 26e1573..5b2c974 100644 --- a/src/Taskwarrior.php +++ b/src/Taskwarrior.php @@ -5,7 +5,6 @@ namespace DavidBadura\Taskwarrior; use DavidBadura\Taskwarrior\Config\Config; use DavidBadura\Taskwarrior\Exception\CommandException; use DavidBadura\Taskwarrior\Exception\TaskwarriorException; -use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\Process\Process; use Webmozart\Assert\Assert; use Webmozart\PathUtil\Path; @@ -63,13 +62,12 @@ class Taskwarrior 'rc:' . $this->taskrc, 'rc.data.location=' . $this->taskData, 'rc.json.array=true', - 'rc.json.depends.array=false', 'rc.confirmation=no', ), $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())); } @@ -196,14 +194,7 @@ class Taskwarrior */ public function import($json) { - $fs = new Filesystem(); - - $file = tempnam(sys_get_temp_dir(), 'task') . '.json'; - $fs->dumpFile($file, $json); - - $output = $this->command('import', null, [$file]); - - $fs->remove($file); + $output = $this->command('import', null, ['-'], $json); if ($uuid = self::parseUuid($output)) { return $uuid; @@ -225,10 +216,11 @@ class Taskwarrior * @param string $command * @param string|string[] $filter * @param array $options + * @param string $input * @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]; @@ -251,6 +243,11 @@ class Taskwarrior } $process = new Process($this->createCommandLine($parts)); + + if ($input) { + $process->setInput($input); + } + $process->run(); if (!$process->isSuccessful()) { diff --git a/tests/TaskManagerTest.php b/tests/TaskManagerTest.php index 0d3d27c..a3df9e1 100644 --- a/tests/TaskManagerTest.php +++ b/tests/TaskManagerTest.php @@ -422,6 +422,11 @@ class TaskManagerTest extends \PHPUnit_Framework_TestCase { $task1 = new Task(); $task1->setDescription('foo1'); + + $this->taskManager->save($task1); + + $this->assertEquals(0, $task1->getUrgency()); + $task1->setDue($this->createDateTime('1989-01-08 11:12:13')); $this->taskManager->save($task1);