| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace autocomplete; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use terminal\Terminal; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \Config; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | final class Autocomplete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     protected $prev; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     protected $cur; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     function __construct( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |         Config              $config, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |         Terminal            $terminal, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |         Filesystem          $filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     ) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         $this->config               = $config; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         $this->terminal             = $terminal; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         $this->filesystem           = $filesystem; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         $arguments  = $this->terminal->getArguments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |         $this->prev = empty($arguments['prev']) ? '' : $arguments['prev']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |         $this->cur  = empty($arguments['cur']) ? '' : $arguments['cur']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * get list of addons at output path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @return array - array of strings - name of addons | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |     public function getAddonsList() | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  |         $addonsPath = sanitize_filename( | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  |             $this->config->get('path') | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  |             . $this->config->get('filesystem.output_path_relative') . '../' | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  |         ); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |         $dirs = $this->filesystem->listDirs($addonsPath); | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  |         return array_map(function($dir) { | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  |             $paths = explode('/', $dir); | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |             return end($paths); | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |         }, $dirs); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * Suggests a param which still is not used | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @param string $argument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * @param array|callable $values - possible values for this argument (option) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * or function that returns $values | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return void|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function queueArgument(string $argument, $values = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $arguments  = $this->terminal->getArguments(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $option     = '--' . $argument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         if (empty($arguments[$argument])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |             return [$option]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($this->prev === $option) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             if (is_callable($values)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 return call_user_func($values); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |              | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             return $values; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Combines suggestions and returns actual suggestion | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @param array $queue | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function combineQueueParam(...$queue) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         foreach ($queue as $argument) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |             if (is_array($argument)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 return $argument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         return []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 93 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |  |