1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Yii\Debug\Collector\Console; |
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
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
15
|
|
|
use Yiisoft\Yii\Console\Output\ConsoleBufferedOutput; |
16
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorInterface; |
17
|
|
|
use Yiisoft\Yii\Debug\Collector\CollectorTrait; |
18
|
|
|
use Yiisoft\Yii\Debug\Collector\IndexCollectorInterface; |
19
|
|
|
|
20
|
|
|
use function is_object; |
21
|
|
|
|
22
|
|
|
final class CommandCollector implements CollectorInterface, IndexCollectorInterface |
23
|
|
|
{ |
24
|
|
|
use CollectorTrait; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Let -1 mean that it was not set during the process. |
28
|
|
|
*/ |
29
|
|
|
private const UNDEFINED_EXIT_CODE = -1; |
30
|
|
|
private array $commands = []; |
31
|
|
|
|
32
|
|
|
public function getCollected(): array |
33
|
|
|
{ |
34
|
|
|
return $this->commands; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function collect(ConsoleEvent|ConsoleErrorEvent|ConsoleTerminateEvent $event): void |
38
|
|
|
{ |
39
|
|
|
if (!is_object($event) || !$this->isActive()) { |
40
|
|
|
return; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
$command = $event->getCommand(); |
44
|
|
|
|
45
|
|
|
if ($event instanceof ConsoleErrorEvent) { |
46
|
|
|
$this->commands[$event::class] = [ |
47
|
|
|
'name' => $event->getInput()->getFirstArgument() ?? '', |
48
|
|
|
'command' => $command, |
49
|
|
|
'input' => $this->castInputToString($event->getInput()), |
50
|
|
|
'output' => $this->fetchOutput($event->getOutput()), |
51
|
|
|
'error' => $event->getError()->getMessage(), |
52
|
|
|
'exitCode' => $event->getExitCode(), |
53
|
|
|
]; |
54
|
|
|
|
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if ($event instanceof ConsoleTerminateEvent) { |
59
|
|
|
$this->commands[$event::class] = [ |
60
|
|
|
'name' => $command->getName(), |
61
|
|
|
'command' => $command, |
62
|
|
|
'input' => $this->castInputToString($event->getInput()), |
63
|
|
|
'output' => $this->fetchOutput($event->getOutput()), |
64
|
|
|
'exitCode' => $event->getExitCode(), |
65
|
|
|
]; |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ($event instanceof ConsoleEvent) { |
|
|
|
|
70
|
|
|
$this->commands[$event::class] = [ |
71
|
|
|
'name' => $command->getName(), |
72
|
|
|
'command' => $command, |
73
|
|
|
'input' => $this->castInputToString($event->getInput()), |
74
|
|
|
'output' => $this->fetchOutput($event->getOutput()), |
75
|
|
|
'arguments' => $command->getDefinition()->getArguments(), |
76
|
|
|
'options' => $command->getDefinition()->getOptions(), |
77
|
|
|
]; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getIndexData(): array |
82
|
|
|
{ |
83
|
|
|
$eventTypes = [ |
84
|
|
|
ConsoleErrorEvent::class, |
85
|
|
|
ConsoleTerminateEvent::class, |
86
|
|
|
ConsoleCommandEvent::class, |
87
|
|
|
]; |
88
|
|
|
|
89
|
|
|
$commandEvent = null; |
90
|
|
|
foreach ($eventTypes as $eventType) { |
91
|
|
|
if (!array_key_exists($eventType, $this->commands)) { |
92
|
|
|
continue; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$commandEvent = $this->commands[$eventType]; |
96
|
|
|
break; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if ($commandEvent === null) { |
100
|
|
|
$types = array_keys($this->commands); |
101
|
|
|
throw new RuntimeException( |
102
|
|
|
sprintf( |
103
|
|
|
'Unsupported event type encountered among "%s". Supported only "%s"', |
104
|
|
|
implode('", "', $types), |
105
|
|
|
implode('", "', $eventTypes), |
106
|
|
|
) |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return [ |
111
|
|
|
'command' => [ |
112
|
|
|
'name' => $commandEvent['name'], |
113
|
|
|
'class' => $commandEvent['command'] instanceof Command ? $commandEvent['command']::class : null, |
114
|
|
|
'input' => $commandEvent['input'], |
115
|
|
|
'exitCode' => $commandEvent['exitCode'] ?? self::UNDEFINED_EXIT_CODE, |
116
|
|
|
], |
117
|
|
|
]; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
private function reset(): void |
|
|
|
|
121
|
|
|
{ |
122
|
|
|
$this->commands = []; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
private function fetchOutput(OutputInterface $output): ?string |
126
|
|
|
{ |
127
|
|
|
return $output instanceof ConsoleBufferedOutput ? $output->fetch() : null; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
private function castInputToString(InputInterface $input): ?string |
131
|
|
|
{ |
132
|
|
|
return method_exists($input, '__toString') ? $input->__toString() : null; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|