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\Completion\CompletionInput;
15use Symfony\Component\Console\Completion\CompletionSuggestions;
16use Symfony\Component\Console\Input\InputArgument;
17use Symfony\Component\Console\Input\InputInterface;
18use Symfony\Component\Console\Input\InputOption;
19use Symfony\Component\Console\Output\ConsoleOutputInterface;
20use Symfony\Component\Console\Output\OutputInterface;
21use Symfony\Component\Process\Process;
22
23/**
24 * Dumps the completion script for the current shell.
25 *
26 * @author Wouter de Jong <wouter@wouterj.nl>
27 */
28final class DumpCompletionCommand extends Command
29{
30    protected static $defaultName = 'completion';
31    protected static $defaultDescription = 'Dump the shell completion script';
32
33    public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
34    {
35        if ($input->mustSuggestArgumentValuesFor('shell')) {
36            $suggestions->suggestValues($this->getSupportedShells());
37        }
38    }
39
40    protected function configure()
41    {
42        $fullCommand = $_SERVER['PHP_SELF'];
43        $commandName = basename($fullCommand);
44        $fullCommand = realpath($fullCommand) ?: $fullCommand;
45
46        $this
47            ->setHelp(<<<EOH
48The <info>%command.name%</> command dumps the shell completion script required
49to use shell autocompletion (currently only bash completion is supported).
50
51<comment>Static installation
52-------------------</>
53
54Dump the script to a global completion file and restart your shell:
55
56    <info>%command.full_name% bash | sudo tee /etc/bash_completion.d/${commandName}</>
57
58Or dump the script to a local file and source it:
59
60    <info>%command.full_name% bash > completion.sh</>
61
62    <comment># source the file whenever you use the project</>
63    <info>source completion.sh</>
64
65    <comment># or add this line at the end of your "~/.bashrc" file:</>
66    <info>source /path/to/completion.sh</>
67
68<comment>Dynamic installation
69--------------------</>
70
71Add this add the end of your shell configuration file (e.g. <info>"~/.bashrc"</>):
72
73    <info>eval "$(${fullCommand} completion bash)"</>
74EOH
75            )
76            ->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')
77            ->addOption('debug', null, InputOption::VALUE_NONE, 'Tail the completion debug log')
78        ;
79    }
80
81    protected function execute(InputInterface $input, OutputInterface $output): int
82    {
83        $commandName = basename($_SERVER['argv'][0]);
84
85        if ($input->getOption('debug')) {
86            $this->tailDebugLog($commandName, $output);
87
88            return self::SUCCESS;
89        }
90
91        $shell = $input->getArgument('shell') ?? self::guessShell();
92        $completionFile = __DIR__.'/../Resources/completion.'.$shell;
93        if (!file_exists($completionFile)) {
94            $supportedShells = $this->getSupportedShells();
95
96            ($output instanceof ConsoleOutputInterface ? $output->getErrorOutput() : $output)
97                ->writeln(sprintf('<error>Detected shell "%s", which is not supported by Symfony shell completion (supported shells: "%s").</>', $shell, implode('", "', $supportedShells)));
98
99            return self::INVALID;
100        }
101
102        $output->write(str_replace(['{{ COMMAND_NAME }}', '{{ VERSION }}'], [$commandName, $this->getApplication()->getVersion()], file_get_contents($completionFile)));
103
104        return self::SUCCESS;
105    }
106
107    private static function guessShell(): string
108    {
109        return basename($_SERVER['SHELL'] ?? '');
110    }
111
112    private function tailDebugLog(string $commandName, OutputInterface $output): void
113    {
114        $debugFile = sys_get_temp_dir().'/sf_'.$commandName.'.log';
115        if (!file_exists($debugFile)) {
116            touch($debugFile);
117        }
118        $process = new Process(['tail', '-f', $debugFile], null, null, null, 0);
119        $process->run(function (string $type, string $line) use ($output): void {
120            $output->write($line);
121        });
122    }
123
124    /**
125     * @return string[]
126     */
127    private function getSupportedShells(): array
128    {
129        return array_map(function ($f) {
130            return pathinfo($f, \PATHINFO_EXTENSION);
131        }, glob(__DIR__.'/../Resources/completion.*'));
132    }
133}
134