| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace JetFire\Routing\Matcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use JetFire\Routing\Dispatcher\ControllerDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use JetFire\Routing\Dispatcher\TemplateDispatcher; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use JetFire\Routing\Router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Class SmartMatch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @package JetFire\Routing\Match | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class UriMatcher implements MatcherInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $request = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     private $resolver = ['isControllerAndTemplate']; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     private $dispatcher = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         'isTemplate' => TemplateDispatcher::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         'isController' => ControllerDispatcher::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         'isControllerAndTemplate' => [ControllerDispatcher::class, TemplateDispatcher::class], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param Router $router | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function __construct(Router $router) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $this->router = $router; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @param array $resolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function setResolver($resolver = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         $this->resolver = $resolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param string $resolver | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     public function addResolver($resolver) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->resolver[] = $resolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     public function getResolver() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         return $this->resolver; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param array $dispatcher | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 75 |  |  |     public function setDispatcher($dispatcher = []) | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |         $this->dispatcher = $dispatcher; | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |      * @param $method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     public function addDispatcher($method, $class) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->dispatcher[$method] = $class; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     public function match() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         foreach ($this->resolver as $resolver) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |             if (is_array($target = call_user_func([$this, $resolver]))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                 $this->setTarget($target); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                 return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      * @param array $target | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     public function setTarget($target = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         $this->router->route->setDetail($this->request); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->router->route->setTarget($target); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @return array|bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function isControllerAndTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         if (is_array($ctrl = $this->isController())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |             if (is_array($tpl = $this->isTemplate())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |                 return array_merge(array_merge($ctrl, $tpl), [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |                     'dispatcher' => $this->dispatcher['isControllerAndTemplate'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |                 ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             return $ctrl; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         return $this->isTemplate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |      * @return bool|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |     public function isTemplate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |         foreach ($this->router->getConfig()['templateExtension'] as $extension) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |             for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |                 $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '', $this->router->route->getUrl())); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |                 $end = array_pop($url); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |                 $url = implode('/', array_map('ucwords', $url)) . '/' . $end; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |                 if (is_file(($template = rtrim($this->router->collection->getRoutes('view_dir_' . $i), '/') . $url . $extension))) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |                     $this->request['collection_index'] = $i; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |                     return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                         'dispatcher' => $this->dispatcher['isTemplate'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                         'template' => $template, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                         'extension' => substr(strrchr($extension, "."), 1), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |                         'callback' => $this->router->getConfig()['templateCallback'] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |                     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |      * @return bool|array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |     public function isController() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         $routes = array_slice(explode('/', $this->router->route->getUrl()), 1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         $i = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         do { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             $route = ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |             if (isset($route[0])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |                 $class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i) . ucfirst($route[0]) . 'Controller')) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |                     ? $this->router->collection->getRoutes('ctrl_namespace_' . $i) . ucfirst($route[0]) . 'Controller' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |                     : ucfirst($route[0]) . 'Controller'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |                 $route[1] = isset($route[1]) ? $route[1] : 'index'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |                 if (method_exists($class, $route[1])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |                     $this->request['parameters'] = array_slice($route, 2); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |                     $this->request['collection_index'] = $i; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |                     return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |                         'dispatcher' => $this->dispatcher['isController'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |                         'di' => $this->router->getConfig()['di'], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |                         'controller' => $class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |                         'action' => $route[1] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |                     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |                 } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  |             ++$i; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |         } while ($i < $this->router->collection->countRoutes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  |         return false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 185 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 186 |  |  |  |