| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 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\Messenger; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Application; |
| 15 |
use Symfony\Component\Console\Command\Command; |
| 16 |
use Symfony\Component\Console\Exception\RunCommandFailedException; |
| 17 |
use Symfony\Component\Console\Input\StringInput; |
| 18 |
use Symfony\Component\Console\Output\BufferedOutput; |
| 19 |
use Symfony\Component\Messenger\Exception\RecoverableExceptionInterface; |
| 20 |
use Symfony\Component\Messenger\Exception\UnrecoverableExceptionInterface; |
| 21 |
|
| 22 |
/** |
| 23 |
* @author Kevin Bond <[email protected]> |
| 24 |
*/ |
| 25 |
final class RunCommandMessageHandler |
| 26 |
{ |
| 27 |
public function __construct( |
| 28 |
private readonly Application $application, |
| 29 |
) { |
| 30 |
} |
| 31 |
|
| 32 |
public function __invoke(RunCommandMessage $message): RunCommandContext |
| 33 |
{ |
| 34 |
$input = new StringInput($message->input); |
| 35 |
$output = new BufferedOutput(); |
| 36 |
|
| 37 |
$this->application->setCatchExceptions($message->catchExceptions); |
| 38 |
|
| 39 |
try { |
| 40 |
$exitCode = $this->application->run($input, $output); |
| 41 |
} catch (UnrecoverableExceptionInterface|RecoverableExceptionInterface $e) { |
| 42 |
throw $e; |
| 43 |
} catch (\Throwable $e) { |
| 44 |
throw new RunCommandFailedException($e, new RunCommandContext($message, Command::FAILURE, $output->fetch())); |
| 45 |
} |
| 46 |
|
| 47 |
if ($message->throwOnFailure && Command::SUCCESS !== $exitCode) { |
| 48 |
throw new RunCommandFailedException(\sprintf('Command "%s" exited with code "%s".', $message->input, $exitCode), new RunCommandContext($message, $exitCode, $output->fetch())); |
| 49 |
} |
| 50 |
|
| 51 |
return new RunCommandContext($message, $exitCode, $output->fetch()); |
| 52 |
} |
| 53 |
} |