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