| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | namespace KMLaravel\ApiGenerator\BuildClasses; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use KMLaravel\ApiGenerator\Facade\KMFileFacade; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class KMControllerBuilder extends KMBaseBuilder | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      * @var string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     protected $typeToMake = "Controller"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected $paths = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |         "{{ controller_namespace }}" => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         "{{ model_path }}" => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         "{{ resource_path }}" => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |         "{{ request_path }}" => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |         "{{ controller_path }}" => [], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @desc this property use keys in paths property to determine which path we will replace with class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @example if we want to replace {{ model_class }} in controller.stub , so we will get $this->classes['{{ model_path }}'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * you will find full logic process in pathsAndClassesReplacer function | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     protected $classes = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         "{{ model_path }}" => "{{ model_class }}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         "{{ resource_path }}" => "{{ resource_class }}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         "{{ request_path }}" => "{{ request_class }}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         "{{ controller_path }}" => "{{ controller_class }}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         "{{ controller_namespace }}" => "{{ controller_namespace }}", | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param array $columns | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function controllerReplacer($columns, $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         // build paths and classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         $this->classReplacer($options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         $file = KMFileFacade::getFilesFromStubsAsStream("Controller"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         // get controller path we create in callArtisan function from Controllers Folder to inject paths and classes | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $fileInApp = app_path("Http/Controllers/$this->fileName.php"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $this->pathsAndClassesReplacer($file, $fileInApp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |      * @param array $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     public function build($column, $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         $this->controllerReplacer($column, $options); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         return $this->filepath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * @param array $column | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param array $options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * @desc this function run as simply artisan make:controller command if the base controller dose not exits. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |     public function basicBuild($column, $options = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |         return $this->filepath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param array $option | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @desc | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * Convert options to array to use foreach at all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * Find intersecting values between paths and actor inputs | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * get full class namespace from options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      * get class name and fill paths array | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  |     protected function classReplacer($option) | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  |         if (!gettype($option) == 'array') $option = [$option]; | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  |         $controllerNameSpace = $this->checkIfFileInSubFolder("App\Http\Controllers" , $this->fileName); | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  |         $option['{{ controller_namespace }}'] = $controllerNameSpace; | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  |         foreach ($option as $path => $classNameSpace) { | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  |             $className = $this->getCLassName($classNameSpace); | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  |             $classNameSpace = str_replace('/' , '\\' , $classNameSpace); | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  |             $className = $this->array_last_value(explode('/' , $className)); | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  |             $this->paths[$path] = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 92 |  |  |                 $classNameSpace => $className, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param string|mixed $file | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param mixed | string $fileInApp | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @return false|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @desc the function is doing : | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * get the namespace from path which is now the main keys in this array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @example | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * $paths = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * "App\User" => "User" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * then get this class "User" | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * and we put this in last file we modified in $newFile variable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |     protected function pathsAndClassesReplacer($file, $fileInApp) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         // set default value for file , and at next loop we will override this value for build this controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         $newFile = $file; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         foreach ($this->paths as $itemToReplace => $values) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             $nameSpace = array_key_first($values) ?? "{{ chose_your_name_space_here }}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             $classType = $this->classes[$itemToReplace]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             $class = $values[$nameSpace] ?? "{{ chose_your_class_here }}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |             $fileReplaced = str_replace([$itemToReplace, $classType], [$nameSpace, $class], $newFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |             $newFile = $fileReplaced; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         return file_put_contents($fileInApp, $newFile); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 124 |  |  |  |