ConsoleErrorEvent.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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 $error;
  22. private $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->setAccessible(true);
  41. $r->setValue($this->error, $this->exitCode);
  42. }
  43. public function getExitCode(): int
  44. {
  45. return null !== $this->exitCode ? $this->exitCode : (\is_int($this->error->getCode()) && 0 !== $this->error->getCode() ? $this->error->getCode() : 1);
  46. }
  47. }