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