|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Php\Pie\Util; |
| 6 | + |
| 7 | +use Symfony\Component\Process\Exception\ProcessFailedException; |
| 8 | +use Symfony\Component\Process\Process; |
| 9 | + |
| 10 | +use function file_put_contents; |
| 11 | +use function sprintf; |
| 12 | +use function strlen; |
| 13 | +use function sys_get_temp_dir; |
| 14 | +use function uniqid; |
| 15 | + |
| 16 | +use const DIRECTORY_SEPARATOR; |
| 17 | + |
| 18 | +class ProcessFailedWithLimitedOutput extends ProcessFailedException |
| 19 | +{ |
| 20 | + public static function fromProcessFailedException(ProcessFailedException $previous): self|ProcessFailedException |
| 21 | + { |
| 22 | + $process = $previous->getProcess(); |
| 23 | + |
| 24 | + if (strlen($process->getOutput()) > 5000) { |
| 25 | + $tempOutputFile = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('pie_make_output_', true); |
| 26 | + file_put_contents( |
| 27 | + $tempOutputFile, |
| 28 | + sprintf( |
| 29 | + "Output:\n================\n%s\n\nError Output:\n================\n%s", |
| 30 | + $process->getOutput(), |
| 31 | + $process->getErrorOutput(), |
| 32 | + ), |
| 33 | + ); |
| 34 | + |
| 35 | + return new self($process, $tempOutputFile); |
| 36 | + } |
| 37 | + |
| 38 | + return $previous; |
| 39 | + } |
| 40 | + |
| 41 | + public function __construct(Process $process, string $fileWithOutput) |
| 42 | + { |
| 43 | + parent::__construct($process); |
| 44 | + |
| 45 | + $error = sprintf( |
| 46 | + 'The command "%s" failed. Output was saved in "%s" as it was too long' . "\n\nExit Code: %s(%s)\n\nWorking directory: %s", |
| 47 | + $process->getCommandLine(), |
| 48 | + $fileWithOutput, |
| 49 | + $process->getExitCode() ?? '?', |
| 50 | + $process->getExitCodeText() ?? '?', |
| 51 | + $process->getWorkingDirectory() ?? '?', |
| 52 | + ); |
| 53 | + |
| 54 | + $this->message = $error; |
| 55 | + } |
| 56 | +} |
0 commit comments