Minor dependency updates
[yaffs-website] / vendor / stecman / symfony-console-completion / src / CompletionCommand.php
1 <?php
2
3 namespace Stecman\Component\Symfony\Console\BashCompletion;
4
5 use Symfony\Component\Console\Command\Command as SymfonyCommand;
6 use Symfony\Component\Console\Input\InputDefinition;
7 use Symfony\Component\Console\Input\InputInterface;
8 use Symfony\Component\Console\Input\InputOption;
9 use Symfony\Component\Console\Output\OutputInterface;
10
11 class CompletionCommand extends SymfonyCommand
12 {
13
14     /**
15      * @var CompletionHandler
16      */
17     protected $handler;
18
19     protected function configure()
20     {
21         $this
22             ->setName('_completion')
23             ->setDefinition($this->createDefinition())
24             ->setDescription('BASH completion hook.')
25             ->setHelp(<<<END
26 To enable BASH completion, run:
27
28     <comment>eval `[program] _completion -g`</comment>.
29
30 Or for an alias:
31
32     <comment>eval `[program] _completion -g -p [alias]`</comment>.
33
34 END
35             );
36     }
37
38     /**
39      * {@inheritdoc}
40      */
41     public function getNativeDefinition()
42     {
43         return $this->createDefinition();
44     }
45
46     protected function execute(InputInterface $input, OutputInterface $output)
47     {
48         $this->handler = new CompletionHandler($this->getApplication());
49         $handler = $this->handler;
50
51         if ($input->getOption('generate-hook')) {
52             global $argv;
53             $program = $argv[0];
54
55             $factory = new HookFactory();
56             $alias = $input->getOption('program');
57             $multiple = (bool)$input->getOption('multiple');
58
59             // When completing for multiple apps having absolute path in the alias doesn't make sense.
60             if (!$alias && $multiple) {
61                 $alias = basename($program);
62             }
63
64             $hook = $factory->generateHook(
65                 $input->getOption('shell-type') ?: $this->getShellType(),
66                 $program,
67                 $alias,
68                 $multiple
69             );
70
71             $output->write($hook, true);
72         } else {
73             $handler->setContext(new EnvironmentCompletionContext());
74             $output->write($this->runCompletion(), true);
75         }
76     }
77
78     /**
79      * Run the completion handler and return a filtered list of results
80      *
81      * @deprecated - This will be removed in 1.0.0 in favour of CompletionCommand::configureCompletion
82      *
83      * @return string[]
84      */
85     protected function runCompletion()
86     {
87         $this->configureCompletion($this->handler);
88         return $this->handler->runCompletion();
89     }
90
91     /**
92      * Configure the CompletionHandler instance before it is run
93      *
94      * @param CompletionHandler $handler
95      */
96     protected function configureCompletion(CompletionHandler $handler)
97     {
98         // Override this method to configure custom value completions
99     }
100
101     /**
102      * Determine the shell type for use with HookFactory
103      *
104      * @return string
105      */
106     protected function getShellType()
107     {
108         if (!getenv('SHELL')) {
109             throw new \RuntimeException('Could not read SHELL environment variable. Please specify your shell type using the --shell-type option.');
110         }
111
112         return basename(getenv('SHELL'));
113     }
114
115     protected function createDefinition()
116     {
117         return new InputDefinition(array(
118             new InputOption(
119                 'generate-hook',
120                 'g',
121                 InputOption::VALUE_NONE,
122                 'Generate BASH code that sets up completion for this application.'
123             ),
124             new InputOption(
125                 'program',
126                 'p',
127                 InputOption::VALUE_REQUIRED,
128                 "Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>."
129             ),
130             new InputOption(
131                 'multiple',
132                 'm',
133                 InputOption::VALUE_NONE,
134                 "Generated hook can be used for multiple applications."
135             ),
136             new InputOption(
137                 'shell-type',
138                 null,
139                 InputOption::VALUE_OPTIONAL,
140                 'Set the shell type (zsh or bash). Otherwise this is determined automatically.'
141             ),
142         ));
143     }
144 }