@@ -32,7 +32,7 @@ discard block |
||
32 | 32 | private $dispatcher = [ |
33 | 33 | 'isTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher', |
34 | 34 | 'isController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher', |
35 | - 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
35 | + 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
36 | 36 | ]; |
37 | 37 | |
38 | 38 | /** |
@@ -46,14 +46,14 @@ discard block |
||
46 | 46 | /** |
47 | 47 | * @param array $resolver |
48 | 48 | */ |
49 | - public function setResolver($resolver = []){ |
|
49 | + public function setResolver($resolver = []) { |
|
50 | 50 | $this->resolver = $resolver; |
51 | 51 | } |
52 | 52 | |
53 | 53 | /** |
54 | 54 | * @param string $resolver |
55 | 55 | */ |
56 | - public function addResolver($resolver){ |
|
56 | + public function addResolver($resolver) { |
|
57 | 57 | $this->resolver[] = $resolver; |
58 | 58 | } |
59 | 59 | |
@@ -78,7 +78,7 @@ discard block |
||
78 | 78 | * @param $class |
79 | 79 | * @return mixed|void |
80 | 80 | */ |
81 | - public function addDispatcher($method,$class){ |
|
81 | + public function addDispatcher($method, $class) { |
|
82 | 82 | $this->dispatcher[$method] = $class; |
83 | 83 | } |
84 | 84 | |
@@ -87,8 +87,8 @@ discard block |
||
87 | 87 | */ |
88 | 88 | public function match() |
89 | 89 | { |
90 | - foreach($this->resolver as $resolver){ |
|
91 | - if(is_array($target = call_user_func([$this,$resolver]))) { |
|
90 | + foreach ($this->resolver as $resolver) { |
|
91 | + if (is_array($target = call_user_func([$this, $resolver]))) { |
|
92 | 92 | $this->setTarget($target); |
93 | 93 | $this->router->response->setStatusCode(202); |
94 | 94 | return true; |
@@ -100,21 +100,21 @@ discard block |
||
100 | 100 | /** |
101 | 101 | * @param array $target |
102 | 102 | */ |
103 | - public function setTarget($target = []){ |
|
103 | + public function setTarget($target = []) { |
|
104 | 104 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
105 | 105 | $this->router->route->setDetail($this->request); |
106 | 106 | $this->router->route->setTarget($target); |
107 | - $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_'.$index)); |
|
108 | - $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_'.$index)); |
|
107 | + $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index)); |
|
108 | + $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index)); |
|
109 | 109 | } |
110 | 110 | |
111 | 111 | /** |
112 | 112 | * @return array|bool |
113 | 113 | */ |
114 | - public function isControllerAndTemplate(){ |
|
115 | - if(is_array($ctrl = $this->isController())) { |
|
114 | + public function isControllerAndTemplate() { |
|
115 | + if (is_array($ctrl = $this->isController())) { |
|
116 | 116 | if (is_array($tpl = $this->isTemplate())) { |
117 | - return array_merge(array_merge($ctrl, $tpl),[ |
|
117 | + return array_merge(array_merge($ctrl, $tpl), [ |
|
118 | 118 | 'dispatcher' => $this->dispatcher['isControllerAndTemplate'] |
119 | 119 | ]); |
120 | 120 | } |
@@ -130,9 +130,9 @@ discard block |
||
130 | 130 | { |
131 | 131 | foreach ($this->router->getConfig()['templateExtension'] as $extension) { |
132 | 132 | for ($i = 0; $i < $this->router->collection->countRoutes; ++$i) { |
133 | - $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '',$this->router->route->getUrl())); |
|
133 | + $url = explode('/', str_replace($this->router->collection->getRoutes('prefix_' . $i), '', $this->router->route->getUrl())); |
|
134 | 134 | $end = array_pop($url); |
135 | - $url = implode('/', array_map('ucwords', $url)).'/'.$end; |
|
135 | + $url = implode('/', array_map('ucwords', $url)) . '/' . $end; |
|
136 | 136 | if (is_file(($template = rtrim($this->router->collection->getRoutes('view_dir_' . $i), '/') . $url . $extension))) { |
137 | 137 | $this->request['collection_index'] = $i; |
138 | 138 | return [ |
@@ -154,13 +154,13 @@ discard block |
||
154 | 154 | { |
155 | 155 | $routes = array_slice(explode('/', $this->router->route->getUrl()), 1); |
156 | 156 | $i = 0; |
157 | - do{ |
|
158 | - $route = ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes; |
|
157 | + do { |
|
158 | + $route = ('/' . $routes[0] == $this->router->collection->getRoutes('prefix_' . $i)) ? array_slice($routes, 1) : $routes; |
|
159 | 159 | if (isset($route[0])) { |
160 | - $class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller')) |
|
161 | - ? $this->router->collection->getRoutes('ctrl_namespace_' . $i). ucfirst($route[0]) . 'Controller' |
|
160 | + $class = (class_exists($this->router->collection->getRoutes('ctrl_namespace_' . $i) . ucfirst($route[0]) . 'Controller')) |
|
161 | + ? $this->router->collection->getRoutes('ctrl_namespace_' . $i) . ucfirst($route[0]) . 'Controller' |
|
162 | 162 | : ucfirst($route[0]) . 'Controller'; |
163 | - $route[1] = isset($route[1])?$route[1]:'index'; |
|
163 | + $route[1] = isset($route[1]) ? $route[1] : 'index'; |
|
164 | 164 | if (method_exists($class, $route[1])) { |
165 | 165 | $this->request['parameters'] = array_slice($route, 2); |
166 | 166 | $this->request['collection_index'] = $i; |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | } |
174 | 174 | } |
175 | 175 | ++$i; |
176 | - }while($i < $this->router->collection->countRoutes); |
|
176 | + }while ($i < $this->router->collection->countRoutes); |
|
177 | 177 | return false; |
178 | 178 | } |
179 | 179 |
@@ -25,7 +25,7 @@ discard block |
||
25 | 25 | /** |
26 | 26 | * @var array |
27 | 27 | */ |
28 | - private $resolver = ['isClosureAndTemplate','isControllerAndTemplate','isTemplate']; |
|
28 | + private $resolver = ['isClosureAndTemplate', 'isControllerAndTemplate', 'isTemplate']; |
|
29 | 29 | |
30 | 30 | /** |
31 | 31 | * @var array |
@@ -34,8 +34,8 @@ discard block |
||
34 | 34 | 'isClosure' => 'JetFire\Routing\Dispatcher\ClosureDispatcher', |
35 | 35 | 'isController' => 'JetFire\Routing\Dispatcher\ControllerDispatcher', |
36 | 36 | 'isTemplate' => 'JetFire\Routing\Dispatcher\TemplateDispatcher', |
37 | - 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
38 | - 'isClosureAndTemplate' => ['JetFire\Routing\Dispatcher\ClosureDispatcher','JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
37 | + 'isControllerAndTemplate' => ['JetFire\Routing\Dispatcher\ControllerDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
38 | + 'isClosureAndTemplate' => ['JetFire\Routing\Dispatcher\ClosureDispatcher', 'JetFire\Routing\Dispatcher\TemplateDispatcher'], |
|
39 | 39 | ]; |
40 | 40 | |
41 | 41 | /** |
@@ -49,14 +49,14 @@ discard block |
||
49 | 49 | /** |
50 | 50 | * @param array $resolver |
51 | 51 | */ |
52 | - public function setResolver($resolver = []){ |
|
52 | + public function setResolver($resolver = []) { |
|
53 | 53 | $this->resolver = $resolver; |
54 | 54 | } |
55 | 55 | |
56 | 56 | /** |
57 | 57 | * @param string $resolver |
58 | 58 | */ |
59 | - public function addResolver($resolver){ |
|
59 | + public function addResolver($resolver) { |
|
60 | 60 | $this->resolver[] = $resolver; |
61 | 61 | } |
62 | 62 | |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | /** |
72 | 72 | * @param array $dispatcher |
73 | 73 | */ |
74 | - public function setDispatcher($dispatcher = []){ |
|
74 | + public function setDispatcher($dispatcher = []) { |
|
75 | 75 | $this->dispatcher = $dispatcher; |
76 | 76 | } |
77 | 77 | |
@@ -81,7 +81,7 @@ discard block |
||
81 | 81 | * @return mixed|void |
82 | 82 | * @internal param array $dispatcher |
83 | 83 | */ |
84 | - public function addDispatcher($method,$class){ |
|
84 | + public function addDispatcher($method, $class) { |
|
85 | 85 | $this->dispatcher[$method] = $class; |
86 | 86 | } |
87 | 87 | |
@@ -142,9 +142,9 @@ discard block |
||
142 | 142 | */ |
143 | 143 | private function generateTarget() |
144 | 144 | { |
145 | - if($this->validMethod()) |
|
146 | - foreach($this->resolver as $resolver) |
|
147 | - if (is_array($target = call_user_func_array([$this,$resolver],[$this->router->route->getCallback()]))) { |
|
145 | + if ($this->validMethod()) |
|
146 | + foreach ($this->resolver as $resolver) |
|
147 | + if (is_array($target = call_user_func_array([$this, $resolver], [$this->router->route->getCallback()]))) { |
|
148 | 148 | $this->setTarget($target); |
149 | 149 | $this->router->response->setStatusCode(202); |
150 | 150 | return true; |
@@ -156,20 +156,20 @@ discard block |
||
156 | 156 | /** |
157 | 157 | * @param array $target |
158 | 158 | */ |
159 | - public function setTarget($target = []){ |
|
159 | + public function setTarget($target = []) { |
|
160 | 160 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
161 | 161 | $this->router->route->setDetail($this->request); |
162 | 162 | $this->router->route->setTarget($target); |
163 | - $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_'.$index)); |
|
164 | - $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_'.$index)); |
|
163 | + $this->router->route->addTarget('block', $this->router->collection->getRoutes('block_' . $index)); |
|
164 | + $this->router->route->addTarget('view_dir', $this->router->collection->getRoutes('view_dir_' . $index)); |
|
165 | 165 | } |
166 | 166 | |
167 | 167 | /** |
168 | 168 | * |
169 | 169 | */ |
170 | - private function setCallback(){ |
|
170 | + private function setCallback() { |
|
171 | 171 | if (isset($this->request['params'])) { |
172 | - if(is_callable($this->request['params'])) |
|
172 | + if (is_callable($this->request['params'])) |
|
173 | 173 | $this->router->route->setCallback($this->request['params']); |
174 | 174 | else { |
175 | 175 | (is_array($this->request['params']) && isset($this->request['params']['use'])) |
@@ -186,7 +186,7 @@ discard block |
||
186 | 186 | */ |
187 | 187 | public function validMethod() |
188 | 188 | { |
189 | - if(is_callable($this->request['params']))return true; |
|
189 | + if (is_callable($this->request['params']))return true; |
|
190 | 190 | if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') |
191 | 191 | return (isset($this->request['params']['ajax']) && $this->request['params']['ajax'] === true) ? true : false; |
192 | 192 | $method = (isset($this->request['params']['method'])) ? $this->request['params']['method'] : ['GET']; |
@@ -198,10 +198,10 @@ discard block |
||
198 | 198 | * @return array|bool |
199 | 199 | * @throws \Exception |
200 | 200 | */ |
201 | - public function isClosureAndTemplate($callback){ |
|
202 | - if(is_array($cls = $this->isClosure($callback))) { |
|
201 | + public function isClosureAndTemplate($callback) { |
|
202 | + if (is_array($cls = $this->isClosure($callback))) { |
|
203 | 203 | if (is_array($this->request['params']) && isset($this->request['params']['template']) && is_array($tpl = $this->isTemplate($this->request['params']['template']))) { |
204 | - return array_merge(array_merge($cls, $tpl),[ |
|
204 | + return array_merge(array_merge($cls, $tpl), [ |
|
205 | 205 | 'dispatcher' => $this->dispatcher['isClosureAndTemplate'] |
206 | 206 | ]); |
207 | 207 | } |
@@ -215,10 +215,10 @@ discard block |
||
215 | 215 | * @return array|bool |
216 | 216 | * @throws \Exception |
217 | 217 | */ |
218 | - public function isControllerAndTemplate($callback){ |
|
219 | - if(is_array($ctrl = $this->isController($callback))) { |
|
218 | + public function isControllerAndTemplate($callback) { |
|
219 | + if (is_array($ctrl = $this->isController($callback))) { |
|
220 | 220 | if (is_array($this->request['params']) && isset($this->request['params']['template']) && is_array($tpl = $this->isTemplate($this->request['params']['template']))) { |
221 | - return array_merge(array_merge($ctrl, $tpl),[ |
|
221 | + return array_merge(array_merge($ctrl, $tpl), [ |
|
222 | 222 | 'dispatcher' => $this->dispatcher['isControllerAndTemplate'] |
223 | 223 | ]); |
224 | 224 | } |
@@ -256,7 +256,7 @@ discard block |
||
256 | 256 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
257 | 257 | $class = (class_exists($routes[0])) |
258 | 258 | ? $routes[0] |
259 | - : $this->router->collection->getRoutes()['ctrl_namespace_'.$index].$routes[0]; |
|
259 | + : $this->router->collection->getRoutes()['ctrl_namespace_' . $index] . $routes[0]; |
|
260 | 260 | if (!class_exists($class)) |
261 | 261 | throw new \Exception('Class "' . $class . '." is not found'); |
262 | 262 | if (method_exists($class, $routes[1])) { |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | */ |
280 | 280 | public function isTemplate($callback) |
281 | 281 | { |
282 | - if(is_string($callback)) { |
|
282 | + if (is_string($callback)) { |
|
283 | 283 | $path = trim($callback, '/'); |
284 | 284 | $extension = substr(strrchr($path, "."), 1); |
285 | 285 | $index = isset($this->request['collection_index']) ? $this->request['collection_index'] : 0; |
@@ -296,7 +296,7 @@ discard block |
||
296 | 296 | } |
297 | 297 | } |
298 | 298 | } |
299 | - if(is_null($target)) |
|
299 | + if (is_null($target)) |
|
300 | 300 | throw new \Exception('Template file "' . $path . '" is not found in "' . $viewDir . '"'); |
301 | 301 | return [ |
302 | 302 | 'dispatcher' => $this->dispatcher['isTemplate'], |
@@ -49,13 +49,13 @@ discard block |
||
49 | 49 | * @param ResponseInterface $response |
50 | 50 | * @param Route $route |
51 | 51 | */ |
52 | - public function __construct(RouteCollection $collection,ResponseInterface $response = null,Route $route = null) |
|
52 | + public function __construct(RouteCollection $collection, ResponseInterface $response = null, Route $route = null) |
|
53 | 53 | { |
54 | 54 | $this->collection = $collection; |
55 | - $this->response = is_null($response)? new Response() : $response; |
|
55 | + $this->response = is_null($response) ? new Response() : $response; |
|
56 | 56 | $this->response->setStatusCode(404); |
57 | - $this->route = is_null($route)? new Route() : $route; |
|
58 | - $this->config['di'] = function($class){ |
|
57 | + $this->route = is_null($route) ? new Route() : $route; |
|
58 | + $this->config['di'] = function($class) { |
|
59 | 59 | return new $class; |
60 | 60 | }; |
61 | 61 | } |
@@ -79,8 +79,8 @@ discard block |
||
79 | 79 | /** |
80 | 80 | * @param object|array $matcher |
81 | 81 | */ |
82 | - public function setMatcher($matcher){ |
|
83 | - if(is_object($matcher)) |
|
82 | + public function setMatcher($matcher) { |
|
83 | + if (is_object($matcher)) |
|
84 | 84 | $matcher = [$matcher]; |
85 | 85 | $this->matcher = $matcher; |
86 | 86 | } |
@@ -88,7 +88,7 @@ discard block |
||
88 | 88 | /** |
89 | 89 | * @param string $matcher |
90 | 90 | */ |
91 | - public function addMatcher($matcher){ |
|
91 | + public function addMatcher($matcher) { |
|
92 | 92 | $this->matcher[] = $matcher; |
93 | 93 | } |
94 | 94 | |
@@ -133,9 +133,9 @@ discard block |
||
133 | 133 | */ |
134 | 134 | public function callTarget() |
135 | 135 | { |
136 | - $target = is_array($this->route->getTarget('dispatcher'))?$this->route->getTarget('dispatcher'):[$this->route->getTarget('dispatcher')]; |
|
137 | - if(!empty($target)) { |
|
138 | - foreach($target as $call) { |
|
136 | + $target = is_array($this->route->getTarget('dispatcher')) ? $this->route->getTarget('dispatcher') : [$this->route->getTarget('dispatcher')]; |
|
137 | + if (!empty($target)) { |
|
138 | + foreach ($target as $call) { |
|
139 | 139 | $this->dispatcher = new $call($this->route, $this->response); |
140 | 140 | call_user_func([$this->dispatcher, 'call']); |
141 | 141 | } |
@@ -170,11 +170,11 @@ discard block |
||
170 | 170 | if (isset($this->route->getDetail()['response_templates']) && isset($this->route->getDetail()['response_templates'][$code = $this->response->getStatusCode()])) { |
171 | 171 | $this->route->setCallback($this->route->getDetail()['response_templates'][$code]); |
172 | 172 | $matcher = null; |
173 | - foreach($this->matcher as $instance) if($instance instanceof ArrayMatcher) $matcher = $instance; |
|
174 | - if(is_null($matcher))$matcher = new ArrayMatcher($this); |
|
173 | + foreach ($this->matcher as $instance) if ($instance instanceof ArrayMatcher) $matcher = $instance; |
|
174 | + if (is_null($matcher))$matcher = new ArrayMatcher($this); |
|
175 | 175 | foreach (call_user_func([$matcher, 'getResolver']) as $match) |
176 | - if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))){ |
|
177 | - call_user_func_array([$matcher, 'setTarget'],[$target]); |
|
176 | + if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))) { |
|
177 | + call_user_func_array([$matcher, 'setTarget'], [$target]); |
|
178 | 178 | $this->callTarget(); |
179 | 179 | break; |
180 | 180 | } |
@@ -80,8 +80,9 @@ discard block |
||
80 | 80 | * @param object|array $matcher |
81 | 81 | */ |
82 | 82 | public function setMatcher($matcher){ |
83 | - if(is_object($matcher)) |
|
84 | - $matcher = [$matcher]; |
|
83 | + if(is_object($matcher)) { |
|
84 | + $matcher = [$matcher]; |
|
85 | + } |
|
85 | 86 | $this->matcher = $matcher; |
86 | 87 | } |
87 | 88 | |
@@ -98,7 +99,9 @@ discard block |
||
98 | 99 | public function run() |
99 | 100 | { |
100 | 101 | $this->setUrl(); |
101 | - if ($this->config['generateRoutesPath']) $this->collection->generateRoutesPath(); |
|
102 | + if ($this->config['generateRoutesPath']) { |
|
103 | + $this->collection->generateRoutesPath(); |
|
104 | + } |
|
102 | 105 | if ($this->match()) { |
103 | 106 | $this->handle(); |
104 | 107 | $this->callTarget(); |
@@ -111,8 +114,9 @@ discard block |
||
111 | 114 | */ |
112 | 115 | public function setUrl($url = null) |
113 | 116 | { |
114 | - if (is_null($url)) |
|
115 | - $url = (isset($_GET['url'])) ? $_GET['url'] : substr(str_replace(str_replace('/index.php', '', $_SERVER['SCRIPT_NAME']), '', $_SERVER['REQUEST_URI']), 1); |
|
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); |
|
119 | + } |
|
116 | 120 | $this->route->setUrl('/' . trim(explode('?', $url)[0], '/')); |
117 | 121 | } |
118 | 122 | |
@@ -122,8 +126,9 @@ discard block |
||
122 | 126 | public function match() |
123 | 127 | { |
124 | 128 | foreach ($this->matcher as $key => $matcher) { |
125 | - if (call_user_func([$this->matcher[$key], 'match'])) |
|
126 | - return true; |
|
129 | + if (call_user_func([$this->matcher[$key], 'match'])) { |
|
130 | + return true; |
|
131 | + } |
|
127 | 132 | } |
128 | 133 | return false; |
129 | 134 | } |
@@ -170,11 +175,16 @@ discard block |
||
170 | 175 | if (isset($this->route->getDetail()['response_templates']) && isset($this->route->getDetail()['response_templates'][$code = $this->response->getStatusCode()])) { |
171 | 176 | $this->route->setCallback($this->route->getDetail()['response_templates'][$code]); |
172 | 177 | $matcher = null; |
173 | - foreach($this->matcher as $instance) if($instance instanceof ArrayMatcher) $matcher = $instance; |
|
174 | - if(is_null($matcher))$matcher = new ArrayMatcher($this); |
|
175 | - foreach (call_user_func([$matcher, 'getResolver']) as $match) |
|
176 | - if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))){ |
|
177 | - call_user_func_array([$matcher, 'setTarget'],[$target]); |
|
178 | + foreach($this->matcher as $instance) { |
|
179 | + if($instance instanceof ArrayMatcher) $matcher = $instance; |
|
180 | + } |
|
181 | + if(is_null($matcher)) { |
|
182 | + $matcher = new ArrayMatcher($this); |
|
183 | + } |
|
184 | + foreach (call_user_func([$matcher, 'getResolver']) as $match) { |
|
185 | + if (is_array($target = call_user_func_array([$matcher, $match], [$this->route->getCallback()]))){ |
|
186 | + call_user_func_array([$matcher, 'setTarget'],[$target]); |
|
187 | + } |
|
178 | 188 | $this->callTarget(); |
179 | 189 | break; |
180 | 190 | } |