Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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     use \Robo\Common\CommandReceiver;
15
16     /**
17      * @var string
18      */
19     protected $executable;
20
21     protected $result;
22
23     /**
24      * @var string[]
25      */
26     protected $exec = [];
27
28     /**
29      * @var bool
30      */
31     protected $stopOnFail = false;
32
33     /**
34      * {@inheritdoc}
35      */
36     public function getCommand()
37     {
38         $commands = [];
39         foreach ($this->exec as $command) {
40             $commands[] = $this->receiveCommand($command);
41         }
42
43         return implode(' && ', $commands);
44     }
45
46     /**
47      * @param string $executable
48      *
49      * @return $this
50      */
51     public function executable($executable)
52     {
53         $this->executable = $executable;
54         return $this;
55     }
56
57     /**
58      * @param string|string[]|CommandInterface $command
59      *
60      * @return $this
61      */
62     public function exec($command)
63     {
64         if (is_array($command)) {
65             $command = implode(' ', array_filter($command));
66         }
67
68         if (is_string($command)) {
69             $command = $this->executable . ' ' . $this->stripExecutableFromCommand($command);
70             $command = trim($command);
71         }
72         
73         $this->exec[] = $command;
74         
75         return $this;
76     }
77
78     /**
79      * @param bool $stopOnFail
80      *
81      * @return $this
82      */
83     public function stopOnFail($stopOnFail = true)
84     {
85         $this->stopOnFail = $stopOnFail;
86         return $this;
87     }
88
89     public function result($result)
90     {
91         $this->result = $result;
92         return $this;
93     }
94
95     /**
96      * @param string $command
97      *
98      * @return string
99      */
100     protected function stripExecutableFromCommand($command)
101     {
102         $command = trim($command);
103         $executable = $this->executable . ' ';
104         if (strpos($command, $executable) === 0) {
105             $command = substr($command, strlen($executable));
106         }
107         return $command;
108     }
109
110     /**
111      * {@inheritdoc}
112      */
113     public function run()
114     {
115         if (empty($this->exec)) {
116             throw new TaskException($this, 'You must add at least one command');
117         }
118         // If 'stopOnFail' is not set, or if there is only one command to run,
119         // then execute the single command to run.
120         if (!$this->stopOnFail || (count($this->exec) == 1)) {
121             $this->printTaskInfo('{command}', ['command' => $this->getCommand()]);
122             return $this->executeCommand($this->getCommand());
123         }
124
125         // When executing multiple commands in 'stopOnFail' mode, run them
126         // one at a time so that the result will have the exact command
127         // that failed available to the caller. This is at the expense of
128         // losing the output from all successful commands.
129         $data = [];
130         $message = '';
131         $result = null;
132         foreach ($this->exec as $command) {
133             $this->printTaskInfo("Executing {command}", ['command' => $command]);
134             $result = $this->executeCommand($command);
135             $result->accumulateExecutionTime($data);
136             $message = $result->accumulateMessage($message);
137             $data = $result->mergeData($data);
138             if (!$result->wasSuccessful()) {
139                 return $result;
140             }
141         }
142
143         return $result;
144     }
145 }