3 namespace Robo\Collection;
5 use Robo\Task\BaseTask;
6 use Robo\Contract\TaskInterface;
7 use Robo\Contract\RollbackInterface;
8 use Robo\Contract\CompletionInterface;
9 use Robo\Contract\WrappedTaskInterface;
12 * Creates a task wrapper that will manage rollback and collection
13 * management to a task when it runs. Tasks are automatically
14 * wrapped in a CompletionWrapper when added to a task collection.
16 * Clients may need to wrap their task in a CompletionWrapper if it
17 * creates temporary objects.
19 * @see \Robo\Task\Filesystem\loadTasks::taskTmpDir
21 class CompletionWrapper extends BaseTask implements WrappedTaskInterface
24 * @var \Robo\Collection\Collection
29 * @var \Robo\Contract\TaskInterface
34 * @var NULL|\Robo\Contract\TaskInterface
36 private $rollbackTask;
39 * Create a CompletionWrapper.
41 * Temporary tasks are always wrapped in a CompletionWrapper, as are
42 * any tasks that are added to a collection. If a temporary task
43 * is added to a collection, then it is first unwrapped from its
44 * CompletionWrapper (via its original() method), and then added to a
45 * new CompletionWrapper for the collection it is added to.
47 * In this way, when the CompletionWrapper is finally executed, the
48 * task's rollback and completion handlers will be registered on
49 * whichever collection it was registered on.
51 * @todo Why not CollectionInterface the type of the $collection argument?
53 * @param \Robo\Collection\Collection $collection
54 * @param \Robo\Contract\TaskInterface $task
55 * @param \Robo\Contract\TaskInterface|NULL $rollbackTask
57 public function __construct(Collection $collection, TaskInterface $task, TaskInterface $rollbackTask = null)
59 $this->collection = $collection;
60 $this->task = ($task instanceof WrappedTaskInterface) ? $task->original() : $task;
61 $this->rollbackTask = $rollbackTask;
67 public function original()
73 * Before running this task, register its rollback and completion
74 * handlers on its collection. The reason this class exists is to
75 * defer registration of rollback and completion tasks until 'run()' time.
77 * @return \Robo\Result
81 if ($this->rollbackTask) {
82 $this->collection->registerRollback($this->rollbackTask);
84 if ($this->task instanceof RollbackInterface) {
85 $this->collection->registerRollback(new CallableTask([$this->task, 'rollback'], $this->task));
87 if ($this->task instanceof CompletionInterface) {
88 $this->collection->registerCompletion(new CallableTask([$this->task, 'complete'], $this->task));
91 return $this->task->run();
95 * Make this wrapper object act like the class it wraps.
97 * @param string $function
102 public function __call($function, $args)
104 return call_user_func_array(array($this->task, $function), $args);