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