ConsoleErrorEvent.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Event;
  11. use Symfony\Component\Console\Command\Command;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. /**
  15. * Allows to handle throwables thrown while running a command.
  16. *
  17. * @author Wouter de Jong <wouter@wouterj.nl>
  18. */
  19. final class ConsoleErrorEvent extends ConsoleEvent
  20. {
  21. private int $exitCode;
  22. public function __construct(
  23. InputInterface $input,
  24. OutputInterface $output,
  25. private \Throwable $error,
  26. ?Command $command = null,
  27. ) {
  28. parent::__construct($command, $input, $output);
  29. }
  30. public function getError(): \Throwable
  31. {
  32. return $this->error;
  33. }
  34. public function setError(\Throwable $error): void
  35. {
  36. $this->error = $error;
  37. }
  38. public function setExitCode(int $exitCode): void
  39. {
  40. $this->exitCode = $exitCode;
  41. $r = new \ReflectionProperty($this->error, 'code');
  42. $r->setValue($this->error, $this->exitCode);
  43. }
  44. public function getExitCode(): int
  45. {
  46. return $this->exitCode ?? (\is_int($this->error->getCode()) && 0 !== $this->error->getCode() ? $this->error->getCode() : 1);
  47. }
  48. }