Chris@14
|
1 <?php
|
Chris@14
|
2
|
Chris@14
|
3 /*
|
Chris@14
|
4 * This file is part of the Symfony package.
|
Chris@14
|
5 *
|
Chris@14
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@14
|
7 *
|
Chris@14
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@14
|
9 * file that was distributed with this source code.
|
Chris@14
|
10 */
|
Chris@14
|
11
|
Chris@14
|
12 namespace Symfony\Component\Console\Event;
|
Chris@14
|
13
|
Chris@14
|
14 use Symfony\Component\Console\Command\Command;
|
Chris@14
|
15 use Symfony\Component\Console\Exception\InvalidArgumentException;
|
Chris@14
|
16 use Symfony\Component\Console\Input\InputInterface;
|
Chris@14
|
17 use Symfony\Component\Console\Output\OutputInterface;
|
Chris@14
|
18
|
Chris@14
|
19 /**
|
Chris@14
|
20 * Allows to handle throwables thrown while running a command.
|
Chris@14
|
21 *
|
Chris@14
|
22 * @author Wouter de Jong <wouter@wouterj.nl>
|
Chris@14
|
23 */
|
Chris@14
|
24 final class ConsoleErrorEvent extends ConsoleEvent
|
Chris@14
|
25 {
|
Chris@14
|
26 private $error;
|
Chris@14
|
27 private $exitCode;
|
Chris@14
|
28
|
Chris@14
|
29 public function __construct(InputInterface $input, OutputInterface $output, $error, Command $command = null)
|
Chris@14
|
30 {
|
Chris@14
|
31 parent::__construct($command, $input, $output);
|
Chris@14
|
32
|
Chris@14
|
33 $this->setError($error);
|
Chris@14
|
34 }
|
Chris@14
|
35
|
Chris@14
|
36 /**
|
Chris@14
|
37 * Returns the thrown error/exception.
|
Chris@14
|
38 *
|
Chris@14
|
39 * @return \Throwable
|
Chris@14
|
40 */
|
Chris@14
|
41 public function getError()
|
Chris@14
|
42 {
|
Chris@14
|
43 return $this->error;
|
Chris@14
|
44 }
|
Chris@14
|
45
|
Chris@14
|
46 /**
|
Chris@14
|
47 * Replaces the thrown error/exception.
|
Chris@14
|
48 *
|
Chris@14
|
49 * @param \Throwable $error
|
Chris@14
|
50 */
|
Chris@14
|
51 public function setError($error)
|
Chris@14
|
52 {
|
Chris@14
|
53 if (!$error instanceof \Throwable && !$error instanceof \Exception) {
|
Chris@17
|
54 throw new InvalidArgumentException(sprintf('The error passed to ConsoleErrorEvent must be an instance of \Throwable or \Exception, "%s" was passed instead.', \is_object($error) ? \get_class($error) : \gettype($error)));
|
Chris@14
|
55 }
|
Chris@14
|
56
|
Chris@14
|
57 $this->error = $error;
|
Chris@14
|
58 }
|
Chris@14
|
59
|
Chris@14
|
60 /**
|
Chris@14
|
61 * Sets the exit code.
|
Chris@14
|
62 *
|
Chris@14
|
63 * @param int $exitCode The command exit code
|
Chris@14
|
64 */
|
Chris@14
|
65 public function setExitCode($exitCode)
|
Chris@14
|
66 {
|
Chris@14
|
67 $this->exitCode = (int) $exitCode;
|
Chris@14
|
68
|
Chris@14
|
69 $r = new \ReflectionProperty($this->error, 'code');
|
Chris@14
|
70 $r->setAccessible(true);
|
Chris@14
|
71 $r->setValue($this->error, $this->exitCode);
|
Chris@14
|
72 }
|
Chris@14
|
73
|
Chris@14
|
74 /**
|
Chris@14
|
75 * Gets the exit code.
|
Chris@14
|
76 *
|
Chris@14
|
77 * @return int The command exit code
|
Chris@14
|
78 */
|
Chris@14
|
79 public function getExitCode()
|
Chris@14
|
80 {
|
Chris@17
|
81 return null !== $this->exitCode ? $this->exitCode : (\is_int($this->error->getCode()) && 0 !== $this->error->getCode() ? $this->error->getCode() : 1);
|
Chris@14
|
82 }
|
Chris@14
|
83 }
|