ConsoleErrorEvent.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 \Throwable $error;
  22. private int $exitCode;
  23. public function __construct(InputInterface $input, OutputInterface $output, \Throwable $error, ?Command $command = null)
  24. {
  25. parent::__construct($command, $input, $output);
  26. $this->error = $error;
  27. }
  28. public function getError(): \Throwable
  29. {
  30. return $this->error;
  31. }
  32. public function setError(\Throwable $error): void
  33. {
  34. $this->error = $error;
  35. }
  36. public function setExitCode(int $exitCode): void
  37. {
  38. $this->exitCode = $exitCode;
  39. $r = new \ReflectionProperty($this->error, 'code');
  40. $r->setValue($this->error, $this->exitCode);
  41. }
  42. public function getExitCode(): int
  43. {
  44. return $this->exitCode ?? (\is_int($this->error->getCode()) && 0 !== $this->error->getCode() ? $this->error->getCode() : 1);
  45. }
  46. }