| 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\Attribute\AsCommand; |
| 15 |
use Symfony\Component\Console\Input\InputArgument; |
| 16 |
use Symfony\Component\Console\Input\InputInterface; |
| 17 |
use Symfony\Component\Console\Input\InputOption; |
| 18 |
use Symfony\Component\Console\Output\ConsoleOutputInterface; |
| 19 |
use Symfony\Component\Console\Output\OutputInterface; |
| 20 |
use Symfony\Component\Process\Process; |
| 21 |
|
| 22 |
/** |
| 23 |
* Dumps the completion script for the current shell. |
| 24 |
* |
| 25 |
* @author Wouter de Jong <[email protected]> |
| 26 |
*/ |
| 27 |
#[AsCommand(name: 'completion', description: 'Dump the shell completion script')] |
| 28 |
final class DumpCompletionCommand extends Command |
| 29 |
{ |
| 30 |
private array $supportedShells; |
| 31 |
|
| 32 |
protected function configure(): void |
| 33 |
{ |
| 34 |
$fullCommand = $_SERVER['PHP_SELF']; |
| 35 |
$commandName = basename($fullCommand); |
| 36 |
$fullCommand = @realpath($fullCommand) ?: $fullCommand; |
| 37 |
|
| 38 |
$shell = self::guessShell(); |
| 39 |
[$rcFile, $completionFile] = match ($shell) { |
| 40 |
'fish' => ['~/.config/fish/config.fish', "/etc/fish/completions/$commandName.fish"], |
| 41 |
'zsh' => ['~/.zshrc', '$fpath[1]/_'.$commandName], |
| 42 |
default => ['~/.bashrc', "/etc/bash_completion.d/$commandName"], |
| 43 |
}; |
| 44 |
|
| 45 |
$supportedShells = implode(', ', $this->getSupportedShells()); |
| 46 |
|
| 47 |
$this |
| 48 |
->setHelp(<<<EOH |
| 49 |
The <info>%command.name%</> command dumps the shell completion script required |
| 50 |
to use shell autocompletion (currently, {$supportedShells} completion are supported). |
| 51 |
|
| 52 |
<comment>Static installation |
| 53 |
-------------------</> |
| 54 |
|
| 55 |
Dump the script to a global completion file and restart your shell: |
| 56 |
|
| 57 |
<info>%command.full_name% {$shell} | sudo tee {$completionFile}</> |
| 58 |
|
| 59 |
Or dump the script to a local file and source it: |
| 60 |
|
| 61 |
<info>%command.full_name% {$shell} > completion.sh</> |
| 62 |
|
| 63 |
<comment># source the file whenever you use the project</> |
| 64 |
<info>source completion.sh</> |
| 65 |
|
| 66 |
<comment># or add this line at the end of your "{$rcFile}" file:</> |
| 67 |
<info>source /path/to/completion.sh</> |
| 68 |
|
| 69 |
<comment>Dynamic installation |
| 70 |
--------------------</> |
| 71 |
|
| 72 |
Add this to the end of your shell configuration file (e.g. <info>"{$rcFile}"</>): |
| 73 |
|
| 74 |
<info>eval "$({$fullCommand} completion {$shell})"</> |
| 75 |
EOH |
| 76 |
) |
| 77 |
->addArgument('shell', InputArgument::OPTIONAL, 'The shell type (e.g. "bash"), the value of the "$SHELL" env var will be used if this is not given', null, $this->getSupportedShells(...)) |
| 78 |
->addOption('debug', null, InputOption::VALUE_NONE, 'Tail the completion debug log') |
| 79 |
; |
| 80 |
} |
| 81 |
|
| 82 |
protected function execute(InputInterface $input, OutputInterface $output): int |
| 83 |
{ |
| 84 |
$commandName = basename($_SERVER['argv'][0]); |
| 85 |
|
| 86 |
if ($input->getOption('debug')) { |
| 87 |
$this->tailDebugLog($commandName, $output); |
| 88 |
|
| 89 |
return 0; |
| 90 |
} |
| 91 |
|
| 92 |
$shell = $input->getArgument('shell') ?? self::guessShell(); |
| 93 |
$completionFile = __DIR__.'/../Resources/completion.'.$shell; |
| 94 |
if (!file_exists($completionFile)) { |
| 95 |
$supportedShells = $this->getSupportedShells(); |
| 96 |
|
| 97 |
if ($output instanceof ConsoleOutputInterface) { |
| 98 |
$output = $output->getErrorOutput(); |
| 99 |
} |
| 100 |
if ($shell) { |
| 101 |
$output->writeln(\sprintf('<error>Detected shell "%s", which is not supported by Symfony shell completion (supported shells: "%s").</>', $shell, implode('", "', $supportedShells))); |
| 102 |
} else { |
| 103 |
$output->writeln(\sprintf('<error>Shell not detected, Symfony shell completion only supports "%s").</>', implode('", "', $supportedShells))); |
| 104 |
} |
| 105 |
|
| 106 |
return 2; |
| 107 |
} |
| 108 |
|
| 109 |
$output->write(str_replace(['{{ COMMAND_NAME }}', '{{ VERSION }}'], [$commandName, CompleteCommand::COMPLETION_API_VERSION], file_get_contents($completionFile))); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
private static function guessShell(): string |
| 115 |
{ |
| 116 |
return basename($_SERVER['SHELL'] ?? ''); |
| 117 |
} |
| 118 |
|
| 119 |
private function tailDebugLog(string $commandName, OutputInterface $output): void |
| 120 |
{ |
| 121 |
$debugFile = sys_get_temp_dir().'/sf_'.$commandName.'.log'; |
| 122 |
if (!file_exists($debugFile)) { |
| 123 |
touch($debugFile); |
| 124 |
} |
| 125 |
$process = new Process(['tail', '-f', $debugFile], null, null, null, 0); |
| 126 |
$process->run(static function (string $type, string $line) use ($output): void { |
| 127 |
$output->write($line); |
| 128 |
}); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* @return string[] |
| 133 |
*/ |
| 134 |
private function getSupportedShells(): array |
| 135 |
{ |
| 136 |
if (isset($this->supportedShells)) { |
| 137 |
return $this->supportedShells; |
| 138 |
} |
| 139 |
|
| 140 |
$shells = []; |
| 141 |
|
| 142 |
foreach (new \DirectoryIterator(__DIR__.'/../Resources/') as $file) { |
| 143 |
if (str_starts_with($file->getBasename(), 'completion.') && $file->isFile()) { |
| 144 |
$shells[] = $file->getExtension(); |
| 145 |
} |
| 146 |
} |
| 147 |
sort($shells); |
| 148 |
|
| 149 |
return $this->supportedShells = $shells; |
| 150 |
} |
| 151 |
} |