Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / symfony / console / Helper / ProcessHelper.php
1 <?php
2
3 /*
4  * This file is part of the Symfony package.
5  *
6  * (c) Fabien Potencier <fabien@symfony.com>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Symfony\Component\Console\Helper;
13
14 use Symfony\Component\Console\Output\ConsoleOutputInterface;
15 use Symfony\Component\Console\Output\OutputInterface;
16 use Symfony\Component\Process\Exception\ProcessFailedException;
17 use Symfony\Component\Process\Process;
18
19 /**
20  * The ProcessHelper class provides helpers to run external processes.
21  *
22  * @author Fabien Potencier <fabien@symfony.com>
23  */
24 class ProcessHelper extends Helper
25 {
26     /**
27      * Runs an external process.
28      *
29      * @param OutputInterface      $output    An OutputInterface instance
30      * @param string|array|Process $cmd       An instance of Process or an array of arguments to escape and run or a command to run
31      * @param string|null          $error     An error message that must be displayed if something went wrong
32      * @param callable|null        $callback  A PHP callback to run whenever there is some
33      *                                        output available on STDOUT or STDERR
34      * @param int                  $verbosity The threshold for verbosity
35      *
36      * @return Process The process that ran
37      */
38     public function run(OutputInterface $output, $cmd, $error = null, callable $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
39     {
40         if ($output instanceof ConsoleOutputInterface) {
41             $output = $output->getErrorOutput();
42         }
43
44         $formatter = $this->getHelperSet()->get('debug_formatter');
45
46         if ($cmd instanceof Process) {
47             $process = $cmd;
48         } else {
49             $process = new Process($cmd);
50         }
51
52         if ($verbosity <= $output->getVerbosity()) {
53             $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
54         }
55
56         if ($output->isDebug()) {
57             $callback = $this->wrapCallback($output, $process, $callback);
58         }
59
60         $process->run($callback);
61
62         if ($verbosity <= $output->getVerbosity()) {
63             $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode());
64             $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful()));
65         }
66
67         if (!$process->isSuccessful() && null !== $error) {
68             $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
69         }
70
71         return $process;
72     }
73
74     /**
75      * Runs the process.
76      *
77      * This is identical to run() except that an exception is thrown if the process
78      * exits with a non-zero exit code.
79      *
80      * @param OutputInterface $output   An OutputInterface instance
81      * @param string|Process  $cmd      An instance of Process or a command to run
82      * @param string|null     $error    An error message that must be displayed if something went wrong
83      * @param callable|null   $callback A PHP callback to run whenever there is some
84      *                                  output available on STDOUT or STDERR
85      *
86      * @return Process The process that ran
87      *
88      * @throws ProcessFailedException
89      *
90      * @see run()
91      */
92     public function mustRun(OutputInterface $output, $cmd, $error = null, callable $callback = null)
93     {
94         $process = $this->run($output, $cmd, $error, $callback);
95
96         if (!$process->isSuccessful()) {
97             throw new ProcessFailedException($process);
98         }
99
100         return $process;
101     }
102
103     /**
104      * Wraps a Process callback to add debugging output.
105      *
106      * @param OutputInterface $output   An OutputInterface interface
107      * @param Process         $process  The Process
108      * @param callable|null   $callback A PHP callable
109      *
110      * @return callable
111      */
112     public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null)
113     {
114         if ($output instanceof ConsoleOutputInterface) {
115             $output = $output->getErrorOutput();
116         }
117
118         $formatter = $this->getHelperSet()->get('debug_formatter');
119
120         return function ($type, $buffer) use ($output, $process, $callback, $formatter) {
121             $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type));
122
123             if (null !== $callback) {
124                 \call_user_func($callback, $type, $buffer);
125             }
126         };
127     }
128
129     private function escapeString($str)
130     {
131         return str_replace('<', '\\<', $str);
132     }
133
134     /**
135      * {@inheritdoc}
136      */
137     public function getName()
138     {
139         return 'process';
140     }
141 }