|
| 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 | + |
| 12 | +namespace Symfony\Component\Console\Command; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Completion\CompletionInput; |
| 15 | +use Symfony\Component\Console\Completion\CompletionInterface; |
| 16 | +use Symfony\Component\Console\Completion\CompletionSuggestions; |
| 17 | +use Symfony\Component\Console\Completion\Output\BashCompletionOutput; |
| 18 | +use Symfony\Component\Console\Exception\CommandNotFoundException; |
| 19 | +use Symfony\Component\Console\Exception\ExceptionInterface; |
| 20 | +use Symfony\Component\Console\Input\InputInterface; |
| 21 | +use Symfony\Component\Console\Input\InputOption; |
| 22 | +use Symfony\Component\Console\Output\OutputInterface; |
| 23 | + |
| 24 | +/** |
| 25 | + * Responsible for providing the values to the shell completion. |
| 26 | + * |
| 27 | + * @author Wouter de Jong <wouter@wouterj.nl> |
| 28 | + */ |
| 29 | +final class CompleteCommand extends Command |
| 30 | +{ |
| 31 | + protected static $defaultName = '|_complete'; |
| 32 | + protected static $defaultDescription = 'Internal command to provide shell completion suggestions'; |
| 33 | + |
| 34 | + private static $completionOutputs = [ |
| 35 | + 'bash' => BashCompletionOutput::class, |
| 36 | + ]; |
| 37 | + |
| 38 | + private $isDebug = false; |
| 39 | + |
| 40 | + protected function configure(): void |
| 41 | + { |
| 42 | + $this |
| 43 | + ->addOption('shell', 's', InputOption::VALUE_REQUIRED, 'The shell type (e.g. "bash" or "zsh")') |
| 44 | + ->addOption('input', 'i', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'An array of input tokens (e.g. COMP_WORDS or argv)') |
| 45 | + ->addOption('current', 'c', InputOption::VALUE_REQUIRED, 'The index of the "input" array that the cursor is in (e.g. COMP_CWORD)') |
| 46 | + ->addOption('symfony', 'S', InputOption::VALUE_REQUIRED, 'The version of the completion script') |
| 47 | + ; |
| 48 | + } |
| 49 | + |
| 50 | + protected function initialize(InputInterface $input, OutputInterface $output) |
| 51 | + { |
| 52 | + $this->isDebug = filter_var(getenv('SYMFONY_COMPLETION_DEBUG'), \FILTER_VALIDATE_BOOLEAN); |
| 53 | + } |
| 54 | + |
| 55 | + protected function execute(InputInterface $input, OutputInterface $output): int |
| 56 | + { |
| 57 | + try { |
| 58 | + // uncomment when a bugfix or BC break has been introduced in the shell completion scripts |
| 59 | + //$version = $input->getOption('symfony'); |
| 60 | + //if ($version && version_compare($version, 'x.y', '>=')) { |
| 61 | + // $message = sprintf('Completion script version is not supported ("%s" given, ">=x.y" required).', $version); |
| 62 | + // $this->log($message); |
| 63 | + |
| 64 | + // $output->writeln($message.' Install the Symfony completion script again by using the "completion" command.'); |
| 65 | + |
| 66 | + // return 126; |
| 67 | + //} |
| 68 | + |
| 69 | + $shell = $input->getOption('shell'); |
| 70 | + if (!$shell) { |
| 71 | + throw new \RuntimeException('The "--shell" option must be set.'); |
| 72 | + } |
| 73 | + |
| 74 | + if (!$completionOutput = self::$completionOutputs[$shell] ?? false) { |
| 75 | + throw new \RuntimeException(sprintf('Shell completion is not supported for your shell: "%s" (supported: "%s").', $shell, implode('", "', array_keys(self::$completionOutputs)))); |
| 76 | + } |
| 77 | + |
| 78 | + $completionInput = $this->createCompletionInput($input); |
| 79 | + $suggestions = new CompletionSuggestions(); |
| 80 | + |
| 81 | + $this->log([ |
| 82 | + '', |
| 83 | + '<comment>'.date('Y-m-d H:i:s').'</>', |
| 84 | + '<info>Input:</> <comment>("|" indicates the cursor position)</>', |
| 85 | + ' '.(string) $completionInput, |
| 86 | + '<info>Messages:</>', |
| 87 | + ]); |
| 88 | + |
| 89 | + $command = $this->findCommand($completionInput, $output); |
| 90 | + if (null === $command) { |
| 91 | + $this->log(' No command found, completing using the Application class.'); |
| 92 | + |
| 93 | + $this->getApplication()->complete($completionInput, $suggestions); |
| 94 | + } elseif ( |
| 95 | + $completionInput->mustSuggestArgumentValuesFor('command') |
| 96 | + && $command->getName() !== $completionInput->getCompletionValue() |
| 97 | + ) { |
| 98 | + $this->log(' No command found, completing using the Application class.'); |
| 99 | + |
| 100 | + // expand shortcut names ("cache:cl<TAB>") into their full name ("cache:clear") |
| 101 | + $suggestions->suggestValue($command->getName()); |
| 102 | + } else { |
| 103 | + $command->mergeApplicationDefinition(); |
| 104 | + $completionInput->bind($command->getDefinition()); |
| 105 | + |
| 106 | + if (CompletionInput::TYPE_OPTION_NAME === $completionInput->getCompletionType()) { |
| 107 | + $this->log(' Completing option names for the <comment>'.\get_class($command instanceof LazyCommand ? $command->getCommand() : $command).'</> command.'); |
| 108 | + |
| 109 | + $suggestions->suggestOptions($command->getDefinition()->getOptions()); |
| 110 | + } elseif ($command instanceof CompletionInterface) { |
| 111 | + $this->log([ |
| 112 | + ' Completing using the <comment>'.\get_class($command).'</> class.', |
| 113 | + ' Completing <comment>'.$completionInput->getCompletionType().'</> for <comment>'.$completionInput->getCompletionName().'</>', |
| 114 | + ]); |
| 115 | + if (null !== $compval = $completionInput->getCompletionValue()) { |
| 116 | + $this->log(' Current value: <comment>'.$compval.'</>'); |
| 117 | + } |
| 118 | + |
| 119 | + $command->complete($completionInput, $suggestions); |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + $completionOutput = new $completionOutput(); |
| 124 | + |
| 125 | + $this->log('<info>Suggestions:</>'); |
| 126 | + if ($options = $suggestions->getOptionSuggestions()) { |
| 127 | + $this->log(' --'.implode(' --', array_map(function ($o) { return $o->getName(); }, $options))); |
| 128 | + } elseif ($values = $suggestions->getValueSuggestions()) { |
| 129 | + $this->log(' '.implode(' ', $values)); |
| 130 | + } else { |
| 131 | + $this->log(' <comment>No suggestions were provided</>'); |
| 132 | + } |
| 133 | + |
| 134 | + $completionOutput->write($suggestions, $output); |
| 135 | + } catch (\Throwable $e) { |
| 136 | + $this->log([ |
| 137 | + '<error>Error!</error>', |
| 138 | + (string) $e, |
| 139 | + ]); |
| 140 | + |
| 141 | + if ($output->isDebug()) { |
| 142 | + throw $e; |
| 143 | + } |
| 144 | + |
| 145 | + return self::FAILURE; |
| 146 | + } |
| 147 | + |
| 148 | + return self::SUCCESS; |
| 149 | + } |
| 150 | + |
| 151 | + private function createCompletionInput(InputInterface $input): CompletionInput |
| 152 | + { |
| 153 | + $currentIndex = $input->getOption('current'); |
| 154 | + if (!$currentIndex || !ctype_digit($currentIndex)) { |
| 155 | + throw new \RuntimeException('The "--current" option must be set and it must be an integer.'); |
| 156 | + } |
| 157 | + |
| 158 | + $completionInput = CompletionInput::fromTokens(array_map( |
| 159 | + function (string $i): string { return trim($i, "'"); }, |
| 160 | + $input->getOption('input') |
| 161 | + ), (int) $currentIndex); |
| 162 | + |
| 163 | + try { |
| 164 | + $completionInput->bind($this->getApplication()->getDefinition()); |
| 165 | + } catch (ExceptionInterface $e) { |
| 166 | + } |
| 167 | + |
| 168 | + return $completionInput; |
| 169 | + } |
| 170 | + |
| 171 | + private function findCommand(CompletionInput $completionInput, OutputInterface $output): ?Command |
| 172 | + { |
| 173 | + try { |
| 174 | + $inputName = $completionInput->getFirstArgument(); |
| 175 | + if (null === $inputName) { |
| 176 | + return null; |
| 177 | + } |
| 178 | + |
| 179 | + return $this->getApplication()->find($inputName); |
| 180 | + } catch (CommandNotFoundException $e) { |
| 181 | + } |
| 182 | + |
| 183 | + return null; |
| 184 | + } |
| 185 | + |
| 186 | + private function log($messages): void |
| 187 | + { |
| 188 | + if (!$this->isDebug) { |
| 189 | + return; |
| 190 | + } |
| 191 | + |
| 192 | + $commandName = basename($_SERVER['argv'][0]); |
| 193 | + file_put_contents(sys_get_temp_dir().'/sf_'.$commandName.'.log', implode(\PHP_EOL, (array) $messages).\PHP_EOL, \FILE_APPEND); |
| 194 | + } |
| 195 | +} |
0 commit comments