| 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 |  |  |         // Hide this command from listing if supported | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |         // Command::setHidden() was not available before Symfony 3.2.0 | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         if (method_exists($this, 'setHidden')) { | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |             $this->setHidden(true); | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     public function getNativeDefinition() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         return $this->createDefinition(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     protected function execute(InputInterface $input, OutputInterface $output) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |         $this->handler = new CompletionHandler($this->getApplication()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         $handler = $this->handler; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |         if ($input->getOption('generate-hook')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             global $argv; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             $program = $argv[0]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |             $factory = new HookFactory(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |             $alias = $input->getOption('program'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |             $multiple = (bool)$input->getOption('multiple'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             if (!$alias) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |                 $alias = basename($program); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |             $hook = $factory->generateHook( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                 $input->getOption('shell-type') ?: $this->getShellType(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 $program, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                 $alias, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 $multiple | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |             $output->write($hook, true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |             $handler->setContext(new EnvironmentCompletionContext()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |             $output->write($this->runCompletion(), true); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * Run the completion handler and return a filtered list of results | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      * @deprecated - This will be removed in 1.0.0 in favour of CompletionCommand::configureCompletion | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return string[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     protected function runCompletion() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         $this->configureCompletion($this->handler); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         return $this->handler->runCompletion(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * Configure the CompletionHandler instance before it is run | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param CompletionHandler $handler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     protected function configureCompletion(CompletionHandler $handler) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |         // Override this method to configure custom value completions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * Determine the shell type for use with HookFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     protected function getShellType() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         if (!getenv('SHELL')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             throw new \RuntimeException('Could not read SHELL environment variable. Please specify your shell type using the --shell-type option.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         return basename(getenv('SHELL')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     protected function createDefinition() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         return new InputDefinition(array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |             new InputOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 'generate-hook', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |                 'g', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |                 InputOption::VALUE_NONE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 'Generate BASH code that sets up completion for this application.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             new InputOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 'program', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |                 'p', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |                 InputOption::VALUE_REQUIRED, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |                 "Program name that should trigger completion\n<comment>(defaults to the absolute application path)</comment>." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |             new InputOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |                 'multiple', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |                 'm', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 InputOption::VALUE_NONE, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 "Generated hook can be used for multiple applications." | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |             new InputOption( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                 'shell-type', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                 null, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 InputOption::VALUE_OPTIONAL, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                 'Set the shell type (zsh or bash). Otherwise this is determined automatically.' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             ), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         )); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 150 |  |  |  |