@@ -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 | } |
@@ -7,7 +7,7 @@ discard block |
||
| 7 | 7 | * Class Response |
| 8 | 8 | * @package JetFire\Routing |
| 9 | 9 | */ |
| 10 | -class Response implements ResponseInterface{ |
|
| 10 | +class Response implements ResponseInterface { |
|
| 11 | 11 | |
| 12 | 12 | /** |
| 13 | 13 | * @var array |
@@ -53,7 +53,7 @@ discard block |
||
| 53 | 53 | public static $statusTexts = array( |
| 54 | 54 | 100 => 'Continue', |
| 55 | 55 | 101 => 'Switching Protocols', |
| 56 | - 102 => 'Processing', // RFC2518 |
|
| 56 | + 102 => 'Processing', // RFC2518 |
|
| 57 | 57 | 200 => 'OK', |
| 58 | 58 | 201 => 'Created', |
| 59 | 59 | 202 => 'Accepted', |
@@ -61,9 +61,9 @@ discard block |
||
| 61 | 61 | 204 => 'No Content', |
| 62 | 62 | 205 => 'Reset Content', |
| 63 | 63 | 206 => 'Partial Content', |
| 64 | - 207 => 'Multi-Status', // RFC4918 |
|
| 65 | - 208 => 'Already Reported', // RFC5842 |
|
| 66 | - 226 => 'IM Used', // RFC3229 |
|
| 64 | + 207 => 'Multi-Status', // RFC4918 |
|
| 65 | + 208 => 'Already Reported', // RFC5842 |
|
| 66 | + 226 => 'IM Used', // RFC3229 |
|
| 67 | 67 | 300 => 'Multiple Choices', |
| 68 | 68 | 301 => 'Moved Permanently', |
| 69 | 69 | 302 => 'Found', |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | 304 => 'Not Modified', |
| 72 | 72 | 305 => 'Use Proxy', |
| 73 | 73 | 307 => 'Temporary Redirect', |
| 74 | - 308 => 'Permanent Redirect', // RFC7238 |
|
| 74 | + 308 => 'Permanent Redirect', // RFC7238 |
|
| 75 | 75 | 400 => 'Bad Request', |
| 76 | 76 | 401 => 'Unauthorized', |
| 77 | 77 | 402 => 'Payment Required', |
@@ -90,26 +90,26 @@ discard block |
||
| 90 | 90 | 415 => 'Unsupported Media Type', |
| 91 | 91 | 416 => 'Range Not Satisfiable', |
| 92 | 92 | 417 => 'Expectation Failed', |
| 93 | - 418 => 'I\'m a teapot', // RFC2324 |
|
| 94 | - 422 => 'Unprocessable Entity', // RFC4918 |
|
| 95 | - 423 => 'Locked', // RFC4918 |
|
| 96 | - 424 => 'Failed Dependency', // RFC4918 |
|
| 97 | - 425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817 |
|
| 98 | - 426 => 'Upgrade Required', // RFC2817 |
|
| 99 | - 428 => 'Precondition Required', // RFC6585 |
|
| 100 | - 429 => 'Too Many Requests', // RFC6585 |
|
| 101 | - 431 => 'Request Header Fields Too Large', // RFC6585 |
|
| 93 | + 418 => 'I\'m a teapot', // RFC2324 |
|
| 94 | + 422 => 'Unprocessable Entity', // RFC4918 |
|
| 95 | + 423 => 'Locked', // RFC4918 |
|
| 96 | + 424 => 'Failed Dependency', // RFC4918 |
|
| 97 | + 425 => 'Reserved for WebDAV advanced collections expired proposal', // RFC2817 |
|
| 98 | + 426 => 'Upgrade Required', // RFC2817 |
|
| 99 | + 428 => 'Precondition Required', // RFC6585 |
|
| 100 | + 429 => 'Too Many Requests', // RFC6585 |
|
| 101 | + 431 => 'Request Header Fields Too Large', // RFC6585 |
|
| 102 | 102 | 500 => 'Internal Server Error', |
| 103 | 103 | 501 => 'Not Implemented', |
| 104 | 104 | 502 => 'Bad Gateway', |
| 105 | 105 | 503 => 'Service Unavailable', |
| 106 | 106 | 504 => 'Gateway Timeout', |
| 107 | 107 | 505 => 'HTTP Version Not Supported', |
| 108 | - 506 => 'Variant Also Negotiates (Experimental)', // RFC2295 |
|
| 109 | - 507 => 'Insufficient Storage', // RFC4918 |
|
| 110 | - 508 => 'Loop Detected', // RFC5842 |
|
| 111 | - 510 => 'Not Extended', // RFC2774 |
|
| 112 | - 511 => 'Network Authentication Required', // RFC6585 |
|
| 108 | + 506 => 'Variant Also Negotiates (Experimental)', // RFC2295 |
|
| 109 | + 507 => 'Insufficient Storage', // RFC4918 |
|
| 110 | + 508 => 'Loop Detected', // RFC5842 |
|
| 111 | + 510 => 'Not Extended', // RFC2774 |
|
| 112 | + 511 => 'Network Authentication Required', // RFC6585 |
|
| 113 | 113 | ); |
| 114 | 114 | |
| 115 | 115 | /** |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | */ |
| 167 | 167 | public function sendHeaders() |
| 168 | 168 | { |
| 169 | - foreach($this->headers as $key => $content) |
|
| 169 | + foreach ($this->headers as $key => $content) |
|
| 170 | 170 | header($key.' : '.$content); |
| 171 | 171 | http_response_code($this->getStatusCode()); |
| 172 | 172 | return $this; |
@@ -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 | |
@@ -41,7 +41,7 @@ |
||
| 41 | 41 | * @param $method |
| 42 | 42 | * @param $class |
| 43 | 43 | */ |
| 44 | - public function addDispatcher($method,$class); |
|
| 44 | + public function addDispatcher($method, $class); |
|
| 45 | 45 | |
| 46 | 46 | /** |
| 47 | 47 | * @return mixed |
@@ -166,7 +166,7 @@ discard block |
||
| 166 | 166 | } |
| 167 | 167 | |
| 168 | 168 | /** |
| 169 | - * @param $regex |
|
| 169 | + * @param string $regex |
|
| 170 | 170 | * @return bool |
| 171 | 171 | */ |
| 172 | 172 | private function routeMatch($regex) |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | } |
| 217 | 217 | |
| 218 | 218 | /** |
| 219 | - * @param $key |
|
| 219 | + * @param string $key |
|
| 220 | 220 | */ |
| 221 | 221 | private function checkRequest($key) |
| 222 | 222 | { |
@@ -125,7 +125,9 @@ discard block |
||
| 125 | 125 | $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'])); |
| 126 | 126 | $host = explode(':', $host)[0]; |
| 127 | 127 | $domain = $this->router->collection->getDomain($url); |
| 128 | - if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
|
| 128 | + if (!empty($this->request['subdomain']) && $route[0] == '/') {
|
|
| 129 | + $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
|
| 130 | + } |
|
| 129 | 131 | if ($route[0] == '/') { |
| 130 | 132 | return ($host != $domain) ? false : true; |
| 131 | 133 | } elseif ($route[0] != '/' && $host != $domain) { |
@@ -333,14 +335,18 @@ discard block |
||
| 333 | 335 | { |
| 334 | 336 | if (is_string($callback) && strpos($callback, '@') !== false) { |
| 335 | 337 | $routes = explode('@', $callback); |
| 336 | - if (!isset($routes[1])) $routes[1] = 'index'; |
|
| 338 | + if (!isset($routes[1])) {
|
|
| 339 | + $routes[1] = 'index'; |
|
| 340 | + } |
|
| 337 | 341 | if ($routes[1] == '{method}') { |
| 338 | 342 | $params = explode('/', trim(preg_replace('#' . rtrim(str_replace('*', '', $this->request['route']), '/') . '#', '', $this->router->route->getUrl()), '/')); |
| 339 | 343 | $routes[1] = empty($params[0]) ? 'index' : $params[0]; |
| 340 | 344 | $this->request['@method'] = $routes[1]; |
| 341 | 345 | array_shift($params); |
| 342 | 346 | $this->request['parameters'] = array_merge($this->request['parameters'], $params); |
| 343 | - if (preg_match('/[A-Z]/', $routes[1])) return false; |
|
| 347 | + if (preg_match('/[A-Z]/', $routes[1])) {
|
|
| 348 | + return false; |
|
| 349 | + } |
|
| 344 | 350 | $routes[1] = lcfirst(str_replace(' ', '', ucwords(str_replace('-', ' ', $routes[1])))); |
| 345 | 351 | } |
| 346 | 352 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
@@ -378,7 +384,9 @@ discard block |
||
| 378 | 384 | $target = null; |
| 379 | 385 | if (in_array('.' . $extension, $this->router->getConfig()['templateExtension'])){ |
| 380 | 386 | foreach ($viewDir as $dir) { |
| 381 | - if (is_file($fullPath = rtrim($dir, '/') . '/' . $path) || is_file($fullPath = $path)) $target = $fullPath; |
|
| 387 | + if (is_file($fullPath = rtrim($dir, '/') . '/' . $path) || is_file($fullPath = $path)) {
|
|
| 388 | + $target = $fullPath; |
|
| 389 | + } |
|
| 382 | 390 | } |
| 383 | 391 | } |
| 384 | 392 | if(is_null($target)){ |
@@ -97,15 +97,15 @@ discard block |
||
| 97 | 97 | { |
| 98 | 98 | $this->request = []; |
| 99 | 99 | for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
| 100 | - $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_' . $i) != '') ? $this->router->collection->getRoutes('prefix_' . $i) : ''; |
|
| 101 | - $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_' . $i) != '') ? $this->router->collection->getRoutes('subdomain_' . $i) : ''; |
|
| 102 | - foreach ($this->router->collection->getRoutes('routes_' . $i) as $route => $params) { |
|
| 100 | + $this->request['prefix'] = ($this->router->collection->getRoutes('prefix_'.$i) != '') ? $this->router->collection->getRoutes('prefix_'.$i) : ''; |
|
| 101 | + $this->request['subdomain'] = ($this->router->collection->getRoutes('subdomain_'.$i) != '') ? $this->router->collection->getRoutes('subdomain_'.$i) : ''; |
|
| 102 | + foreach ($this->router->collection->getRoutes('routes_'.$i) as $route => $params) { |
|
| 103 | 103 | $this->request['params'] = $params; |
| 104 | 104 | $this->request['collection_index'] = $i; |
| 105 | 105 | if ($this->checkSubdomain($route)) { |
| 106 | 106 | $route = strstr($route, '/'); |
| 107 | - $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/' . trim(trim($this->request['prefix'], '/') . '/' . trim($route, '/'), '/')); |
|
| 108 | - if ($this->routeMatch('#^' . $this->request['route'] . '$#')) { |
|
| 107 | + $this->request['route'] = preg_replace_callback('#:([\w]+)#', [$this, 'paramMatch'], '/'.trim(trim($this->request['prefix'], '/').'/'.trim($route, '/'), '/')); |
|
| 108 | + if ($this->routeMatch('#^'.$this->request['route'].'$#')) { |
|
| 109 | 109 | $this->setCallback(); |
| 110 | 110 | return $this->generateTarget(); |
| 111 | 111 | } |
@@ -121,19 +121,19 @@ discard block |
||
| 121 | 121 | */ |
| 122 | 122 | private function checkSubdomain($route) |
| 123 | 123 | { |
| 124 | - $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http') . '://' . ($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'])); |
|
| 124 | + $url = (isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http').'://'.($host = (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'])); |
|
| 125 | 125 | $host = explode(':', $host)[0]; |
| 126 | 126 | $domain = $this->router->collection->getDomain($url); |
| 127 | - if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.') . '.' . $domain . $route; |
|
| 127 | + if (!empty($this->request['subdomain']) && $route[0] == '/') $route = trim($this->request['subdomain'], '.').'.'.$domain.$route; |
|
| 128 | 128 | if ($route[0] == '/') { |
| 129 | 129 | return ($host != $domain) ? false : true; |
| 130 | 130 | } elseif ($route[0] != '/' && $host != $domain) { |
| 131 | 131 | $route = substr($route, 0, strpos($route, "/")); |
| 132 | 132 | $route = str_replace('{host}', $domain, $route); |
| 133 | 133 | $route = preg_replace_callback('#{subdomain}#', [$this, 'subdomainMatch'], $route); |
| 134 | - if (preg_match('#^' . $route . '$#', $host, $this->request['called_subdomain'])) { |
|
| 134 | + if (preg_match('#^'.$route.'$#', $host, $this->request['called_subdomain'])) { |
|
| 135 | 135 | $this->request['called_subdomain'] = array_shift($this->request['called_subdomain']); |
| 136 | - $this->request['subdomain'] = str_replace('.' . $domain, '', $host); |
|
| 136 | + $this->request['subdomain'] = str_replace('.'.$domain, '', $host); |
|
| 137 | 137 | return true; |
| 138 | 138 | } |
| 139 | 139 | } |
@@ -146,7 +146,7 @@ discard block |
||
| 146 | 146 | private function subdomainMatch() |
| 147 | 147 | { |
| 148 | 148 | if (is_array($this->request['params']) && isset($this->request['params']['subdomain'])) { |
| 149 | - return '(' . $this->request['params']['subdomain'] . ')'; |
|
| 149 | + return '('.$this->request['params']['subdomain'].')'; |
|
| 150 | 150 | } |
| 151 | 151 | return '([^/]+)'; |
| 152 | 152 | } |
@@ -159,11 +159,11 @@ discard block |
||
| 159 | 159 | { |
| 160 | 160 | if (is_array($this->request['params']) && isset($this->request['params']['arguments'][$match[1]])) { |
| 161 | 161 | $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->request['params']['arguments'][$match[1]]); |
| 162 | - return '(' . $this->request['params']['arguments'][$match[1]] . ')'; |
|
| 162 | + return '('.$this->request['params']['arguments'][$match[1]].')'; |
|
| 163 | 163 | } |
| 164 | - if(isset($this->router->collection->getRoutes('params_' . $this->request['collection_index'])['arguments'][$match[1]])){ |
|
| 165 | - $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->router->collection->getRoutes('params_' . $this->request['collection_index'])['arguments'][$match[1]]); |
|
| 166 | - return '(' . $this->request['params']['arguments'][$match[1]] . ')'; |
|
| 164 | + if (isset($this->router->collection->getRoutes('params_'.$this->request['collection_index'])['arguments'][$match[1]])) { |
|
| 165 | + $this->request['params']['arguments'][$match[1]] = str_replace('(', '(?:', $this->router->collection->getRoutes('params_'.$this->request['collection_index'])['arguments'][$match[1]]); |
|
| 166 | + return '('.$this->request['params']['arguments'][$match[1]].')'; |
|
| 167 | 167 | } |
| 168 | 168 | return '([^/]+)'; |
| 169 | 169 | } |
@@ -174,7 +174,7 @@ discard block |
||
| 174 | 174 | */ |
| 175 | 175 | private function routeMatch($regex) |
| 176 | 176 | { |
| 177 | - $regex = (substr($this->request['route'], -1) == '*') ? '#^' . $this->request['route'] . '#' : $regex; |
|
| 177 | + $regex = (substr($this->request['route'], -1) == '*') ? '#^'.$this->request['route'].'#' : $regex; |
|
| 178 | 178 | if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) { |
| 179 | 179 | array_shift($this->request['parameters']); |
| 180 | 180 | return true; |
@@ -209,9 +209,9 @@ discard block |
||
| 209 | 209 | $this->checkRequest('prefix'); |
| 210 | 210 | $this->router->route->setDetail($this->request); |
| 211 | 211 | $this->router->route->setTarget($target); |
| 212 | - $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index)); |
|
| 213 | - $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index)); |
|
| 214 | - $this->router->route->addTarget('params', $this->router->collection->getRoutes('params_' . $index)); |
|
| 212 | + $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_'.$index)); |
|
| 213 | + $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_'.$index)); |
|
| 214 | + $this->router->route->addTarget('params', $this->router->collection->getRoutes('params_'.$index)); |
|
| 215 | 215 | } |
| 216 | 216 | |
| 217 | 217 | /** |
@@ -222,8 +222,8 @@ discard block |
||
| 222 | 222 | if (strpos($this->request[$key], ':') !== false && isset($this->request['parameters'][0])) { |
| 223 | 223 | $replacements = $this->request['parameters']; |
| 224 | 224 | $keys = []; |
| 225 | - $this->request['@' . $key] = $this->request[$key]; |
|
| 226 | - $this->request[$key] = preg_replace_callback('#:([\w?]+)#', function ($matches) use (&$replacements, &$keys) { |
|
| 225 | + $this->request['@'.$key] = $this->request[$key]; |
|
| 226 | + $this->request[$key] = preg_replace_callback('#:([\w?]+)#', function($matches) use (&$replacements, &$keys) { |
|
| 227 | 227 | $route_key = preg_replace("/[^A-Za-z0-9\\-_:]/", '', $matches[0]); |
| 228 | 228 | $keys[$route_key] = isset($replacements[0]) ? $replacements[0] : null; |
| 229 | 229 | return is_null($keys[$route_key]) ? '' : array_shift($replacements); |
@@ -339,7 +339,7 @@ discard block |
||
| 339 | 339 | $routes = explode('@', $callback); |
| 340 | 340 | if (!isset($routes[1])) $routes[1] = 'index'; |
| 341 | 341 | if ($routes[1] == '{method}') { |
| 342 | - $params = explode('/', trim(preg_replace('#' . rtrim(str_replace('*', '', $this->request['route']), '/') . '#', '', $this->router->route->getUrl()), '/')); |
|
| 342 | + $params = explode('/', trim(preg_replace('#'.rtrim(str_replace('*', '', $this->request['route']), '/').'#', '', $this->router->route->getUrl()), '/')); |
|
| 343 | 343 | $routes[1] = empty($params[0]) ? 'index' : $params[0]; |
| 344 | 344 | $this->request['@method'] = $routes[1]; |
| 345 | 345 | array_shift($params); |
@@ -350,7 +350,7 @@ discard block |
||
| 350 | 350 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 351 | 351 | $class = (class_exists($routes[0])) |
| 352 | 352 | ? $routes[0] |
| 353 | - : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0]; |
|
| 353 | + : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0]; |
|
| 354 | 354 | if (method_exists($class, $routes[1])) { |
| 355 | 355 | return [ |
| 356 | 356 | 'dispatcher' => $this->dispatcher['isController'], |
@@ -360,7 +360,7 @@ discard block |
||
| 360 | 360 | ]; |
| 361 | 361 | } |
| 362 | 362 | if (!strpos($callback, '{method}') !== false) { |
| 363 | - throw new \Exception('The required method "' . $routes[1] . '" is not found in "' . $class . '"'); |
|
| 363 | + throw new \Exception('The required method "'.$routes[1].'" is not found in "'.$class.'"'); |
|
| 364 | 364 | } |
| 365 | 365 | } |
| 366 | 366 | return false; |
@@ -378,17 +378,17 @@ discard block |
||
| 378 | 378 | $path = str_replace('{template}', $replace, trim($callback, '/')); |
| 379 | 379 | $extension = substr(strrchr($path, "."), 1); |
| 380 | 380 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 381 | - $viewDir = is_array($viewDir = $this->router->collection->getRoutes('view_dir_' . $index)) ? $viewDir : [$viewDir]; |
|
| 381 | + $viewDir = is_array($viewDir = $this->router->collection->getRoutes('view_dir_'.$index)) ? $viewDir : [$viewDir]; |
|
| 382 | 382 | $target = null; |
| 383 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension'])){ |
|
| 383 | + if (in_array('.'.$extension, $this->router->getConfig()['templateExtension'])) { |
|
| 384 | 384 | foreach ($viewDir as $dir) { |
| 385 | - if (is_file($fullPath = rtrim($dir, '/') . '/' . $path) || is_file($fullPath = $path)) $target = $fullPath; |
|
| 385 | + if (is_file($fullPath = rtrim($dir, '/').'/'.$path) || is_file($fullPath = $path)) $target = $fullPath; |
|
| 386 | 386 | } |
| 387 | 387 | } |
| 388 | - if(is_null($target)){ |
|
| 388 | + if (is_null($target)) { |
|
| 389 | 389 | foreach ($viewDir as $dir) { |
| 390 | 390 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
| 391 | - if (is_file($fullPath = rtrim($dir, '/') . '/' . $path . $ext) || is_file($fullPath = $path . $ext)) { |
|
| 391 | + if (is_file($fullPath = rtrim($dir, '/').'/'.$path.$ext) || is_file($fullPath = $path.$ext)) { |
|
| 392 | 392 | $target = $fullPath; |
| 393 | 393 | $extension = substr(strrchr($ext, "."), 1); |
| 394 | 394 | break; |
@@ -44,8 +44,12 @@ |
||
| 44 | 44 | $this->router->response->setContent(call_user_func_array($this->router->route->getTarget('callback')[$this->router->route->getTarget('extension')], [$this->router->route])); |
| 45 | 45 | } else { |
| 46 | 46 | ob_start(); |
| 47 | - if (isset($this->router->route->getTarget()['data'])) extract($this->router->route->getTarget('data')); |
|
| 48 | - if (isset($this->router->route->getParams()['data'])) extract($this->router->route->getParams()['data']); |
|
| 47 | + if (isset($this->router->route->getTarget()['data'])) {
|
|
| 48 | + extract($this->router->route->getTarget('data'));
|
|
| 49 | + } |
|
| 50 | + if (isset($this->router->route->getParams()['data'])) {
|
|
| 51 | + extract($this->router->route->getParams()['data']); |
|
| 52 | + } |
|
| 49 | 53 | require($this->router->route->getTarget('template')); |
| 50 | 54 | $this->router->response->setContent(ob_get_clean()); |
| 51 | 55 | } |
@@ -40,7 +40,7 @@ |
||
| 40 | 40 | public function call() |
| 41 | 41 | { |
| 42 | 42 | if (!is_file($this->router->route->getTarget('template'))) { |
| 43 | - throw new \Exception('Template file not found : "' . $this->router->route->getTarget('template') . '"'); |
|
| 43 | + throw new \Exception('Template file not found : "'.$this->router->route->getTarget('template').'"'); |
|
| 44 | 44 | } |
| 45 | 45 | $this->setContentType($this->router->route->getTarget('extension')); |
| 46 | 46 | if (isset($this->router->route->getTarget('callback')[$this->router->route->getTarget('extension')])) { |
@@ -57,7 +57,7 @@ discard block |
||
| 57 | 57 | $this->collection = $collection; |
| 58 | 58 | $this->response = is_null($response) ? new Response() : $response; |
| 59 | 59 | $this->route = is_null($route) ? new Route() : $route; |
| 60 | - $this->config['di'] = function ($class) { |
|
| 60 | + $this->config['di'] = function($class) { |
|
| 61 | 61 | return new $class; |
| 62 | 62 | }; |
| 63 | 63 | } |
@@ -123,10 +123,10 @@ discard block |
||
| 123 | 123 | if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath(); |
| 124 | 124 | if ($this->match() === true) { |
| 125 | 125 | $this->callMiddleware('before'); |
| 126 | - if (!in_array(substr($this->response->getStatusCode(), 0, 1), [3,4,5])) { |
|
| 126 | + if (!in_array(substr($this->response->getStatusCode(), 0, 1), [3, 4, 5])) { |
|
| 127 | 127 | $this->callTarget(); |
| 128 | 128 | } |
| 129 | - }else{ |
|
| 129 | + } else { |
|
| 130 | 130 | $this->response->setStatusCode(404); |
| 131 | 131 | } |
| 132 | 132 | $this->callMiddleware('after'); |
@@ -157,7 +157,7 @@ discard block |
||
| 157 | 157 | { |
| 158 | 158 | if (is_null($url)) |
| 159 | 159 | $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
| 160 | - $this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
|
| 160 | + $this->route->setUrl('/'.trim(explode('?', $url)[0], '/')); |
|
| 161 | 161 | } |
| 162 | 162 | |
| 163 | 163 | /** |
@@ -120,13 +120,15 @@ discard block |
||
| 120 | 120 | public function run() |
| 121 | 121 | { |
| 122 | 122 | $this->setUrl(); |
| 123 | - if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath(); |
|
| 123 | + if ($this->config['generateRoutesPath']) {
|
|
| 124 | + $this->collection->generateRoutesPath(); |
|
| 125 | + } |
|
| 124 | 126 | if ($this->match() === true) { |
| 125 | 127 | $this->callMiddleware('before'); |
| 126 | 128 | if (!in_array(substr($this->response->getStatusCode(), 0, 1), [3,4,5])) { |
| 127 | 129 | $this->callTarget(); |
| 128 | 130 | } |
| 129 | - }else{ |
|
| 131 | + } else{ |
|
| 130 | 132 | $this->response->setStatusCode(404); |
| 131 | 133 | } |
| 132 | 134 | $this->callMiddleware('after'); |
@@ -155,8 +157,9 @@ discard block |
||
| 155 | 157 | */ |
| 156 | 158 | public function setUrl($url = null) |
| 157 | 159 | { |
| 158 | - if (is_null($url)) |
|
| 159 | - $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
|
| 160 | + if (is_null($url)) {
|
|
| 161 | + $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1);
|
|
| 162 | + } |
|
| 160 | 163 | $this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
| 161 | 164 | } |
| 162 | 165 | |
@@ -166,7 +169,9 @@ discard block |
||
| 166 | 169 | public function match() |
| 167 | 170 | { |
| 168 | 171 | foreach ($this->matcher as $key => $matcher) { |
| 169 | - if (call_user_func([$this->matcher[$key], 'match'])) return true; |
|
| 172 | + if (call_user_func([$this->matcher[$key], 'match'])) {
|
|
| 173 | + return true; |
|
| 174 | + } |
|
| 170 | 175 | } |
| 171 | 176 | return false; |
| 172 | 177 | } |
@@ -135,7 +135,7 @@ |
||
| 135 | 135 | |
| 136 | 136 | /** |
| 137 | 137 | * @description call the middleware before and after the target |
| 138 | - * @param $action |
|
| 138 | + * @param string $action |
|
| 139 | 139 | */ |
| 140 | 140 | public function callMiddleware($action) |
| 141 | 141 | { |
@@ -154,10 +154,10 @@ discard block |
||
| 154 | 154 | * @param $handlers |
| 155 | 155 | * @param array $params |
| 156 | 156 | */ |
| 157 | - private function callHandlers($handlers, $params = []){ |
|
| 157 | + private function callHandlers($handlers, $params = []) { |
|
| 158 | 158 | $handlers = is_array($handlers) ? $handlers : [$handlers]; |
| 159 | 159 | foreach ($handlers as $handler) { |
| 160 | - if($this->next && $this->handle($handler, $params) !== true){ |
|
| 160 | + if ($this->next && $this->handle($handler, $params) !== true) { |
|
| 161 | 161 | break; |
| 162 | 162 | } |
| 163 | 163 | } |
@@ -185,13 +185,13 @@ discard block |
||
| 185 | 185 | } |
| 186 | 186 | $dependencies = array_merge($dependencies, [$this->router->route]); |
| 187 | 187 | $response = $reflectionMethod->invokeArgs($instance, $dependencies); |
| 188 | - if(is_array($response) && isset($response['call'])){ |
|
| 189 | - if(isset($response['response']) && $response['response'] instanceof ResponseInterface){ |
|
| 188 | + if (is_array($response) && isset($response['call'])) { |
|
| 189 | + if (isset($response['response']) && $response['response'] instanceof ResponseInterface) { |
|
| 190 | 190 | $this->router->response = $response['response']; |
| 191 | 191 | } |
| 192 | - $params = isset($response['params']) ? $response['params']: []; |
|
| 192 | + $params = isset($response['params']) ? $response['params'] : []; |
|
| 193 | 193 | $this->callHandlers($response['call'], $params); |
| 194 | - $this->next = isset($response['next']) ? (bool)$response['next'] : false; |
|
| 194 | + $this->next = isset($response['next']) ? (bool) $response['next'] : false; |
|
| 195 | 195 | } else if ($response instanceof ResponseInterface) { |
| 196 | 196 | $this->router->response = $response; |
| 197 | 197 | } |
@@ -39,7 +39,7 @@ discard block |
||
| 39 | 39 | { |
| 40 | 40 | |
| 41 | 41 | if (!class_exists($this->router->route->getTarget('controller'))) { |
| 42 | - throw new \Exception('Class not found : "' . $this->router->route->getTarget('controller') . '"'); |
|
| 42 | + throw new \Exception('Class not found : "'.$this->router->route->getTarget('controller').'"'); |
|
| 43 | 43 | } |
| 44 | 44 | |
| 45 | 45 | $classInstance = [ |
@@ -92,7 +92,7 @@ discard block |
||
| 92 | 92 | { |
| 93 | 93 | $reflector = new ReflectionClass($this->router->route->getTarget('controller')); |
| 94 | 94 | if (!$reflector->isInstantiable()) { |
| 95 | - throw new \Exception('Target [' . $this->router->route->getTarget('controller') . '] is not instantiable.'); |
|
| 95 | + throw new \Exception('Target ['.$this->router->route->getTarget('controller').'] is not instantiable.'); |
|
| 96 | 96 | } |
| 97 | 97 | $constructor = $reflector->getConstructor(); |
| 98 | 98 | if (is_null($constructor)) { |
@@ -108,9 +108,9 @@ discard block |
||
| 108 | 108 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 109 | 109 | $this->router->route->setDetail($this->request); |
| 110 | 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 | - $this->router->route->addTarget('params', $this->router->collection->getRoutes('params_' . $index)); |
|
| 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 | + $this->router->route->addTarget('params', $this->router->collection->getRoutes('params_'.$index)); |
|
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | /** |
@@ -136,12 +136,12 @@ discard block |
||
| 136 | 136 | { |
| 137 | 137 | foreach ($this->router->getConfig()['templateExtension'] as $extension) { |
| 138 | 138 | for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
| 139 | - $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '', $this->router->route->getUrl())); |
|
| 139 | + $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_'.$i), '', $this->router->route->getUrl())); |
|
| 140 | 140 | $end = array_pop($url); |
| 141 | - $url = implode('/', array_map('ucwords', $url)) . '/' . $end; |
|
| 142 | - $viewDir = is_array($viewDir = $this->router->collection->getRoutes('view_dir_' . $i)) ? $viewDir : [$viewDir]; |
|
| 141 | + $url = implode('/', array_map('ucwords', $url)).'/'.$end; |
|
| 142 | + $viewDir = is_array($viewDir = $this->router->collection->getRoutes('view_dir_'.$i)) ? $viewDir : [$viewDir]; |
|
| 143 | 143 | foreach ($viewDir as $dir) { |
| 144 | - if (is_file(($template = rtrim($dir, '/') . $url . $extension))) { |
|
| 144 | + if (is_file(($template = rtrim($dir, '/').$url.$extension))) { |
|
| 145 | 145 | $this->request['collection_index'] = $i; |
| 146 | 146 | return [ |
| 147 | 147 | 'dispatcher' => $this->dispatcher['isTemplate'], |
@@ -164,11 +164,11 @@ discard block |
||
| 164 | 164 | $routes = array_slice(explode('/', $this->router->route->getUrl()), 1); |
| 165 | 165 | $i = 0; |
| 166 | 166 | do { |
| 167 | - $route = ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes; |
|
| 167 | + $route = ('/'.$routes[0] == $this->router->collection->getRoutes('prefix_'.$i)) ? array_slice($routes, 1) : $routes; |
|
| 168 | 168 | if (isset($route[0])) { |
| 169 | - $class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i) . ucfirst($route[0]) . 'Controller')) |
|
| 170 | - ? $this->router->collection->getRoutes('ctrl_namespace_' . $i) . ucfirst($route[0]) . 'Controller' |
|
| 171 | - : ucfirst($route[0]) . 'Controller'; |
|
| 169 | + $class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_'.$i).ucfirst($route[0]).'Controller')) |
|
| 170 | + ? $this->router->collection->getRoutes('ctrl_namespace_'.$i).ucfirst($route[0]).'Controller' |
|
| 171 | + : ucfirst($route[0]).'Controller'; |
|
| 172 | 172 | $route[1] = isset($route[1]) ? $route[1] : 'index'; |
| 173 | 173 | if (method_exists($class, $route[1])) { |
| 174 | 174 | $this->request['parameters'] = array_slice($route, 2); |
@@ -182,7 +182,7 @@ discard block |
||
| 182 | 182 | } |
| 183 | 183 | } |
| 184 | 184 | ++$i; |
| 185 | - } while ($i < $this->router->collection->countRoutes); |
|
| 185 | + }while ($i < $this->router->collection->countRoutes); |
|
| 186 | 186 | return false; |
| 187 | 187 | } |
| 188 | 188 | |