| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace crocodicstudio\crudbooster\CBCoreModule; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Route; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | class RouteController | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 8 |  |  | { | 
            
                                                                        
                            
            
                                    
            
            
                | 9 |  |  |     public static function routeController($prefix, $controller, $namespace = null) | 
            
                                                                        
                            
            
                                    
            
            
                | 10 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 11 |  |  |         $prefix = trim($prefix, '/').'/'; | 
            
                                                                        
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 13 |  |  |         try { | 
            
                                                                        
                            
            
                                    
            
            
                | 14 |  |  |             Route::get($prefix, ['uses' => $controller.'@getIndex', 'as' => $controller.'GetIndex']); | 
            
                                                                        
                            
            
                                    
            
            
                | 15 |  |  |             $ctrl = self::getControllerPath($controller, $namespace); | 
            
                                                                        
                            
            
                                    
            
            
                | 16 |  |  |             foreach (self::getControllerMethods($ctrl) as $method) { | 
            
                                                                        
                            
            
                                    
            
            
                | 17 |  |  |                 self::setRoute($prefix, $controller, $method); | 
            
                                                                        
                            
            
                                    
            
            
                | 18 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 19 |  |  |         } catch (\Exception $e) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param $prefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @param $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @param $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param $wildcards | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private static function routePost($prefix, $controller, $method, $wildcards) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         $methodName = substr($method->name, 4); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $slug = array_filter(preg_split('/(?=[A-Z])/', $methodName)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |         $slug = strtolower(implode('-', $slug)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         Route::post($prefix.$slug.$wildcards, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |             'uses' => $controller.'@'.$method->name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |             'as' => $controller.'Post'.$methodName, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param $prefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @param $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * @param $wildcards | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     private static function routeGet($prefix, $controller, $method, $wildcards) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $methodName = substr($method->name, 3); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         $slug = array_filter(preg_split('/(?=[A-Z])/', $methodName)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $slug = strtolower(implode('-', $slug)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |         $slug = ($slug == 'index') ? '' : $slug; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         Route::get($prefix.$slug.$wildcards, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             ['uses' => $controller.'@'.$method->name, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'as' => $controller.'Get'.$methodName]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param $ctrl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return array|\ReflectionMethod[] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * @throws \ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     private static function getControllerMethods($ctrl) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         $controller_methods = (new \ReflectionClass($ctrl))->getMethods(\ReflectionMethod::IS_PUBLIC); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $controller_methods = array_filter($controller_methods, function ($method) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             return ($method->class !== 'Illuminate\Routing\Controller' && $method->name !== 'getIndex'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         }); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $controller_methods; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param $namespace | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     private static function getControllerPath($controller, $namespace) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         $ns = $namespace ?: ctrlNamespace(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $ctrl = $ns.'\\'.$controller; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         return $ctrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |      * @param $prefix | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @param $controller | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      * @param $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     private static function setRoute($prefix, $controller, $method) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $wildcards = '/{one?}/{two?}/{three?}/{four?}/{five?}'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         if (str_start($method, 'get')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             self::routeGet($prefix, $controller, $method, $wildcards); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         } elseif (str_start($method, 'post')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             self::routePost($prefix, $controller, $method, $wildcards); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 99 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 100 |  |  | } |