| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
* This file is part of the Symfony package. |
| 5 |
* |
| 6 |
* (c) Fabien Potencier <[email protected]> |
| 7 |
* |
| 8 |
* For the full copyright and license information, please view the LICENSE |
| 9 |
* file that was distributed with this source code. |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace Symfony\Component\Console\DataCollector; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Command\Command; |
| 15 |
use Symfony\Component\Console\Debug\CliRequest; |
| 16 |
use Symfony\Component\Console\Output\OutputInterface; |
| 17 |
use Symfony\Component\Console\SignalRegistry\SignalMap; |
| 18 |
use Symfony\Component\HttpFoundation\Request; |
| 19 |
use Symfony\Component\HttpFoundation\Response; |
| 20 |
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
| 21 |
use Symfony\Component\VarDumper\Cloner\Data; |
| 22 |
|
| 23 |
/** |
| 24 |
* @internal |
| 25 |
* |
| 26 |
* @author Jules Pietri <[email protected]> |
| 27 |
*/ |
| 28 |
final class CommandDataCollector extends DataCollector |
| 29 |
{ |
| 30 |
public function collect(Request $request, Response $response, ?\Throwable $exception = null): void |
| 31 |
{ |
| 32 |
if (!$request instanceof CliRequest) { |
| 33 |
return; |
| 34 |
} |
| 35 |
|
| 36 |
$command = $request->command; |
| 37 |
$application = $command->getApplication(); |
| 38 |
|
| 39 |
$this->data = [ |
| 40 |
'command' => $command->invokableCommandInfo ?? $this->cloneVar($command->command), |
| 41 |
'exit_code' => $command->exitCode, |
| 42 |
'interrupted_by_signal' => $command->interruptedBySignal, |
| 43 |
'duration' => $command->duration, |
| 44 |
'max_memory_usage' => $command->maxMemoryUsage, |
| 45 |
'verbosity_level' => match ($command->output->getVerbosity()) { |
| 46 |
OutputInterface::VERBOSITY_SILENT => 'silent', |
| 47 |
OutputInterface::VERBOSITY_QUIET => 'quiet', |
| 48 |
OutputInterface::VERBOSITY_NORMAL => 'normal', |
| 49 |
OutputInterface::VERBOSITY_VERBOSE => 'verbose', |
| 50 |
OutputInterface::VERBOSITY_VERY_VERBOSE => 'very verbose', |
| 51 |
OutputInterface::VERBOSITY_DEBUG => 'debug', |
| 52 |
}, |
| 53 |
'interactive' => $command->isInteractive, |
| 54 |
'validate_input' => !$command->ignoreValidation, |
| 55 |
'enabled' => $command->isEnabled(), |
| 56 |
'visible' => !$command->isHidden(), |
| 57 |
'input' => $this->cloneVar($command->input), |
| 58 |
'output' => $this->cloneVar($command->output), |
| 59 |
'interactive_inputs' => array_map($this->cloneVar(...), $command->interactiveInputs), |
| 60 |
'signalable' => $command->getSubscribedSignals(), |
| 61 |
'handled_signals' => $command->handledSignals, |
| 62 |
'helper_set' => array_map($this->cloneVar(...), iterator_to_array($command->getHelperSet())), |
| 63 |
]; |
| 64 |
|
| 65 |
$baseDefinition = $application->getDefinition(); |
| 66 |
|
| 67 |
foreach ($command->arguments as $argName => $argValue) { |
| 68 |
if ($baseDefinition->hasArgument($argName)) { |
| 69 |
$this->data['application_inputs'][$argName] = $this->cloneVar($argValue); |
| 70 |
} else { |
| 71 |
$this->data['arguments'][$argName] = $this->cloneVar($argValue); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
foreach ($command->options as $optName => $optValue) { |
| 76 |
if ($baseDefinition->hasOption($optName)) { |
| 77 |
$this->data['application_inputs']['--'.$optName] = $this->cloneVar($optValue); |
| 78 |
} else { |
| 79 |
$this->data['options'][$optName] = $this->cloneVar($optValue); |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
public function getName(): string |
| 85 |
{ |
| 86 |
return 'command'; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* @return array{ |
| 91 |
* class?: class-string, |
| 92 |
* executor?: string, |
| 93 |
* file: string, |
| 94 |
* line: int, |
| 95 |
* } |
| 96 |
*/ |
| 97 |
public function getCommand(): array |
| 98 |
{ |
| 99 |
if (\is_array($this->data['command'])) { |
| 100 |
return $this->data['command']; |
| 101 |
} |
| 102 |
|
| 103 |
$class = $this->data['command']->getType(); |
| 104 |
$r = new \ReflectionMethod($class, 'execute'); |
| 105 |
|
| 106 |
if (Command::class !== $r->getDeclaringClass()) { |
| 107 |
return [ |
| 108 |
'executor' => $class.'::'.$r->name, |
| 109 |
'file' => $r->getFileName(), |
| 110 |
'line' => $r->getStartLine(), |
| 111 |
]; |
| 112 |
} |
| 113 |
|
| 114 |
$r = new \ReflectionClass($class); |
| 115 |
|
| 116 |
return [ |
| 117 |
'class' => $class, |
| 118 |
'file' => $r->getFileName(), |
| 119 |
'line' => $r->getStartLine(), |
| 120 |
]; |
| 121 |
} |
| 122 |
|
| 123 |
public function getInterruptedBySignal(): ?string |
| 124 |
{ |
| 125 |
if (isset($this->data['interrupted_by_signal'])) { |
| 126 |
return \sprintf('%s (%d)', SignalMap::getSignalName($this->data['interrupted_by_signal']), $this->data['interrupted_by_signal']); |
| 127 |
} |
| 128 |
|
| 129 |
return null; |
| 130 |
} |
| 131 |
|
| 132 |
public function getDuration(): string |
| 133 |
{ |
| 134 |
return $this->data['duration']; |
| 135 |
} |
| 136 |
|
| 137 |
public function getMaxMemoryUsage(): string |
| 138 |
{ |
| 139 |
return $this->data['max_memory_usage']; |
| 140 |
} |
| 141 |
|
| 142 |
public function getVerbosityLevel(): string |
| 143 |
{ |
| 144 |
return $this->data['verbosity_level']; |
| 145 |
} |
| 146 |
|
| 147 |
public function getInteractive(): bool |
| 148 |
{ |
| 149 |
return $this->data['interactive']; |
| 150 |
} |
| 151 |
|
| 152 |
public function getValidateInput(): bool |
| 153 |
{ |
| 154 |
return $this->data['validate_input']; |
| 155 |
} |
| 156 |
|
| 157 |
public function getEnabled(): bool |
| 158 |
{ |
| 159 |
return $this->data['enabled']; |
| 160 |
} |
| 161 |
|
| 162 |
public function getVisible(): bool |
| 163 |
{ |
| 164 |
return $this->data['visible']; |
| 165 |
} |
| 166 |
|
| 167 |
public function getInput(): Data |
| 168 |
{ |
| 169 |
return $this->data['input']; |
| 170 |
} |
| 171 |
|
| 172 |
public function getOutput(): Data |
| 173 |
{ |
| 174 |
return $this->data['output']; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* @return Data[] |
| 179 |
*/ |
| 180 |
public function getArguments(): array |
| 181 |
{ |
| 182 |
return $this->data['arguments'] ?? []; |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* @return Data[] |
| 187 |
*/ |
| 188 |
public function getOptions(): array |
| 189 |
{ |
| 190 |
return $this->data['options'] ?? []; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* @return Data[] |
| 195 |
*/ |
| 196 |
public function getApplicationInputs(): array |
| 197 |
{ |
| 198 |
return $this->data['application_inputs'] ?? []; |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* @return Data[] |
| 203 |
*/ |
| 204 |
public function getInteractiveInputs(): array |
| 205 |
{ |
| 206 |
return $this->data['interactive_inputs'] ?? []; |
| 207 |
} |
| 208 |
|
| 209 |
public function getSignalable(): array |
| 210 |
{ |
| 211 |
return array_map( |
| 212 |
static fn (int $signal): string => \sprintf('%s (%d)', SignalMap::getSignalName($signal), $signal), |
| 213 |
$this->data['signalable'] |
| 214 |
); |
| 215 |
} |
| 216 |
|
| 217 |
public function getHandledSignals(): array |
| 218 |
{ |
| 219 |
$keys = array_map( |
| 220 |
static fn (int $signal): string => \sprintf('%s (%d)', SignalMap::getSignalName($signal), $signal), |
| 221 |
array_keys($this->data['handled_signals']) |
| 222 |
); |
| 223 |
|
| 224 |
return array_combine($keys, array_values($this->data['handled_signals'])); |
| 225 |
} |
| 226 |
|
| 227 |
/** |
| 228 |
* @return Data[] |
| 229 |
*/ |
| 230 |
public function getHelperSet(): array |
| 231 |
{ |
| 232 |
return $this->data['helper_set'] ?? []; |
| 233 |
} |
| 234 |
|
| 235 |
public function reset(): void |
| 236 |
{ |
| 237 |
$this->data = []; |
| 238 |
} |
| 239 |
} |