refactor: rename Taskwarrior folder TW
This commit is contained in:
11
src/TW/Commands/IStrategy.php
Normal file
11
src/TW/Commands/IStrategy.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
namespace Aerex\TaskwarriorPlugin\Taskwarrior\Commands;
|
||||
|
||||
use Aerex\TaskwarriorPlugin\Taskwarrior\Task;
|
||||
|
||||
interface IStrategy {
|
||||
public function add(Task $task);
|
||||
public function count(Task $task);
|
||||
}
|
||||
|
||||
?>
|
||||
115
src/TW/Commands/TodoStrategy.php
Normal file
115
src/TW/Commands/TodoStrategy.php
Normal file
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Aerex\TaskwarriorPlugin\Taskwarrior\Commands;
|
||||
use Aerex\TaskwarriorPlugin\Taskwarrior\Task;
|
||||
use Aerex\TaskwarriorPlugin\Config;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Symfony\Component\Process\Exception\ProcessFailedException;
|
||||
use Aerex\TaskwarriorPlugin\Exceptions\TaskwarriorCommandException;
|
||||
|
||||
|
||||
|
||||
class TodoStrategy implements IStrategy {
|
||||
private $config;
|
||||
|
||||
public function __construct(Config $config){
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function count($uuid){
|
||||
$cmd[] = $this->config->getTaskBin();
|
||||
$cmd[] = sprintf('%s count', $uuid);
|
||||
return $this->executeCommand($cmd);
|
||||
}
|
||||
|
||||
public function modify($task){
|
||||
$cmd[] = $this->config->getTaskBin();
|
||||
|
||||
$uuid = $task->getUuid();
|
||||
$taskJson = $task->convertToArray();
|
||||
|
||||
// Append modify command
|
||||
$cmd[] = sprintf(' %s modify ', $uuid);
|
||||
|
||||
$document = $task->getDescription();
|
||||
|
||||
// Append as first modifier description if set
|
||||
|
||||
if(isset($document)){
|
||||
$cmd[] = $document;
|
||||
}
|
||||
|
||||
// Append on modifiers
|
||||
foreach($taskJson as $prop => $value){
|
||||
if(isset($value) && $value != null){
|
||||
$cmd[] = sprintf(' %s: %s ', $prop, $value);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->executeCommand($cmd);
|
||||
|
||||
}
|
||||
|
||||
public function add(Task $task){
|
||||
$cmd[] = $this->config->getTaskBin();
|
||||
$cmd[] = 'add';
|
||||
|
||||
if($task->getDescription() != null){
|
||||
$cmd[] = sprintf('"%s"', (string)$task->getDescription());
|
||||
}
|
||||
|
||||
if($task->getCategories() != null){
|
||||
$categories = implode(' +', (string)$task->getCategories());
|
||||
$cmd[] = $categories;
|
||||
}
|
||||
|
||||
if($task->getDue() != null){
|
||||
$cmd[] = sprintf("due:%s",$task->getDue('Y-m-dTH:i:s'));
|
||||
}
|
||||
|
||||
if($task->getRecurrence() != null){
|
||||
$rrule = $task->getRecurrence()->getParts();
|
||||
$cmd[] = sprintf('recur:%s', $rrule['FREQ']);
|
||||
if(isset($rrule['UNTIL'])){
|
||||
$cmd[] = sprintf('until:%s', $rrule['UNTIL']);
|
||||
}
|
||||
}
|
||||
|
||||
if($task->getStatus() != null){
|
||||
$cmd[] = sprintf('status:%s', (string)$task->getStatus());
|
||||
}
|
||||
|
||||
return $this->executeCommand($cmd);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
private function executeCommand($command){
|
||||
$rcOptions = $this->config->getOptions();
|
||||
|
||||
foreach ($rcOptions as $rcOption) {
|
||||
$command[] = $rcOption;
|
||||
}
|
||||
|
||||
$cmdString = implode(' ', $command);
|
||||
echo $cmdString;
|
||||
$process = new Process($cmdString);
|
||||
|
||||
try {
|
||||
$process->mustRun();
|
||||
// clear cmd queue
|
||||
$this->cmd = [];
|
||||
return $process->getOutput();
|
||||
} catch(ProcessFailedException $error){
|
||||
throw new TaskwarriorCommandException($error->getMesage());
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
302
src/TW/Task.php
Normal file
302
src/TW/Task.php
Normal file
@@ -0,0 +1,302 @@
|
||||
<?php
|
||||
|
||||
namespace Aerex\TaskwarriorPlugin\TW;
|
||||
use Zend\Validator\Uuid;
|
||||
use Carbon\Carbon;
|
||||
use Sabre\VObject\Component\VTodo;
|
||||
|
||||
/**
|
||||
* Task
|
||||
* @author Aerex
|
||||
*/
|
||||
class Task {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @JMS\Type("string")
|
||||
*/
|
||||
private $uuid;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
* @JMS\Type("string")
|
||||
*/
|
||||
private $description;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
*/
|
||||
private $priority;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
*/
|
||||
private $project;
|
||||
|
||||
/**
|
||||
* @var Carbon
|
||||
*
|
||||
*/
|
||||
private $due;
|
||||
|
||||
/**
|
||||
* @var Carbon
|
||||
*
|
||||
*/
|
||||
private $wait;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*
|
||||
*/
|
||||
private $tags;
|
||||
|
||||
/**
|
||||
* @var float
|
||||
*
|
||||
*/
|
||||
private $urgency;
|
||||
|
||||
/**
|
||||
* @var Carbon
|
||||
*
|
||||
*/
|
||||
private $entry;
|
||||
|
||||
/**
|
||||
* @var Carbon
|
||||
*
|
||||
*/
|
||||
private $start;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
*/
|
||||
private $recur;
|
||||
|
||||
/**
|
||||
* @var Carbon
|
||||
*
|
||||
*/
|
||||
private $until;
|
||||
|
||||
/**
|
||||
* @var Annotation[]
|
||||
*
|
||||
*/
|
||||
private $annotations = [];
|
||||
|
||||
/**
|
||||
* @var Carbon
|
||||
*
|
||||
*/
|
||||
private $modified;
|
||||
|
||||
/**
|
||||
* @var Carbon
|
||||
*
|
||||
*/
|
||||
private $end;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*
|
||||
*/
|
||||
private $status;
|
||||
|
||||
/**
|
||||
* @var Task[]|ArrayCollection
|
||||
*
|
||||
*/
|
||||
private $depends;
|
||||
|
||||
|
||||
public function __construct($UUID=null){
|
||||
$validator = new Uuid();
|
||||
|
||||
if(!isset($UUID)){
|
||||
$this->uuid = uniqid();
|
||||
} else if(isset($UUID) && !$validator->isValid($UUID)){
|
||||
throw new Exception(sprintf('%s is not a valid uuid', $UUID));
|
||||
}
|
||||
|
||||
$this->uuid = $UUID;
|
||||
|
||||
|
||||
}
|
||||
|
||||
public function getUuid(){
|
||||
return $this->uuid;
|
||||
|
||||
}
|
||||
|
||||
public function convertToArray(){
|
||||
$tagsString = null;
|
||||
$dependsString = null;
|
||||
$dueString = null;
|
||||
|
||||
// Process array properties
|
||||
if(isset($this->tags)){
|
||||
$tagsString = implode(',', $this->tags);
|
||||
}
|
||||
if(isset($this->depends)){
|
||||
$dependsString = implode(',', $this->depends);
|
||||
}
|
||||
|
||||
// Process date properties
|
||||
if(isset($this->due)){
|
||||
$dueString = $this->due->format('Y-m-dTH:i:s');
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
return array(
|
||||
"status" => $this->status,
|
||||
"start" => $this->start,
|
||||
"wait" => $this->wait,
|
||||
"end" => $this->end,
|
||||
"entry" => $this->entry,
|
||||
"priority" => $this->priority,
|
||||
"project" => $this->project,
|
||||
"due" => $this->due,
|
||||
"tags" => $tagsString,
|
||||
"urgency" => $this->urgency,
|
||||
"recu" => $this->recur,
|
||||
"until" => $this->until,
|
||||
"tags" => $this->annotations,
|
||||
"modified" => $this->modified,
|
||||
"depends" => $dependsString
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*
|
||||
* If description is not available attempt to summary, otherwise throw Exception
|
||||
*/
|
||||
public function setDescription(VTodo $component){
|
||||
if(!isset($component->DESCRIPTION) && isset($component->SUMMARY)){
|
||||
$this->description = $component->SUMMARY;
|
||||
} else if(!isset($component->DESCRIPTION) && !isset($component->SUMMARY)){
|
||||
throw new Exception("Task must have a description or summary");
|
||||
} else {
|
||||
$this->description = $component->DESCRIPTION;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getDescription(){
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
public function setEntryTime(VTodo $document){
|
||||
|
||||
if(isset($document->DTSTAMP)){
|
||||
$this->entry = new Carbon($document->DTSTAMP->getDateTime()->format(\DateTime::W3C));
|
||||
} else {
|
||||
throw new Exception('Task must have a entry time');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function getEntryTime(){
|
||||
return $this->entry;
|
||||
}
|
||||
|
||||
public function setStartTime(VTodo $document){
|
||||
if(isset($document->DTSTART)){
|
||||
$this->start = new Carbon($document->DTSTART->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
}
|
||||
|
||||
public function getStartTime(){
|
||||
return $this->start;
|
||||
|
||||
}
|
||||
|
||||
public function setModifiedTime(VTodo $document){
|
||||
if(isset($document->{'LAST-MODIFIED'})){
|
||||
$this->modified = new Carbon($document->{'LAST-MODIFIED'}->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
}
|
||||
|
||||
public function getModifiedTime(){
|
||||
return $this->modified;
|
||||
}
|
||||
|
||||
public function setDue(VTodo $document){
|
||||
|
||||
if(isset($document->DUE)){
|
||||
$this->due = new Carbon($document->DUE->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
}
|
||||
public function getDue($format=null){
|
||||
if($format != null){
|
||||
return $this->due->format($format);
|
||||
}
|
||||
|
||||
return $this->due;
|
||||
}
|
||||
|
||||
public function setStopTime(VTodo $document){
|
||||
if(isset($document->DTEND)){
|
||||
$this->end = new Carbon($document->DTEND->getDateTime()->format(\DateTime::W3C));
|
||||
}
|
||||
}
|
||||
|
||||
public function getStopTime(){
|
||||
return $this->end;
|
||||
}
|
||||
|
||||
public function setCategories(VTodo $document){
|
||||
if(isset($document->CATEGORIES)){
|
||||
$this->tags = explode(',', (string)$document->CATEGORIES);
|
||||
}
|
||||
}
|
||||
|
||||
public function getCategories(){
|
||||
return $this->tags;
|
||||
}
|
||||
|
||||
public function setStatus(VTodo $document){
|
||||
if(isset($document->STATUS)){
|
||||
switch((string)$document->STATUS){
|
||||
case 'NEEDS-ACTION':
|
||||
$this->status = 'pending';
|
||||
break;
|
||||
case 'COMPLETED':
|
||||
$this->status = 'completed';
|
||||
break;
|
||||
case 'CANCELED':
|
||||
$this->status = 'deleted';
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function getRecurrence(){
|
||||
return $this->recur;
|
||||
|
||||
}
|
||||
public function setRecurrence(VTodo $document){
|
||||
if(isset($document->RRULE)){
|
||||
$this->recur = $document->RRULE;
|
||||
}
|
||||
}
|
||||
|
||||
public function getStatus(){
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
84
src/TW/Taskwarrior.php
Normal file
84
src/TW/Taskwarrior.php
Normal file
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
|
||||
namespace Aerex\TaskwarriorPlugin\TW;
|
||||
use Symfony\Component\Process\Process;
|
||||
use Aerex\TaskwarriorPlugin\Commands\IStrategy;
|
||||
use Aerex\TaskwarriorPlugin\Config;
|
||||
|
||||
class Taskwarrior {
|
||||
|
||||
|
||||
/**
|
||||
* @var IStrategy
|
||||
*/
|
||||
private $strategy;
|
||||
|
||||
/**
|
||||
* @var TaskwarriorConfig
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $bin;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $taskrc;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $taskData;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $rcOptions;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
|
||||
public function __construct(Config $config){
|
||||
if(!isset($config)){
|
||||
$this->config = new Config();
|
||||
}
|
||||
$this->config = $config;
|
||||
}
|
||||
public function getConfig(){
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
public function setStrategy($strategy){
|
||||
$this->strategy = $strategy;
|
||||
}
|
||||
|
||||
public function createTask($uuid){
|
||||
$task = new Task($uuid);
|
||||
return $task;
|
||||
}
|
||||
|
||||
|
||||
public function add($task){
|
||||
$this->strategy->add($task);
|
||||
}
|
||||
|
||||
public function count($uuid){
|
||||
return $this->strategy->count($uuid);
|
||||
}
|
||||
|
||||
public function modify($task){
|
||||
return $this->strategy->modify($task);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
?>
|
||||
|
||||
|
||||
|
||||
95
src/TW/TaskwarriorManager.php
Normal file
95
src/TW/TaskwarriorManager.php
Normal file
@@ -0,0 +1,95 @@
|
||||
<?php
|
||||
|
||||
namespace Aerex\TaskwarriorPlugin\TW;
|
||||
|
||||
use Aerex\TaskwarriorPlugin\TW\Taskwarrior;
|
||||
use Aerex\TaskwarriorPlugin\TW\Task;
|
||||
use Aerex\TaskwarriorPlugin\TW\Commands\TodoStrategy;
|
||||
use Sabre\VObject\Component\VTodo;
|
||||
|
||||
class TaskwarriorManager {
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @var TodoStrategy
|
||||
*/
|
||||
private $todoStrategy;
|
||||
|
||||
|
||||
public function __construct($taskwarrior){
|
||||
if(!isset($taskwarrior)){
|
||||
$this->taskwarrior = new Taskwarrior();
|
||||
} else {
|
||||
$this->taskwarrior = $taskwarrior;
|
||||
}
|
||||
|
||||
// Initialize strategies
|
||||
$this->todoStrategy = new TodoStrategy($this->taskwarrior->getConfig());
|
||||
}
|
||||
|
||||
|
||||
public function addTask(VTodo $document){
|
||||
|
||||
$this->taskwarrior->setStrategy($this->todoStrategy);
|
||||
|
||||
$task = $this->taskwarrior->createTask((string)$document->UID);
|
||||
|
||||
$task->setDescription($document);
|
||||
|
||||
$task->setEntryTime($document);
|
||||
|
||||
$task->setDue($document);
|
||||
|
||||
$task->setCategories($document);
|
||||
|
||||
$task->setRecurrence($document);
|
||||
|
||||
return $this->taskwarrior->add($task);
|
||||
|
||||
}
|
||||
|
||||
public function updateTask(VTodo $document){
|
||||
|
||||
$task = $this->taskwarrior->createTask((string)$document->UID);
|
||||
|
||||
$task->setDescription($document);
|
||||
|
||||
$task->setEntryTime($document);
|
||||
|
||||
$task->setStartTime($document);
|
||||
|
||||
$task->setModifiedTime($document);
|
||||
|
||||
$task->setStopTime($document);
|
||||
|
||||
$task->setDue($document);
|
||||
|
||||
$task->setCategories($document);
|
||||
|
||||
$task->setStatus($document);
|
||||
|
||||
$task->setRecurrence($document);
|
||||
|
||||
$updatedTask = $this->taskwarrior->modify($task);
|
||||
|
||||
return $upatedTask;
|
||||
}
|
||||
|
||||
public function taskExists($taskUuid){
|
||||
|
||||
$this->taskwarrior->setStrategy($this->todoStrategy);
|
||||
$exists = $this->taskwarrior->count((string)$taskUuid);
|
||||
echo("exists " . $exists);
|
||||
if($exists === "1"){
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user