| 1 |  |  | <?php namespace Distilleries\Expendable\Console; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | use Illuminate\Console\DetectsApplicationNamespace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Illuminate\Filesystem\Filesystem; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Symfony\Component\Console\Input\InputArgument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Symfony\Component\Console\Input\InputOption; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Distilleries\Expendable\Console\Lib\Generators\ComponentGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class ComponentMakeCommand extends \Illuminate\Console\GeneratorCommand { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     use DetectsApplicationNamespace; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      * The console command name. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     protected $name = 'expendable:component.make'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $states; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     protected $model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $form; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     protected $datatable; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     protected $template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * The console command description. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $description = 'Creates a controller for a backend component builder class.'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      * @var ComponentGenerator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     protected $formGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 | 288 |  |     public function __construct(Filesystem $files, ComponentGenerator $formGenerator) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 288 |  |         parent::__construct($files); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 288 |  |         $this->formGenerator = $formGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 12 |  |     public function handle() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 12 |  |         $this->initOptions(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 12 |  |         parent::handle(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      * Get the console command arguments. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 288 |  |     protected function getArguments() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 288 |  |             array('name', InputArgument::REQUIRED, 'Full path for Component class.'), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * Get the console command options. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 288 |  |     protected function getOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return array( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 288 |  |             array('states', [], InputOption::VALUE_OPTIONAL, 'States use on the controller', ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 | 144 |  |             array('template', null, InputOption::VALUE_OPTIONAL, 'Template use to generate the controller', ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 144 |  |             array('model', null, InputOption::VALUE_OPTIONAL, 'Model use', ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 144 |  |             array('datatable', null, InputOption::VALUE_OPTIONAL, 'Datatable use', ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 144 |  |             array('form', null, InputOption::VALUE_OPTIONAL, 'Form Use', ''), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 12 |  |     protected function initOptions() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 12 |  |         $states = $this->option('states'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 | 12 |  |         if (!empty($states)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 8 |  |             $this->states = explode(',', $states); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         } else | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |         { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 4 |  |             $this->states = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 12 |  |         $this->template  = $this->option('template'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 12 |  |         $this->model     = $this->option('model'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 12 |  |         $this->datatable = $this->option('datatable'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 12 |  |         $this->form      = $this->option('form'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 101 | 12 |  |     protected function getTemplate() | 
            
                                                                        
                            
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 104 | 12 |  |         if (empty($this->template)) | 
            
                                                                        
                            
            
                                    
            
            
                | 105 |  |  |         { | 
            
                                                                        
                            
            
                                    
            
            
                | 106 | 10 |  |             $defaultComponent = 0; | 
            
                                                                        
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 108 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 109 | 10 |  |             foreach ($this->states as $state) | 
            
                                                                        
                            
            
                                    
            
            
                | 110 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 111 | 8 |  |                 if (strpos($state, 'DatatableStateContract') !== false || (strpos($state, 'FormStateContract') !== false)) | 
            
                                                                        
                            
            
                                    
            
            
                | 112 |  |  |                 { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 | 6 |  |                     $defaultComponent++; | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |                 } | 
            
                                                                        
                            
            
                                    
            
            
                | 116 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 119 | 10 |  |             if ($defaultComponent == 2 && !empty($this->model)) | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 121 | 4 |  |                 $template = 'controller-base-component-class-template'; | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 6 |  |             } else if (!empty($this->model)) | 
            
                                                                        
                            
            
                                    
            
            
                | 123 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 124 | 2 |  |                 $template = 'controller-base-model-class-template'; | 
            
                                                                        
                            
            
                                    
            
            
                | 125 |  |  |             } else | 
            
                                                                        
                            
            
                                    
            
            
                | 126 |  |  |             { | 
            
                                                                        
                            
            
                                    
            
            
                | 127 | 4 |  |                 $template = 'controller-base-class-template'; | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 130 | 10 |  |             return $template; | 
            
                                                                        
                            
            
                                    
            
            
                | 131 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 133 | 2 |  |         return $this->template; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * Replace the class name for the given stub. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      * @param  string $stub | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @param  string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 | 12 |  |     protected function replaceClass($stub, $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 12 |  |         $formGenerator = $this->formGenerator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 12 |  |         $stub = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 12 |  |             '{{class}}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 | 12 |  |             $formGenerator->getClassInfo($name)->className, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 | 6 |  |             $stub | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 12 |  |         $stub = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 12 |  |             '{{model}}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 | 12 |  |             $formGenerator->getClassModel($this->model), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 6 |  |             $stub | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 | 12 |  |         $stub = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 12 |  |             '{{datatable}}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 12 |  |             $this->datatable, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 6 |  |             $stub | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 12 |  |         $stub = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 12 |  |             '{{trait}}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 12 |  |             $formGenerator->getTrait($this->states), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 | 6 |  |             $stub | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 | 12 |  |         $stub = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 | 12 |  |             '{{app}}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 | 12 |  |             $this->getAppNamespace(), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 | 6 |  |             $stub | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 | 12 |  |         $stub = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 | 12 |  |             '{{implement}}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 | 12 |  |             $formGenerator->getImplementation($this->states), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 | 6 |  |             $stub | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 | 12 |  |         $stub = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 | 12 |  |             '{{form}}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 | 12 |  |             $this->form, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 | 6 |  |             $stub | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 | 12 |  |         return $stub; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * Replace the namespace for the given stub. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * @param  string $stub | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      * @param  string $name | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 | 12 |  |     protected function replaceNamespace(&$stub, $name) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 | 12 |  |         $stub = str_replace( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 | 12 |  |             '{{namespace}}', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 | 12 |  |             $this->formGenerator->getClassInfo($name)->namespace, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 | 6 |  |             $stub | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 | 12 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 213 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 214 |  |  |      * Get the desired class name from the input. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 215 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 216 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 217 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 218 | 12 |  |     protected function getNameInput() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 219 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 220 | 12 |  |         return str_replace('/', '\\', $this->argument('name')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 221 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 222 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 223 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 224 |  |  |      * Get the stub file for the generator. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 225 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 226 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 227 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 228 | 12 |  |     protected function getStub() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 229 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 230 | 12 |  |         return __DIR__.'/Lib/stubs/'.$this->getTemplate().'.stub'; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 231 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 232 |  |  | } |