| 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\Command; |
| 13 |
|
| 14 |
use Symfony\Component\Console\Application; |
| 15 |
use Symfony\Component\Console\Completion\CompletionInput; |
| 16 |
use Symfony\Component\Console\Completion\CompletionSuggestions; |
| 17 |
use Symfony\Component\Console\Helper\HelperInterface; |
| 18 |
use Symfony\Component\Console\Helper\HelperSet; |
| 19 |
use Symfony\Component\Console\Input\InputDefinition; |
| 20 |
use Symfony\Component\Console\Input\InputInterface; |
| 21 |
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
| 22 |
use Symfony\Component\Console\Output\OutputInterface; |
| 23 |
use Symfony\Component\Stopwatch\Stopwatch; |
| 24 |
|
| 25 |
/** |
| 26 |
* @internal |
| 27 |
* |
| 28 |
* @author Jules Pietri <[email protected]> |
| 29 |
*/ |
| 30 |
final class TraceableCommand extends Command |
| 31 |
{ |
| 32 |
public readonly Command $command; |
| 33 |
public int $exitCode; |
| 34 |
public ?int $interruptedBySignal = null; |
| 35 |
public bool $ignoreValidation; |
| 36 |
public bool $isInteractive = false; |
| 37 |
public string $duration = 'n/a'; |
| 38 |
public string $maxMemoryUsage = 'n/a'; |
| 39 |
public InputInterface $input; |
| 40 |
public OutputInterface $output; |
| 41 |
/** @var array<string, mixed> */ |
| 42 |
public array $arguments; |
| 43 |
/** @var array<string, mixed> */ |
| 44 |
public array $options; |
| 45 |
/** @var array<string, mixed> */ |
| 46 |
public array $interactiveInputs = []; |
| 47 |
public array $handledSignals = []; |
| 48 |
public ?array $invokableCommandInfo = null; |
| 49 |
|
| 50 |
public function __construct( |
| 51 |
Command $command, |
| 52 |
private readonly Stopwatch $stopwatch, |
| 53 |
) { |
| 54 |
if ($command instanceof LazyCommand) { |
| 55 |
$command = $command->getCommand(); |
| 56 |
} |
| 57 |
|
| 58 |
$this->command = $command; |
| 59 |
|
| 60 |
// prevent call to self::getDefaultDescription() |
| 61 |
$this->setDescription($command->getDescription()); |
| 62 |
|
| 63 |
parent::__construct($command->getName()); |
| 64 |
|
| 65 |
// init below enables calling {@see parent::run()} |
| 66 |
[$code, $processTitle, $ignoreValidationErrors] = \Closure::bind(fn () => [$this->code, $this->processTitle, $this->ignoreValidationErrors], $command, Command::class)(); |
| 67 |
|
| 68 |
if (\is_callable($code)) { |
| 69 |
$this->setCode($code); |
| 70 |
} |
| 71 |
|
| 72 |
if ($processTitle) { |
| 73 |
parent::setProcessTitle($processTitle); |
| 74 |
} |
| 75 |
|
| 76 |
if ($ignoreValidationErrors) { |
| 77 |
parent::ignoreValidationErrors(); |
| 78 |
} |
| 79 |
|
| 80 |
$this->ignoreValidation = $ignoreValidationErrors; |
| 81 |
} |
| 82 |
|
| 83 |
public function __call(string $name, array $arguments): mixed |
| 84 |
{ |
| 85 |
return $this->command->{$name}(...$arguments); |
| 86 |
} |
| 87 |
|
| 88 |
public function getSubscribedSignals(): array |
| 89 |
{ |
| 90 |
return $this->command->getSubscribedSignals(); |
| 91 |
} |
| 92 |
|
| 93 |
public function handleSignal(int $signal, int|false $previousExitCode = 0): int|false |
| 94 |
{ |
| 95 |
$event = $this->stopwatch->start($this->getName().'.handle_signal'); |
| 96 |
|
| 97 |
$exit = $this->command->handleSignal($signal, $previousExitCode); |
| 98 |
|
| 99 |
$event->stop(); |
| 100 |
|
| 101 |
if (!isset($this->handledSignals[$signal])) { |
| 102 |
$this->handledSignals[$signal] = [ |
| 103 |
'handled' => 0, |
| 104 |
'duration' => 0, |
| 105 |
'memory' => 0, |
| 106 |
]; |
| 107 |
} |
| 108 |
|
| 109 |
++$this->handledSignals[$signal]['handled']; |
| 110 |
$this->handledSignals[$signal]['duration'] += $event->getDuration(); |
| 111 |
$this->handledSignals[$signal]['memory'] = max( |
| 112 |
$this->handledSignals[$signal]['memory'], |
| 113 |
$event->getMemory() >> 20 |
| 114 |
); |
| 115 |
|
| 116 |
return $exit; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* {@inheritdoc} |
| 121 |
* |
| 122 |
* Calling parent method is required to be used in {@see parent::run()}. |
| 123 |
*/ |
| 124 |
public function ignoreValidationErrors(): void |
| 125 |
{ |
| 126 |
$this->ignoreValidation = true; |
| 127 |
$this->command->ignoreValidationErrors(); |
| 128 |
|
| 129 |
parent::ignoreValidationErrors(); |
| 130 |
} |
| 131 |
|
| 132 |
public function setApplication(?Application $application = null): void |
| 133 |
{ |
| 134 |
$this->command->setApplication($application); |
| 135 |
} |
| 136 |
|
| 137 |
public function getApplication(): ?Application |
| 138 |
{ |
| 139 |
return $this->command->getApplication(); |
| 140 |
} |
| 141 |
|
| 142 |
public function setHelperSet(HelperSet $helperSet): void |
| 143 |
{ |
| 144 |
$this->command->setHelperSet($helperSet); |
| 145 |
} |
| 146 |
|
| 147 |
public function getHelperSet(): ?HelperSet |
| 148 |
{ |
| 149 |
return $this->command->getHelperSet(); |
| 150 |
} |
| 151 |
|
| 152 |
public function isEnabled(): bool |
| 153 |
{ |
| 154 |
return $this->command->isEnabled(); |
| 155 |
} |
| 156 |
|
| 157 |
public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
| 158 |
{ |
| 159 |
$this->command->complete($input, $suggestions); |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* {@inheritdoc} |
| 164 |
* |
| 165 |
* Calling parent method is required to be used in {@see parent::run()}. |
| 166 |
*/ |
| 167 |
public function setCode(callable $code): static |
| 168 |
{ |
| 169 |
if ($code instanceof InvokableCommand) { |
| 170 |
$r = \Closure::bind(fn () => $this->invokable, $code, InvokableCommand::class)(); |
| 171 |
|
| 172 |
$this->invokableCommandInfo = [ |
| 173 |
'class' => $r->getClosureScopeClass()->name, |
| 174 |
'file' => $r->getFileName(), |
| 175 |
'line' => $r->getStartLine(), |
| 176 |
]; |
| 177 |
|
| 178 |
// Pass the original callable to avoid double-wrapping in Command::setCode() |
| 179 |
$this->command->setCode($code->getCode()); |
| 180 |
} else { |
| 181 |
$this->command->setCode($code); |
| 182 |
} |
| 183 |
|
| 184 |
return parent::setCode(function (InputInterface $input, OutputInterface $output) use ($code): int { |
| 185 |
$event = $this->stopwatch->start($this->getName().'.code'); |
| 186 |
|
| 187 |
$this->exitCode = $code($input, $output); |
| 188 |
|
| 189 |
$event->stop(); |
| 190 |
|
| 191 |
return $this->exitCode; |
| 192 |
}); |
| 193 |
} |
| 194 |
|
| 195 |
/** |
| 196 |
* @internal |
| 197 |
*/ |
| 198 |
public function mergeApplicationDefinition(bool $mergeArgs = true): void |
| 199 |
{ |
| 200 |
$this->command->mergeApplicationDefinition($mergeArgs); |
| 201 |
} |
| 202 |
|
| 203 |
public function setDefinition(array|InputDefinition $definition): static |
| 204 |
{ |
| 205 |
$this->command->setDefinition($definition); |
| 206 |
|
| 207 |
return $this; |
| 208 |
} |
| 209 |
|
| 210 |
public function getDefinition(): InputDefinition |
| 211 |
{ |
| 212 |
return $this->command->getDefinition(); |
| 213 |
} |
| 214 |
|
| 215 |
public function getNativeDefinition(): InputDefinition |
| 216 |
{ |
| 217 |
return $this->command->getNativeDefinition(); |
| 218 |
} |
| 219 |
|
| 220 |
public function addArgument(string $name, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static |
| 221 |
{ |
| 222 |
$this->command->addArgument($name, $mode, $description, $default, $suggestedValues); |
| 223 |
|
| 224 |
return $this; |
| 225 |
} |
| 226 |
|
| 227 |
public function addOption(string $name, string|array|null $shortcut = null, ?int $mode = null, string $description = '', mixed $default = null, array|\Closure $suggestedValues = []): static |
| 228 |
{ |
| 229 |
$this->command->addOption($name, $shortcut, $mode, $description, $default, $suggestedValues); |
| 230 |
|
| 231 |
return $this; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* {@inheritdoc} |
| 236 |
* |
| 237 |
* Calling parent method is required to be used in {@see parent::run()}. |
| 238 |
*/ |
| 239 |
public function setProcessTitle(string $title): static |
| 240 |
{ |
| 241 |
$this->command->setProcessTitle($title); |
| 242 |
|
| 243 |
return parent::setProcessTitle($title); |
| 244 |
} |
| 245 |
|
| 246 |
public function setHelp(string $help): static |
| 247 |
{ |
| 248 |
$this->command->setHelp($help); |
| 249 |
|
| 250 |
return $this; |
| 251 |
} |
| 252 |
|
| 253 |
public function getHelp(): string |
| 254 |
{ |
| 255 |
return $this->command->getHelp(); |
| 256 |
} |
| 257 |
|
| 258 |
public function getProcessedHelp(): string |
| 259 |
{ |
| 260 |
return $this->command->getProcessedHelp(); |
| 261 |
} |
| 262 |
|
| 263 |
public function getSynopsis(bool $short = false): string |
| 264 |
{ |
| 265 |
return $this->command->getSynopsis($short); |
| 266 |
} |
| 267 |
|
| 268 |
public function addUsage(string $usage): static |
| 269 |
{ |
| 270 |
$this->command->addUsage($usage); |
| 271 |
|
| 272 |
return $this; |
| 273 |
} |
| 274 |
|
| 275 |
public function getUsages(): array |
| 276 |
{ |
| 277 |
return $this->command->getUsages(); |
| 278 |
} |
| 279 |
|
| 280 |
public function getHelper(string $name): HelperInterface |
| 281 |
{ |
| 282 |
return $this->command->getHelper($name); |
| 283 |
} |
| 284 |
|
| 285 |
public function run(InputInterface $input, OutputInterface $output): int |
| 286 |
{ |
| 287 |
$this->input = $input; |
| 288 |
$this->output = $output; |
| 289 |
$initialArguments = $input->getArguments(); |
| 290 |
$initialOptions = $input->getOptions(); |
| 291 |
$event = $this->stopwatch->start($this->getName(), 'command'); |
| 292 |
|
| 293 |
try { |
| 294 |
$this->exitCode = $this->command->run($input, $output); |
| 295 |
} finally { |
| 296 |
$event->stop(); |
| 297 |
|
| 298 |
if ($output instanceof ConsoleOutputInterface && $output->isDebug()) { |
| 299 |
$output->getErrorOutput()->writeln((string) $event); |
| 300 |
} |
| 301 |
|
| 302 |
$this->duration = $event->getDuration().' ms'; |
| 303 |
$this->maxMemoryUsage = ($event->getMemory() >> 20).' MiB'; |
| 304 |
|
| 305 |
$this->arguments = $input->getArguments(); |
| 306 |
$this->options = $input->getOptions(); |
| 307 |
|
| 308 |
$this->extractInteractiveInputs($initialArguments, $initialOptions); |
| 309 |
$this->isInteractive = $this->isInteractive || $this->interactiveInputs; |
| 310 |
} |
| 311 |
|
| 312 |
return $this->exitCode; |
| 313 |
} |
| 314 |
|
| 315 |
protected function initialize(InputInterface $input, OutputInterface $output): void |
| 316 |
{ |
| 317 |
$event = $this->stopwatch->start($this->getName().'.init', 'command'); |
| 318 |
|
| 319 |
$this->command->initialize($input, $output); |
| 320 |
|
| 321 |
$event->stop(); |
| 322 |
} |
| 323 |
|
| 324 |
protected function interact(InputInterface $input, OutputInterface $output): void |
| 325 |
{ |
| 326 |
if (!$this->isInteractive = Command::class !== (new \ReflectionMethod($this->command, 'interact'))->getDeclaringClass()->getName()) { |
| 327 |
return; |
| 328 |
} |
| 329 |
|
| 330 |
$event = $this->stopwatch->start($this->getName().'.interact', 'command'); |
| 331 |
|
| 332 |
$this->command->interact($input, $output); |
| 333 |
|
| 334 |
$event->stop(); |
| 335 |
} |
| 336 |
|
| 337 |
protected function execute(InputInterface $input, OutputInterface $output): int |
| 338 |
{ |
| 339 |
$event = $this->stopwatch->start($this->getName().'.execute', 'command'); |
| 340 |
|
| 341 |
$exitCode = $this->command->execute($input, $output); |
| 342 |
|
| 343 |
$event->stop(); |
| 344 |
|
| 345 |
return $exitCode; |
| 346 |
} |
| 347 |
|
| 348 |
private function extractInteractiveInputs(array $initialArguments, array $initialOptions): void |
| 349 |
{ |
| 350 |
$nativeDefinition = $this->command->getNativeDefinition(); |
| 351 |
|
| 352 |
foreach ($nativeDefinition->getArguments() as $argName => $argument) { |
| 353 |
if (\array_key_exists($argName, $initialArguments) && $initialArguments[$argName] === $this->arguments[$argName]) { |
| 354 |
continue; |
| 355 |
} |
| 356 |
|
| 357 |
$this->interactiveInputs[$argName] = $this->arguments[$argName]; |
| 358 |
} |
| 359 |
|
| 360 |
foreach ($nativeDefinition->getOptions() as $optName => $option) { |
| 361 |
if (\array_key_exists($optName, $initialOptions) && $initialOptions[$optName] === $this->options[$optName]) { |
| 362 |
continue; |
| 363 |
} |
| 364 |
|
| 365 |
$this->interactiveInputs['--'.$optName] = $this->options[$optName]; |
| 366 |
} |
| 367 |
} |
| 368 |
} |