| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 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 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class ProcessHelper extends Helper |
| 27 |
{ |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function run(OutputInterface $output, array|Process $cmd, ?string $error = null, ?callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process |
| 36 |
{ |
| 37 |
if (!class_exists(Process::class)) { |
| 38 |
throw new \LogicException('The ProcessHelper cannot be run as the Process component is not installed. Try running "compose require symfony/process".'); |
| 39 |
} |
| 40 |
|
| 41 |
if ($output instanceof ConsoleOutputInterface) { |
| 42 |
$output = $output->getErrorOutput(); |
| 43 |
} |
| 44 |
|
| 45 |
$formatter = $this->getHelperSet()->get('debug_formatter'); |
| 46 |
|
| 47 |
if ($cmd instanceof Process) { |
| 48 |
$cmd = [$cmd]; |
| 49 |
} |
| 50 |
|
| 51 |
if (\is_string($cmd[0] ?? null)) { |
| 52 |
$process = new Process($cmd); |
| 53 |
$cmd = []; |
| 54 |
} elseif (($cmd[0] ?? null) instanceof Process) { |
| 55 |
$process = $cmd[0]; |
| 56 |
unset($cmd[0]); |
| 57 |
} else { |
| 58 |
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__)); |
| 59 |
} |
| 60 |
|
| 61 |
if ($verbosity <= $output->getVerbosity()) { |
| 62 |
$output->write($formatter->start(spl_object_hash($process), $this->escapeString($process->getCommandLine()))); |
| 63 |
} |
| 64 |
|
| 65 |
if ($output->isDebug()) { |
| 66 |
$callback = $this->wrapCallback($output, $process, $callback); |
| 67 |
} |
| 68 |
|
| 69 |
$process->run($callback, $cmd); |
| 70 |
|
| 71 |
if ($verbosity <= $output->getVerbosity()) { |
| 72 |
$message = $process->isSuccessful() ? 'Command ran successfully' : \sprintf('%s Command did not run successfully', $process->getExitCode()); |
| 73 |
$output->write($formatter->stop(spl_object_hash($process), $message, $process->isSuccessful())); |
| 74 |
} |
| 75 |
|
| 76 |
if (!$process->isSuccessful() && null !== $error) { |
| 77 |
$output->writeln(\sprintf('<error>%s</error>', $this->escapeString($error))); |
| 78 |
} |
| 79 |
|
| 80 |
return $process; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public function mustRun(OutputInterface $output, array|Process $cmd, ?string $error = null, ?callable $callback = null, int $verbosity = OutputInterface::VERBOSITY_VERY_VERBOSE): Process |
| 98 |
{ |
| 99 |
$process = $this->run($output, $cmd, $error, $callback, $verbosity); |
| 100 |
|
| 101 |
if (!$process->isSuccessful()) { |
| 102 |
throw new ProcessFailedException($process); |
| 103 |
} |
| 104 |
|
| 105 |
return $process; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public function wrapCallback(OutputInterface $output, Process $process, ?callable $callback = null): callable |
| 112 |
{ |
| 113 |
if ($output instanceof ConsoleOutputInterface) { |
| 114 |
$output = $output->getErrorOutput(); |
| 115 |
} |
| 116 |
|
| 117 |
$formatter = $this->getHelperSet()->get('debug_formatter'); |
| 118 |
|
| 119 |
return function ($type, $buffer) use ($output, $process, $callback, $formatter) { |
| 120 |
$output->write($formatter->progress(spl_object_hash($process), $this->escapeString($buffer), Process::ERR === $type)); |
| 121 |
|
| 122 |
if (null !== $callback) { |
| 123 |
$callback($type, $buffer); |
| 124 |
} |
| 125 |
}; |
| 126 |
} |
| 127 |
|
| 128 |
private function escapeString(string $str): string |
| 129 |
{ |
| 130 |
return str_replace('<', '\\<', $str); |
| 131 |
} |
| 132 |
|
| 133 |
public function getName(): string |
| 134 |
{ |
| 135 |
return 'process'; |
| 136 |
} |
| 137 |
} |