summaryrefslogtreecommitdiff
path: root/vendor/symfony/console/Command/DumpCompletionCommand.php
diff options
context:
space:
mode:
authorpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
committerpolo <ordipolo@gmx.fr>2024-08-13 23:45:21 +0200
commitbf6655a534a6775d30cafa67bd801276bda1d98d (patch)
treec6381e3f6c81c33eab72508f410b165ba05f7e9c /vendor/symfony/console/Command/DumpCompletionCommand.php
parent94d67a4b51f8e62e7d518cce26a526ae1ec48278 (diff)
downloadAppliGestionPHP-bf6655a534a6775d30cafa67bd801276bda1d98d.zip
VERSION 0.2 doctrine ORM et entités
Diffstat (limited to 'vendor/symfony/console/Command/DumpCompletionCommand.php')
-rw-r--r--vendor/symfony/console/Command/DumpCompletionCommand.php151
1 files changed, 151 insertions, 0 deletions
diff --git a/vendor/symfony/console/Command/DumpCompletionCommand.php b/vendor/symfony/console/Command/DumpCompletionCommand.php
new file mode 100644
index 0000000..be6f545
--- /dev/null
+++ b/vendor/symfony/console/Command/DumpCompletionCommand.php
@@ -0,0 +1,151 @@
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
12namespace Symfony\Component\Console\Command;
13
14use Symfony\Component\Console\Attribute\AsCommand;
15use Symfony\Component\Console\Input\InputArgument;
16use Symfony\Component\Console\Input\InputInterface;
17use Symfony\Component\Console\Input\InputOption;
18use Symfony\Component\Console\Output\ConsoleOutputInterface;
19use Symfony\Component\Console\Output\OutputInterface;
20use Symfony\Component\Process\Process;
21
22/**
23 * Dumps the completion script for the current shell.
24 *
25 * @author Wouter de Jong <wouter@wouterj.nl>
26 */
27#[AsCommand(name: 'completion', description: 'Dump the shell completion script')]
28final 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 = $this->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
49The <info>%command.name%</> command dumps the shell completion script required
50to use shell autocompletion (currently, {$supportedShells} completion are supported).
51
52<comment>Static installation
53-------------------</>
54
55Dump the script to a global completion file and restart your shell:
56
57 <info>%command.full_name% {$shell} | sudo tee {$completionFile}</>
58
59Or 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
72Add this to the end of your shell configuration file (e.g. <info>"{$rcFile}"</>):
73
74 <info>eval "$({$fullCommand} completion {$shell})"</>
75EOH
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(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}