Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / robo / src / Collection / CompletionWrapper.php
1 <?php
2
3 namespace Robo\Collection;
4
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;
10
11 /**
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.
15  *
16  * Clients may need to wrap their task in a CompletionWrapper if it
17  * creates temporary objects.
18  *
19  * @see \Robo\Task\Filesystem\loadTasks::taskTmpDir
20  */
21 class CompletionWrapper extends BaseTask implements WrappedTaskInterface
22 {
23     /**
24      * @var \Robo\Collection\Collection
25      */
26     private $collection;
27
28     /**
29      * @var \Robo\Contract\TaskInterface
30      */
31     private $task;
32
33     /**
34      * @var NULL|\Robo\Contract\TaskInterface
35      */
36     private $rollbackTask;
37
38     /**
39      * Create a CompletionWrapper.
40      *
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.
46      *
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.
50      *
51      * @todo Why not CollectionInterface the type of the $collection argument?
52      *
53      * @param \Robo\Collection\Collection $collection
54      * @param \Robo\Contract\TaskInterface $task
55      * @param \Robo\Contract\TaskInterface|NULL $rollbackTask
56      */
57     public function __construct(Collection $collection, TaskInterface $task, TaskInterface $rollbackTask = null)
58     {
59         $this->collection = $collection;
60         $this->task = ($task instanceof WrappedTaskInterface) ? $task->original() : $task;
61         $this->rollbackTask = $rollbackTask;
62     }
63
64     /**
65      * {@inheritdoc}
66      */
67     public function original()
68     {
69         return $this->task;
70     }
71
72     /**
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.
76      *
77      * @return \Robo\Result
78      */
79     public function run()
80     {
81         if ($this->rollbackTask) {
82             $this->collection->registerRollback($this->rollbackTask);
83         }
84         if ($this->task instanceof RollbackInterface) {
85             $this->collection->registerRollback(new CallableTask([$this->task, 'rollback'], $this->task));
86         }
87         if ($this->task instanceof CompletionInterface) {
88             $this->collection->registerCompletion(new CallableTask([$this->task, 'complete'], $this->task));
89         }
90
91         return $this->task->run();
92     }
93
94     /**
95      * Make this wrapper object act like the class it wraps.
96      *
97      * @param string $function
98      * @param array $args
99      *
100      * @return mixed
101      */
102     public function __call($function, $args)
103     {
104         return call_user_func_array(array($this->task, $function), $args);
105     }
106 }