Total Complexity | 249 |
Total Lines | 1207 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Application often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Application, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
73 | class Application implements ResetInterface |
||
74 | { |
||
75 | private array $commands = []; |
||
76 | private bool $wantHelps = false; |
||
77 | private ?Command $runningCommand = null; |
||
78 | private ?CommandLoaderInterface $commandLoader = null; |
||
79 | private bool $catchExceptions = true; |
||
80 | private bool $catchErrors = false; |
||
81 | private bool $autoExit = true; |
||
82 | private InputDefinition $definition; |
||
83 | private HelperSet $helperSet; |
||
84 | private ?EventDispatcherInterface $dispatcher = null; |
||
85 | private Terminal $terminal; |
||
86 | private string $defaultCommand; |
||
87 | private bool $singleCommand = false; |
||
88 | private bool $initialized = false; |
||
89 | private ?SignalRegistry $signalRegistry = null; |
||
90 | private array $signalsToDispatchEvent = []; |
||
91 | |||
92 | public function __construct( |
||
93 | private string $name = 'UNKNOWN', |
||
94 | private string $version = 'UNKNOWN', |
||
95 | ) { |
||
96 | $this->terminal = new Terminal(); |
||
97 | $this->defaultCommand = 'list'; |
||
98 | if (\defined('SIGINT') && SignalRegistry::isSupported()) { |
||
99 | $this->signalRegistry = new SignalRegistry(); |
||
100 | $this->signalsToDispatchEvent = [\SIGINT, \SIGQUIT, \SIGTERM, \SIGUSR1, \SIGUSR2]; |
||
101 | } |
||
102 | } |
||
103 | |||
104 | /** |
||
105 | * @final |
||
106 | */ |
||
107 | public function setDispatcher(EventDispatcherInterface $dispatcher): void |
||
108 | { |
||
109 | $this->dispatcher = $dispatcher; |
||
110 | } |
||
111 | |||
112 | public function setCommandLoader(CommandLoaderInterface $commandLoader): void |
||
113 | { |
||
114 | $this->commandLoader = $commandLoader; |
||
115 | } |
||
116 | |||
117 | public function getSignalRegistry(): SignalRegistry |
||
118 | { |
||
119 | if (!$this->signalRegistry) { |
||
120 | throw new RuntimeException('Signals are not supported. Make sure that the "pcntl" extension is installed and that "pcntl_*" functions are not disabled by your php.ini\'s "disable_functions" directive.'); |
||
121 | } |
||
122 | |||
123 | return $this->signalRegistry; |
||
124 | } |
||
125 | |||
126 | public function setSignalsToDispatchEvent(int ...$signalsToDispatchEvent): void |
||
127 | { |
||
128 | $this->signalsToDispatchEvent = $signalsToDispatchEvent; |
||
129 | } |
||
130 | |||
131 | /** |
||
132 | * Runs the current application. |
||
133 | * |
||
134 | * @return int 0 if everything went fine, or an error code |
||
135 | * |
||
136 | * @throws \Exception When running fails. Bypass this when {@link setCatchExceptions()}. |
||
137 | */ |
||
138 | public function run(?InputInterface $input = null, ?OutputInterface $output = null): int |
||
139 | { |
||
140 | if (\function_exists('putenv')) { |
||
141 | @putenv('LINES='.$this->terminal->getHeight()); |
||
142 | @putenv('COLUMNS='.$this->terminal->getWidth()); |
||
143 | } |
||
144 | |||
145 | $input ??= new ArgvInput(); |
||
146 | $output ??= new ConsoleOutput(); |
||
147 | |||
148 | $renderException = function (\Throwable $e) use ($output) { |
||
149 | if ($output instanceof ConsoleOutputInterface) { |
||
150 | $this->renderThrowable($e, $output->getErrorOutput()); |
||
151 | } else { |
||
152 | $this->renderThrowable($e, $output); |
||
153 | } |
||
154 | }; |
||
155 | if ($phpHandler = set_exception_handler($renderException)) { |
||
156 | restore_exception_handler(); |
||
157 | if (!\is_array($phpHandler) || !$phpHandler[0] instanceof ErrorHandler) { |
||
158 | $errorHandler = true; |
||
159 | } elseif ($errorHandler = $phpHandler[0]->setExceptionHandler($renderException)) { |
||
160 | $phpHandler[0]->setExceptionHandler($errorHandler); |
||
161 | } |
||
162 | } |
||
163 | |||
164 | try { |
||
165 | $this->configureIO($input, $output); |
||
166 | |||
167 | $exitCode = $this->doRun($input, $output); |
||
168 | } catch (\Throwable $e) { |
||
169 | if ($e instanceof \Exception && !$this->catchExceptions) { |
||
170 | throw $e; |
||
171 | } |
||
172 | if (!$e instanceof \Exception && !$this->catchErrors) { |
||
173 | throw $e; |
||
174 | } |
||
175 | |||
176 | $renderException($e); |
||
177 | |||
178 | $exitCode = $e->getCode(); |
||
179 | if (is_numeric($exitCode)) { |
||
180 | $exitCode = (int) $exitCode; |
||
181 | if ($exitCode <= 0) { |
||
182 | $exitCode = 1; |
||
183 | } |
||
184 | } else { |
||
185 | $exitCode = 1; |
||
186 | } |
||
187 | } finally { |
||
188 | // if the exception handler changed, keep it |
||
189 | // otherwise, unregister $renderException |
||
190 | if (!$phpHandler) { |
||
191 | if (set_exception_handler($renderException) === $renderException) { |
||
192 | restore_exception_handler(); |
||
193 | } |
||
194 | restore_exception_handler(); |
||
195 | } elseif (!$errorHandler) { |
||
196 | $finalHandler = $phpHandler[0]->setExceptionHandler(null); |
||
197 | if ($finalHandler !== $renderException) { |
||
198 | $phpHandler[0]->setExceptionHandler($finalHandler); |
||
199 | } |
||
200 | } |
||
201 | } |
||
202 | |||
203 | if ($this->autoExit) { |
||
204 | if ($exitCode > 255) { |
||
205 | $exitCode = 255; |
||
206 | } |
||
207 | |||
208 | exit($exitCode); |
||
209 | } |
||
210 | |||
211 | return $exitCode; |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Runs the current application. |
||
216 | * |
||
217 | * @return int 0 if everything went fine, or an error code |
||
218 | */ |
||
219 | public function doRun(InputInterface $input, OutputInterface $output): int |
||
220 | { |
||
221 | if (true === $input->hasParameterOption(['--version', '-V'], true)) { |
||
222 | $output->writeln($this->getLongVersion()); |
||
223 | |||
224 | return 0; |
||
225 | } |
||
226 | |||
227 | try { |
||
228 | // Makes ArgvInput::getFirstArgument() able to distinguish an option from an argument. |
||
229 | $input->bind($this->getDefinition()); |
||
230 | } catch (ExceptionInterface) { |
||
231 | // Errors must be ignored, full binding/validation happens later when the command is known. |
||
232 | } |
||
233 | |||
234 | $name = $this->getCommandName($input); |
||
235 | if (true === $input->hasParameterOption(['--help', '-h'], true)) { |
||
236 | if (!$name) { |
||
237 | $name = 'help'; |
||
238 | $input = new ArrayInput(['command_name' => $this->defaultCommand]); |
||
239 | } else { |
||
240 | $this->wantHelps = true; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | if (!$name) { |
||
245 | $name = $this->defaultCommand; |
||
246 | $definition = $this->getDefinition(); |
||
247 | $definition->setArguments(array_merge( |
||
248 | $definition->getArguments(), |
||
249 | [ |
||
250 | 'command' => new InputArgument('command', InputArgument::OPTIONAL, $definition->getArgument('command')->getDescription(), $name), |
||
251 | ] |
||
252 | )); |
||
253 | } |
||
254 | |||
255 | try { |
||
256 | $this->runningCommand = null; |
||
257 | // the command name MUST be the first element of the input |
||
258 | $command = $this->find($name); |
||
259 | } catch (\Throwable $e) { |
||
260 | if (($e instanceof CommandNotFoundException && !$e instanceof NamespaceNotFoundException) && 1 === \count($alternatives = $e->getAlternatives()) && $input->isInteractive()) { |
||
261 | $alternative = $alternatives[0]; |
||
262 | |||
263 | $style = new SymfonyStyle($input, $output); |
||
264 | $output->writeln(''); |
||
265 | $formattedBlock = (new FormatterHelper())->formatBlock(sprintf('Command "%s" is not defined.', $name), 'error', true); |
||
266 | $output->writeln($formattedBlock); |
||
267 | if (!$style->confirm(sprintf('Do you want to run "%s" instead? ', $alternative), false)) { |
||
268 | if (null !== $this->dispatcher) { |
||
269 | $event = new ConsoleErrorEvent($input, $output, $e); |
||
270 | $this->dispatcher->dispatch($event, ConsoleEvents::ERROR); |
||
271 | |||
272 | return $event->getExitCode(); |
||
273 | } |
||
274 | |||
275 | return 1; |
||
276 | } |
||
277 | |||
278 | $command = $this->find($alternative); |
||
279 | } else { |
||
280 | if (null !== $this->dispatcher) { |
||
281 | $event = new ConsoleErrorEvent($input, $output, $e); |
||
282 | $this->dispatcher->dispatch($event, ConsoleEvents::ERROR); |
||
283 | |||
284 | if (0 === $event->getExitCode()) { |
||
285 | return 0; |
||
286 | } |
||
287 | |||
288 | $e = $event->getError(); |
||
289 | } |
||
290 | |||
291 | try { |
||
292 | if ($e instanceof CommandNotFoundException && $namespace = $this->findNamespace($name)) { |
||
293 | $helper = new DescriptorHelper(); |
||
294 | $helper->describe($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output, $this, [ |
||
295 | 'format' => 'txt', |
||
296 | 'raw_text' => false, |
||
297 | 'namespace' => $namespace, |
||
298 | 'short' => false, |
||
299 | ]); |
||
300 | |||
301 | return isset($event) ? $event->getExitCode() : 1; |
||
302 | } |
||
303 | |||
304 | throw $e; |
||
305 | } catch (NamespaceNotFoundException) { |
||
306 | throw $e; |
||
307 | } |
||
308 | } |
||
309 | } |
||
310 | |||
311 | if ($command instanceof LazyCommand) { |
||
312 | $command = $command->getCommand(); |
||
313 | } |
||
314 | |||
315 | $this->runningCommand = $command; |
||
316 | $exitCode = $this->doRunCommand($command, $input, $output); |
||
317 | $this->runningCommand = null; |
||
318 | |||
319 | return $exitCode; |
||
320 | } |
||
321 | |||
322 | public function reset(): void |
||
323 | { |
||
324 | } |
||
325 | |||
326 | public function setHelperSet(HelperSet $helperSet): void |
||
327 | { |
||
328 | $this->helperSet = $helperSet; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Get the helper set associated with the command. |
||
333 | */ |
||
334 | public function getHelperSet(): HelperSet |
||
335 | { |
||
336 | return $this->helperSet ??= $this->getDefaultHelperSet(); |
||
337 | } |
||
338 | |||
339 | public function setDefinition(InputDefinition $definition): void |
||
340 | { |
||
341 | $this->definition = $definition; |
||
342 | } |
||
343 | |||
344 | /** |
||
345 | * Gets the InputDefinition related to this Application. |
||
346 | */ |
||
347 | public function getDefinition(): InputDefinition |
||
348 | { |
||
349 | $this->definition ??= $this->getDefaultInputDefinition(); |
||
350 | |||
351 | if ($this->singleCommand) { |
||
352 | $inputDefinition = $this->definition; |
||
353 | $inputDefinition->setArguments(); |
||
354 | |||
355 | return $inputDefinition; |
||
356 | } |
||
357 | |||
358 | return $this->definition; |
||
359 | } |
||
360 | |||
361 | /** |
||
362 | * Adds suggestions to $suggestions for the current completion input (e.g. option or argument). |
||
363 | */ |
||
364 | public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void |
||
365 | { |
||
366 | if ( |
||
367 | CompletionInput::TYPE_ARGUMENT_VALUE === $input->getCompletionType() |
||
368 | && 'command' === $input->getCompletionName() |
||
369 | ) { |
||
370 | foreach ($this->all() as $name => $command) { |
||
371 | // skip hidden commands and aliased commands as they already get added below |
||
372 | if ($command->isHidden() || $command->getName() !== $name) { |
||
373 | continue; |
||
374 | } |
||
375 | $suggestions->suggestValue(new Suggestion($command->getName(), $command->getDescription())); |
||
376 | foreach ($command->getAliases() as $name) { |
||
377 | $suggestions->suggestValue(new Suggestion($name, $command->getDescription())); |
||
378 | } |
||
379 | } |
||
380 | |||
381 | return; |
||
382 | } |
||
383 | |||
384 | if (CompletionInput::TYPE_OPTION_NAME === $input->getCompletionType()) { |
||
385 | $suggestions->suggestOptions($this->getDefinition()->getOptions()); |
||
386 | |||
387 | return; |
||
388 | } |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * Gets the help message. |
||
393 | */ |
||
394 | public function getHelp(): string |
||
395 | { |
||
396 | return $this->getLongVersion(); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * Gets whether to catch exceptions or not during commands execution. |
||
401 | */ |
||
402 | public function areExceptionsCaught(): bool |
||
403 | { |
||
404 | return $this->catchExceptions; |
||
405 | } |
||
406 | |||
407 | /** |
||
408 | * Sets whether to catch exceptions or not during commands execution. |
||
409 | */ |
||
410 | public function setCatchExceptions(bool $boolean): void |
||
411 | { |
||
412 | $this->catchExceptions = $boolean; |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Sets whether to catch errors or not during commands execution. |
||
417 | */ |
||
418 | public function setCatchErrors(bool $catchErrors = true): void |
||
419 | { |
||
420 | $this->catchErrors = $catchErrors; |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * Gets whether to automatically exit after a command execution or not. |
||
425 | */ |
||
426 | public function isAutoExitEnabled(): bool |
||
427 | { |
||
428 | return $this->autoExit; |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * Sets whether to automatically exit after a command execution or not. |
||
433 | */ |
||
434 | public function setAutoExit(bool $boolean): void |
||
435 | { |
||
436 | $this->autoExit = $boolean; |
||
437 | } |
||
438 | |||
439 | /** |
||
440 | * Gets the name of the application. |
||
441 | */ |
||
442 | public function getName(): string |
||
443 | { |
||
444 | return $this->name; |
||
445 | } |
||
446 | |||
447 | /** |
||
448 | * Sets the application name. |
||
449 | */ |
||
450 | public function setName(string $name): void |
||
451 | { |
||
452 | $this->name = $name; |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * Gets the application version. |
||
457 | */ |
||
458 | public function getVersion(): string |
||
459 | { |
||
460 | return $this->version; |
||
461 | } |
||
462 | |||
463 | /** |
||
464 | * Sets the application version. |
||
465 | */ |
||
466 | public function setVersion(string $version): void |
||
467 | { |
||
468 | $this->version = $version; |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Returns the long version of the application. |
||
473 | */ |
||
474 | public function getLongVersion(): string |
||
475 | { |
||
476 | if ('UNKNOWN' !== $this->getName()) { |
||
477 | if ('UNKNOWN' !== $this->getVersion()) { |
||
478 | return sprintf('%s <info>%s</info>', $this->getName(), $this->getVersion()); |
||
479 | } |
||
480 | |||
481 | return $this->getName(); |
||
482 | } |
||
483 | |||
484 | return 'Console Tool'; |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * Registers a new command. |
||
489 | */ |
||
490 | public function register(string $name): Command |
||
491 | { |
||
492 | return $this->add(new Command($name)); |
||
493 | } |
||
494 | |||
495 | /** |
||
496 | * Adds an array of command objects. |
||
497 | * |
||
498 | * If a Command is not enabled it will not be added. |
||
499 | * |
||
500 | * @param Command[] $commands An array of commands |
||
501 | */ |
||
502 | public function addCommands(array $commands): void |
||
503 | { |
||
504 | foreach ($commands as $command) { |
||
505 | $this->add($command); |
||
506 | } |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * Adds a command object. |
||
511 | * |
||
512 | * If a command with the same name already exists, it will be overridden. |
||
513 | * If the command is not enabled it will not be added. |
||
514 | */ |
||
515 | public function add(Command $command): ?Command |
||
516 | { |
||
517 | $this->init(); |
||
518 | |||
519 | $command->setApplication($this); |
||
520 | |||
521 | if (!$command->isEnabled()) { |
||
522 | $command->setApplication(null); |
||
523 | |||
524 | return null; |
||
525 | } |
||
526 | |||
527 | if (!$command instanceof LazyCommand) { |
||
528 | // Will throw if the command is not correctly initialized. |
||
529 | $command->getDefinition(); |
||
530 | } |
||
531 | |||
532 | if (!$command->getName()) { |
||
533 | throw new LogicException(sprintf('The command defined in "%s" cannot have an empty name.', get_debug_type($command))); |
||
534 | } |
||
535 | |||
536 | $this->commands[$command->getName()] = $command; |
||
537 | |||
538 | foreach ($command->getAliases() as $alias) { |
||
539 | $this->commands[$alias] = $command; |
||
540 | } |
||
541 | |||
542 | return $command; |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * Returns a registered command by name or alias. |
||
547 | * |
||
548 | * @throws CommandNotFoundException When given command name does not exist |
||
549 | */ |
||
550 | public function get(string $name): Command |
||
551 | { |
||
552 | $this->init(); |
||
553 | |||
554 | if (!$this->has($name)) { |
||
555 | throw new CommandNotFoundException(sprintf('The command "%s" does not exist.', $name)); |
||
556 | } |
||
557 | |||
558 | // When the command has a different name than the one used at the command loader level |
||
559 | if (!isset($this->commands[$name])) { |
||
560 | throw new CommandNotFoundException(sprintf('The "%s" command cannot be found because it is registered under multiple names. Make sure you don\'t set a different name via constructor or "setName()".', $name)); |
||
561 | } |
||
562 | |||
563 | $command = $this->commands[$name]; |
||
564 | |||
565 | if ($this->wantHelps) { |
||
566 | $this->wantHelps = false; |
||
567 | |||
568 | $helpCommand = $this->get('help'); |
||
569 | $helpCommand->setCommand($command); |
||
570 | |||
571 | return $helpCommand; |
||
572 | } |
||
573 | |||
574 | return $command; |
||
575 | } |
||
576 | |||
577 | /** |
||
578 | * Returns true if the command exists, false otherwise. |
||
579 | */ |
||
580 | public function has(string $name): bool |
||
581 | { |
||
582 | $this->init(); |
||
583 | |||
584 | return isset($this->commands[$name]) || ($this->commandLoader?->has($name) && $this->add($this->commandLoader->get($name))); |
||
585 | } |
||
586 | |||
587 | /** |
||
588 | * Returns an array of all unique namespaces used by currently registered commands. |
||
589 | * |
||
590 | * It does not return the global namespace which always exists. |
||
591 | * |
||
592 | * @return string[] |
||
593 | */ |
||
594 | public function getNamespaces(): array |
||
595 | { |
||
596 | $namespaces = []; |
||
597 | foreach ($this->all() as $command) { |
||
598 | if ($command->isHidden()) { |
||
599 | continue; |
||
600 | } |
||
601 | |||
602 | $namespaces[] = $this->extractAllNamespaces($command->getName()); |
||
603 | |||
604 | foreach ($command->getAliases() as $alias) { |
||
605 | $namespaces[] = $this->extractAllNamespaces($alias); |
||
606 | } |
||
607 | } |
||
608 | |||
609 | return array_values(array_unique(array_filter(array_merge([], ...$namespaces)))); |
||
610 | } |
||
611 | |||
612 | /** |
||
613 | * Finds a registered namespace by a name or an abbreviation. |
||
614 | * |
||
615 | * @throws NamespaceNotFoundException When namespace is incorrect or ambiguous |
||
616 | */ |
||
617 | public function findNamespace(string $namespace): string |
||
618 | { |
||
619 | $allNamespaces = $this->getNamespaces(); |
||
620 | $expr = implode('[^:]*:', array_map('preg_quote', explode(':', $namespace))).'[^:]*'; |
||
621 | $namespaces = preg_grep('{^'.$expr.'}', $allNamespaces); |
||
622 | |||
623 | if (!$namespaces) { |
||
624 | $message = sprintf('There are no commands defined in the "%s" namespace.', $namespace); |
||
625 | |||
626 | if ($alternatives = $this->findAlternatives($namespace, $allNamespaces)) { |
||
627 | if (1 == \count($alternatives)) { |
||
628 | $message .= "\n\nDid you mean this?\n "; |
||
629 | } else { |
||
630 | $message .= "\n\nDid you mean one of these?\n "; |
||
631 | } |
||
632 | |||
633 | $message .= implode("\n ", $alternatives); |
||
634 | } |
||
635 | |||
636 | throw new NamespaceNotFoundException($message, $alternatives); |
||
637 | } |
||
638 | |||
639 | $exact = \in_array($namespace, $namespaces, true); |
||
640 | if (\count($namespaces) > 1 && !$exact) { |
||
641 | throw new NamespaceNotFoundException(sprintf("The namespace \"%s\" is ambiguous.\nDid you mean one of these?\n%s.", $namespace, $this->getAbbreviationSuggestions(array_values($namespaces))), array_values($namespaces)); |
||
642 | } |
||
643 | |||
644 | return $exact ? $namespace : reset($namespaces); |
||
645 | } |
||
646 | |||
647 | /** |
||
648 | * Finds a command by name or alias. |
||
649 | * |
||
650 | * Contrary to get, this command tries to find the best |
||
651 | * match if you give it an abbreviation of a name or alias. |
||
652 | * |
||
653 | * @throws CommandNotFoundException When command name is incorrect or ambiguous |
||
654 | */ |
||
655 | public function find(string $name): Command |
||
754 | } |
||
755 | |||
756 | /** |
||
757 | * Gets the commands (registered in the given namespace if provided). |
||
758 | * |
||
759 | * The array keys are the full names and the values the command instances. |
||
760 | * |
||
761 | * @return Command[] |
||
762 | */ |
||
763 | public function all(?string $namespace = null): array |
||
798 | } |
||
799 | |||
800 | /** |
||
801 | * Returns an array of possible abbreviations given a set of names. |
||
802 | * |
||
803 | * @return string[][] |
||
804 | */ |
||
805 | public static function getAbbreviations(array $names): array |
||
806 | { |
||
807 | $abbrevs = []; |
||
808 | foreach ($names as $name) { |
||
809 | for ($len = \strlen($name); $len > 0; --$len) { |
||
810 | $abbrev = substr($name, 0, $len); |
||
811 | $abbrevs[$abbrev][] = $name; |
||
812 | } |
||
813 | } |
||
814 | |||
815 | return $abbrevs; |
||
816 | } |
||
817 | |||
818 | public function renderThrowable(\Throwable $e, OutputInterface $output): void |
||
819 | { |
||
820 | $output->writeln('', OutputInterface::VERBOSITY_QUIET); |
||
821 | |||
822 | $this->doRenderThrowable($e, $output); |
||
823 | |||
824 | if (null !== $this->runningCommand) { |
||
825 | $output->writeln(sprintf('<info>%s</info>', OutputFormatter::escape(sprintf($this->runningCommand->getSynopsis(), $this->getName()))), OutputInterface::VERBOSITY_QUIET); |
||
826 | $output->writeln('', OutputInterface::VERBOSITY_QUIET); |
||
827 | } |
||
828 | } |
||
829 | |||
830 | protected function doRenderThrowable(\Throwable $e, OutputInterface $output): void |
||
831 | { |
||
832 | do { |
||
833 | $message = trim($e->getMessage()); |
||
834 | if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
||
835 | $class = get_debug_type($e); |
||
836 | $title = sprintf(' [%s%s] ', $class, 0 !== ($code = $e->getCode()) ? ' ('.$code.')' : ''); |
||
837 | $len = Helper::width($title); |
||
838 | } else { |
||
839 | $len = 0; |
||
840 | } |
||
841 | |||
842 | if (str_contains($message, "@anonymous\0")) { |
||
843 | $message = preg_replace_callback('/[a-zA-Z_\x7f-\xff][\\\\a-zA-Z0-9_\x7f-\xff]*+@anonymous\x00.*?\.php(?:0x?|:[0-9]++\$)[0-9a-fA-F]++/', fn ($m) => class_exists($m[0], false) ? (get_parent_class($m[0]) ?: key(class_implements($m[0])) ?: 'class').'@anonymous' : $m[0], $message); |
||
844 | } |
||
845 | |||
846 | $width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : \PHP_INT_MAX; |
||
847 | $lines = []; |
||
848 | foreach ('' !== $message ? preg_split('/\r?\n/', $message) : [] as $line) { |
||
849 | foreach ($this->splitStringByWidth($line, $width - 4) as $line) { |
||
850 | // pre-format lines to get the right string length |
||
851 | $lineLength = Helper::width($line) + 4; |
||
852 | $lines[] = [$line, $lineLength]; |
||
853 | |||
854 | $len = max($lineLength, $len); |
||
855 | } |
||
856 | } |
||
857 | |||
858 | $messages = []; |
||
859 | if (!$e instanceof ExceptionInterface || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
||
860 | $messages[] = sprintf('<comment>%s</comment>', OutputFormatter::escape(sprintf('In %s line %s:', basename($e->getFile()) ?: 'n/a', $e->getLine() ?: 'n/a'))); |
||
861 | } |
||
862 | $messages[] = $emptyLine = sprintf('<error>%s</error>', str_repeat(' ', $len)); |
||
863 | if ('' === $message || OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
||
864 | $messages[] = sprintf('<error>%s%s</error>', $title, str_repeat(' ', max(0, $len - Helper::width($title)))); |
||
865 | } |
||
866 | foreach ($lines as $line) { |
||
867 | $messages[] = sprintf('<error> %s %s</error>', OutputFormatter::escape($line[0]), str_repeat(' ', $len - $line[1])); |
||
868 | } |
||
869 | $messages[] = $emptyLine; |
||
870 | $messages[] = ''; |
||
871 | |||
872 | $output->writeln($messages, OutputInterface::VERBOSITY_QUIET); |
||
873 | |||
874 | if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) { |
||
875 | $output->writeln('<comment>Exception trace:</comment>', OutputInterface::VERBOSITY_QUIET); |
||
876 | |||
877 | // exception related properties |
||
878 | $trace = $e->getTrace(); |
||
879 | |||
880 | array_unshift($trace, [ |
||
881 | 'function' => '', |
||
882 | 'file' => $e->getFile() ?: 'n/a', |
||
883 | 'line' => $e->getLine() ?: 'n/a', |
||
884 | 'args' => [], |
||
885 | ]); |
||
886 | |||
887 | for ($i = 0, $count = \count($trace); $i < $count; ++$i) { |
||
888 | $class = $trace[$i]['class'] ?? ''; |
||
889 | $type = $trace[$i]['type'] ?? ''; |
||
890 | $function = $trace[$i]['function'] ?? ''; |
||
891 | $file = $trace[$i]['file'] ?? 'n/a'; |
||
892 | $line = $trace[$i]['line'] ?? 'n/a'; |
||
893 | |||
894 | $output->writeln(sprintf(' %s%s at <info>%s:%s</info>', $class, $function ? $type.$function.'()' : '', $file, $line), OutputInterface::VERBOSITY_QUIET); |
||
895 | } |
||
896 | |||
897 | $output->writeln('', OutputInterface::VERBOSITY_QUIET); |
||
898 | } |
||
899 | } while ($e = $e->getPrevious()); |
||
900 | } |
||
901 | |||
902 | /** |
||
903 | * Configures the input and output instances based on the user arguments and options. |
||
904 | */ |
||
905 | protected function configureIO(InputInterface $input, OutputInterface $output): void |
||
906 | { |
||
907 | if (true === $input->hasParameterOption(['--ansi'], true)) { |
||
908 | $output->setDecorated(true); |
||
909 | } elseif (true === $input->hasParameterOption(['--no-ansi'], true)) { |
||
910 | $output->setDecorated(false); |
||
911 | } |
||
912 | |||
913 | if (true === $input->hasParameterOption(['--no-interaction', '-n'], true)) { |
||
914 | $input->setInteractive(false); |
||
915 | } |
||
916 | |||
917 | switch ($shellVerbosity = (int) getenv('SHELL_VERBOSITY')) { |
||
918 | case -1: |
||
919 | $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); |
||
920 | break; |
||
921 | case 1: |
||
922 | $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
||
923 | break; |
||
924 | case 2: |
||
925 | $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
926 | break; |
||
927 | case 3: |
||
928 | $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); |
||
929 | break; |
||
930 | default: |
||
931 | $shellVerbosity = 0; |
||
932 | break; |
||
933 | } |
||
934 | |||
935 | if (true === $input->hasParameterOption(['--quiet', '-q'], true)) { |
||
936 | $output->setVerbosity(OutputInterface::VERBOSITY_QUIET); |
||
937 | $shellVerbosity = -1; |
||
938 | } else { |
||
939 | if ($input->hasParameterOption('-vvv', true) || $input->hasParameterOption('--verbose=3', true) || 3 === $input->getParameterOption('--verbose', false, true)) { |
||
940 | $output->setVerbosity(OutputInterface::VERBOSITY_DEBUG); |
||
941 | $shellVerbosity = 3; |
||
942 | } elseif ($input->hasParameterOption('-vv', true) || $input->hasParameterOption('--verbose=2', true) || 2 === $input->getParameterOption('--verbose', false, true)) { |
||
943 | $output->setVerbosity(OutputInterface::VERBOSITY_VERY_VERBOSE); |
||
944 | $shellVerbosity = 2; |
||
945 | } elseif ($input->hasParameterOption('-v', true) || $input->hasParameterOption('--verbose=1', true) || $input->hasParameterOption('--verbose', true) || $input->getParameterOption('--verbose', false, true)) { |
||
946 | $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE); |
||
947 | $shellVerbosity = 1; |
||
948 | } |
||
949 | } |
||
950 | |||
951 | if (-1 === $shellVerbosity) { |
||
952 | $input->setInteractive(false); |
||
953 | } |
||
954 | |||
955 | if (\function_exists('putenv')) { |
||
956 | @putenv('SHELL_VERBOSITY='.$shellVerbosity); |
||
957 | } |
||
958 | $_ENV['SHELL_VERBOSITY'] = $shellVerbosity; |
||
959 | $_SERVER['SHELL_VERBOSITY'] = $shellVerbosity; |
||
960 | } |
||
961 | |||
962 | /** |
||
963 | * Runs the current command. |
||
964 | * |
||
965 | * If an event dispatcher has been attached to the application, |
||
966 | * events are also dispatched during the life-cycle of the command. |
||
967 | * |
||
968 | * @return int 0 if everything went fine, or an error code |
||
969 | */ |
||
970 | protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output): int |
||
971 | { |
||
972 | foreach ($command->getHelperSet() as $helper) { |
||
973 | if ($helper instanceof InputAwareInterface) { |
||
974 | $helper->setInput($input); |
||
975 | } |
||
976 | } |
||
977 | |||
978 | $commandSignals = $command instanceof SignalableCommandInterface ? $command->getSubscribedSignals() : []; |
||
979 | if ($commandSignals || $this->dispatcher && $this->signalsToDispatchEvent) { |
||
980 | if (!$this->signalRegistry) { |
||
981 | throw new RuntimeException('Unable to subscribe to signal events. Make sure that the "pcntl" extension is installed and that "pcntl_*" functions are not disabled by your php.ini\'s "disable_functions" directive.'); |
||
982 | } |
||
983 | |||
984 | if (Terminal::hasSttyAvailable()) { |
||
985 | $sttyMode = shell_exec('stty -g'); |
||
986 | |||
987 | foreach ([\SIGINT, \SIGQUIT, \SIGTERM] as $signal) { |
||
988 | $this->signalRegistry->register($signal, static fn () => shell_exec('stty '.$sttyMode)); |
||
989 | } |
||
990 | } |
||
991 | |||
992 | if ($this->dispatcher) { |
||
993 | // We register application signals, so that we can dispatch the event |
||
994 | foreach ($this->signalsToDispatchEvent as $signal) { |
||
995 | $event = new ConsoleSignalEvent($command, $input, $output, $signal); |
||
996 | |||
997 | $this->signalRegistry->register($signal, function ($signal) use ($event, $command, $commandSignals) { |
||
998 | $this->dispatcher->dispatch($event, ConsoleEvents::SIGNAL); |
||
999 | $exitCode = $event->getExitCode(); |
||
1000 | |||
1001 | // If the command is signalable, we call the handleSignal() method |
||
1002 | if (\in_array($signal, $commandSignals, true)) { |
||
1003 | $exitCode = $command->handleSignal($signal, $exitCode); |
||
1004 | } |
||
1005 | |||
1006 | if (false !== $exitCode) { |
||
1007 | $event = new ConsoleTerminateEvent($command, $event->getInput(), $event->getOutput(), $exitCode, $signal); |
||
1008 | $this->dispatcher->dispatch($event, ConsoleEvents::TERMINATE); |
||
1009 | |||
1010 | exit($event->getExitCode()); |
||
1011 | } |
||
1012 | }); |
||
1013 | } |
||
1014 | |||
1015 | // then we register command signals, but not if already handled after the dispatcher |
||
1016 | $commandSignals = array_diff($commandSignals, $this->signalsToDispatchEvent); |
||
1017 | } |
||
1018 | |||
1019 | foreach ($commandSignals as $signal) { |
||
1020 | $this->signalRegistry->register($signal, function (int $signal) use ($command): void { |
||
1021 | if (false !== $exitCode = $command->handleSignal($signal)) { |
||
1022 | exit($exitCode); |
||
1023 | } |
||
1024 | }); |
||
1025 | } |
||
1026 | } |
||
1027 | |||
1028 | if (null === $this->dispatcher) { |
||
1029 | return $command->run($input, $output); |
||
1030 | } |
||
1031 | |||
1032 | // bind before the console.command event, so the listeners have access to input options/arguments |
||
1033 | try { |
||
1034 | $command->mergeApplicationDefinition(); |
||
1035 | $input->bind($command->getDefinition()); |
||
1036 | } catch (ExceptionInterface) { |
||
1037 | // ignore invalid options/arguments for now, to allow the event listeners to customize the InputDefinition |
||
1038 | } |
||
1039 | |||
1040 | $event = new ConsoleCommandEvent($command, $input, $output); |
||
1041 | $e = null; |
||
1042 | |||
1043 | try { |
||
1044 | $this->dispatcher->dispatch($event, ConsoleEvents::COMMAND); |
||
1045 | |||
1046 | if ($event->commandShouldRun()) { |
||
1047 | $exitCode = $command->run($input, $output); |
||
1048 | } else { |
||
1049 | $exitCode = ConsoleCommandEvent::RETURN_CODE_DISABLED; |
||
1050 | } |
||
1051 | } catch (\Throwable $e) { |
||
1052 | $event = new ConsoleErrorEvent($input, $output, $e, $command); |
||
1053 | $this->dispatcher->dispatch($event, ConsoleEvents::ERROR); |
||
1054 | $e = $event->getError(); |
||
1055 | |||
1056 | if (0 === $exitCode = $event->getExitCode()) { |
||
1057 | $e = null; |
||
1058 | } |
||
1059 | } |
||
1060 | |||
1061 | $event = new ConsoleTerminateEvent($command, $input, $output, $exitCode); |
||
1062 | $this->dispatcher->dispatch($event, ConsoleEvents::TERMINATE); |
||
1063 | |||
1064 | if (null !== $e) { |
||
1065 | throw $e; |
||
1066 | } |
||
1067 | |||
1068 | return $event->getExitCode(); |
||
1069 | } |
||
1070 | |||
1071 | /** |
||
1072 | * Gets the name of the command based on input. |
||
1073 | */ |
||
1074 | protected function getCommandName(InputInterface $input): ?string |
||
1075 | { |
||
1076 | return $this->singleCommand ? $this->defaultCommand : $input->getFirstArgument(); |
||
1077 | } |
||
1078 | |||
1079 | /** |
||
1080 | * Gets the default input definition. |
||
1081 | */ |
||
1082 | protected function getDefaultInputDefinition(): InputDefinition |
||
1083 | { |
||
1084 | return new InputDefinition([ |
||
1085 | new InputArgument('command', InputArgument::REQUIRED, 'The command to execute'), |
||
1086 | new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display help for the given command. When no command is given display help for the <info>'.$this->defaultCommand.'</info> command'), |
||
1087 | new InputOption('--quiet', '-q', InputOption::VALUE_NONE, 'Do not output any message'), |
||
1088 | new InputOption('--verbose', '-v|vv|vvv', InputOption::VALUE_NONE, 'Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug'), |
||
1089 | new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display this application version'), |
||
1090 | new InputOption('--ansi', '', InputOption::VALUE_NEGATABLE, 'Force (or disable --no-ansi) ANSI output', null), |
||
1091 | new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question'), |
||
1092 | ]); |
||
1093 | } |
||
1094 | |||
1095 | /** |
||
1096 | * Gets the default commands that should always be available. |
||
1097 | * |
||
1098 | * @return Command[] |
||
1099 | */ |
||
1100 | protected function getDefaultCommands(): array |
||
1101 | { |
||
1102 | return [new HelpCommand(), new ListCommand(), new CompleteCommand(), new DumpCompletionCommand()]; |
||
1103 | } |
||
1104 | |||
1105 | /** |
||
1106 | * Gets the default helper set with the helpers that should always be available. |
||
1107 | */ |
||
1108 | protected function getDefaultHelperSet(): HelperSet |
||
1109 | { |
||
1110 | return new HelperSet([ |
||
1111 | new FormatterHelper(), |
||
1112 | new DebugFormatterHelper(), |
||
1113 | new ProcessHelper(), |
||
1114 | new QuestionHelper(), |
||
1115 | ]); |
||
1116 | } |
||
1117 | |||
1118 | /** |
||
1119 | * Returns abbreviated suggestions in string format. |
||
1120 | */ |
||
1121 | private function getAbbreviationSuggestions(array $abbrevs): string |
||
1122 | { |
||
1123 | return ' '.implode("\n ", $abbrevs); |
||
1124 | } |
||
1125 | |||
1126 | /** |
||
1127 | * Returns the namespace part of the command name. |
||
1128 | * |
||
1129 | * This method is not part of public API and should not be used directly. |
||
1130 | */ |
||
1131 | public function extractNamespace(string $name, ?int $limit = null): string |
||
1132 | { |
||
1133 | $parts = explode(':', $name, -1); |
||
1134 | |||
1135 | return implode(':', null === $limit ? $parts : \array_slice($parts, 0, $limit)); |
||
1136 | } |
||
1137 | |||
1138 | /** |
||
1139 | * Finds alternative of $name among $collection, |
||
1140 | * if nothing is found in $collection, try in $abbrevs. |
||
1141 | * |
||
1142 | * @return string[] |
||
1143 | */ |
||
1144 | private function findAlternatives(string $name, iterable $collection): array |
||
1145 | { |
||
1146 | $threshold = 1e3; |
||
1147 | $alternatives = []; |
||
1148 | |||
1149 | $collectionParts = []; |
||
1150 | foreach ($collection as $item) { |
||
1151 | $collectionParts[$item] = explode(':', $item); |
||
1152 | } |
||
1153 | |||
1154 | foreach (explode(':', $name) as $i => $subname) { |
||
1155 | foreach ($collectionParts as $collectionName => $parts) { |
||
1156 | $exists = isset($alternatives[$collectionName]); |
||
1157 | if (!isset($parts[$i]) && $exists) { |
||
1158 | $alternatives[$collectionName] += $threshold; |
||
1159 | continue; |
||
1160 | } elseif (!isset($parts[$i])) { |
||
1161 | continue; |
||
1162 | } |
||
1163 | |||
1164 | $lev = levenshtein($subname, $parts[$i]); |
||
1165 | if ($lev <= \strlen($subname) / 3 || '' !== $subname && str_contains($parts[$i], $subname)) { |
||
1166 | $alternatives[$collectionName] = $exists ? $alternatives[$collectionName] + $lev : $lev; |
||
1167 | } elseif ($exists) { |
||
1168 | $alternatives[$collectionName] += $threshold; |
||
1169 | } |
||
1170 | } |
||
1171 | } |
||
1172 | |||
1173 | foreach ($collection as $item) { |
||
1174 | $lev = levenshtein($name, $item); |
||
1175 | if ($lev <= \strlen($name) / 3 || str_contains($item, $name)) { |
||
1176 | $alternatives[$item] = isset($alternatives[$item]) ? $alternatives[$item] - $lev : $lev; |
||
1177 | } |
||
1178 | } |
||
1179 | |||
1180 | $alternatives = array_filter($alternatives, fn ($lev) => $lev < 2 * $threshold); |
||
1181 | ksort($alternatives, \SORT_NATURAL | \SORT_FLAG_CASE); |
||
1182 | |||
1183 | return array_keys($alternatives); |
||
1184 | } |
||
1185 | |||
1186 | /** |
||
1187 | * Sets the default Command name. |
||
1188 | * |
||
1189 | * @return $this |
||
1190 | */ |
||
1191 | public function setDefaultCommand(string $commandName, bool $isSingleCommand = false): static |
||
1192 | { |
||
1193 | $this->defaultCommand = explode('|', ltrim($commandName, '|'))[0]; |
||
1194 | |||
1195 | if ($isSingleCommand) { |
||
1196 | // Ensure the command exist |
||
1197 | $this->find($commandName); |
||
1198 | |||
1199 | $this->singleCommand = true; |
||
1200 | } |
||
1201 | |||
1202 | return $this; |
||
1203 | } |
||
1204 | |||
1205 | /** |
||
1206 | * @internal |
||
1207 | */ |
||
1208 | public function isSingleCommand(): bool |
||
1211 | } |
||
1212 | |||
1213 | private function splitStringByWidth(string $string, int $width): array |
||
1214 | { |
||
1215 | // str_split is not suitable for multi-byte characters, we should use preg_split to get char array properly. |
||
1216 | // additionally, array_slice() is not enough as some character has doubled width. |
||
1217 | // we need a function to split string not by character count but by string width |
||
1218 | if (false === $encoding = mb_detect_encoding($string, null, true)) { |
||
1219 | return str_split($string, $width); |
||
1220 | } |
||
1221 | |||
1222 | $utf8String = mb_convert_encoding($string, 'utf8', $encoding); |
||
1223 | $lines = []; |
||
1224 | $line = ''; |
||
1225 | |||
1226 | $offset = 0; |
||
1227 | while (preg_match('/.{1,10000}/u', $utf8String, $m, 0, $offset)) { |
||
1228 | $offset += \strlen($m[0]); |
||
1229 | |||
1230 | foreach (preg_split('//u', $m[0]) as $char) { |
||
1231 | // test if $char could be appended to current line |
||
1232 | if (mb_strwidth($line.$char, 'utf8') <= $width) { |
||
1233 | $line .= $char; |
||
1234 | continue; |
||
1235 | } |
||
1236 | // if not, push current line to array and make new line |
||
1237 | $lines[] = str_pad($line, $width); |
||
1238 | $line = $char; |
||
1239 | } |
||
1240 | } |
||
1241 | |||
1242 | $lines[] = \count($lines) ? str_pad($line, $width) : $line; |
||
1243 | |||
1244 | mb_convert_variables($encoding, 'utf8', $lines); |
||
1245 | |||
1246 | return $lines; |
||
1247 | } |
||
1248 | |||
1249 | /** |
||
1250 | * Returns all namespaces of the command name. |
||
1251 | * |
||
1252 | * @return string[] |
||
1253 | */ |
||
1254 | private function extractAllNamespaces(string $name): array |
||
1255 | { |
||
1256 | // -1 as third argument is needed to skip the command short name when exploding |
||
1257 | $parts = explode(':', $name, -1); |
||
1258 | $namespaces = []; |
||
1259 | |||
1260 | foreach ($parts as $part) { |
||
1261 | if (\count($namespaces)) { |
||
1262 | $namespaces[] = end($namespaces).':'.$part; |
||
1263 | } else { |
||
1264 | $namespaces[] = $part; |
||
1265 | } |
||
1266 | } |
||
1267 | |||
1268 | return $namespaces; |
||
1269 | } |
||
1270 | |||
1271 | private function init(): void |
||
1280 | } |
||
1281 | } |
||
1282 | } |
||
1283 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths