1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector; |
6
|
|
|
|
7
|
|
|
use RuntimeException; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Event\ConsoleCommandEvent; |
10
|
|
|
use Symfony\Component\Console\Event\ConsoleErrorEvent; |
11
|
|
|
use Symfony\Component\Console\Event\ConsoleEvent; |
12
|
|
|
use Symfony\Component\Console\Event\ConsoleTerminateEvent; |
13
|
|
|
|
14
|
|
|
use function get_class; |
15
|
|
|
use function is_object; |
16
|
|
|
|
17
|
|
|
final class CommandCollector implements CollectorInterface, IndexCollectorInterface |
18
|
|
|
{ |
19
|
|
|
use CollectorTrait; |
20
|
|
|
|
21
|
|
|
private array $commands = []; |
22
|
|
|
|
23
|
2 |
|
public function getCollected(): array |
24
|
|
|
{ |
25
|
2 |
|
return $this->commands; |
26
|
|
|
} |
27
|
|
|
|
28
|
2 |
|
public function collect(object $event): void |
29
|
|
|
{ |
30
|
2 |
|
if (!is_object($event) || !$this->isActive()) { |
31
|
1 |
|
return; |
32
|
|
|
} |
33
|
|
|
|
34
|
1 |
|
if ($event instanceof ConsoleErrorEvent) { |
35
|
1 |
|
$this->commands[get_class($event)] = [ |
36
|
1 |
|
'name' => $event->getInput()->getFirstArgument() ?? '', |
37
|
1 |
|
'command' => $event->getCommand(), |
38
|
1 |
|
'input' => $event->getInput()->__toString(), |
|
|
|
|
39
|
1 |
|
'output' => $event->getOutput()->fetch(), |
|
|
|
|
40
|
1 |
|
'error' => $event->getError()->getMessage(), |
41
|
1 |
|
'exitCode' => $event->getExitCode(), |
42
|
|
|
]; |
43
|
|
|
|
44
|
1 |
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
if ($event instanceof ConsoleTerminateEvent) { |
48
|
1 |
|
$this->commands[get_class($event)] = [ |
49
|
1 |
|
'name' => $event->getCommand()->getName(), |
50
|
1 |
|
'command' => $event->getCommand(), |
51
|
1 |
|
'input' => $event->getInput()->__toString(), |
52
|
1 |
|
'output' => $event->getOutput()->fetch(), |
53
|
1 |
|
'exitCode' => $event->getExitCode(), |
54
|
|
|
]; |
55
|
1 |
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
1 |
|
if ($event instanceof ConsoleEvent) { |
59
|
1 |
|
$this->commands[get_class($event)] = [ |
60
|
1 |
|
'name' => $event->getCommand()->getName(), |
61
|
1 |
|
'command' => $event->getCommand(), |
62
|
1 |
|
'input' => $event->getInput()->__toString(), |
63
|
1 |
|
'output' => $event->getOutput()->fetch(), |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
1 |
|
public function getIndexData(): array |
69
|
|
|
{ |
70
|
1 |
|
$eventTypes = [ |
71
|
|
|
ConsoleErrorEvent::class, |
72
|
|
|
ConsoleTerminateEvent::class, |
73
|
|
|
ConsoleCommandEvent::class, |
74
|
|
|
]; |
75
|
|
|
|
76
|
1 |
|
$commandEvent = null; |
77
|
1 |
|
foreach ($eventTypes as $eventType) { |
78
|
1 |
|
if (!array_key_exists($eventType, $this->commands)) { |
79
|
|
|
continue; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
$commandEvent = $this->commands[$eventType]; |
83
|
1 |
|
break; |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
if ($commandEvent === null) { |
87
|
|
|
$types = array_keys($this->commands); |
88
|
|
|
throw new RuntimeException( |
89
|
|
|
sprintf( |
90
|
|
|
'Unsupported event type encountered among "%s". Supported only "%s"', |
91
|
|
|
implode('", "', $types), |
92
|
|
|
implode('", "', $eventTypes), |
93
|
|
|
) |
94
|
|
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return [ |
98
|
|
|
'command' => [ |
99
|
1 |
|
'name' => $commandEvent['name'], |
100
|
1 |
|
'class' => $commandEvent['command'] instanceof Command ? get_class($commandEvent['command']) : null, |
101
|
1 |
|
'input' => $commandEvent['input'], |
102
|
|
|
], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
private function reset(): void |
|
|
|
|
107
|
|
|
{ |
108
|
1 |
|
$this->commands = []; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|