Mercurial > hg > isophonics-drupal-site
comparison vendor/symfony/console/Helper/ProcessHelper.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children | 1fec387a4317 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
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 use Symfony\Component\Process\ProcessBuilder; | |
19 | |
20 /** | |
21 * The ProcessHelper class provides helpers to run external processes. | |
22 * | |
23 * @author Fabien Potencier <fabien@symfony.com> | |
24 */ | |
25 class ProcessHelper extends Helper | |
26 { | |
27 /** | |
28 * Runs an external process. | |
29 * | |
30 * @param OutputInterface $output An OutputInterface instance | |
31 * @param string|array|Process $cmd An instance of Process or an array of arguments to escape and run or a command to run | |
32 * @param string|null $error An error message that must be displayed if something went wrong | |
33 * @param callable|null $callback A PHP callback to run whenever there is some | |
34 * output available on STDOUT or STDERR | |
35 * @param int $verbosity The threshold for verbosity | |
36 * | |
37 * @return Process The process that ran | |
38 */ | |
39 public function run(OutputInterface $output, $cmd, $error = null, callable $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE) | |
40 { | |
41 if ($output instanceof ConsoleOutputInterface) { | |
42 $output = $output->getErrorOutput(); | |
43 } | |
44 | |
45 $formatter = $this->getHelperSet()->get('debug_formatter'); | |
46 | |
47 if (is_array($cmd)) { | |
48 $process = ProcessBuilder::create($cmd)->getProcess(); | |
49 } elseif ($cmd instanceof Process) { | |
50 $process = $cmd; | |
51 } else { | |
52 $process = new Process($cmd); | |
53 } | |
54 | |
55 if ($verbosity <= $output->getVerbosity()) { | |
56 $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine()))); | |
57 } | |
58 | |
59 if ($output->isDebug()) { | |
60 $callback = $this->wrapCallback($output, $process, $callback); | |
61 } | |
62 | |
63 $process->run($callback); | |
64 | |
65 if ($verbosity <= $output->getVerbosity()) { | |
66 $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode()); | |
67 $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful())); | |
68 } | |
69 | |
70 if (!$process->isSuccessful() && null !== $error) { | |
71 $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error))); | |
72 } | |
73 | |
74 return $process; | |
75 } | |
76 | |
77 /** | |
78 * Runs the process. | |
79 * | |
80 * This is identical to run() except that an exception is thrown if the process | |
81 * exits with a non-zero exit code. | |
82 * | |
83 * @param OutputInterface $output An OutputInterface instance | |
84 * @param string|Process $cmd An instance of Process or a command to run | |
85 * @param string|null $error An error message that must be displayed if something went wrong | |
86 * @param callable|null $callback A PHP callback to run whenever there is some | |
87 * output available on STDOUT or STDERR | |
88 * | |
89 * @return Process The process that ran | |
90 * | |
91 * @throws ProcessFailedException | |
92 * | |
93 * @see run() | |
94 */ | |
95 public function mustRun(OutputInterface $output, $cmd, $error = null, callable $callback = null) | |
96 { | |
97 $process = $this->run($output, $cmd, $error, $callback); | |
98 | |
99 if (!$process->isSuccessful()) { | |
100 throw new ProcessFailedException($process); | |
101 } | |
102 | |
103 return $process; | |
104 } | |
105 | |
106 /** | |
107 * Wraps a Process callback to add debugging output. | |
108 * | |
109 * @param OutputInterface $output An OutputInterface interface | |
110 * @param Process $process The Process | |
111 * @param callable|null $callback A PHP callable | |
112 * | |
113 * @return callable | |
114 */ | |
115 public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null) | |
116 { | |
117 if ($output instanceof ConsoleOutputInterface) { | |
118 $output = $output->getErrorOutput(); | |
119 } | |
120 | |
121 $formatter = $this->getHelperSet()->get('debug_formatter'); | |
122 | |
123 return function ($type, $buffer) use ($output, $process, $callback, $formatter) { | |
124 $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type)); | |
125 | |
126 if (null !== $callback) { | |
127 call_user_func($callback, $type, $buffer); | |
128 } | |
129 }; | |
130 } | |
131 | |
132 private function escapeString($str) | |
133 { | |
134 return str_replace('<', '\\<', $str); | |
135 } | |
136 | |
137 /** | |
138 * {@inheritdoc} | |
139 */ | |
140 public function getName() | |
141 { | |
142 return 'process'; | |
143 } | |
144 } |