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