ConsoleErrorEvent.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. /**
  22. * @var \Throwable
  23. */
  24. private $error;
  25. /**
  26. * @var int
  27. */
  28. private $exitCode;
  29. /**
  30. * @param \Symfony\Component\Console\Input\InputInterface $input
  31. * @param \Symfony\Component\Console\Output\OutputInterface $output
  32. * @param \Throwable $error
  33. * @param \Symfony\Component\Console\Command\Command|null $command
  34. */
  35. public function __construct($input, $output, $error, $command = null)
  36. {
  37. parent::__construct($command, $input, $output);
  38. $this->error = $error;
  39. }
  40. public function getError()
  41. {
  42. return $this->error;
  43. }
  44. /**
  45. * @param \Throwable $error
  46. */
  47. public function setError($error)
  48. {
  49. $this->error = $error;
  50. }
  51. /**
  52. * @param int $exitCode
  53. */
  54. public function setExitCode($exitCode)
  55. {
  56. $this->exitCode = $exitCode;
  57. $r = new \ReflectionProperty($this->error, 'code');
  58. $r->setValue($this->error, $this->exitCode);
  59. }
  60. public function getExitCode()
  61. {
  62. return $this->exitCode ?? (\is_int($this->error->getCode()) && 0 !== $this->error->getCode() ? $this->error->getCode() : 1);
  63. }
  64. }