Command.php
1 year ago
CompleteCommand.php
1 year ago
DumpCompletionCommand.php
2 years ago
HelpCommand.php
2 years ago
LazyCommand.php
1 year ago
ListCommand.php
2 years ago
LockableTrait.php
1 year ago
SignalableCommandInterface.php
2 years ago
CompleteCommand.php
149 lines
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This file is part of the Symfony package. |
| 5 | * |
| 6 | * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | * |
| 8 | * For the full copyright and license information, please view the LICENSE |
| 9 | * file that was distributed with this source code. |
| 10 | */ |
| 11 | namespace Matomo\Dependencies\Symfony\Component\Console\Command; |
| 12 | |
| 13 | use Matomo\Dependencies\Symfony\Component\Console\Completion\CompletionInput; |
| 14 | use Matomo\Dependencies\Symfony\Component\Console\Completion\CompletionSuggestions; |
| 15 | use Matomo\Dependencies\Symfony\Component\Console\Completion\Output\BashCompletionOutput; |
| 16 | use Matomo\Dependencies\Symfony\Component\Console\Completion\Output\CompletionOutputInterface; |
| 17 | use Matomo\Dependencies\Symfony\Component\Console\Exception\CommandNotFoundException; |
| 18 | use Matomo\Dependencies\Symfony\Component\Console\Exception\ExceptionInterface; |
| 19 | use Matomo\Dependencies\Symfony\Component\Console\Input\InputInterface; |
| 20 | use Matomo\Dependencies\Symfony\Component\Console\Input\InputOption; |
| 21 | use Matomo\Dependencies\Symfony\Component\Console\Output\OutputInterface; |
| 22 | /** |
| 23 | * Responsible for providing the values to the shell completion. |
| 24 | * |
| 25 | * @author Wouter de Jong <wouter@wouterj.nl> |
| 26 | */ |
| 27 | final class CompleteCommand extends Command |
| 28 | { |
| 29 | protected static $defaultName = '|_complete'; |
| 30 | protected static $defaultDescription = 'Internal command to provide shell completion suggestions'; |
| 31 | private $completionOutputs; |
| 32 | private $isDebug = \false; |
| 33 | /** |
| 34 | * @param array<string, class-string<CompletionOutputInterface>> $completionOutputs A list of additional completion outputs, with shell name as key and FQCN as value |
| 35 | */ |
| 36 | public function __construct(array $completionOutputs = []) |
| 37 | { |
| 38 | // must be set before the parent constructor, as the property value is used in configure() |
| 39 | $this->completionOutputs = $completionOutputs + ['bash' => BashCompletionOutput::class]; |
| 40 | parent::__construct(); |
| 41 | } |
| 42 | protected function configure() : void |
| 43 | { |
| 44 | $this->addOption('shell', 's', InputOption::VALUE_REQUIRED, 'The shell type ("' . implode('", "', array_keys($this->completionOutputs)) . '")')->addOption('input', 'i', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'An array of input tokens (e.g. COMP_WORDS or argv)')->addOption('current', 'c', InputOption::VALUE_REQUIRED, 'The index of the "input" array that the cursor is in (e.g. COMP_CWORD)')->addOption('symfony', 'S', InputOption::VALUE_REQUIRED, 'The version of the completion script'); |
| 45 | } |
| 46 | protected function initialize(InputInterface $input, OutputInterface $output) |
| 47 | { |
| 48 | $this->isDebug = filter_var(getenv('SYMFONY_COMPLETION_DEBUG'), \FILTER_VALIDATE_BOOLEAN); |
| 49 | } |
| 50 | protected function execute(InputInterface $input, OutputInterface $output) : int |
| 51 | { |
| 52 | try { |
| 53 | // uncomment when a bugfix or BC break has been introduced in the shell completion scripts |
| 54 | // $version = $input->getOption('symfony'); |
| 55 | // if ($version && version_compare($version, 'x.y', '>=')) { |
| 56 | // $message = sprintf('Completion script version is not supported ("%s" given, ">=x.y" required).', $version); |
| 57 | // $this->log($message); |
| 58 | // $output->writeln($message.' Install the Symfony completion script again by using the "completion" command.'); |
| 59 | // return 126; |
| 60 | // } |
| 61 | $shell = $input->getOption('shell'); |
| 62 | if (!$shell) { |
| 63 | throw new \RuntimeException('The "--shell" option must be set.'); |
| 64 | } |
| 65 | if (!($completionOutput = $this->completionOutputs[$shell] ?? \false)) { |
| 66 | throw new \RuntimeException(sprintf('Shell completion is not supported for your shell: "%s" (supported: "%s").', $shell, implode('", "', array_keys($this->completionOutputs)))); |
| 67 | } |
| 68 | $completionInput = $this->createCompletionInput($input); |
| 69 | $suggestions = new CompletionSuggestions(); |
| 70 | $this->log(['', '<comment>' . date('Y-m-d H:i:s') . '</>', '<info>Input:</> <comment>("|" indicates the cursor position)</>', ' ' . (string) $completionInput, '<info>Command:</>', ' ' . (string) implode(' ', $_SERVER['argv']), '<info>Messages:</>']); |
| 71 | $command = $this->findCommand($completionInput, $output); |
| 72 | if (null === $command) { |
| 73 | $this->log(' No command found, completing using the Application class.'); |
| 74 | $this->getApplication()->complete($completionInput, $suggestions); |
| 75 | } elseif ($completionInput->mustSuggestArgumentValuesFor('command') && $command->getName() !== $completionInput->getCompletionValue() && !\in_array($completionInput->getCompletionValue(), $command->getAliases(), \true)) { |
| 76 | $this->log(' No command found, completing using the Application class.'); |
| 77 | // expand shortcut names ("cache:cl<TAB>") into their full name ("cache:clear") |
| 78 | $suggestions->suggestValues(array_filter(array_merge([$command->getName()], $command->getAliases()))); |
| 79 | } else { |
| 80 | $command->mergeApplicationDefinition(); |
| 81 | $completionInput->bind($command->getDefinition()); |
| 82 | if (CompletionInput::TYPE_OPTION_NAME === $completionInput->getCompletionType()) { |
| 83 | $this->log(' Completing option names for the <comment>' . \get_class($command instanceof LazyCommand ? $command->getCommand() : $command) . '</> command.'); |
| 84 | $suggestions->suggestOptions($command->getDefinition()->getOptions()); |
| 85 | } else { |
| 86 | $this->log([' Completing using the <comment>' . \get_class($command instanceof LazyCommand ? $command->getCommand() : $command) . '</> class.', ' Completing <comment>' . $completionInput->getCompletionType() . '</> for <comment>' . $completionInput->getCompletionName() . '</>']); |
| 87 | if (null !== ($compval = $completionInput->getCompletionValue())) { |
| 88 | $this->log(' Current value: <comment>' . $compval . '</>'); |
| 89 | } |
| 90 | $command->complete($completionInput, $suggestions); |
| 91 | } |
| 92 | } |
| 93 | /** @var CompletionOutputInterface $completionOutput */ |
| 94 | $completionOutput = new $completionOutput(); |
| 95 | $this->log('<info>Suggestions:</>'); |
| 96 | if ($options = $suggestions->getOptionSuggestions()) { |
| 97 | $this->log(' --' . implode(' --', array_map(function ($o) { |
| 98 | return $o->getName(); |
| 99 | }, $options))); |
| 100 | } elseif ($values = $suggestions->getValueSuggestions()) { |
| 101 | $this->log(' ' . implode(' ', $values)); |
| 102 | } else { |
| 103 | $this->log(' <comment>No suggestions were provided</>'); |
| 104 | } |
| 105 | $completionOutput->write($suggestions, $output); |
| 106 | } catch (\Throwable $e) { |
| 107 | $this->log(['<error>Error!</error>', (string) $e]); |
| 108 | if ($output->isDebug()) { |
| 109 | throw $e; |
| 110 | } |
| 111 | return 2; |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | private function createCompletionInput(InputInterface $input) : CompletionInput |
| 116 | { |
| 117 | $currentIndex = $input->getOption('current'); |
| 118 | if (!$currentIndex || !ctype_digit($currentIndex)) { |
| 119 | throw new \RuntimeException('The "--current" option must be set and it must be an integer.'); |
| 120 | } |
| 121 | $completionInput = CompletionInput::fromTokens($input->getOption('input'), (int) $currentIndex); |
| 122 | try { |
| 123 | $completionInput->bind($this->getApplication()->getDefinition()); |
| 124 | } catch (ExceptionInterface $e) { |
| 125 | } |
| 126 | return $completionInput; |
| 127 | } |
| 128 | private function findCommand(CompletionInput $completionInput, OutputInterface $output) : ?Command |
| 129 | { |
| 130 | try { |
| 131 | $inputName = $completionInput->getFirstArgument(); |
| 132 | if (null === $inputName) { |
| 133 | return null; |
| 134 | } |
| 135 | return $this->getApplication()->find($inputName); |
| 136 | } catch (CommandNotFoundException $e) { |
| 137 | } |
| 138 | return null; |
| 139 | } |
| 140 | private function log($messages) : void |
| 141 | { |
| 142 | if (!$this->isDebug) { |
| 143 | return; |
| 144 | } |
| 145 | $commandName = basename($_SERVER['argv'][0]); |
| 146 | file_put_contents(sys_get_temp_dir() . '/sf_' . $commandName . '.log', implode(\PHP_EOL, (array) $messages) . \PHP_EOL, \FILE_APPEND); |
| 147 | } |
| 148 | } |
| 149 |