diff options
Diffstat (limited to 'vendor/symfony/console/Command/CompleteCommand.php')
-rw-r--r-- | vendor/symfony/console/Command/CompleteCommand.php | 212 |
1 files changed, 212 insertions, 0 deletions
diff --git a/vendor/symfony/console/Command/CompleteCommand.php b/vendor/symfony/console/Command/CompleteCommand.php new file mode 100644 index 0000000..38aa737 --- /dev/null +++ b/vendor/symfony/console/Command/CompleteCommand.php | |||
@@ -0,0 +1,212 @@ | |||
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\Attribute\AsCommand; | ||
15 | use Symfony\Component\Console\Completion\CompletionInput; | ||
16 | use Symfony\Component\Console\Completion\CompletionSuggestions; | ||
17 | use Symfony\Component\Console\Completion\Output\BashCompletionOutput; | ||
18 | use Symfony\Component\Console\Completion\Output\CompletionOutputInterface; | ||
19 | use Symfony\Component\Console\Completion\Output\FishCompletionOutput; | ||
20 | use Symfony\Component\Console\Completion\Output\ZshCompletionOutput; | ||
21 | use Symfony\Component\Console\Exception\CommandNotFoundException; | ||
22 | use Symfony\Component\Console\Exception\ExceptionInterface; | ||
23 | use Symfony\Component\Console\Input\InputInterface; | ||
24 | use Symfony\Component\Console\Input\InputOption; | ||
25 | use Symfony\Component\Console\Output\OutputInterface; | ||
26 | |||
27 | /** | ||
28 | * Responsible for providing the values to the shell completion. | ||
29 | * | ||
30 | * @author Wouter de Jong <wouter@wouterj.nl> | ||
31 | */ | ||
32 | #[AsCommand(name: '|_complete', description: 'Internal command to provide shell completion suggestions')] | ||
33 | final class CompleteCommand extends Command | ||
34 | { | ||
35 | public const COMPLETION_API_VERSION = '1'; | ||
36 | |||
37 | private array $completionOutputs; | ||
38 | private bool $isDebug = false; | ||
39 | |||
40 | /** | ||
41 | * @param array<string, class-string<CompletionOutputInterface>> $completionOutputs A list of additional completion outputs, with shell name as key and FQCN as value | ||
42 | */ | ||
43 | public function __construct(array $completionOutputs = []) | ||
44 | { | ||
45 | // must be set before the parent constructor, as the property value is used in configure() | ||
46 | $this->completionOutputs = $completionOutputs + [ | ||
47 | 'bash' => BashCompletionOutput::class, | ||
48 | 'fish' => FishCompletionOutput::class, | ||
49 | 'zsh' => ZshCompletionOutput::class, | ||
50 | ]; | ||
51 | |||
52 | parent::__construct(); | ||
53 | } | ||
54 | |||
55 | protected function configure(): void | ||
56 | { | ||
57 | $this | ||
58 | ->addOption('shell', 's', InputOption::VALUE_REQUIRED, 'The shell type ("'.implode('", "', array_keys($this->completionOutputs)).'")') | ||
59 | ->addOption('input', 'i', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'An array of input tokens (e.g. COMP_WORDS or argv)') | ||
60 | ->addOption('current', 'c', InputOption::VALUE_REQUIRED, 'The index of the "input" array that the cursor is in (e.g. COMP_CWORD)') | ||
61 | ->addOption('api-version', 'a', InputOption::VALUE_REQUIRED, 'The API version of the completion script') | ||
62 | ->addOption('symfony', 'S', InputOption::VALUE_REQUIRED, 'deprecated') | ||
63 | ; | ||
64 | } | ||
65 | |||
66 | protected function initialize(InputInterface $input, OutputInterface $output): void | ||
67 | { | ||
68 | $this->isDebug = filter_var(getenv('SYMFONY_COMPLETION_DEBUG'), \FILTER_VALIDATE_BOOL); | ||
69 | } | ||
70 | |||
71 | protected function execute(InputInterface $input, OutputInterface $output): int | ||
72 | { | ||
73 | try { | ||
74 | // "symfony" must be kept for compat with the shell scripts generated by Symfony Console 5.4 - 6.1 | ||
75 | $version = $input->getOption('symfony') ? '1' : $input->getOption('api-version'); | ||
76 | if ($version && version_compare($version, self::COMPLETION_API_VERSION, '<')) { | ||
77 | $message = sprintf('Completion script version is not supported ("%s" given, ">=%s" required).', $version, self::COMPLETION_API_VERSION); | ||
78 | $this->log($message); | ||
79 | |||
80 | $output->writeln($message.' Install the Symfony completion script again by using the "completion" command.'); | ||
81 | |||
82 | return 126; | ||
83 | } | ||
84 | |||
85 | $shell = $input->getOption('shell'); | ||
86 | if (!$shell) { | ||
87 | throw new \RuntimeException('The "--shell" option must be set.'); | ||
88 | } | ||
89 | |||
90 | if (!$completionOutput = $this->completionOutputs[$shell] ?? false) { | ||
91 | throw new \RuntimeException(sprintf('Shell completion is not supported for your shell: "%s" (supported: "%s").', $shell, implode('", "', array_keys($this->completionOutputs)))); | ||
92 | } | ||
93 | |||
94 | $completionInput = $this->createCompletionInput($input); | ||
95 | $suggestions = new CompletionSuggestions(); | ||
96 | |||
97 | $this->log([ | ||
98 | '', | ||
99 | '<comment>'.date('Y-m-d H:i:s').'</>', | ||
100 | '<info>Input:</> <comment>("|" indicates the cursor position)</>', | ||
101 | ' '.(string) $completionInput, | ||
102 | '<info>Command:</>', | ||
103 | ' '.(string) implode(' ', $_SERVER['argv']), | ||
104 | '<info>Messages:</>', | ||
105 | ]); | ||
106 | |||
107 | $command = $this->findCommand($completionInput, $output); | ||
108 | if (null === $command) { | ||
109 | $this->log(' No command found, completing using the Application class.'); | ||
110 | |||
111 | $this->getApplication()->complete($completionInput, $suggestions); | ||
112 | } elseif ( | ||
113 | $completionInput->mustSuggestArgumentValuesFor('command') | ||
114 | && $command->getName() !== $completionInput->getCompletionValue() | ||
115 | && !\in_array($completionInput->getCompletionValue(), $command->getAliases(), true) | ||
116 | ) { | ||
117 | $this->log(' No command found, completing using the Application class.'); | ||
118 | |||
119 | // expand shortcut names ("cache:cl<TAB>") into their full name ("cache:clear") | ||
120 | $suggestions->suggestValues(array_filter(array_merge([$command->getName()], $command->getAliases()))); | ||
121 | } else { | ||
122 | $command->mergeApplicationDefinition(); | ||
123 | $completionInput->bind($command->getDefinition()); | ||
124 | |||
125 | if (CompletionInput::TYPE_OPTION_NAME === $completionInput->getCompletionType()) { | ||
126 | $this->log(' Completing option names for the <comment>'.($command instanceof LazyCommand ? $command->getCommand() : $command)::class.'</> command.'); | ||
127 | |||
128 | $suggestions->suggestOptions($command->getDefinition()->getOptions()); | ||
129 | } else { | ||
130 | $this->log([ | ||
131 | ' Completing using the <comment>'.($command instanceof LazyCommand ? $command->getCommand() : $command)::class.'</> class.', | ||
132 | ' Completing <comment>'.$completionInput->getCompletionType().'</> for <comment>'.$completionInput->getCompletionName().'</>', | ||
133 | ]); | ||
134 | if (null !== $compval = $completionInput->getCompletionValue()) { | ||
135 | $this->log(' Current value: <comment>'.$compval.'</>'); | ||
136 | } | ||
137 | |||
138 | $command->complete($completionInput, $suggestions); | ||
139 | } | ||
140 | } | ||
141 | |||
142 | /** @var CompletionOutputInterface $completionOutput */ | ||
143 | $completionOutput = new $completionOutput(); | ||
144 | |||
145 | $this->log('<info>Suggestions:</>'); | ||
146 | if ($options = $suggestions->getOptionSuggestions()) { | ||
147 | $this->log(' --'.implode(' --', array_map(fn ($o) => $o->getName(), $options))); | ||
148 | } elseif ($values = $suggestions->getValueSuggestions()) { | ||
149 | $this->log(' '.implode(' ', $values)); | ||
150 | } else { | ||
151 | $this->log(' <comment>No suggestions were provided</>'); | ||
152 | } | ||
153 | |||
154 | $completionOutput->write($suggestions, $output); | ||
155 | } catch (\Throwable $e) { | ||
156 | $this->log([ | ||
157 | '<error>Error!</error>', | ||
158 | (string) $e, | ||
159 | ]); | ||
160 | |||
161 | if ($output->isDebug()) { | ||
162 | throw $e; | ||
163 | } | ||
164 | |||
165 | return 2; | ||
166 | } | ||
167 | |||
168 | return 0; | ||
169 | } | ||
170 | |||
171 | private function createCompletionInput(InputInterface $input): CompletionInput | ||
172 | { | ||
173 | $currentIndex = $input->getOption('current'); | ||
174 | if (!$currentIndex || !ctype_digit($currentIndex)) { | ||
175 | throw new \RuntimeException('The "--current" option must be set and it must be an integer.'); | ||
176 | } | ||
177 | |||
178 | $completionInput = CompletionInput::fromTokens($input->getOption('input'), (int) $currentIndex); | ||
179 | |||
180 | try { | ||
181 | $completionInput->bind($this->getApplication()->getDefinition()); | ||
182 | } catch (ExceptionInterface) { | ||
183 | } | ||
184 | |||
185 | return $completionInput; | ||
186 | } | ||
187 | |||
188 | private function findCommand(CompletionInput $completionInput, OutputInterface $output): ?Command | ||
189 | { | ||
190 | try { | ||
191 | $inputName = $completionInput->getFirstArgument(); | ||
192 | if (null === $inputName) { | ||
193 | return null; | ||
194 | } | ||
195 | |||
196 | return $this->getApplication()->find($inputName); | ||
197 | } catch (CommandNotFoundException) { | ||
198 | } | ||
199 | |||
200 | return null; | ||
201 | } | ||
202 | |||
203 | private function log($messages): void | ||
204 | { | ||
205 | if (!$this->isDebug) { | ||
206 | return; | ||
207 | } | ||
208 | |||
209 | $commandName = basename($_SERVER['argv'][0]); | ||
210 | file_put_contents(sys_get_temp_dir().'/sf_'.$commandName.'.log', implode(\PHP_EOL, (array) $messages).\PHP_EOL, \FILE_APPEND); | ||
211 | } | ||
212 | } | ||