Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / vendor / consolidation / robo / src / Task / CommandStack.php
1 <?php
2
3 namespace Robo\Task;
4
5 use Robo\Common\ExecCommand;
6 use Robo\Contract\PrintedInterface;
7 use Robo\Result;
8 use Robo\Contract\CommandInterface;
9 use Robo\Exception\TaskException;
10
11 abstract class CommandStack extends BaseTask implements CommandInterface, PrintedInterface
12 {
13     use ExecCommand;
14
15     /**
16      * @var string
17      */
18     protected $executable;
19
20     protected $result;
21
22     /**
23      * @var string[]
24      */
25     protected $exec = [];
26
27     /**
28      * @var bool
29      */
30     protected $stopOnFail = false;
31
32     /**
33      * {@inheritdoc}
34      */
35     public function getCommand()
36     {
37         return implode(' && ', $this->exec);
38     }
39
40     /**
41      * @param string $executable
42      *
43      * @return $this
44      */
45     public function executable($executable)
46     {
47         $this->executable = $executable;
48         return $this;
49     }
50
51     /**
52      * @param string|string[] $command
53      *
54      * @return $this
55      */
56     public function exec($command)
57     {
58         if (is_array($command)) {
59             $command = implode(' ', array_filter($command));
60         }
61
62         $command      = $this->executable . ' ' . $this->stripExecutableFromCommand($command);
63         $this->exec[] = trim($command);
64         return $this;
65     }
66
67     /**
68      * @param bool $stopOnFail
69      *
70      * @return $this
71      */
72     public function stopOnFail($stopOnFail = true)
73     {
74         $this->stopOnFail = $stopOnFail;
75         return $this;
76     }
77
78     public function result($result)
79     {
80         $this->result = $result;
81         return $this;
82     }
83
84     /**
85      * @param string $command
86      *
87      * @return string
88      */
89     protected function stripExecutableFromCommand($command)
90     {
91         $command = trim($command);
92         $executable = $this->executable . ' ';
93         if (strpos($command, $executable) === 0) {
94             $command = substr($command, strlen($executable));
95         }
96         return $command;
97     }
98
99     /**
100      * {@inheritdoc}
101      */
102     public function run()
103     {
104         if (empty($this->exec)) {
105             throw new TaskException($this, 'You must add at least one command');
106         }
107         // If 'stopOnFail' is not set, or if there is only one command to run,
108         // then execute the single command to run.
109         if (!$this->stopOnFail || (count($this->exec) == 1)) {
110             $this->printTaskInfo('{command}', ['command' => $this->getCommand()]);
111             return $this->executeCommand($this->getCommand());
112         }
113
114         // When executing multiple commands in 'stopOnFail' mode, run them
115         // one at a time so that the result will have the exact command
116         // that failed available to the caller. This is at the expense of
117         // losing the output from all successful commands.
118         $data = [];
119         $message = '';
120         $result = null;
121         foreach ($this->exec as $command) {
122             $this->printTaskInfo("Executing {command}", ['command' => $command]);
123             $result = $this->executeCommand($command);
124             $result->accumulateExecutionTime($data);
125             $message = $result->accumulateMessage($message);
126             $data = $result->mergeData($data);
127             if (!$result->wasSuccessful()) {
128                 return $result;
129             }
130         }
131
132         return $result;
133     }
134 }