Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 991 | Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

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