123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- /*
- * This file is part of the Symfony package.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Symfony\Component\Console\Helper;
- use Symfony\Component\Console\Output\ConsoleOutputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\Process\Exception\ProcessFailedException;
- use Symfony\Component\Process\Process;
- /**
- * The ProcessHelper class provides helpers to run external processes.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- *
- * @final since Symfony 4.2
- */
- class ProcessHelper extends Helper
- {
- /**
- * Runs an external process.
- *
- * @param OutputInterface $output An OutputInterface instance
- * @param array|Process $cmd An instance of Process or an array of the command and arguments
- * @param string|null $error An error message that must be displayed if something went wrong
- * @param callable|null $callback A PHP callback to run whenever there is some
- * output available on STDOUT or STDERR
- * @param int $verbosity The threshold for verbosity
- *
- * @return Process The process that ran
- */
- public function run(OutputInterface $output, $cmd, $error = null, callable $callback = null, $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE)
- {
- if ($output instanceof ConsoleOutputInterface) {
- $output = $output->getErrorOutput();
- }
- $formatter = $this->getHelperSet()->get('debug_formatter');
- if ($cmd instanceof Process) {
- $cmd = [$cmd];
- }
- if (!\is_array($cmd)) {
- @trigger_error(sprintf('Passing a command as a string to "%s()" is deprecated since Symfony 4.2, pass it the command as an array of arguments instead.', __METHOD__), E_USER_DEPRECATED);
- $cmd = [\method_exists(Process::class, 'fromShellCommandline') ? Process::fromShellCommandline($cmd) : new Process($cmd)];
- }
- if (\is_string($cmd[0] ?? null)) {
- $process = new Process($cmd);
- $cmd = [];
- } elseif (($cmd[0] ?? null) instanceof Process) {
- $process = $cmd[0];
- unset($cmd[0]);
- } else {
- throw new \InvalidArgumentException(sprintf('Invalid command provided to "%s()": the command should be an array whose first element is either the path to the binary to run or a "Process" object.', __METHOD__));
- }
- if ($verbosity <= $output->getVerbosity()) {
- $output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine())));
- }
- if ($output->isDebug()) {
- $callback = $this->wrapCallback($output, $process, $callback);
- }
- $process->run($callback, $cmd);
- if ($verbosity <= $output->getVerbosity()) {
- $message = $process->isSuccessful() ? 'Command ran successfully' : sprintf('%s Command did not run successfully', $process->getExitCode());
- $output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful()));
- }
- if (!$process->isSuccessful() && null !== $error) {
- $output->writeln(sprintf('<error>%s</error>', $this->escapeString($error)));
- }
- return $process;
- }
- /**
- * Runs the process.
- *
- * This is identical to run() except that an exception is thrown if the process
- * exits with a non-zero exit code.
- *
- * @param OutputInterface $output An OutputInterface instance
- * @param string|Process $cmd An instance of Process or a command to run
- * @param string|null $error An error message that must be displayed if something went wrong
- * @param callable|null $callback A PHP callback to run whenever there is some
- * output available on STDOUT or STDERR
- *
- * @return Process The process that ran
- *
- * @throws ProcessFailedException
- *
- * @see run()
- */
- public function mustRun(OutputInterface $output, $cmd, $error = null, callable $callback = null)
- {
- $process = $this->run($output, $cmd, $error, $callback);
- if (!$process->isSuccessful()) {
- throw new ProcessFailedException($process);
- }
- return $process;
- }
- /**
- * Wraps a Process callback to add debugging output.
- *
- * @param OutputInterface $output An OutputInterface interface
- * @param Process $process The Process
- * @param callable|null $callback A PHP callable
- *
- * @return callable
- */
- public function wrapCallback(OutputInterface $output, Process $process, callable $callback = null)
- {
- if ($output instanceof ConsoleOutputInterface) {
- $output = $output->getErrorOutput();
- }
- $formatter = $this->getHelperSet()->get('debug_formatter');
- return function ($type, $buffer) use ($output, $process, $callback, $formatter) {
- $output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type));
- if (null !== $callback) {
- $callback($type, $buffer);
- }
- };
- }
- private function escapeString($str)
- {
- return str_replace('<', '\\<', $str);
- }
- /**
- * {@inheritdoc}
- */
- public function getName()
- {
- return 'process';
- }
- }
|