@@ -166,8 +166,9 @@ |
||
166 | 166 | */ |
167 | 167 | public function sendHeaders() |
168 | 168 | { |
169 | - foreach($this->headers as $key => $content) |
|
170 | - header($key.' : '.$content); |
|
169 | + foreach($this->headers as $key => $content) { |
|
170 | + header($key.' : '.$content); |
|
171 | + } |
|
171 | 172 | http_response_code($this->getStatusCode()); |
172 | 173 | return $this; |
173 | 174 | } |
@@ -52,10 +52,18 @@ discard block |
||
52 | 52 | */ |
53 | 53 | public function set($args = []) |
54 | 54 | { |
55 | - if (isset($args['name'])) $this->name = $args['name']; |
|
56 | - if (isset($args['callback'])) $this->callback = $args['callback']; |
|
57 | - if (isset($args['target'])) $this->target = $args['target']; |
|
58 | - if (isset($args['detail'])) $this->detail = $args['detail']; |
|
55 | + if (isset($args['name'])) { |
|
56 | + $this->name = $args['name']; |
|
57 | + } |
|
58 | + if (isset($args['callback'])) { |
|
59 | + $this->callback = $args['callback']; |
|
60 | + } |
|
61 | + if (isset($args['target'])) { |
|
62 | + $this->target = $args['target']; |
|
63 | + } |
|
64 | + if (isset($args['detail'])) { |
|
65 | + $this->detail = $args['detail']; |
|
66 | + } |
|
59 | 67 | } |
60 | 68 | |
61 | 69 | /** |
@@ -145,8 +153,9 @@ discard block |
||
145 | 153 | */ |
146 | 154 | public function getTarget($key = null) |
147 | 155 | { |
148 | - if (!is_null($key)) |
|
149 | - return isset($this->target[$key]) ? $this->target[$key] : ''; |
|
156 | + if (!is_null($key)) { |
|
157 | + return isset($this->target[$key]) ? $this->target[$key] : ''; |
|
158 | + } |
|
150 | 159 | return empty($this->target) ? '' : $this->target; |
151 | 160 | } |
152 | 161 | |
@@ -165,8 +174,9 @@ discard block |
||
165 | 174 | */ |
166 | 175 | public function hasTarget($key = null) |
167 | 176 | { |
168 | - if (!is_null($key)) |
|
169 | - return isset($this->target[$key]) ? true : false; |
|
177 | + if (!is_null($key)) { |
|
178 | + return isset($this->target[$key]) ? true : false; |
|
179 | + } |
|
170 | 180 | return empty($this->target) ? false : true; |
171 | 181 | } |
172 | 182 |
@@ -90,8 +90,9 @@ |
||
90 | 90 | foreach($this->resolver as $resolver){ |
91 | 91 | if(is_array($target = call_user_func([$this,$resolver]))) { |
92 | 92 | $this->setTarget($target); |
93 | - if($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) |
|
94 | - return null; |
|
93 | + if($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) { |
|
94 | + return null; |
|
95 | + } |
|
95 | 96 | $this->router->response->setStatusCode(202); |
96 | 97 | return true; |
97 | 98 | } |
@@ -29,9 +29,10 @@ discard block |
||
29 | 29 | */ |
30 | 30 | public function globalMiddleware() |
31 | 31 | { |
32 | - if (isset($this->router->collection->middleware['global_middleware'])) |
|
33 | - foreach ($this->router->collection->middleware['global_middleware'] as $class) |
|
34 | - if (class_exists($class)) return $this->callHandler($class); |
|
32 | + if (isset($this->router->collection->middleware['global_middleware'])) { |
|
33 | + foreach ($this->router->collection->middleware['global_middleware'] as $class) |
|
34 | + if (class_exists($class)) return $this->callHandler($class); |
|
35 | + } |
|
35 | 36 | return null; |
36 | 37 | } |
37 | 38 | |
@@ -40,16 +41,18 @@ discard block |
||
40 | 41 | */ |
41 | 42 | public function blockMiddleware() |
42 | 43 | { |
43 | - if (isset($this->router->collection->middleware['block_middleware'])) |
|
44 | - if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
45 | - $blocks = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]; |
|
44 | + if (isset($this->router->collection->middleware['block_middleware'])) { |
|
45 | + if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
46 | + $blocks = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]; |
|
47 | + } |
|
46 | 48 | if (is_array($blocks)) { |
47 | - foreach ($blocks as $block) |
|
48 | - if (class_exists($block)) |
|
49 | - if($this->callHandler($block) === false) return false; |
|
50 | - } |
|
51 | - elseif (is_string($blocks) && class_exists($blocks)) |
|
52 | - return $this->callHandler($blocks); |
|
49 | + foreach ($blocks as $block) { |
|
50 | + if (class_exists($block)) |
|
51 | + if($this->callHandler($block) === false) return false; |
|
52 | + } |
|
53 | + } elseif (is_string($blocks) && class_exists($blocks)) { |
|
54 | + return $this->callHandler($blocks); |
|
55 | + } |
|
53 | 56 | } |
54 | 57 | return null; |
55 | 58 | } |
@@ -64,10 +67,12 @@ discard block |
||
64 | 67 | if (isset($this->router->collection->middleware['class_middleware'][$ctrl]) && class_exists($this->router->route->getTarget('controller'))) { |
65 | 68 | $classes = $this->router->collection->middleware['class_middleware'][$ctrl]; |
66 | 69 | if(is_array($classes)){ |
67 | - foreach ($classes as $class) |
|
68 | - if($this->callHandler($class) === false)return false; |
|
69 | - }elseif(is_string($classes)) |
|
70 | - return $this->callHandler($classes); |
|
70 | + foreach ($classes as $class) { |
|
71 | + if($this->callHandler($class) === false)return false; |
|
72 | + } |
|
73 | + } elseif(is_string($classes)) { |
|
74 | + return $this->callHandler($classes); |
|
75 | + } |
|
71 | 76 | } |
72 | 77 | } |
73 | 78 | return null; |
@@ -78,14 +83,17 @@ discard block |
||
78 | 83 | */ |
79 | 84 | public function routeMiddleware() |
80 | 85 | { |
81 | - if (isset($this->router->collection->middleware['route_middleware'])) |
|
82 | - if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
83 | - $classes = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
86 | + if (isset($this->router->collection->middleware['route_middleware'])) { |
|
87 | + if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
88 | + $classes = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
89 | + } |
|
84 | 90 | if(is_array($classes)){ |
85 | - foreach ($classes as $class) |
|
86 | - if($this->callHandler($class) === false)return false; |
|
87 | - }elseif(is_string($classes)) |
|
88 | - return $this->callHandler($classes); |
|
91 | + foreach ($classes as $class) { |
|
92 | + if($this->callHandler($class) === false)return false; |
|
93 | + } |
|
94 | + } elseif(is_string($classes)) { |
|
95 | + return $this->callHandler($classes); |
|
96 | + } |
|
89 | 97 | } |
90 | 98 | return null; |
91 | 99 | } |
@@ -99,9 +107,10 @@ discard block |
||
99 | 107 | if (method_exists($instance, 'handle')) { |
100 | 108 | $reflectionMethod = new ReflectionMethod($instance, 'handle'); |
101 | 109 | $dependencies = []; |
102 | - foreach ($reflectionMethod->getParameters() as $arg) |
|
103 | - if (!is_null($arg->getClass())) |
|
104 | - $dependencies[] = $this->getClass($arg->getClass()->name); |
|
110 | + foreach ($reflectionMethod->getParameters() as $arg) { |
|
111 | + if (!is_null($arg->getClass())) |
|
112 | + $dependencies[] = $this->getClass($arg->getClass()->name); |
|
113 | + } |
|
105 | 114 | $dependencies = array_merge($dependencies, [$this->router->route]); |
106 | 115 | return $reflectionMethod->invokeArgs($instance, $dependencies); |
107 | 116 | } |
@@ -47,14 +47,19 @@ |
||
47 | 47 | */ |
48 | 48 | public function call() |
49 | 49 | { |
50 | - if ($this->response->getStatusCode() == 202) |
|
51 | - $this->setContentType($this->route->getTarget('extension')); |
|
52 | - if (isset($this->route->getTarget('callback')[$this->route->getTarget('extension')])) |
|
53 | - $this->response->setContent(call_user_func_array($this->route->getTarget('callback')[$this->route->getTarget('extension')], [$this->route])); |
|
54 | - else { |
|
50 | + if ($this->response->getStatusCode() == 202) { |
|
51 | + $this->setContentType($this->route->getTarget('extension')); |
|
52 | + } |
|
53 | + if (isset($this->route->getTarget('callback')[$this->route->getTarget('extension')])) { |
|
54 | + $this->response->setContent(call_user_func_array($this->route->getTarget('callback')[$this->route->getTarget('extension')], [$this->route])); |
|
55 | + } else { |
|
55 | 56 | ob_start(); |
56 | - if (isset($this->route->getTarget()['data'])) extract($this->route->getTarget('data')); |
|
57 | - if (isset($this->route->getParams()['data'])) extract($this->route->getParams()['data']); |
|
57 | + if (isset($this->route->getTarget()['data'])) { |
|
58 | + extract($this->route->getTarget('data')); |
|
59 | + } |
|
60 | + if (isset($this->route->getParams()['data'])) { |
|
61 | + extract($this->route->getParams()['data']); |
|
62 | + } |
|
58 | 63 | require($this->route->getTarget('template')); |
59 | 64 | $this->response->setContent(ob_get_clean()); |
60 | 65 | } |
@@ -43,8 +43,11 @@ |
||
43 | 43 | $this->response->setHeaders(['Content-Type' => 'text/html']); |
44 | 44 | } |
45 | 45 | $params = ($this->route->getParameters() == '') ? [] : $this->route->getParameters(); |
46 | - if (is_array($content = call_user_func_array($this->route->getTarget('closure'), $params))) $this->route->addTarget('data', $content); |
|
47 | - elseif (!is_null($content)) $this->response->setContent($content); |
|
46 | + if (is_array($content = call_user_func_array($this->route->getTarget('closure'), $params))) { |
|
47 | + $this->route->addTarget('data', $content); |
|
48 | + } elseif (!is_null($content)) { |
|
49 | + $this->response->setContent($content); |
|
50 | + } |
|
48 | 51 | } |
49 | 52 | |
50 | 53 | } |
@@ -37,7 +37,9 @@ discard block |
||
37 | 37 | */ |
38 | 38 | public function __construct($routes = null, $options = []) |
39 | 39 | { |
40 | - if (!is_null($routes) || !empty($options)) $this->addRoutes($routes, $options); |
|
40 | + if (!is_null($routes) || !empty($options)) { |
|
41 | + $this->addRoutes($routes, $options); |
|
42 | + } |
|
41 | 43 | } |
42 | 44 | |
43 | 45 | /** |
@@ -47,10 +49,16 @@ discard block |
||
47 | 49 | public function addRoutes($routes = null, $options = []) |
48 | 50 | { |
49 | 51 | if (!is_null($routes) && !is_array($routes)) { |
50 | - if (strpos($routes, '.php') === false) $routes = trim($routes, '/') . '/'; |
|
51 | - if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) $routes = $routesFile; |
|
52 | - elseif (is_file($routes) && is_array($routesFile = include $routes)) $routes = $routesFile; |
|
53 | - else throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given'); |
|
52 | + if (strpos($routes, '.php') === false) { |
|
53 | + $routes = trim($routes, '/') . '/'; |
|
54 | + } |
|
55 | + if (is_file($routes . '/routes.php') && is_array($routesFile = include $routes . '/routes.php')) { |
|
56 | + $routes = $routesFile; |
|
57 | + } elseif (is_file($routes) && is_array($routesFile = include $routes)) { |
|
58 | + $routes = $routesFile; |
|
59 | + } else { |
|
60 | + throw new \InvalidArgumentException('Argument for "' . get_called_class() . '" constructor is not recognized. Expected argument array or file containing array but "' . $routes . '" given'); |
|
61 | + } |
|
54 | 62 | } |
55 | 63 | $this->routes['routes_' . $this->countRoutes] = is_array($routes) ? $routes : []; |
56 | 64 | $this->setRoutes($options, $this->countRoutes); |
@@ -63,8 +71,9 @@ discard block |
||
63 | 71 | */ |
64 | 72 | public function getRoutes($key = null) |
65 | 73 | { |
66 | - if (!is_null($key)) |
|
67 | - return isset($this->routes[$key]) ? $this->routes[$key] : ''; |
|
74 | + if (!is_null($key)) { |
|
75 | + return isset($this->routes[$key]) ? $this->routes[$key] : ''; |
|
76 | + } |
|
68 | 77 | return $this->routes; |
69 | 78 | } |
70 | 79 | |
@@ -75,12 +84,18 @@ discard block |
||
75 | 84 | { |
76 | 85 | if (is_array($args)) { |
77 | 86 | $nbrArgs = count($args); |
78 | - for ($i = 0; $i < $nbrArgs; ++$i) |
|
79 | - $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
80 | - } elseif (is_string($args)) |
|
81 | - for ($i = 0; $i < $this->countRoutes; ++$i) |
|
82 | - $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
83 | - if ($this->countRoutes == 0) $this->countRoutes++; |
|
87 | + for ($i = 0; $i < $nbrArgs; ++$i) { |
|
88 | + $this->routes['prefix_' . $i] = '/' . trim($args[$i], '/'); |
|
89 | + } |
|
90 | + } elseif (is_string($args)) { |
|
91 | + for ($i = 0; |
|
92 | + } |
|
93 | + $i < $this->countRoutes; ++$i) { |
|
94 | + $this->routes['prefix_' . $i] = '/' . trim($args, '/'); |
|
95 | + } |
|
96 | + if ($this->countRoutes == 0) { |
|
97 | + $this->countRoutes++; |
|
98 | + } |
|
84 | 99 | } |
85 | 100 | |
86 | 101 | /** |
@@ -92,10 +107,14 @@ discard block |
||
92 | 107 | for ($i = 0; $i < $nbrArgs; ++$i) { |
93 | 108 | if (is_array($args[$i])) { |
94 | 109 | $this->setRoutes($args[$i], $i); |
95 | - if (!isset($this->routes['routes_' . $i])) $this->routes['routes_' . $i] = []; |
|
110 | + if (!isset($this->routes['routes_' . $i])) { |
|
111 | + $this->routes['routes_' . $i] = []; |
|
112 | + } |
|
96 | 113 | } |
97 | 114 | } |
98 | - if ($this->countRoutes == 0) $this->countRoutes++; |
|
115 | + if ($this->countRoutes == 0) { |
|
116 | + $this->countRoutes++; |
|
117 | + } |
|
99 | 118 | } |
100 | 119 | |
101 | 120 | /** |
@@ -117,12 +136,16 @@ discard block |
||
117 | 136 | */ |
118 | 137 | public function setMiddleware($middleware) |
119 | 138 | { |
120 | - if (is_string($middleware)) $middleware = rtrim($middleware, '/'); |
|
121 | - if (is_array($middleware)) |
|
122 | - $this->middleware = $middleware; |
|
123 | - elseif (is_file($middleware) && is_array($mid = include $middleware)) |
|
124 | - $this->middleware = $mid; |
|
125 | - else throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file'); |
|
139 | + if (is_string($middleware)) { |
|
140 | + $middleware = rtrim($middleware, '/'); |
|
141 | + } |
|
142 | + if (is_array($middleware)) { |
|
143 | + $this->middleware = $middleware; |
|
144 | + } elseif (is_file($middleware) && is_array($mid = include $middleware)) { |
|
145 | + $this->middleware = $mid; |
|
146 | + } else { |
|
147 | + throw new \InvalidArgumentException('Accepted argument for setMiddleware are array and array file'); |
|
148 | + } |
|
126 | 149 | } |
127 | 150 | |
128 | 151 | /** |
@@ -138,21 +161,23 @@ discard block |
||
138 | 161 | ? $protocol . '://' . $domain . ((!empty($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] != 80) ? ':' . $_SERVER['SERVER_PORT'] : '') . str_replace('/' . $script_file, '', $_SERVER['SCRIPT_NAME']) |
139 | 162 | : $root; |
140 | 163 | $new_domain = $this->getDomain($root); |
141 | - if (!is_null($domain) && strpos($domain, $new_domain) !== false) |
|
142 | - $root = str_replace($domain, $new_domain, $root); |
|
164 | + if (!is_null($domain) && strpos($domain, $new_domain) !== false) { |
|
165 | + $root = str_replace($domain, $new_domain, $root); |
|
166 | + } |
|
143 | 167 | $count = 0; |
144 | 168 | for ($i = 0; $i < $this->countRoutes; ++$i) { |
145 | 169 | $prefix = (isset($this->routes['prefix_' . $i])) ? $this->routes['prefix_' . $i] : ''; |
146 | 170 | $subdomain = (isset($this->routes['subdomain_' . $i])) ? $this->routes['subdomain_' . $i] : ''; |
147 | 171 | $url = (!empty($subdomain)) ? str_replace($protocol.'://',$protocol.'://'.$subdomain.'.' ,$root) : $root; |
148 | - if (isset($this->routes['routes_' . $i])) |
|
149 | - foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
172 | + if (isset($this->routes['routes_' . $i])) { |
|
173 | + foreach ($this->routes['routes_' . $i] as $route => $dependencies) { |
|
150 | 174 | if (is_array($dependencies) && isset($dependencies['use'])) |
151 | - $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
152 | - elseif (!is_array($dependencies)) |
|
153 | - $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
154 | - else |
|
155 | - $use = $route; |
|
175 | + $use = (is_callable($dependencies['use'])) ? 'closure-' . $count : trim($dependencies['use'], '/'); |
|
176 | + } elseif (!is_array($dependencies)) { |
|
177 | + $use = (is_callable($dependencies)) ? 'closure-' . $count : trim($dependencies, '/'); |
|
178 | + } else { |
|
179 | + $use = $route; |
|
180 | + } |
|
156 | 181 | if (isset($route[0]) && $route[0] == '/') { |
157 | 182 | (!is_callable($dependencies) && isset($dependencies['name'])) |
158 | 183 | ? $this->routesByName[$use . '#' . $dependencies['name']] = $url . $prefix . $route |
@@ -193,9 +218,14 @@ discard block |
||
193 | 218 | foreach ($this->routesByName as $key => $route) { |
194 | 219 | $param = explode('#', $key); |
195 | 220 | $route = str_replace('{subdomain}', $subdomain, $route); |
196 | - foreach ($params as $key2 => $value) $route = str_replace(':' . $key2, $value, $route); |
|
197 | - if ($param[0] == trim($name, '/')) return $route; |
|
198 | - else if (isset($param[1]) && $param[1] == $name) return $route; |
|
221 | + foreach ($params as $key2 => $value) { |
|
222 | + $route = str_replace(':' . $key2, $value, $route); |
|
223 | + } |
|
224 | + if ($param[0] == trim($name, '/')) { |
|
225 | + return $route; |
|
226 | + } else if (isset($param[1]) && $param[1] == $name) { |
|
227 | + return $route; |
|
228 | + } |
|
199 | 229 | } |
200 | 230 | return null; |
201 | 231 | } |
@@ -105,7 +105,9 @@ discard block |
||
105 | 105 | $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/')); |
106 | 106 | if ($this->routeMatch('#^' . $this->request['route'] . '$#')) { |
107 | 107 | $this->setCallback(); |
108 | - if (!is_null($response = $this->generateTarget())) return $response; |
|
108 | + if (!is_null($response = $this->generateTarget())) { |
|
109 | + return $response; |
|
110 | + } |
|
109 | 111 | } |
110 | 112 | } |
111 | 113 | } |
@@ -122,7 +124,9 @@ discard block |
||
122 | 124 | $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'])); |
123 | 125 | $host = explode(':', $host)[0]; |
124 | 126 | $domain = $this->router->collection->getDomain($url); |
125 | - if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
|
127 | + if (!empty($this->request['subdomain']) && $route[0] == '/') { |
|
128 | + $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
|
129 | + } |
|
126 | 130 | if ($route[0] == '/') { |
127 | 131 | return ($host != $domain) ? false : true; |
128 | 132 | } elseif ($route[0] != '/' && $host != $domain) { |
@@ -170,8 +174,9 @@ discard block |
||
170 | 174 | { |
171 | 175 | if (substr($this->request['route'], -1) == '*') { |
172 | 176 | $pos = strpos($this->request['route'], '*'); |
173 | - if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) |
|
174 | - return true; |
|
177 | + if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) { |
|
178 | + return true; |
|
179 | + } |
|
175 | 180 | } |
176 | 181 | if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) { |
177 | 182 | array_shift($this->request['parameters']); |
@@ -189,8 +194,9 @@ discard block |
||
189 | 194 | foreach ($this->resolver as $resolver) { |
190 | 195 | if (is_array($target = call_user_func_array([$this, $resolver], [$this->router->route->getCallback()]))) { |
191 | 196 | $this->setTarget($target); |
192 | - if ($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) |
|
193 | - return null; |
|
197 | + if ($this->router->middleware->globalMiddleware() === false || $this->router->middleware->blockMiddleware() === false || $this->router->middleware->classMiddleware() === false || $this->router->middleware->routeMiddleware() === false) { |
|
198 | + return null; |
|
199 | + } |
|
194 | 200 | $this->router->response->setStatusCode(202); |
195 | 201 | return true; |
196 | 202 | } |
@@ -233,14 +239,18 @@ discard block |
||
233 | 239 | private function setCallback() |
234 | 240 | { |
235 | 241 | if (isset($this->request['params'])) { |
236 | - if (is_callable($this->request['params'])) |
|
237 | - $this->router->route->setCallback($this->request['params']); |
|
238 | - else { |
|
242 | + if (is_callable($this->request['params'])) { |
|
243 | + $this->router->route->setCallback($this->request['params']); |
|
244 | + } else { |
|
239 | 245 | (is_array($this->request['params']) && isset($this->request['params']['use'])) |
240 | 246 | ? $this->router->route->setCallback($this->request['params']['use']) |
241 | 247 | : $this->router->route->setCallback($this->request['params']); |
242 | - if (isset($this->request['params']['name'])) $this->router->route->setName($this->request['params']['name']); |
|
243 | - if (isset($this->request['params']['method'])) $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
248 | + if (isset($this->request['params']['name'])) { |
|
249 | + $this->router->route->setName($this->request['params']['name']); |
|
250 | + } |
|
251 | + if (isset($this->request['params']['method'])) { |
|
252 | + $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
253 | + } |
|
244 | 254 | } |
245 | 255 | } |
246 | 256 | } |
@@ -250,9 +260,12 @@ discard block |
||
250 | 260 | */ |
251 | 261 | public function validMethod() |
252 | 262 | { |
253 | - if (is_callable($this->request['params'])) return true; |
|
254 | - if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') |
|
255 | - return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
263 | + if (is_callable($this->request['params'])) { |
|
264 | + return true; |
|
265 | + } |
|
266 | + if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { |
|
267 | + return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
268 | + } |
|
256 | 269 | $method = (isset($this->request['params']['method'])) ? $this->request['params']['method'] : ['GET']; |
257 | 270 | return (in_array($this->router->route->getMethod(), $method)) ? true : false; |
258 | 271 | } |
@@ -318,20 +331,25 @@ discard block |
||
318 | 331 | { |
319 | 332 | if (is_string($callback) && strpos($callback, '@') !== false) { |
320 | 333 | $routes = explode('@', $callback); |
321 | - if (!isset($routes[1])) $routes[1] = 'index'; |
|
334 | + if (!isset($routes[1])) { |
|
335 | + $routes[1] = 'index'; |
|
336 | + } |
|
322 | 337 | if ($routes[1] == '{method}') { |
323 | 338 | $this->request['parameters'] = explode('/', str_replace(str_replace('*', '', $this->request['route']), '', $this->router->route->getUrl())); |
324 | 339 | $routes[1] = $this->request['parameters'][0]; |
325 | 340 | array_shift($this->request['parameters']); |
326 | - if (preg_match('/[A-Z]/', $routes[1])) return false; |
|
341 | + if (preg_match('/[A-Z]/', $routes[1])) { |
|
342 | + return false; |
|
343 | + } |
|
327 | 344 | $routes[1] = lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $routes[1])))); |
328 | 345 | } |
329 | 346 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
330 | 347 | $class = (class_exists($routes[0])) |
331 | 348 | ? $routes[0] |
332 | 349 | : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0]; |
333 | - if (!class_exists($class)) |
|
334 | - throw new \Exception('Class "' . $class . '." is not found'); |
|
350 | + if (!class_exists($class)) { |
|
351 | + throw new \Exception('Class "' . $class . '." is not found'); |
|
352 | + } |
|
335 | 353 | if (method_exists($class, $routes[1])) { |
336 | 354 | return [ |
337 | 355 | 'dispatcher' => $this->dispatcher['isController'], |
@@ -340,8 +358,9 @@ discard block |
||
340 | 358 | 'action' => $routes[1] |
341 | 359 | ]; |
342 | 360 | } |
343 | - if (!strpos($callback, '{method}') !== false) |
|
344 | - throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"'); |
|
361 | + if (!strpos($callback, '{method}') !== false) { |
|
362 | + throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"'); |
|
363 | + } |
|
345 | 364 | } |
346 | 365 | return false; |
347 | 366 | } |
@@ -359,9 +378,9 @@ discard block |
||
359 | 378 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
360 | 379 | $viewDir = $this->router->collection->getRoutes('view_dir_' . $index); |
361 | 380 | $target = null; |
362 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) |
|
363 | - $target = $fullPath; |
|
364 | - else { |
|
381 | + if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) { |
|
382 | + $target = $fullPath; |
|
383 | + } else { |
|
365 | 384 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
366 | 385 | if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) { |
367 | 386 | $target = $fullPath; |
@@ -370,8 +389,9 @@ discard block |
||
370 | 389 | } |
371 | 390 | } |
372 | 391 | } |
373 | - if (is_null($target)) |
|
374 | - throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
392 | + if (is_null($target)) { |
|
393 | + throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
394 | + } |
|
375 | 395 | return [ |
376 | 396 | 'dispatcher' => $this->dispatcher['isTemplate'], |
377 | 397 | 'template' => $target, |
@@ -85,8 +85,9 @@ discard block |
||
85 | 85 | */ |
86 | 86 | public function setMatcher($matcher) |
87 | 87 | { |
88 | - if (is_object($matcher)) |
|
89 | - $matcher = [$matcher]; |
|
88 | + if (is_object($matcher)) { |
|
89 | + $matcher = [$matcher]; |
|
90 | + } |
|
90 | 91 | $this->matcher = $matcher; |
91 | 92 | } |
92 | 93 | |
@@ -104,8 +105,12 @@ discard block |
||
104 | 105 | public function run() |
105 | 106 | { |
106 | 107 | $this->setUrl(); |
107 | - if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath(); |
|
108 | - if ($this->match()) $this->callTarget(); |
|
108 | + if ($this->config['generateRoutesPath']) { |
|
109 | + $this->collection->generateRoutesPath(); |
|
110 | + } |
|
111 | + if ($this->match()) { |
|
112 | + $this->callTarget(); |
|
113 | + } |
|
109 | 114 | $this->callResponse(); |
110 | 115 | } |
111 | 116 | |
@@ -114,8 +119,9 @@ discard block |
||
114 | 119 | */ |
115 | 120 | public function setUrl($url = null) |
116 | 121 | { |
117 | - if (is_null($url)) |
|
118 | - $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
|
122 | + if (is_null($url)) { |
|
123 | + $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
|
124 | + } |
|
119 | 125 | $this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
120 | 126 | } |
121 | 127 | |
@@ -125,7 +131,9 @@ discard block |
||
125 | 131 | public function match() |
126 | 132 | { |
127 | 133 | foreach ($this->matcher as $key => $matcher) { |
128 | - if (call_user_func([$this->matcher[$key], 'match'])) return true; |
|
134 | + if (call_user_func([$this->matcher[$key], 'match'])) { |
|
135 | + return true; |
|
136 | + } |
|
129 | 137 | } |
130 | 138 | return false; |
131 | 139 | } |
@@ -160,11 +168,16 @@ discard block |
||
160 | 168 | if (isset($this->route->getDetail()['response_templates']) && isset($this->route->getDetail()['response_templates'][$code = $this->response->getStatusCode()])) { |
161 | 169 | $this->route->setCallback($this->route->getDetail()['response_templates'][$code]); |
162 | 170 | $matcher = null; |
163 | - foreach ($this->matcher as $instance) if ($instance instanceof ArrayMatcher) $matcher = $instance; |
|
164 | - if (is_null($matcher)) $matcher = new ArrayMatcher($this); |
|
165 | - foreach (call_user_func([$matcher, 'getResolver']) as $match) |
|
166 | - if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))) { |
|
167 | - call_user_func_array([$matcher, 'setTarget'], [$target]); |
|
171 | + foreach ($this->matcher as $instance) { |
|
172 | + if ($instance instanceof ArrayMatcher) $matcher = $instance; |
|
173 | + } |
|
174 | + if (is_null($matcher)) { |
|
175 | + $matcher = new ArrayMatcher($this); |
|
176 | + } |
|
177 | + foreach (call_user_func([$matcher, 'getResolver']) as $match) { |
|
178 | + if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))) { |
|
179 | + call_user_func_array([$matcher, 'setTarget'], [$target]); |
|
180 | + } |
|
168 | 181 | $this->callTarget(); |
169 | 182 | break; |
170 | 183 | } |
@@ -198,9 +211,9 @@ discard block |
||
198 | 211 | $reflectionMethod = new ReflectionMethod($controller, $method); |
199 | 212 | $dependencies = []; |
200 | 213 | foreach ($reflectionMethod->getParameters() as $arg) { |
201 | - if (isset($methodArgs[$arg->name])) |
|
202 | - array_push($dependencies, $methodArgs[$arg->name]); |
|
203 | - else if (!is_null($arg->getClass())) { |
|
214 | + if (isset($methodArgs[$arg->name])) { |
|
215 | + array_push($dependencies, $methodArgs[$arg->name]); |
|
216 | + } else if (!is_null($arg->getClass())) { |
|
204 | 217 | array_push($dependencies, call_user_func_array($this->getConfig()['di'], [$arg->getClass()->name])); |
205 | 218 | } |
206 | 219 | } |
@@ -220,18 +233,20 @@ discard block |
||
220 | 233 | public function callClass($controller, $ctrlArgs = [], $classInstance = []) |
221 | 234 | { |
222 | 235 | $reflector = new ReflectionClass($controller); |
223 | - if (!$reflector->isInstantiable()) |
|
224 | - throw new \Exception('Controller [' . $controller . '] is not instantiable.'); |
|
236 | + if (!$reflector->isInstantiable()) { |
|
237 | + throw new \Exception('Controller [' . $controller . '] is not instantiable.'); |
|
238 | + } |
|
225 | 239 | $constructor = $reflector->getConstructor(); |
226 | - if (is_null($constructor)) |
|
227 | - return call_user_func_array($this->getConfig()['di'], [$controller]); |
|
240 | + if (is_null($constructor)) { |
|
241 | + return call_user_func_array($this->getConfig()['di'], [$controller]); |
|
242 | + } |
|
228 | 243 | $dependencies = []; |
229 | 244 | foreach ($constructor->getParameters() as $arg) { |
230 | - if (isset($ctrlArgs[$arg->name])) |
|
231 | - array_push($dependencies, $ctrlArgs[$arg->name]); |
|
232 | - else if (isset($classInstance[$arg->getClass()->name])) |
|
233 | - array_push($dependencies, $classInstance[$arg->getClass()->name]); |
|
234 | - else if (!is_null($arg->getClass())) { |
|
245 | + if (isset($ctrlArgs[$arg->name])) { |
|
246 | + array_push($dependencies, $ctrlArgs[$arg->name]); |
|
247 | + } else if (isset($classInstance[$arg->getClass()->name])) { |
|
248 | + array_push($dependencies, $classInstance[$arg->getClass()->name]); |
|
249 | + } else if (!is_null($arg->getClass())) { |
|
235 | 250 | array_push($dependencies, call_user_func_array($this->getConfig()['di'], [$arg->getClass()->name])); |
236 | 251 | } |
237 | 252 | } |