1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of Collision Adapter Symfony. |
7
|
|
|
* |
8
|
|
|
* (c) Nuno Maduro <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace NunoMaduro\CollisionAdapterSymfony\EventListener; |
15
|
|
|
|
16
|
|
|
use Whoops\Exception\Inspector; |
17
|
|
|
use NunoMaduro\Collision\Writer; |
18
|
|
|
use Symfony\Component\Console\Event\ConsoleErrorEvent; |
19
|
|
|
use Symfony\Component\Console\Exception\ExceptionInterface; |
20
|
|
|
use NunoMaduro\Collision\Contracts\Writer as WriterContract; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* This is an Collision Adapter Symfony Error Listener implementation. |
24
|
|
|
* |
25
|
|
|
* @author Nuno Maduro <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class ErrorListener |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* Holds an instance of the Collision Writer. |
31
|
|
|
* |
32
|
|
|
* @var \NunoMaduro\Collision\Contracts\Writer |
33
|
|
|
*/ |
34
|
|
|
private $writer; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates an new instance of the Error Listener. |
38
|
|
|
* |
39
|
|
|
* @param \NunoMaduro\Collision\Contracts\Writer|null $writer |
40
|
|
|
*/ |
41
|
5 |
|
public function __construct(WriterContract $writer = null) |
42
|
|
|
{ |
43
|
5 |
|
$this->writer = $writer ? clone $writer : new Writer(); |
44
|
5 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* This event should be attached to an {@Event("Symfony\Component\Console\Event\ConsoleErrorEvent")}. |
48
|
|
|
* |
49
|
|
|
* Retrieves error from the provided event, and writes a collision exception to the |
50
|
|
|
* current event output. It also sets the event ExitCode to `O` avoiding the |
51
|
|
|
* exception to be rendered by the default Symfony console application. |
52
|
|
|
* |
53
|
|
|
* @param \Symfony\Component\Console\Event\ConsoleErrorEvent $event |
54
|
|
|
*/ |
55
|
4 |
|
public function onConsoleError(ConsoleErrorEvent $event): void |
56
|
|
|
{ |
57
|
4 |
|
$error = $event->getError(); |
58
|
|
|
|
59
|
4 |
|
if (! ($error instanceof ExceptionInterface)) { |
60
|
3 |
|
$this->writer->setOutput($event->getOutput()); |
61
|
|
|
|
62
|
3 |
|
$this->writer->write(new Inspector($error)); |
63
|
|
|
|
64
|
3 |
|
$event->setExitCode(0); |
65
|
|
|
} |
66
|
4 |
|
} |
67
|
|
|
} |
68
|
|
|
|