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