@@ -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 |
@@ -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 | } |