@@ -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 | /** |
@@ -144,8 +144,8 @@ discard block |
||
| 144 | 144 | public function __toString() |
| 145 | 145 | { |
| 146 | 146 | return |
| 147 | - sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText)."\r\n". |
|
| 148 | - $this->headers."\r\n". |
|
| 147 | + sprintf('HTTP/%s %s %s', $this->version, $this->statusCode, $this->statusText) . "\r\n" . |
|
| 148 | + $this->headers . "\r\n" . |
|
| 149 | 149 | $this->getContent(); |
| 150 | 150 | } |
| 151 | 151 | |
@@ -166,8 +166,8 @@ discard block |
||
| 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 | http_response_code($this->getStatusCode()); |
| 172 | 172 | return $this; |
| 173 | 173 | } |
@@ -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 | } |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | /** |
| 37 | 37 | * @param Route $route |
| 38 | 38 | */ |
| 39 | - public function __construct(Route $route,ResponseInterface $response) |
|
| 39 | + public function __construct(Route $route, ResponseInterface $response) |
|
| 40 | 40 | { |
| 41 | 41 | $this->route = $route; |
| 42 | 42 | $this->response = $response; |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | /** |
| 59 | 59 | * @param $extension |
| 60 | 60 | */ |
| 61 | - public function setContentType($extension){ |
|
| 61 | + public function setContentType($extension) { |
|
| 62 | 62 | $this->response->setStatusCode(200); |
| 63 | 63 | isset($this->types[$extension]) |
| 64 | 64 | ? $this->response->setHeaders(['Content-Type' => $this->types[$extension]]) |
@@ -47,11 +47,12 @@ |
||
| 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 | 57 | require($this->route->getTarget('template')); |
| 57 | 58 | $this->response->setContent(ob_get_clean()); |
@@ -15,7 +15,7 @@ |
||
| 15 | 15 | /** |
| 16 | 16 | * @param Route $route |
| 17 | 17 | */ |
| 18 | - public function __construct(Route $route,ResponseInterface $response); |
|
| 18 | + public function __construct(Route $route, ResponseInterface $response); |
|
| 19 | 19 | |
| 20 | 20 | /** |
| 21 | 21 | * @return mixed |
@@ -25,7 +25,7 @@ |
||
| 25 | 25 | /** |
| 26 | 26 | * @param Route $route |
| 27 | 27 | */ |
| 28 | - public function __construct(Route $route,ResponseInterface $response) |
|
| 28 | + public function __construct(Route $route, ResponseInterface $response) |
|
| 29 | 29 | { |
| 30 | 30 | $this->route = $route; |
| 31 | 31 | $this->response = $response; |
@@ -127,7 +127,7 @@ discard block |
||
| 127 | 127 | */ |
| 128 | 128 | public function setDetail($detail) |
| 129 | 129 | { |
| 130 | - $this->detail = array_merge($detail,$this->detail); |
|
| 130 | + $this->detail = array_merge($detail, $this->detail); |
|
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | /** |
@@ -173,8 +173,8 @@ discard block |
||
| 173 | 173 | /** |
| 174 | 174 | * @return array |
| 175 | 175 | */ |
| 176 | - public function getData(){ |
|
| 177 | - return (isset($this->getDetail()['data']) && is_array($this->getDetail()['data']))?$this->getDetail()['data']:[]; |
|
| 176 | + public function getData() { |
|
| 177 | + return (isset($this->getDetail()['data']) && is_array($this->getDetail()['data'])) ? $this->getDetail()['data'] : []; |
|
| 178 | 178 | } |
| 179 | 179 | |
| 180 | 180 | /** |
@@ -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 | |
@@ -64,7 +64,7 @@ |
||
| 64 | 64 | } |
| 65 | 65 | |
| 66 | 66 | /** |
| 67 | - * @param $regex |
|
| 67 | + * @param string $regex |
|
| 68 | 68 | * @return bool |
| 69 | 69 | */ |
| 70 | 70 | private function routeMatch($regex) |
@@ -24,7 +24,7 @@ discard block |
||
| 24 | 24 | /** |
| 25 | 25 | * @var array |
| 26 | 26 | */ |
| 27 | - private $matcher = ['matchClosure','matchController','matchTemplate']; |
|
| 27 | + private $matcher = ['matchClosure', 'matchController', 'matchTemplate']; |
|
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | 30 | * @var array |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | /** |
| 47 | 47 | * @param string $matcher |
| 48 | 48 | */ |
| 49 | - public function addMatcher($matcher){ |
|
| 49 | + public function addMatcher($matcher) { |
|
| 50 | 50 | $this->matcher[] = $matcher; |
| 51 | 51 | } |
| 52 | 52 | |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | /** |
| 62 | 62 | * @param array $dispatcher |
| 63 | 63 | */ |
| 64 | - public function setDispatcher($dispatcher = []){ |
|
| 64 | + public function setDispatcher($dispatcher = []) { |
|
| 65 | 65 | $this->dispatcher = $dispatcher; |
| 66 | 66 | } |
| 67 | 67 | |
@@ -71,7 +71,7 @@ discard block |
||
| 71 | 71 | * @return mixed|void |
| 72 | 72 | * @internal param array $dispatcher |
| 73 | 73 | */ |
| 74 | - public function addDispatcher($method,$class){ |
|
| 74 | + public function addDispatcher($method, $class) { |
|
| 75 | 75 | $this->dispatcher[$method] = $class; |
| 76 | 76 | } |
| 77 | 77 | |
@@ -133,14 +133,14 @@ discard block |
||
| 133 | 133 | */ |
| 134 | 134 | private function generateTarget() |
| 135 | 135 | { |
| 136 | - if($this->validMethod()) { |
|
| 137 | - foreach($this->matcher as $match) |
|
| 138 | - if(call_user_func_array([$this,$match],[$this->router->route->getCallback()])) break; |
|
| 136 | + if ($this->validMethod()) { |
|
| 137 | + foreach ($this->matcher as $match) |
|
| 138 | + if (call_user_func_array([$this, $match], [$this->router->route->getCallback()])) break; |
|
| 139 | 139 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 140 | - $this->router->route->addTarget('block',$this->router->collection->getRoutes('block_'.$index)); |
|
| 141 | - $this->router->route->addTarget('view_dir',$this->router->collection->getRoutes('view_dir_'.$index)); |
|
| 140 | + $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index)); |
|
| 141 | + $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index)); |
|
| 142 | 142 | $this->router->response->setStatusCode(202); |
| 143 | - }else |
|
| 143 | + } else |
|
| 144 | 144 | $this->router->response->setStatusCode(405); |
| 145 | 145 | return $this->router->route->hasTarget(); |
| 146 | 146 | } |
@@ -148,9 +148,9 @@ discard block |
||
| 148 | 148 | /** |
| 149 | 149 | * |
| 150 | 150 | */ |
| 151 | - private function setRoute(){ |
|
| 151 | + private function setRoute() { |
|
| 152 | 152 | if (isset($this->request['params'])) { |
| 153 | - if(is_callable($this->request['params'])) |
|
| 153 | + if (is_callable($this->request['params'])) |
|
| 154 | 154 | $this->router->route->setCallback($this->request['params']); |
| 155 | 155 | else { |
| 156 | 156 | (is_array($this->request['params']) && isset($this->request['params']['use'])) |
@@ -168,7 +168,7 @@ discard block |
||
| 168 | 168 | */ |
| 169 | 169 | public function validMethod() |
| 170 | 170 | { |
| 171 | - if(is_callable($this->request['params']))return true; |
|
| 171 | + if (is_callable($this->request['params']))return true; |
|
| 172 | 172 | if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') |
| 173 | 173 | return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
| 174 | 174 | $method = (isset($this->router->route->getDetail()['params']['method'])) ? $this->router->route->getDetail()['params']['method'] : ['GET']; |
@@ -205,7 +205,7 @@ discard block |
||
| 205 | 205 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 206 | 206 | $class = (class_exists($routes[0])) |
| 207 | 207 | ? $routes[0] |
| 208 | - : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0]; |
|
| 208 | + : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0]; |
|
| 209 | 209 | if (!class_exists($class)) |
| 210 | 210 | throw new \Exception('Class "' . $class . '." is not found'); |
| 211 | 211 | if (method_exists($class, $routes[1])) { |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | */ |
| 230 | 230 | public function matchTemplate($callback) |
| 231 | 231 | { |
| 232 | - if(is_string($callback)) { |
|
| 232 | + if (is_string($callback)) { |
|
| 233 | 233 | $path = trim($callback, '/'); |
| 234 | 234 | $extension = substr(strrchr($path, "."), 1); |
| 235 | 235 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
@@ -246,7 +246,7 @@ discard block |
||
| 246 | 246 | } |
| 247 | 247 | } |
| 248 | 248 | } |
| 249 | - if(is_null($target)) |
|
| 249 | + if (is_null($target)) |
|
| 250 | 250 | throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
| 251 | 251 | $this->router->route->setTarget([ |
| 252 | 252 | 'dispatcher' => $this->dispatcher['matchTemplate'], |
@@ -118,8 +118,9 @@ discard block |
||
| 118 | 118 | { |
| 119 | 119 | if (substr($this->request['route'], -1) == '*') { |
| 120 | 120 | $pos = strpos($this->request['route'], '*'); |
| 121 | - if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) |
|
| 122 | - return true; |
|
| 121 | + if (substr($this->router->route->getUrl(), 0, $pos) == substr($this->request['route'], 0, $pos) && isset($this->request['params'])) {
|
|
| 122 | + return true; |
|
| 123 | + } |
|
| 123 | 124 | } |
| 124 | 125 | if (preg_match($regex, $this->router->route->getUrl(), $this->request['parameters'])) { |
| 125 | 126 | array_shift($this->request['parameters']); |
@@ -134,14 +135,16 @@ discard block |
||
| 134 | 135 | private function generateTarget() |
| 135 | 136 | { |
| 136 | 137 | if($this->validMethod()) { |
| 137 | - foreach($this->matcher as $match) |
|
| 138 | - if(call_user_func_array([$this,$match],[$this->router->route->getCallback()])) break; |
|
| 138 | + foreach($this->matcher as $match) {
|
|
| 139 | + if(call_user_func_array([$this,$match],[$this->router->route->getCallback()])) break; |
|
| 140 | + } |
|
| 139 | 141 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 140 | 142 | $this->router->route->addTarget('block',$this->router->collection->getRoutes('block_'.$index)); |
| 141 | 143 | $this->router->route->addTarget('view_dir',$this->router->collection->getRoutes('view_dir_'.$index)); |
| 142 | 144 | $this->router->response->setStatusCode(202); |
| 143 | - }else |
|
| 144 | - $this->router->response->setStatusCode(405); |
|
| 145 | + } else {
|
|
| 146 | + $this->router->response->setStatusCode(405); |
|
| 147 | + } |
|
| 145 | 148 | return $this->router->route->hasTarget(); |
| 146 | 149 | } |
| 147 | 150 | |
@@ -150,14 +153,18 @@ discard block |
||
| 150 | 153 | */ |
| 151 | 154 | private function setRoute(){ |
| 152 | 155 | if (isset($this->request['params'])) { |
| 153 | - if(is_callable($this->request['params'])) |
|
| 154 | - $this->router->route->setCallback($this->request['params']); |
|
| 155 | - else { |
|
| 156 | + if(is_callable($this->request['params'])) {
|
|
| 157 | + $this->router->route->setCallback($this->request['params']); |
|
| 158 | + } else { |
|
| 156 | 159 | (is_array($this->request['params']) && isset($this->request['params']['use'])) |
| 157 | 160 | ? $this->router->route->setCallback($this->request['params']['use']) |
| 158 | 161 | : $this->router->route->setCallback($this->request['params']); |
| 159 | - if (isset($this->request['params']['name'])) $this->router->route->setName($this->request['params']['name']); |
|
| 160 | - if (isset($this->request['params']['method'])) $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
| 162 | + if (isset($this->request['params']['name'])) {
|
|
| 163 | + $this->router->route->setName($this->request['params']['name']); |
|
| 164 | + } |
|
| 165 | + if (isset($this->request['params']['method'])) {
|
|
| 166 | + $this->request['params']['method'] = is_array($this->request['params']['method']) ? $this->request['params']['method'] : [$this->request['params']['method']]; |
|
| 167 | + } |
|
| 161 | 168 | } |
| 162 | 169 | } |
| 163 | 170 | $this->router->route->setDetail($this->request); |
@@ -168,9 +175,12 @@ discard block |
||
| 168 | 175 | */ |
| 169 | 176 | public function validMethod() |
| 170 | 177 | { |
| 171 | - if(is_callable($this->request['params']))return true; |
|
| 172 | - if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') |
|
| 173 | - return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
| 178 | + if(is_callable($this->request['params'])) {
|
|
| 179 | + return true; |
|
| 180 | + } |
|
| 181 | + if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
|
|
| 182 | + return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
|
| 183 | + } |
|
| 174 | 184 | $method = (isset($this->router->route->getDetail()['params']['method'])) ? $this->router->route->getDetail()['params']['method'] : ['GET']; |
| 175 | 185 | return (in_array($this->router->route->getMethod(), $method)) ? true : false; |
| 176 | 186 | } |
@@ -201,13 +211,16 @@ discard block |
||
| 201 | 211 | { |
| 202 | 212 | if (is_string($callback) && strpos($callback, '@') !== false) { |
| 203 | 213 | $routes = explode('@', $callback); |
| 204 | - if (!isset($routes[1])) $routes[1] = 'index'; |
|
| 214 | + if (!isset($routes[1])) {
|
|
| 215 | + $routes[1] = 'index'; |
|
| 216 | + } |
|
| 205 | 217 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 206 | 218 | $class = (class_exists($routes[0])) |
| 207 | 219 | ? $routes[0] |
| 208 | 220 | : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0]; |
| 209 | - if (!class_exists($class)) |
|
| 210 | - throw new \Exception('Class "' . $class . '." is not found'); |
|
| 221 | + if (!class_exists($class)) {
|
|
| 222 | + throw new \Exception('Class "' . $class . '." is not found');
|
|
| 223 | + } |
|
| 211 | 224 | if (method_exists($class, $routes[1])) { |
| 212 | 225 | $this->router->route->setTarget([ |
| 213 | 226 | 'dispatcher' => $this->dispatcher['matchController'], |
@@ -235,9 +248,9 @@ discard block |
||
| 235 | 248 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
| 236 | 249 | $viewDir = $this->router->collection->getRoutes('view_dir_' . $index); |
| 237 | 250 | $target = null; |
| 238 | - if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) |
|
| 239 | - $target = $fullPath; |
|
| 240 | - else { |
|
| 251 | + if (in_array('.' . $extension, $this->router->getConfig()['templateExtension']) && (is_file($fullPath = $viewDir . $path) || is_file($fullPath = $path))) {
|
|
| 252 | + $target = $fullPath; |
|
| 253 | + } else { |
|
| 241 | 254 | foreach ($this->router->getConfig()['templateExtension'] as $ext) { |
| 242 | 255 | if (is_file($fullPath = $viewDir . $path . $ext) || is_file($fullPath = $path . $ext)) { |
| 243 | 256 | $target = $fullPath; |
@@ -246,8 +259,9 @@ discard block |
||
| 246 | 259 | } |
| 247 | 260 | } |
| 248 | 261 | } |
| 249 | - if(is_null($target)) |
|
| 250 | - throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
|
| 262 | + if(is_null($target)) {
|
|
| 263 | + throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"');
|
|
| 264 | + } |
|
| 251 | 265 | $this->router->route->setTarget([ |
| 252 | 266 | 'dispatcher' => $this->dispatcher['matchTemplate'], |
| 253 | 267 | 'template' => $target, |
@@ -27,7 +27,7 @@ discard block |
||
| 27 | 27 | /** |
| 28 | 28 | * @param Route $route |
| 29 | 29 | */ |
| 30 | - public function __construct(Route $route,ResponseInterface $response) |
|
| 30 | + public function __construct(Route $route, ResponseInterface $response) |
|
| 31 | 31 | { |
| 32 | 32 | $this->route = $route; |
| 33 | 33 | $this->response = $response; |
@@ -44,8 +44,8 @@ discard block |
||
| 44 | 44 | $dependencies = []; |
| 45 | 45 | foreach ($reflectionMethod->getParameters() as $arg) |
| 46 | 46 | if (!is_null($arg->getClass())) |
| 47 | - $dependencies[] = call_user_func_array($this->route->getTarget('di'),[$arg->getClass()->name]); |
|
| 48 | - $dependencies = array_merge($dependencies,($this->route->getParameters() == '') ? [] : $this->route->getParameters()); |
|
| 47 | + $dependencies[] = call_user_func_array($this->route->getTarget('di'), [$arg->getClass()->name]); |
|
| 48 | + $dependencies = array_merge($dependencies, ($this->route->getParameters() == '') ? [] : $this->route->getParameters()); |
|
| 49 | 49 | if ($this->response->getStatusCode() == 202) |
| 50 | 50 | $this->response->setStatusCode(200); |
| 51 | 51 | return $this->response->setContent($reflectionMethod->invokeArgs($this->getController(), $dependencies)); |
@@ -64,13 +64,13 @@ discard block |
||
| 64 | 64 | $constructor = $reflector->getConstructor(); |
| 65 | 65 | if (is_null($constructor)) { |
| 66 | 66 | $class = $this->route->getTarget('controller'); |
| 67 | - return call_user_func_array($this->route->getTarget('di'),[$class]); |
|
| 67 | + return call_user_func_array($this->route->getTarget('di'), [$class]); |
|
| 68 | 68 | } |
| 69 | 69 | $dependencies = $constructor->getParameters(); |
| 70 | 70 | $arguments = []; |
| 71 | 71 | foreach ($dependencies as $dep) { |
| 72 | 72 | $class = $dep->getClass()->name; |
| 73 | - array_push($arguments, call_user_func_array($this->route->getTarget('di'),[$class])); |
|
| 73 | + array_push($arguments, call_user_func_array($this->route->getTarget('di'), [$class])); |
|
| 74 | 74 | } |
| 75 | 75 | return $reflector->newInstanceArgs($arguments); |
| 76 | 76 | } |
@@ -42,12 +42,14 @@ discard block |
||
| 42 | 42 | { |
| 43 | 43 | $reflectionMethod = new ReflectionMethod($this->route->getTarget('controller'), $this->route->getTarget('action')); |
| 44 | 44 | $dependencies = []; |
| 45 | - foreach ($reflectionMethod->getParameters() as $arg) |
|
| 46 | - if (!is_null($arg->getClass())) |
|
| 47 | - $dependencies[] = call_user_func_array($this->route->getTarget('di'),[$arg->getClass()->name]); |
|
| 45 | + foreach ($reflectionMethod->getParameters() as $arg) {
|
|
| 46 | + if (!is_null($arg->getClass())) |
|
| 47 | + $dependencies[] = call_user_func_array($this->route->getTarget('di'),[$arg->getClass()->name]);
|
|
| 48 | + } |
|
| 48 | 49 | $dependencies = array_merge($dependencies,($this->route->getParameters() == '') ? [] : $this->route->getParameters()); |
| 49 | - if ($this->response->getStatusCode() == 202) |
|
| 50 | - $this->response->setStatusCode(200); |
|
| 50 | + if ($this->response->getStatusCode() == 202) {
|
|
| 51 | + $this->response->setStatusCode(200); |
|
| 52 | + } |
|
| 51 | 53 | return $this->response->setContent($reflectionMethod->invokeArgs($this->getController(), $dependencies)); |
| 52 | 54 | } |
| 53 | 55 | |
@@ -59,8 +61,9 @@ discard block |
||
| 59 | 61 | private function getController() |
| 60 | 62 | { |
| 61 | 63 | $reflector = new ReflectionClass($this->route->getTarget('controller')); |
| 62 | - if (!$reflector->isInstantiable()) |
|
| 63 | - throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.'); |
|
| 64 | + if (!$reflector->isInstantiable()) {
|
|
| 65 | + throw new \Exception('Target [' . $this->route->getTarget('controller') . '] is not instantiable.');
|
|
| 66 | + } |
|
| 64 | 67 | $constructor = $reflector->getConstructor(); |
| 65 | 68 | if (is_null($constructor)) { |
| 66 | 69 | $class = $this->route->getTarget('controller'); |
@@ -32,7 +32,7 @@ discard block |
||
| 32 | 32 | if (isset($this->router->collection->middleware['global_middleware'])) |
| 33 | 33 | foreach ($this->router->collection->middleware['global_middleware'] as $mid) { |
| 34 | 34 | if (class_exists($mid)) { |
| 35 | - $mid_global = call_user_func($this->router->getConfig()['di'],$mid); |
|
| 35 | + $mid_global = call_user_func($this->router->getConfig()['di'], $mid); |
|
| 36 | 36 | if (method_exists($mid_global, 'handle')) $this->callHandler($mid_global); |
| 37 | 37 | } |
| 38 | 38 | } |
@@ -46,7 +46,7 @@ discard block |
||
| 46 | 46 | if (isset($this->router->collection->middleware['block_middleware'])) |
| 47 | 47 | if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getBlock()]) && class_exists($this->router->collection->middleware['block_middleware'][$this->router->route->getBlock()])) { |
| 48 | 48 | $class = $this->router->collection->middleware['block_middleware'][$this->router->route->getBlock()]; |
| 49 | - $mid_block = call_user_func($this->router->getConfig()['di'],$class); |
|
| 49 | + $mid_block = call_user_func($this->router->getConfig()['di'], $class); |
|
| 50 | 50 | if (method_exists($mid_block, 'handle')) $this->callHandler($mid_block); |
| 51 | 51 | } |
| 52 | 52 | } |
@@ -60,7 +60,7 @@ discard block |
||
| 60 | 60 | $ctrl = str_replace('\\', '/', $this->router->route->getTarget('controller')); |
| 61 | 61 | if (isset($this->router->collection->middleware['class_middleware'][$ctrl]) && class_exists($this->router->route->getTarget('controller'))) { |
| 62 | 62 | $class = $this->router->collection->middleware['class_middleware'][$ctrl]; |
| 63 | - $mid_class = call_user_func($this->router->getConfig()['di'],$class); |
|
| 63 | + $mid_class = call_user_func($this->router->getConfig()['di'], $class); |
|
| 64 | 64 | if (method_exists($mid_class, 'handle')) $this->callHandler($mid_class); |
| 65 | 65 | } |
| 66 | 66 | } |
@@ -74,7 +74,7 @@ discard block |
||
| 74 | 74 | if (isset($this->router->collection->middleware['route_middleware'])) |
| 75 | 75 | if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
| 76 | 76 | $class = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
| 77 | - $mid_route = call_user_func($this->router->getConfig()['di'],$class); |
|
| 77 | + $mid_route = call_user_func($this->router->getConfig()['di'], $class); |
|
| 78 | 78 | if (method_exists($mid_route, 'handle')) $this->callHandler($mid_route); |
| 79 | 79 | } |
| 80 | 80 | } |
@@ -83,13 +83,13 @@ discard block |
||
| 83 | 83 | * @param $instance |
| 84 | 84 | * @return mixed |
| 85 | 85 | */ |
| 86 | - private function callHandler($instance){ |
|
| 86 | + private function callHandler($instance) { |
|
| 87 | 87 | $reflectionMethod = new ReflectionMethod($instance, 'handle'); |
| 88 | 88 | $dependencies = []; |
| 89 | 89 | foreach ($reflectionMethod->getParameters() as $arg) |
| 90 | 90 | if (!is_null($arg->getClass())) |
| 91 | 91 | $dependencies[] = $this->getClass($arg->getClass()->name); |
| 92 | - $dependencies = array_merge($dependencies,[$this->router->route]); |
|
| 92 | + $dependencies = array_merge($dependencies, [$this->router->route]); |
|
| 93 | 93 | return $reflectionMethod->invokeArgs($instance, $dependencies); |
| 94 | 94 | } |
| 95 | 95 | |
@@ -97,8 +97,8 @@ discard block |
||
| 97 | 97 | * @param $class |
| 98 | 98 | * @return Route|RouteCollection|Router|mixed |
| 99 | 99 | */ |
| 100 | - private function getClass($class){ |
|
| 101 | - switch($class){ |
|
| 100 | + private function getClass($class) { |
|
| 101 | + switch ($class) { |
|
| 102 | 102 | case 'JetFire\Routing\Route': |
| 103 | 103 | return $this->router->route; |
| 104 | 104 | break; |
@@ -109,7 +109,7 @@ discard block |
||
| 109 | 109 | return $this->router->collection; |
| 110 | 110 | break; |
| 111 | 111 | default: |
| 112 | - return call_user_func_array($this->router->getConfig()['di'],[$class]); |
|
| 112 | + return call_user_func_array($this->router->getConfig()['di'], [$class]); |
|
| 113 | 113 | break; |
| 114 | 114 | } |
| 115 | 115 | } |
@@ -29,11 +29,14 @@ 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 $mid) { |
|
| 32 | + if (isset($this->router->collection->middleware['global_middleware'])) {
|
|
| 33 | + foreach ($this->router->collection->middleware['global_middleware'] as $mid) { |
|
| 34 | 34 | if (class_exists($mid)) { |
| 35 | - $mid_global = call_user_func($this->router->getConfig()['di'],$mid); |
|
| 36 | - if (method_exists($mid_global, 'handle')) $this->callHandler($mid_global); |
|
| 35 | + $mid_global = call_user_func($this->router->getConfig()['di'],$mid); |
|
| 36 | + } |
|
| 37 | + if (method_exists($mid_global, 'handle')) {
|
|
| 38 | + $this->callHandler($mid_global); |
|
| 39 | + } |
|
| 37 | 40 | } |
| 38 | 41 | } |
| 39 | 42 | } |
@@ -43,11 +46,14 @@ discard block |
||
| 43 | 46 | */ |
| 44 | 47 | public function blockMiddleware() |
| 45 | 48 | { |
| 46 | - if (isset($this->router->collection->middleware['block_middleware'])) |
|
| 47 | - if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]) && class_exists($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
| 48 | - $class = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]; |
|
| 49 | + if (isset($this->router->collection->middleware['block_middleware'])) {
|
|
| 50 | + if (isset($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')]) && class_exists($this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')])) { |
|
| 51 | + $class = $this->router->collection->middleware['block_middleware'][$this->router->route->getTarget('block')];
|
|
| 52 | + } |
|
| 49 | 53 | $mid_block = call_user_func($this->router->getConfig()['di'],$class); |
| 50 | - if (method_exists($mid_block, 'handle')) $this->callHandler($mid_block); |
|
| 54 | + if (method_exists($mid_block, 'handle')) {
|
|
| 55 | + $this->callHandler($mid_block); |
|
| 56 | + } |
|
| 51 | 57 | } |
| 52 | 58 | } |
| 53 | 59 | |
@@ -61,7 +67,9 @@ discard block |
||
| 61 | 67 | if (isset($this->router->collection->middleware['class_middleware'][$ctrl]) && class_exists($this->router->route->getTarget('controller'))) { |
| 62 | 68 | $class = $this->router->collection->middleware['class_middleware'][$ctrl]; |
| 63 | 69 | $mid_class = call_user_func($this->router->getConfig()['di'],$class); |
| 64 | - if (method_exists($mid_class, 'handle')) $this->callHandler($mid_class); |
|
| 70 | + if (method_exists($mid_class, 'handle')) {
|
|
| 71 | + $this->callHandler($mid_class); |
|
| 72 | + } |
|
| 65 | 73 | } |
| 66 | 74 | } |
| 67 | 75 | } |
@@ -71,11 +79,14 @@ discard block |
||
| 71 | 79 | */ |
| 72 | 80 | public function routeMiddleware() |
| 73 | 81 | { |
| 74 | - if (isset($this->router->collection->middleware['route_middleware'])) |
|
| 75 | - if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
| 76 | - $class = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
| 82 | + if (isset($this->router->collection->middleware['route_middleware'])) {
|
|
| 83 | + if (isset($this->router->route->getPath()['middleware']) && class_exists($this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']])) { |
|
| 84 | + $class = $this->router->collection->middleware['route_middleware'][$this->router->route->getPath()['middleware']]; |
|
| 85 | + } |
|
| 77 | 86 | $mid_route = call_user_func($this->router->getConfig()['di'],$class); |
| 78 | - if (method_exists($mid_route, 'handle')) $this->callHandler($mid_route); |
|
| 87 | + if (method_exists($mid_route, 'handle')) {
|
|
| 88 | + $this->callHandler($mid_route); |
|
| 89 | + } |
|
| 79 | 90 | } |
| 80 | 91 | } |
| 81 | 92 | |
@@ -86,9 +97,10 @@ discard block |
||
| 86 | 97 | private function callHandler($instance){ |
| 87 | 98 | $reflectionMethod = new ReflectionMethod($instance, 'handle'); |
| 88 | 99 | $dependencies = []; |
| 89 | - foreach ($reflectionMethod->getParameters() as $arg) |
|
| 90 | - if (!is_null($arg->getClass())) |
|
| 91 | - $dependencies[] = $this->getClass($arg->getClass()->name); |
|
| 100 | + foreach ($reflectionMethod->getParameters() as $arg) {
|
|
| 101 | + if (!is_null($arg->getClass())) |
|
| 102 | + $dependencies[] = $this->getClass($arg->getClass()->name); |
|
| 103 | + } |
|
| 92 | 104 | $dependencies = array_merge($dependencies,[$this->router->route]); |
| 93 | 105 | return $reflectionMethod->invokeArgs($instance, $dependencies); |
| 94 | 106 | } |
@@ -41,5 +41,5 @@ |
||
| 41 | 41 | * @param $class |
| 42 | 42 | * @return mixed |
| 43 | 43 | */ |
| 44 | - public function addDispatcher($method,$class); |
|
| 44 | + public function addDispatcher($method, $class); |
|
| 45 | 45 | } |