@@ -54,7 +54,7 @@ discard block |
||
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /** |
| 57 | - * @param null $key |
|
| 57 | + * @param string $key |
|
| 58 | 58 | * @return array |
| 59 | 59 | */ |
| 60 | 60 | public function getRoutes($key = null) |
@@ -96,7 +96,7 @@ discard block |
||
| 96 | 96 | |
| 97 | 97 | /** |
| 98 | 98 | * @param array $args |
| 99 | - * @param $i |
|
| 99 | + * @param integer $i |
|
| 100 | 100 | */ |
| 101 | 101 | private function setRoutes($args = [], $i) |
| 102 | 102 | { |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | /** |
| 155 | - * @param $url |
|
| 155 | + * @param string $url |
|
| 156 | 156 | * @return bool |
| 157 | 157 | */ |
| 158 | 158 | public function getDomain($url) |
@@ -43,12 +43,12 @@ discard block |
||
| 43 | 43 | public function addRoutes($routes = null, $options = []) |
| 44 | 44 | { |
| 45 | 45 | if (!is_null($routes) && !is_array($routes)) { |
| 46 | - if (strpos($routes, '.php') === false) $routes = trim($routes, '/') . '/'; |
|
| 47 | - if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) $routes = $routesFile; |
|
| 46 | + if (strpos($routes, '.php') === false) $routes = trim($routes, '/').'/'; |
|
| 47 | + if (is_file($routes.'/routes.php') && is_array($routesFile = include $routes.'/routes.php')) $routes = $routesFile; |
|
| 48 | 48 | elseif (is_file($routes) && is_array($routesFile = include $routes)) $routes = $routesFile; |
| 49 | - else throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given'); |
|
| 49 | + else throw new \InvalidArgumentException('Argument for "'.get_called_class().'" constructor is not recognized. Expected argument array or file containing array but "'.$routes.'" given'); |
|
| 50 | 50 | } |
| 51 | - $this->routes['routes_' . $this->countRoutes] = is_array($routes) ? $routes : []; |
|
| 51 | + $this->routes['routes_'.$this->countRoutes] = is_array($routes) ? $routes : []; |
|
| 52 | 52 | $this->setRoutes($options, $this->countRoutes); |
| 53 | 53 | $this->countRoutes++; |
| 54 | 54 | } |
@@ -72,10 +72,10 @@ discard block |
||
| 72 | 72 | if (is_array($args)) { |
| 73 | 73 | $nbrArgs = count($args); |
| 74 | 74 | for ($i = 0; $i < $nbrArgs; ++$i) |
| 75 | - $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
| 75 | + $this->routes['prefix_'.$i] = '/'.trim($args[$i], '/'); |
|
| 76 | 76 | } elseif (is_string($args)) |
| 77 | 77 | for ($i = 0; $i < $this->countRoutes; ++$i) |
| 78 | - $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
| 78 | + $this->routes['prefix_'.$i] = '/'.trim($args, '/'); |
|
| 79 | 79 | if ($this->countRoutes == 0) $this->countRoutes++; |
| 80 | 80 | } |
| 81 | 81 | |
@@ -88,7 +88,7 @@ discard block |
||
| 88 | 88 | for ($i = 0; $i < $nbrArgs; ++$i) { |
| 89 | 89 | if (is_array($args[$i])) { |
| 90 | 90 | $this->setRoutes($args[$i], $i); |
| 91 | - if (!isset($this->routes['routes_' . $i])) $this->routes['routes_' . $i] = []; |
|
| 91 | + if (!isset($this->routes['routes_'.$i])) $this->routes['routes_'.$i] = []; |
|
| 92 | 92 | } |
| 93 | 93 | } |
| 94 | 94 | if ($this->countRoutes == 0) $this->countRoutes++; |
@@ -100,11 +100,11 @@ discard block |
||
| 100 | 100 | */ |
| 101 | 101 | private function setRoutes($args = [], $i) |
| 102 | 102 | { |
| 103 | - $this->routes['block_' . $i] = (isset($args['block']) && !empty($args['block'])) ? rtrim($args['block'], '/') . '/' : ''; |
|
| 104 | - $this->routes['view_dir_' . $i] = (isset($args['view_dir']) && !empty($args['view_dir'])) ? rtrim($args['view_dir'], '/') . '/' : ''; |
|
| 105 | - $this->routes['ctrl_namespace_' . $i] = (isset($args['ctrl_namespace']) && !empty($args['ctrl_namespace'])) ? trim($args['ctrl_namespace'], '\\') . '\\' : ''; |
|
| 106 | - $this->routes['prefix_' . $i] = (isset($args['prefix']) && !empty($args['prefix'])) ? '/' . trim($args['prefix'], '/') : ''; |
|
| 107 | - $this->routes['subdomain_' . $i] = (isset($args['subdomain'])) ? $args['subdomain'] : ''; |
|
| 103 | + $this->routes['block_'.$i] = (isset($args['block']) && !empty($args['block'])) ? rtrim($args['block'], '/').'/' : ''; |
|
| 104 | + $this->routes['view_dir_'.$i] = (isset($args['view_dir']) && !empty($args['view_dir'])) ? rtrim($args['view_dir'], '/').'/' : ''; |
|
| 105 | + $this->routes['ctrl_namespace_'.$i] = (isset($args['ctrl_namespace']) && !empty($args['ctrl_namespace'])) ? trim($args['ctrl_namespace'], '\\').'\\' : ''; |
|
| 106 | + $this->routes['prefix_'.$i] = (isset($args['prefix']) && !empty($args['prefix'])) ? '/'.trim($args['prefix'], '/') : ''; |
|
| 107 | + $this->routes['subdomain_'.$i] = (isset($args['subdomain'])) ? $args['subdomain'] : ''; |
|
| 108 | 108 | } |
| 109 | 109 | |
| 110 | 110 | /** |
@@ -126,24 +126,24 @@ discard block |
||
| 126 | 126 | */ |
| 127 | 127 | public function generateRoutesPath() |
| 128 | 128 | { |
| 129 | - $root = (isset($_SERVER['REQUEST_SCHEME'])?$_SERVER['REQUEST_SCHEME']:'http') . '://' . ($domain = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])) . str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']); |
|
| 129 | + $root = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http').'://'.($domain = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])).str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']); |
|
| 130 | 130 | if (strpos($domain, ($new_domain = $this->getDomain($root))) !== false) |
| 131 | 131 | $root = str_replace($domain, $new_domain, $root); |
| 132 | 132 | $count = 0; |
| 133 | 133 | for ($i = 0; $i < $this->countRoutes; ++$i) { |
| 134 | - $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : ''; |
|
| 135 | - if (isset($this->routes['routes_' . $i])) |
|
| 136 | - foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
| 134 | + $prefix = (isset($this->routes['prefix_'.$i])) ? $this->routes['prefix_'.$i] : ''; |
|
| 135 | + if (isset($this->routes['routes_'.$i])) |
|
| 136 | + foreach ($this->routes['routes_'.$i] as $route => $dependencies) { |
|
| 137 | 137 | if (is_array($dependencies) && isset($dependencies['use'])) |
| 138 | - $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
| 138 | + $use = (is_callable($dependencies['use'])) ? 'closure-'.$count : trim($dependencies['use'], '/'); |
|
| 139 | 139 | elseif (!is_array($dependencies)) |
| 140 | - $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
| 140 | + $use = (is_callable($dependencies)) ? 'closure-'.$count : trim($dependencies, '/'); |
|
| 141 | 141 | else |
| 142 | 142 | $use = $route; |
| 143 | 143 | if (isset($route[0]) && $route[0] == '/') { |
| 144 | - (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use . '#' . $dependencies['name']] = $root . $prefix . $route : $this->routesByName[$use] = $root . $prefix . $route; |
|
| 144 | + (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use.'#'.$dependencies['name']] = $root.$prefix.$route : $this->routesByName[$use] = $root.$prefix.$route; |
|
| 145 | 145 | } else { |
| 146 | - (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use . '#' . $dependencies['name']] = $_SERVER['REQUEST_SCHEME'] . '://' . str_replace('{host}', $new_domain, $route) . $prefix : $this->routesByName[$use] = $_SERVER['REQUEST_SCHEME'] . '://' . str_replace('{host}', $new_domain, $route) . $prefix; |
|
| 146 | + (!is_callable($dependencies) && isset($dependencies['name'])) ? $this->routesByName[$use.'#'.$dependencies['name']] = $_SERVER['REQUEST_SCHEME'].'://'.str_replace('{host}', $new_domain, $route).$prefix : $this->routesByName[$use] = $_SERVER['REQUEST_SCHEME'].'://'.str_replace('{host}', $new_domain, $route).$prefix; |
|
| 147 | 147 | } |
| 148 | 148 | $count++; |
| 149 | 149 | } |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | foreach ($this->routesByName as $key => $route) { |
| 177 | 177 | $param = explode('#', $key); |
| 178 | 178 | $route = str_replace('{subdomain}', $subdomain, $route); |
| 179 | - foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route); |
|
| 179 | + foreach ($params as $key2 => $value) $route = str_replace(':'.$key2, $value, $route); |
|
| 180 | 180 | if ($param[0] == trim($name, '/')) return $route; |
| 181 | 181 | else if (isset($param[1]) && $param[1] == $name) return $route; |
| 182 | 182 | } |
@@ -25,7 +25,7 @@ discard block |
||
| 25 | 25 | /** |
| 26 | 26 | * @var array |
| 27 | 27 | */ |
| 28 | - private $resolver = ['isClosureAndTemplate','isControllerAndTemplate','isTemplate']; |
|
| 28 | + private $resolver = ['isClosureAndTemplate', 'isControllerAndTemplate', 'isTemplate']; |
|
| 29 | 29 | |
| 30 | 30 | /** |
| 31 | 31 | * @var array |
@@ -34,8 +34,8 @@ discard block |
||
| 34 | 34 | 'isClosure' => 'JetFire\Routing\Dispatcher\ClosureDispatcher', |
| 35 | 35 | 'isController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher', |
| 36 | 36 | 'isTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher', |
| 37 | - 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
| 38 | - 'isClosureAndTemplate' => ['JetFire\Routing\Dispatcher\ClosureDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
| 37 | + 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
| 38 | + 'isClosureAndTemplate' => ['JetFire\Routing\Dispatcher\ClosureDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
| 39 | 39 | ]; |
| 40 | 40 | |
| 41 | 41 | /** |
@@ -49,14 +49,14 @@ discard block |
||
| 49 | 49 | /** |
| 50 | 50 | * @param array $resolver |
| 51 | 51 | */ |
| 52 | - public function setResolver($resolver = []){ |
|
| 52 | + public function setResolver($resolver = []) { |
|
| 53 | 53 | $this->resolver = $resolver; |
| 54 | 54 | } |
| 55 | 55 | |
| 56 | 56 | /** |
| 57 | 57 | * @param string $resolver |
| 58 | 58 | */ |
| 59 | - public function addResolver($resolver){ |
|
| 59 | + public function addResolver($resolver) { |
|
| 60 | 60 | $this->resolver[] = $resolver; |
| 61 | 61 | } |
| 62 | 62 | |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | /** |
| 72 | 72 | * @param array $dispatcher |
| 73 | 73 | */ |
| 74 | - public function setDispatcher($dispatcher = []){ |
|
| 74 | + public function setDispatcher($dispatcher = []) { |
|
| 75 | 75 | $this->dispatcher = $dispatcher; |
| 76 | 76 | } |
| 77 | 77 | |
@@ -81,7 +81,7 @@ discard block |
||
| 81 | 81 | * @return mixed|void |
| 82 | 82 | * @internal param array $dispatcher |
| 83 | 83 | */ |
| 84 | - public function addDispatcher($method,$class){ |
|
| 84 | + public function addDispatcher($method, $class) { |
|
| 85 | 85 | $this->dispatcher[$method] = $class; |
| 86 | 86 | } |
| 87 | 87 | |
@@ -92,15 +92,15 @@ discard block |
||
| 92 | 92 | { |
| 93 | 93 | $this->request = []; |
| 94 | 94 | for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
| 95 | - $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_' . $i) != '') ? $this->router->collection->getRoutes('prefix_' . $i) : ''; |
|
| 96 | - $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_' . $i) != '') ? $this->router->collection->getRoutes('subdomain_' . $i) : ''; |
|
| 97 | - foreach ($this->router->collection->getRoutes('routes_' . $i) as $route => $params) { |
|
| 95 | + $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_'.$i) != '') ? $this->router->collection->getRoutes('prefix_'.$i) : ''; |
|
| 96 | + $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_'.$i) != '') ? $this->router->collection->getRoutes('subdomain_'.$i) : ''; |
|
| 97 | + foreach ($this->router->collection->getRoutes('routes_'.$i) as $route => $params) { |
|
| 98 | 98 | $this->request['params'] = $params; |
| 99 | 99 | $this->request['collection_index'] = $i; |
| 100 | - if($this->checkSubdomain($route)) { |
|
| 100 | + if ($this->checkSubdomain($route)) { |
|
| 101 | 101 | $route = strstr($route, '/'); |
| 102 | - $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/')); |
|
| 103 | - if ($this->routeMatch('#^' . $this->request['route'] . '$#')) { |
|
| 102 | + $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/'.trim(trim($this->request['prefix'], '/').'/'.trim($route, '/'), '/')); |
|
| 103 | + if ($this->routeMatch('#^'.$this->request['route'].'$#')) { |
|
| 104 | 104 | $this->setCallback(); |
| 105 | 105 | return $this->generateTarget(); |
| 106 | 106 | } |
@@ -114,17 +114,17 @@ discard block |
||
| 114 | 114 | * @param $route |
| 115 | 115 | * @return bool |
| 116 | 116 | */ |
| 117 | - private function checkSubdomain($route){ |
|
| 118 | - $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])); |
|
| 117 | + private function checkSubdomain($route) { |
|
| 118 | + $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http').'://'.($host = (isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : $_SERVER['SERVER_NAME'])); |
|
| 119 | 119 | $domain = $this->router->collection->getDomain($url); |
| 120 | - if(!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'],'.').'.'.$domain.$route; |
|
| 121 | - if($route[0] == '/'){ |
|
| 120 | + if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.').'.'.$domain.$route; |
|
| 121 | + if ($route[0] == '/') { |
|
| 122 | 122 | return ($host != $domain) ? false : true; |
| 123 | - }elseif($route[0] != '/' && $host != $domain){ |
|
| 123 | + }elseif ($route[0] != '/' && $host != $domain) { |
|
| 124 | 124 | $route = substr($route, 0, strpos($route, "/")); |
| 125 | 125 | $route = str_replace('{host}', $domain, $route); |
| 126 | 126 | $route = preg_replace_callback('#{subdomain}#', [$this, 'subdomainMatch'], $route); |
| 127 | - if (preg_match('#^' . $route . '$#', $host, $this->request['called_subdomain'])) { |
|
| 127 | + if (preg_match('#^'.$route.'$#', $host, $this->request['called_subdomain'])) { |
|
| 128 | 128 | $this->request['called_subdomain'] = array_shift($this->request['called_subdomain']); |
| 129 | 129 | return true; |
| 130 | 130 | } |
@@ -139,7 +139,7 @@ discard block |
||
| 139 | 139 | { |
| 140 | 140 | if (is_array($this->request['params']) && isset($this->request['params']['subdomain'])) { |
| 141 | 141 | $this->request['params']['subdomain'] = str_replace('(', '(?:', $this->request['params']['subdomain']); |
| 142 | - return '(' . $this->request['params']['subdomain'] . ')'; |
|
| 142 | + return '('.$this->request['params']['subdomain'].')'; |
|
| 143 | 143 | } |
| 144 | 144 | return '([^/]+)'; |
| 145 | 145 | } |
@@ -152,7 +152,7 @@ discard block |
||
| 152 | 152 | { |
| 153 | 153 | if (is_array($this->request['params']) && isset($this->request['params']['arguments'][$match[1]])) { |
| 154 | 154 | $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->request['params']['arguments'][$match[1]]); |
| 155 | - return '(' . $this->request['params']['arguments'][$match[1]] . ')'; |
|
| 155 | + return '('.$this->request['params']['arguments'][$match[1]].')'; |
|
| 156 | 156 | } |
| 157 | 157 | return '([^/]+)'; |
| 158 | 158 | } |
@@ -180,9 +180,9 @@ discard block |
||
| 180 | 180 | */ |
| 181 | 181 | private function generateTarget() |
| 182 | 182 | { |
| 183 | - if($this->validMethod()) |
|
| 184 | - foreach($this->resolver as $resolver) |
|
| 185 | - if (is_array($target = call_user_func_array([$this,$resolver],[$this->router->route->getCallback()]))) { |
|
| 183 | + if ($this->validMethod()) |
|
| 184 | + foreach ($this->resolver as $resolver) |
|
| 185 | + if (is_array($target = call_user_func_array([$this, $resolver], [$this->router->route->getCallback()]))) { |
|
| 186 | 186 | $this->setTarget($target); |
| 187 | 187 | $this->router->response->setStatusCode(202); |
| 188 | 188 | return true; |
@@ -194,7 +194,7 @@ discard block |
||
| 194 | 194 | /** |
| 195 | 195 | * @param array $target |
| 196 | 196 | */ |
| 197 | - public function setTarget($target = []){ |
|
| 197 | + public function setTarget($target = []) { |
|
| 198 | 198 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 199 | 199 | $this->router->route->setDetail($this->request); |
| 200 | 200 | $this->router->route->setTarget($target); |
@@ -205,9 +205,9 @@ discard block |
||
| 205 | 205 | /** |
| 206 | 206 | * |
| 207 | 207 | */ |
| 208 | - private function setCallback(){ |
|
| 208 | + private function setCallback() { |
|
| 209 | 209 | if (isset($this->request['params'])) { |
| 210 | - if(is_callable($this->request['params'])) |
|
| 210 | + if (is_callable($this->request['params'])) |
|
| 211 | 211 | $this->router->route->setCallback($this->request['params']); |
| 212 | 212 | else { |
| 213 | 213 | (is_array($this->request['params']) && isset($this->request['params']['use'])) |
@@ -224,7 +224,7 @@ discard block |
||
| 224 | 224 | */ |
| 225 | 225 | public function validMethod() |
| 226 | 226 | { |
| 227 | - if(is_callable($this->request['params']))return true; |
|
| 227 | + if (is_callable($this->request['params']))return true; |
|
| 228 | 228 | if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') |
| 229 | 229 | return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
| 230 | 230 | $method = (isset($this->request['params']['method'])) ? $this->request['params']['method'] : ['GET']; |
@@ -236,10 +236,10 @@ discard block |
||
| 236 | 236 | * @return array|bool |
| 237 | 237 | * @throws \Exception |
| 238 | 238 | */ |
| 239 | - public function isClosureAndTemplate($callback){ |
|
| 240 | - if(is_array($cls = $this->isClosure($callback))) { |
|
| 239 | + public function isClosureAndTemplate($callback) { |
|
| 240 | + if (is_array($cls = $this->isClosure($callback))) { |
|
| 241 | 241 | if (is_array($this->request['params']) && isset($this->request['params']['template']) && is_array($tpl = $this->isTemplate($this->request['params']['template']))) { |
| 242 | - return array_merge(array_merge($cls, $tpl),[ |
|
| 242 | + return array_merge(array_merge($cls, $tpl), [ |
|
| 243 | 243 | 'dispatcher' => $this->dispatcher['isClosureAndTemplate'] |
| 244 | 244 | ]); |
| 245 | 245 | } |
@@ -253,10 +253,10 @@ discard block |
||
| 253 | 253 | * @return array|bool |
| 254 | 254 | * @throws \Exception |
| 255 | 255 | */ |
| 256 | - public function isControllerAndTemplate($callback){ |
|
| 257 | - if(is_array($ctrl = $this->isController($callback))) { |
|
| 256 | + public function isControllerAndTemplate($callback) { |
|
| 257 | + if (is_array($ctrl = $this->isController($callback))) { |
|
| 258 | 258 | if (is_array($this->request['params']) && isset($this->request['params']['template']) && is_array($tpl = $this->isTemplate($this->request['params']['template']))) { |
| 259 | - return array_merge(array_merge($ctrl, $tpl),[ |
|
| 259 | + return array_merge(array_merge($ctrl, $tpl), [ |
|
| 260 | 260 | 'dispatcher' => $this->dispatcher['isControllerAndTemplate'] |
| 261 | 261 | ]); |
| 262 | 262 | } |
@@ -296,7 +296,7 @@ discard block |
||
| 296 | 296 | ? $routes[0] |
| 297 | 297 | : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0]; |
| 298 | 298 | if (!class_exists($class)) |
| 299 | - throw new \Exception('Class "' . $class . '." is not found'); |
|
| 299 | + throw new \Exception('Class "'.$class.'." is not found'); |
|
| 300 | 300 | if (method_exists($class, $routes[1])) { |
| 301 | 301 | return [ |
| 302 | 302 | 'dispatcher' => $this->dispatcher['isController'], |
@@ -305,7 +305,7 @@ discard block |
||
| 305 | 305 | 'action' => $routes[1] |
| 306 | 306 | ]; |
| 307 | 307 | } |
| 308 | - throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"'); |
|
| 308 | + throw new \Exception('The required method "'.$routes[1].'" is not found in "'.$class.'"'); |
|
| 309 | 309 | } |
| 310 | 310 | return false; |
| 311 | 311 | } |
@@ -317,25 +317,25 @@ discard block |
||
| 317 | 317 | */ |
| 318 | 318 | public function isTemplate($callback) |
| 319 | 319 | { |
| 320 | - if(is_string($callback)) { |
|
| 320 | + if (is_string($callback)) { |
|
| 321 | 321 | $path = trim($callback, '/'); |
| 322 | 322 | $extension = substr(strrchr($path, "."), 1); |
| 323 | 323 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 324 | - $viewDir = $this->router->collection->getRoutes('view_dir_' . $index); |
|
| 324 | + $viewDir = $this->router->collection->getRoutes('view_dir_'.$index); |
|
| 325 | 325 | $target = null; |
| 326 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) |
|
| 326 | + if (in_array('.'.$extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir.$path) || is_file($fullPath = $path))) |
|
| 327 | 327 | $target = $fullPath; |
| 328 | 328 | else { |
| 329 | 329 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
| 330 | - if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) { |
|
| 330 | + if (is_file($fullPath = $viewDir.$path.$ext) || is_file($fullPath = $path.$ext)) { |
|
| 331 | 331 | $target = $fullPath; |
| 332 | 332 | $extension = substr(strrchr($ext, "."), 1); |
| 333 | 333 | break; |
| 334 | 334 | } |
| 335 | 335 | } |
| 336 | 336 | } |
| 337 | - if(is_null($target)) |
|
| 338 | - throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
| 337 | + if (is_null($target)) |
|
| 338 | + throw new \Exception('Template file "'.$path.'" is not found in "'.$viewDir.'"'); |
|
| 339 | 339 | return [ |
| 340 | 340 | 'dispatcher' => $this->dispatcher['isTemplate'], |
| 341 | 341 | 'template' => $target, |
@@ -28,30 +28,30 @@ discard block |
||
| 28 | 28 | /** |
| 29 | 29 | * @return array |
| 30 | 30 | */ |
| 31 | - public function getLoadedClass(){ |
|
| 31 | + public function getLoadedClass() { |
|
| 32 | 32 | return $this->loaded_classes; |
| 33 | 33 | } |
| 34 | 34 | |
| 35 | 35 | /** |
| 36 | 36 | * @return array |
| 37 | 37 | */ |
| 38 | - public function getNamespaces(){ |
|
| 38 | + public function getNamespaces() { |
|
| 39 | 39 | return $this->loaded_classes; |
| 40 | 40 | } |
| 41 | 41 | |
| 42 | 42 | /** |
| 43 | 43 | * @return array |
| 44 | 44 | */ |
| 45 | - public function getClassCollection(){ |
|
| 45 | + public function getClassCollection() { |
|
| 46 | 46 | return $this->loaded_classes; |
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | /** |
| 50 | 50 | * @param null $base_path |
| 51 | 51 | */ |
| 52 | - public function register($base_path = null){ |
|
| 53 | - if(!is_null($base_path))$this->base_path = rtrim($base_path,'/'); |
|
| 54 | - spl_autoload_register(array($this,'loadClass')); |
|
| 52 | + public function register($base_path = null) { |
|
| 53 | + if (!is_null($base_path))$this->base_path = rtrim($base_path, '/'); |
|
| 54 | + spl_autoload_register(array($this, 'loadClass')); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | /** |
@@ -65,13 +65,13 @@ discard block |
||
| 65 | 65 | /** |
| 66 | 66 | * @param $class |
| 67 | 67 | */ |
| 68 | - private function loadClass($class){ |
|
| 69 | - if(!isset($this->loaded_classes[$class])) { |
|
| 68 | + private function loadClass($class) { |
|
| 69 | + if (!isset($this->loaded_classes[$class])) { |
|
| 70 | 70 | if (isset($this->class_collection[$class]) && $this->loadFile($this->class_collection[$class], $class)) return; |
| 71 | - elseif($this->findClass($class))return; |
|
| 72 | - else{ |
|
| 71 | + elseif ($this->findClass($class))return; |
|
| 72 | + else { |
|
| 73 | 73 | $file = $this->base_path.'/'.str_replace('\\', '/', $class).'.php'; |
| 74 | - $this->loadFile($file,$class); |
|
| 74 | + $this->loadFile($file, $class); |
|
| 75 | 75 | return; |
| 76 | 76 | } |
| 77 | 77 | } |
@@ -82,8 +82,8 @@ discard block |
||
| 82 | 82 | * @param $class |
| 83 | 83 | * @return bool |
| 84 | 84 | */ |
| 85 | - private function loadFile($file,$class){ |
|
| 86 | - if(is_file($file)){ |
|
| 85 | + private function loadFile($file, $class) { |
|
| 86 | + if (is_file($file)) { |
|
| 87 | 87 | require $file; |
| 88 | 88 | $this->loaded_classes[$class] = $file; |
| 89 | 89 | return true; |
@@ -96,15 +96,15 @@ discard block |
||
| 96 | 96 | * @param $class |
| 97 | 97 | * @return bool |
| 98 | 98 | */ |
| 99 | - private function findClass($class){ |
|
| 99 | + private function findClass($class) { |
|
| 100 | 100 | $prefix = $class; |
| 101 | 101 | while (false !== $pos = strrpos($prefix, '\\')) { |
| 102 | 102 | $prefix = substr($class, 0, $pos + 1); |
| 103 | 103 | $relative_class = substr($class, $pos + 1); |
| 104 | - if(isset($this->namespaces[$prefix])){ |
|
| 105 | - foreach($this->namespaces[$prefix] as $dir) { |
|
| 106 | - $file = $dir . str_replace('\\', '/', $relative_class) . '.php'; |
|
| 107 | - if ($this->loadFile($file,$class)) return true; |
|
| 104 | + if (isset($this->namespaces[$prefix])) { |
|
| 105 | + foreach ($this->namespaces[$prefix] as $dir) { |
|
| 106 | + $file = $dir.str_replace('\\', '/', $relative_class).'.php'; |
|
| 107 | + if ($this->loadFile($file, $class)) return true; |
|
| 108 | 108 | } |
| 109 | 109 | } |
| 110 | 110 | $prefix = rtrim($prefix, '\\'); |
@@ -120,13 +120,13 @@ discard block |
||
| 120 | 120 | */ |
| 121 | 121 | public function addNamespace($prefix, $base_dirs, $prepend = false) |
| 122 | 122 | { |
| 123 | - if(is_array($base_dirs)) |
|
| 124 | - foreach($base_dirs as $dir) |
|
| 123 | + if (is_array($base_dirs)) |
|
| 124 | + foreach ($base_dirs as $dir) |
|
| 125 | 125 | $this->addNamespace($prefix, $dir, $prepend); |
| 126 | - else{ |
|
| 126 | + else { |
|
| 127 | 127 | $prefix = trim($prefix, '\\').'\\'; |
| 128 | 128 | $base_dir = rtrim($base_dirs, '/').'/'; |
| 129 | - if(!isset($this->namespaces[$prefix])) |
|
| 129 | + if (!isset($this->namespaces[$prefix])) |
|
| 130 | 130 | $this->namespaces[$prefix] = []; |
| 131 | 131 | ($prepend) |
| 132 | 132 | ? array_unshift($this->namespaces[$prefix], $base_dir) |
@@ -137,27 +137,27 @@ discard block |
||
| 137 | 137 | /** |
| 138 | 138 | * @param array $prefixes |
| 139 | 139 | */ |
| 140 | - public function setNamespaces($prefixes){ |
|
| 141 | - if(is_string($prefixes))$prefixes = include $prefixes; |
|
| 142 | - if(is_array($prefixes)) |
|
| 143 | - foreach($prefixes as $prefix => $base_dir) |
|
| 144 | - $this->addNamespace($prefix,$base_dir); |
|
| 140 | + public function setNamespaces($prefixes) { |
|
| 141 | + if (is_string($prefixes))$prefixes = include $prefixes; |
|
| 142 | + if (is_array($prefixes)) |
|
| 143 | + foreach ($prefixes as $prefix => $base_dir) |
|
| 144 | + $this->addNamespace($prefix, $base_dir); |
|
| 145 | 145 | } |
| 146 | 146 | |
| 147 | 147 | /** |
| 148 | 148 | * @param $class |
| 149 | 149 | * @param $path |
| 150 | 150 | */ |
| 151 | - public function addClass($class,$path){ |
|
| 151 | + public function addClass($class, $path) { |
|
| 152 | 152 | $this->class_collection[$class] = $path; |
| 153 | 153 | } |
| 154 | 154 | |
| 155 | 155 | /** |
| 156 | 156 | * @param array $collection |
| 157 | 157 | */ |
| 158 | - public function setClassCollection($collection){ |
|
| 159 | - if(is_string($collection))$collection = include $collection; |
|
| 160 | - if(is_array($collection)) |
|
| 161 | - $this->class_collection = array_merge($this->class_collection,$collection); |
|
| 158 | + public function setClassCollection($collection) { |
|
| 159 | + if (is_string($collection))$collection = include $collection; |
|
| 160 | + if (is_array($collection)) |
|
| 161 | + $this->class_collection = array_merge($this->class_collection, $collection); |
|
| 162 | 162 | } |
| 163 | 163 | } |
@@ -4,28 +4,28 @@ |
||
| 4 | 4 | |
| 5 | 5 | require __DIR__.'/Autoload.php'; |
| 6 | 6 | |
| 7 | -$autoload = new Autoload(); |
|
| 8 | -$autoload->addNamespace('JetFire\Routing',[ |
|
| 7 | +$autoload = new Autoload(); |
|
| 8 | +$autoload->addNamespace('JetFire\Routing', [ |
|
| 9 | 9 | __DIR__.'/src/Routing', |
| 10 | 10 | __DIR__.'/', |
| 11 | 11 | ]); |
| 12 | -$autoload->addClass('Normal1Controller',__DIR__.'/Block1/Normal1Controller.php'); |
|
| 12 | +$autoload->addClass('Normal1Controller', __DIR__.'/Block1/Normal1Controller.php'); |
|
| 13 | 13 | $autoload->register(); |
| 14 | 14 | |
| 15 | 15 | |
| 16 | 16 | // Create RouteCollection instance |
| 17 | 17 | $collection = new RouteCollection(); |
| 18 | -$collection->addRoutes(__DIR__.'/Block1/routes.php',['view_dir'=>__DIR__.'/Block1/Views','prefix'=>'block']); |
|
| 18 | +$collection->addRoutes(__DIR__.'/Block1/routes.php', ['view_dir'=>__DIR__.'/Block1/Views', 'prefix'=>'block']); |
|
| 19 | 19 | $router = new \JetFire\Routing\Router($collection); |
| 20 | 20 | $matcher = new \JetFire\Routing\Matcher\ArrayMatcher($router); |
| 21 | 21 | |
| 22 | 22 | $router->addMatcher($matcher); |
| 23 | 23 | $router->setResponses([ |
| 24 | 24 | // you can use a closure to handle error |
| 25 | - '404' => function(){ |
|
| 25 | + '404' => function() { |
|
| 26 | 26 | return '404'; |
| 27 | 27 | }, |
| 28 | - '405' => function(){ |
|
| 28 | + '405' => function() { |
|
| 29 | 29 | return '405'; |
| 30 | 30 | } |
| 31 | 31 | ]); |
@@ -25,9 +25,9 @@ |
||
| 25 | 25 | ], |
| 26 | 26 | |
| 27 | 27 | '/search1-:id-:name' => [ |
| 28 | - 'use' => function($id,$name){ |
|
| 28 | + 'use' => function($id, $name) { |
|
| 29 | 29 | return 'Search'.$id.$name; |
| 30 | 30 | }, |
| 31 | - 'arguments' => ['id' => '[0-9]+','name' => '[a-z]*'], |
|
| 31 | + 'arguments' => ['id' => '[0-9]+', 'name' => '[a-z]*'], |
|
| 32 | 32 | ], |
| 33 | 33 | ]; |
| 34 | 34 | \ No newline at end of file |
@@ -2,11 +2,11 @@ |
||
| 2 | 2 | |
| 3 | 3 | class Normal1Controller { |
| 4 | 4 | |
| 5 | - public function contact(){ |
|
| 5 | + public function contact() { |
|
| 6 | 6 | echo 'Contact1'; |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | - public function user($name = null){ |
|
| 9 | + public function user($name = null) { |
|
| 10 | 10 | echo 'User : '.$name; |
| 11 | 11 | } |
| 12 | 12 | |
@@ -3,16 +3,16 @@ |
||
| 3 | 3 | |
| 4 | 4 | class Namespace1Controller { |
| 5 | 5 | |
| 6 | - public function index(){ |
|
| 6 | + public function index() { |
|
| 7 | 7 | return ['message' => 'index']; |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | - public function index2($id){ |
|
| 11 | - if($id == 2){ |
|
| 10 | + public function index2($id) { |
|
| 11 | + if ($id == 2) { |
|
| 12 | 12 | header('Location: http://localhost/JetFire/Routing/block/search1-1-tse'); |
| 13 | 13 | return true; |
| 14 | - }else { |
|
| 15 | - return 'Index-' . $id; |
|
| 14 | + } else { |
|
| 15 | + return 'Index-'.$id; |
|
| 16 | 16 | } |
| 17 | 17 | } |
| 18 | 18 | |