@@ -26,19 +26,19 @@ |
||
26 | 26 | <pre> |
27 | 27 | <?php |
28 | 28 | if ($match) { |
29 | - foreach ($match as $key => $value) { |
|
30 | - echo '<p>' . $key . ': '; |
|
31 | - if (is_array($value)) { |
|
32 | - echo '<ul>'; |
|
33 | - foreach ($value as $k => $v) { |
|
34 | - echo '<li>'.$k.': '.$v.'</li>'; |
|
35 | - } |
|
36 | - echo '</ul>'; |
|
37 | - } else { |
|
38 | - echo $value; |
|
39 | - } |
|
40 | - echo '</p>'; |
|
41 | - } |
|
29 | + foreach ($match as $key => $value) { |
|
30 | + echo '<p>' . $key . ': '; |
|
31 | + if (is_array($value)) { |
|
32 | + echo '<ul>'; |
|
33 | + foreach ($value as $k => $v) { |
|
34 | + echo '<li>'.$k.': '.$v.'</li>'; |
|
35 | + } |
|
36 | + echo '</ul>'; |
|
37 | + } else { |
|
38 | + echo $value; |
|
39 | + } |
|
40 | + echo '</p>'; |
|
41 | + } |
|
42 | 42 | } |
43 | 43 | ?> |
44 | 44 | </pre> |
@@ -31,7 +31,7 @@ |
||
31 | 31 | if (is_array($value)) { |
32 | 32 | echo '<ul>'; |
33 | 33 | foreach ($value as $k => $v) { |
34 | - echo '<li>'.$k.': '.$v.'</li>'; |
|
34 | + echo '<li>' . $k . ': ' . $v . '</li>'; |
|
35 | 35 | } |
36 | 36 | echo '</ul>'; |
37 | 37 | } else { |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | |
91 | 91 | return array( |
92 | 92 | 'target' => $handler[2], |
93 | - 'params' => array_filter($this->parser->getParams(), function ($k) { |
|
93 | + 'params' => array_filter($this->parser->getParams(), function($k) { |
|
94 | 94 | return !is_numeric($k); |
95 | 95 | }, ARRAY_FILTER_USE_KEY), |
96 | 96 | 'name' => $handler[3] |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | public function __call($method, $arguments) |
109 | 109 | { |
110 | 110 | if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
111 | - throw new RouterException($method . ' not exist in the '. __CLASS__); |
|
111 | + throw new RouterException($method . ' not exist in the ' . __CLASS__); |
|
112 | 112 | } |
113 | 113 | |
114 | 114 | $methods = $method == 'all' ? implode('|', $this->all) : $method; |
@@ -5,215 +5,215 @@ |
||
5 | 5 | |
6 | 6 | class Router |
7 | 7 | { |
8 | - protected $routes = []; |
|
9 | - protected $namedRoutes = []; |
|
10 | - protected $basePath = ''; |
|
11 | - protected $all = ['get', 'post']; |
|
12 | - protected $server; |
|
13 | - /** |
|
14 | - * @var RouterParser |
|
15 | - */ |
|
16 | - private $parser; |
|
17 | - |
|
18 | - /** |
|
19 | - * Create router in one call from config. |
|
20 | - * |
|
21 | - * @param RouterParser $parser |
|
22 | - * @param array $routes |
|
23 | - * @param string $basePath |
|
24 | - * @param array|null $server |
|
25 | - */ |
|
26 | - public function __construct(RouterParser $parser, $routes = [], $basePath = '', $server = null) |
|
27 | - { |
|
28 | - $this->setRoutes($routes); |
|
29 | - $this->setBasePath($basePath); |
|
30 | - $this->parser = $parser; |
|
31 | - $this->server = $server; |
|
32 | - } |
|
33 | - |
|
34 | - /** |
|
35 | - * Map a route to a target |
|
36 | - * |
|
37 | - * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE) |
|
38 | - * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id] |
|
39 | - * @param mixed $target The target where this route should point to. Can be anything. |
|
40 | - * @param string $routeName Optional name of this route. Supply if you want to reverse route this url in your application. |
|
41 | - */ |
|
42 | - public function map($method, $route, $target, $routeName = null) |
|
43 | - { |
|
44 | - if (is_string($routeName)) { |
|
45 | - $this->handleException($routeName, "Can not redeclare route '%s'", true); |
|
46 | - $this->namedRoutes[$routeName] = $route; |
|
47 | - } |
|
48 | - |
|
49 | - $this->routes[] = array($method, $route, $target, $routeName); |
|
50 | - } |
|
51 | - |
|
52 | - /** |
|
53 | - * Reversed routing |
|
54 | - * Generate the URL for a named route. Replace regexes with supplied parameters |
|
55 | - * |
|
56 | - * @param string $routeName The name of the route. |
|
57 | - * @param array $params Associative array of parameters to replace placeholders with. |
|
58 | - * |
|
59 | - * @return string The URL of the route with named parameters in place. |
|
60 | - */ |
|
61 | - public function generate($routeName, array $params = []) |
|
62 | - { |
|
63 | - $this->handleException($routeName, "Route '%s' does not exist.", false); |
|
64 | - |
|
65 | - $route = $this->namedRoutes[$routeName]; |
|
66 | - |
|
67 | - return $this->parser->generateUrl($this->basePath, $route, $params); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Match a given Request Url against stored routes |
|
72 | - * @param string $requestUrl |
|
73 | - * @param string $requestMethod |
|
74 | - * |
|
75 | - * @return array|boolean Array with route information on success, false on failure (no match). |
|
76 | - */ |
|
77 | - public function match($requestUrl = null, $requestMethod = null) |
|
78 | - { |
|
79 | - $requestUrl = $this->getRequestUrl($requestUrl); |
|
80 | - |
|
81 | - // set Request Method if it isn't passed as a parameter |
|
82 | - if (is_null($requestMethod)) { |
|
83 | - $requestMethod = $this->server['REQUEST_METHOD']; |
|
84 | - } |
|
85 | - |
|
86 | - foreach ($this->routes as $handler) { |
|
87 | - if (!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) { |
|
88 | - continue; |
|
89 | - } |
|
90 | - |
|
91 | - return array( |
|
92 | - 'target' => $handler[2], |
|
93 | - 'params' => array_filter($this->parser->getParams(), function ($k) { |
|
94 | - return !is_numeric($k); |
|
95 | - }, ARRAY_FILTER_USE_KEY), |
|
96 | - 'name' => $handler[3] |
|
97 | - ); |
|
98 | - } |
|
99 | - |
|
100 | - return false; |
|
101 | - } |
|
102 | - |
|
103 | - /** |
|
104 | - * @param $method |
|
105 | - * @param $arguments |
|
106 | - * |
|
107 | - * @throws RouterException |
|
108 | - */ |
|
109 | - public function __call($method, $arguments) |
|
110 | - { |
|
111 | - if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
|
112 | - throw new RouterException($method . ' not exist in the '. __CLASS__); |
|
113 | - } |
|
114 | - |
|
115 | - $methods = $method == 'all' ? implode('|', $this->all) : $method; |
|
116 | - |
|
117 | - $route = array_merge([$methods], $arguments); |
|
118 | - |
|
119 | - call_user_func_array([$this, 'map'], $route); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * Retrieves all routes. |
|
124 | - * Useful if you want to process or display routes. |
|
125 | - * |
|
126 | - * @return array All routes. |
|
127 | - */ |
|
128 | - public function getRoutes() |
|
129 | - { |
|
130 | - return $this->routes; |
|
131 | - } |
|
132 | - |
|
133 | - /** |
|
134 | - * Add multiple routes at once from array in the following format: |
|
135 | - * |
|
136 | - * $routes = array( |
|
137 | - * array($method, $route, $target, $name) |
|
138 | - * ); |
|
139 | - * |
|
140 | - * @param array|Iterator $routes |
|
141 | - * |
|
142 | - * @return void |
|
143 | - */ |
|
144 | - public function setRoutes($routes) |
|
145 | - { |
|
146 | - if (!is_array($routes) && !$routes instanceof Iterator) { |
|
147 | - throw new RouterException('Routes should be an array or an instance of Iterator'); |
|
148 | - } |
|
149 | - if (!empty($routes)) { |
|
150 | - foreach ($routes as $route) { |
|
151 | - call_user_func_array(array($this, 'map'), $route); |
|
152 | - } |
|
153 | - } |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Set the base path. |
|
158 | - * Useful if you are running your application from a subdirectory. |
|
159 | - * |
|
160 | - * @param $basePath |
|
161 | - */ |
|
162 | - public function setBasePath($basePath) |
|
163 | - { |
|
164 | - $this->basePath = $basePath; |
|
165 | - } |
|
166 | - |
|
167 | - /** |
|
168 | - * @param $routeName |
|
169 | - * @param $message |
|
170 | - * @param bool $cmpTo |
|
171 | - * |
|
172 | - * @throws RouterException |
|
173 | - */ |
|
174 | - private function handleException($routeName, $message, $cmpTo) |
|
175 | - { |
|
176 | - if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) { |
|
177 | - throw new RouterException(sprintf($message, $routeName)); |
|
178 | - } |
|
179 | - } |
|
180 | - |
|
181 | - /** |
|
182 | - * @param $requestUrl |
|
183 | - * |
|
184 | - * @return mixed |
|
185 | - */ |
|
186 | - private function getRequestUrl($requestUrl = null) |
|
187 | - { |
|
188 | - // set Request Url if it isn't passed as parameter |
|
189 | - if (is_null($requestUrl)) { |
|
190 | - $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); |
|
191 | - } |
|
192 | - |
|
193 | - return str_replace($this->basePath, '', strtok($requestUrl, '?')); |
|
194 | - } |
|
195 | - |
|
196 | - /** |
|
197 | - * @return RouterParser |
|
198 | - */ |
|
199 | - public function getParser() |
|
200 | - { |
|
201 | - return $this->parser; |
|
202 | - } |
|
203 | - |
|
204 | - |
|
205 | - public function getNamedRoutes() |
|
206 | - { |
|
207 | - return $this->namedRoutes; |
|
208 | - } |
|
209 | - |
|
210 | - public function getBasePath() |
|
211 | - { |
|
212 | - return $this->basePath; |
|
213 | - } |
|
214 | - |
|
215 | - public function setServer($server) |
|
216 | - { |
|
217 | - $this->server = $server; |
|
218 | - } |
|
8 | + protected $routes = []; |
|
9 | + protected $namedRoutes = []; |
|
10 | + protected $basePath = ''; |
|
11 | + protected $all = ['get', 'post']; |
|
12 | + protected $server; |
|
13 | + /** |
|
14 | + * @var RouterParser |
|
15 | + */ |
|
16 | + private $parser; |
|
17 | + |
|
18 | + /** |
|
19 | + * Create router in one call from config. |
|
20 | + * |
|
21 | + * @param RouterParser $parser |
|
22 | + * @param array $routes |
|
23 | + * @param string $basePath |
|
24 | + * @param array|null $server |
|
25 | + */ |
|
26 | + public function __construct(RouterParser $parser, $routes = [], $basePath = '', $server = null) |
|
27 | + { |
|
28 | + $this->setRoutes($routes); |
|
29 | + $this->setBasePath($basePath); |
|
30 | + $this->parser = $parser; |
|
31 | + $this->server = $server; |
|
32 | + } |
|
33 | + |
|
34 | + /** |
|
35 | + * Map a route to a target |
|
36 | + * |
|
37 | + * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE) |
|
38 | + * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id] |
|
39 | + * @param mixed $target The target where this route should point to. Can be anything. |
|
40 | + * @param string $routeName Optional name of this route. Supply if you want to reverse route this url in your application. |
|
41 | + */ |
|
42 | + public function map($method, $route, $target, $routeName = null) |
|
43 | + { |
|
44 | + if (is_string($routeName)) { |
|
45 | + $this->handleException($routeName, "Can not redeclare route '%s'", true); |
|
46 | + $this->namedRoutes[$routeName] = $route; |
|
47 | + } |
|
48 | + |
|
49 | + $this->routes[] = array($method, $route, $target, $routeName); |
|
50 | + } |
|
51 | + |
|
52 | + /** |
|
53 | + * Reversed routing |
|
54 | + * Generate the URL for a named route. Replace regexes with supplied parameters |
|
55 | + * |
|
56 | + * @param string $routeName The name of the route. |
|
57 | + * @param array $params Associative array of parameters to replace placeholders with. |
|
58 | + * |
|
59 | + * @return string The URL of the route with named parameters in place. |
|
60 | + */ |
|
61 | + public function generate($routeName, array $params = []) |
|
62 | + { |
|
63 | + $this->handleException($routeName, "Route '%s' does not exist.", false); |
|
64 | + |
|
65 | + $route = $this->namedRoutes[$routeName]; |
|
66 | + |
|
67 | + return $this->parser->generateUrl($this->basePath, $route, $params); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Match a given Request Url against stored routes |
|
72 | + * @param string $requestUrl |
|
73 | + * @param string $requestMethod |
|
74 | + * |
|
75 | + * @return array|boolean Array with route information on success, false on failure (no match). |
|
76 | + */ |
|
77 | + public function match($requestUrl = null, $requestMethod = null) |
|
78 | + { |
|
79 | + $requestUrl = $this->getRequestUrl($requestUrl); |
|
80 | + |
|
81 | + // set Request Method if it isn't passed as a parameter |
|
82 | + if (is_null($requestMethod)) { |
|
83 | + $requestMethod = $this->server['REQUEST_METHOD']; |
|
84 | + } |
|
85 | + |
|
86 | + foreach ($this->routes as $handler) { |
|
87 | + if (!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) { |
|
88 | + continue; |
|
89 | + } |
|
90 | + |
|
91 | + return array( |
|
92 | + 'target' => $handler[2], |
|
93 | + 'params' => array_filter($this->parser->getParams(), function ($k) { |
|
94 | + return !is_numeric($k); |
|
95 | + }, ARRAY_FILTER_USE_KEY), |
|
96 | + 'name' => $handler[3] |
|
97 | + ); |
|
98 | + } |
|
99 | + |
|
100 | + return false; |
|
101 | + } |
|
102 | + |
|
103 | + /** |
|
104 | + * @param $method |
|
105 | + * @param $arguments |
|
106 | + * |
|
107 | + * @throws RouterException |
|
108 | + */ |
|
109 | + public function __call($method, $arguments) |
|
110 | + { |
|
111 | + if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
|
112 | + throw new RouterException($method . ' not exist in the '. __CLASS__); |
|
113 | + } |
|
114 | + |
|
115 | + $methods = $method == 'all' ? implode('|', $this->all) : $method; |
|
116 | + |
|
117 | + $route = array_merge([$methods], $arguments); |
|
118 | + |
|
119 | + call_user_func_array([$this, 'map'], $route); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * Retrieves all routes. |
|
124 | + * Useful if you want to process or display routes. |
|
125 | + * |
|
126 | + * @return array All routes. |
|
127 | + */ |
|
128 | + public function getRoutes() |
|
129 | + { |
|
130 | + return $this->routes; |
|
131 | + } |
|
132 | + |
|
133 | + /** |
|
134 | + * Add multiple routes at once from array in the following format: |
|
135 | + * |
|
136 | + * $routes = array( |
|
137 | + * array($method, $route, $target, $name) |
|
138 | + * ); |
|
139 | + * |
|
140 | + * @param array|Iterator $routes |
|
141 | + * |
|
142 | + * @return void |
|
143 | + */ |
|
144 | + public function setRoutes($routes) |
|
145 | + { |
|
146 | + if (!is_array($routes) && !$routes instanceof Iterator) { |
|
147 | + throw new RouterException('Routes should be an array or an instance of Iterator'); |
|
148 | + } |
|
149 | + if (!empty($routes)) { |
|
150 | + foreach ($routes as $route) { |
|
151 | + call_user_func_array(array($this, 'map'), $route); |
|
152 | + } |
|
153 | + } |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Set the base path. |
|
158 | + * Useful if you are running your application from a subdirectory. |
|
159 | + * |
|
160 | + * @param $basePath |
|
161 | + */ |
|
162 | + public function setBasePath($basePath) |
|
163 | + { |
|
164 | + $this->basePath = $basePath; |
|
165 | + } |
|
166 | + |
|
167 | + /** |
|
168 | + * @param $routeName |
|
169 | + * @param $message |
|
170 | + * @param bool $cmpTo |
|
171 | + * |
|
172 | + * @throws RouterException |
|
173 | + */ |
|
174 | + private function handleException($routeName, $message, $cmpTo) |
|
175 | + { |
|
176 | + if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) { |
|
177 | + throw new RouterException(sprintf($message, $routeName)); |
|
178 | + } |
|
179 | + } |
|
180 | + |
|
181 | + /** |
|
182 | + * @param $requestUrl |
|
183 | + * |
|
184 | + * @return mixed |
|
185 | + */ |
|
186 | + private function getRequestUrl($requestUrl = null) |
|
187 | + { |
|
188 | + // set Request Url if it isn't passed as parameter |
|
189 | + if (is_null($requestUrl)) { |
|
190 | + $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); |
|
191 | + } |
|
192 | + |
|
193 | + return str_replace($this->basePath, '', strtok($requestUrl, '?')); |
|
194 | + } |
|
195 | + |
|
196 | + /** |
|
197 | + * @return RouterParser |
|
198 | + */ |
|
199 | + public function getParser() |
|
200 | + { |
|
201 | + return $this->parser; |
|
202 | + } |
|
203 | + |
|
204 | + |
|
205 | + public function getNamedRoutes() |
|
206 | + { |
|
207 | + return $this->namedRoutes; |
|
208 | + } |
|
209 | + |
|
210 | + public function getBasePath() |
|
211 | + { |
|
212 | + return $this->basePath; |
|
213 | + } |
|
214 | + |
|
215 | + public function setServer($server) |
|
216 | + { |
|
217 | + $this->server = $server; |
|
218 | + } |
|
219 | 219 | } |
@@ -172,7 +172,7 @@ |
||
172 | 172 | { |
173 | 173 | $cPointer = $nPointer; |
174 | 174 | $regex = in_array($cPointer, array('[', '(', '.')); |
175 | - if (!$regex && isset($routeString[$iPointer+1])) { |
|
175 | + if (!$regex && isset($routeString[$iPointer + 1])) { |
|
176 | 176 | $nPointer = $routeString[$iPointer + 1]; |
177 | 177 | $regex = in_array($nPointer, array('?', '+', '*', '{')); |
178 | 178 | } |
@@ -3,212 +3,212 @@ |
||
3 | 3 | |
4 | 4 | class RouterParser implements RouterParserInterface |
5 | 5 | { |
6 | - protected $params = []; |
|
7 | - protected $matchTypes = [ |
|
8 | - 'i' => '[0-9]++', |
|
9 | - 'a' => '[0-9A-Za-z]++', |
|
10 | - 'h' => '[0-9A-Fa-f]++', |
|
11 | - '*' => '.+?', |
|
12 | - '**' => '.++', |
|
13 | - '' => '[^/\.]++' |
|
14 | - ]; |
|
15 | - |
|
16 | - /** |
|
17 | - * Create router in one call from config. |
|
18 | - * |
|
19 | - * @param array $matchTypes |
|
20 | - */ |
|
21 | - public function __construct($matchTypes = []) |
|
22 | - { |
|
23 | - $this->setMatchTypes($matchTypes); |
|
24 | - } |
|
25 | - |
|
26 | - /** |
|
27 | - * Add named match types. It uses array_merge so keys can be overwritten. |
|
28 | - * |
|
29 | - * @param array $matchTypes The key is the name and the value is the regex. |
|
30 | - */ |
|
31 | - public function setMatchTypes($matchTypes) |
|
32 | - { |
|
33 | - $this->matchTypes = array_merge($this->matchTypes, $matchTypes); |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Get the url from a route name |
|
38 | - * |
|
39 | - * @param string $basePath |
|
40 | - * @param string $route |
|
41 | - * @param array $params |
|
42 | - * |
|
43 | - * @return string |
|
44 | - */ |
|
45 | - public function generateUrl($basePath, $route, array $params) |
|
46 | - { |
|
47 | - $url = $basePath . $route; |
|
48 | - |
|
49 | - if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
50 | - foreach ($matches as $match) { |
|
51 | - $pre = $match[1]; |
|
52 | - $param = $match[3]; |
|
53 | - $block = $pre ? substr($match[0], 1) : $match[0]; |
|
54 | - |
|
55 | - if (isset($params[$param])) { |
|
56 | - $url = str_replace($block, $params[$param], $url); |
|
57 | - } elseif ($match[4]) { |
|
58 | - $url = str_replace($pre . $block, '', $url); |
|
59 | - } |
|
60 | - } |
|
61 | - } |
|
62 | - |
|
63 | - return $url; |
|
64 | - } |
|
65 | - |
|
66 | - /** |
|
67 | - * @param string $method |
|
68 | - * @param string $requestMethod |
|
69 | - * @param string $routeString |
|
70 | - * @param string $requestUrl |
|
71 | - * |
|
72 | - * @return mixed |
|
73 | - */ |
|
74 | - public function methodMatch($method, $requestMethod, $routeString, $requestUrl) |
|
75 | - { |
|
76 | - $methods = explode('|', $method); |
|
77 | - |
|
78 | - if (preg_grep("/{$requestMethod}/i", $methods)) { |
|
79 | - if ($routeString == '*') { |
|
80 | - return true; |
|
81 | - } elseif (isset($routeString[0]) && $routeString[0] == '@') { |
|
82 | - return preg_match('`' . substr($routeString, 1) . '`u', $requestUrl, $this->params); |
|
83 | - } elseif (($position = strpos($routeString, '[')) === false) { |
|
84 | - return strcmp($requestUrl, $routeString) === 0; |
|
85 | - } |
|
86 | - if (strncmp($requestUrl, $routeString, $position) !== 0) { |
|
87 | - return false; |
|
88 | - } |
|
89 | - |
|
90 | - return preg_match($this->compileRoute($routeString, $requestUrl), $requestUrl, $this->params); |
|
91 | - } |
|
92 | - |
|
93 | - return false; |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Compile the regex for a given route (EXPENSIVE) |
|
98 | - * |
|
99 | - * @param $routeString |
|
100 | - * @param $requestUrl |
|
101 | - * |
|
102 | - * @return string |
|
103 | - */ |
|
104 | - private function compileRoute($routeString, $requestUrl) |
|
105 | - { |
|
106 | - $route = $this->getRoute($routeString, $requestUrl); |
|
107 | - |
|
108 | - if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
109 | - $matchTypes = $this->matchTypes; |
|
110 | - foreach ($matches as $match) { |
|
111 | - $pattern = $this->getRoutePattern($matchTypes, $match[1], $match[2], $match[3], $match[4]); |
|
112 | - $route = str_replace($match[0], $pattern, $route); |
|
113 | - } |
|
114 | - } |
|
115 | - |
|
116 | - return "`^$route$`u"; |
|
117 | - } |
|
118 | - |
|
119 | - /** |
|
120 | - * @param $matchTypes |
|
121 | - * @param $pre |
|
122 | - * @param $type |
|
123 | - * @param $param |
|
124 | - * @param $optional |
|
125 | - * |
|
126 | - * @return string |
|
127 | - */ |
|
128 | - private function getRoutePattern($matchTypes, $pre, $type, $param, $optional) |
|
129 | - { |
|
130 | - if (isset($matchTypes[$type])) { |
|
131 | - $type = $matchTypes[$type]; |
|
132 | - } |
|
133 | - if ($pre === '.') { |
|
134 | - $pre = '\.'; |
|
135 | - } |
|
136 | - |
|
137 | - //Older versions of PCRE require the 'P' in (?P<named>) |
|
138 | - return '(?:' |
|
139 | - . (!empty($pre) ? $pre : null) |
|
140 | - . '(' |
|
141 | - . (!empty($param) ? "?P<$param>" : null) |
|
142 | - . $type |
|
143 | - . '))' |
|
144 | - . (!empty($optional) ? '?' : null); |
|
145 | - } |
|
146 | - |
|
147 | - /** |
|
148 | - * @param $routeString |
|
149 | - * @param $requestUrl |
|
150 | - * |
|
151 | - * @return bool|string |
|
152 | - */ |
|
153 | - private function getRoute($routeString, $requestUrl) |
|
154 | - { |
|
155 | - $iPointer = $jPointer = 0; |
|
156 | - $nPointer = isset($routeString[0]) ? $routeString[0] : null; |
|
157 | - $regex = $route = false; |
|
158 | - |
|
159 | - // Find the longest non-regex substring and match it against the URI |
|
160 | - while (true) { |
|
161 | - if (!isset($routeString[$iPointer])) { |
|
162 | - break; |
|
163 | - } |
|
164 | - if ($regex === false) { |
|
165 | - if (!$this->isValidRouteRegex($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) { |
|
166 | - continue; |
|
167 | - } |
|
168 | - $jPointer++; |
|
169 | - } |
|
170 | - $route .= $routeString[$iPointer++]; |
|
171 | - } |
|
172 | - |
|
173 | - return $route; |
|
174 | - } |
|
175 | - |
|
176 | - /** |
|
177 | - * @param $nPointer |
|
178 | - * @param $jPointer |
|
179 | - * @param $iPointer |
|
180 | - * @param $routeString |
|
181 | - * @param $requestUrl |
|
182 | - * |
|
183 | - * @return bool |
|
184 | - */ |
|
185 | - private function isValidRouteRegex($nPointer, $jPointer, $iPointer, $routeString, $requestUrl) |
|
186 | - { |
|
187 | - $cPointer = $nPointer; |
|
188 | - $regex = in_array($cPointer, array('[', '(', '.')); |
|
189 | - if (!$regex && isset($routeString[$iPointer+1])) { |
|
190 | - $nPointer = $routeString[$iPointer + 1]; |
|
191 | - $regex = in_array($nPointer, array('?', '+', '*', '{')); |
|
192 | - } |
|
193 | - if (!$regex && $cPointer !== '/' && (!isset($requestUrl[$jPointer]) || $cPointer !== $requestUrl[$jPointer])) { |
|
194 | - return false; |
|
195 | - } |
|
196 | - return true; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * @return array |
|
201 | - */ |
|
202 | - public function getParams() |
|
203 | - { |
|
204 | - return $this->params; |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * @return array |
|
209 | - */ |
|
210 | - public function getMatchTypes() |
|
211 | - { |
|
212 | - return $this->matchTypes; |
|
213 | - } |
|
6 | + protected $params = []; |
|
7 | + protected $matchTypes = [ |
|
8 | + 'i' => '[0-9]++', |
|
9 | + 'a' => '[0-9A-Za-z]++', |
|
10 | + 'h' => '[0-9A-Fa-f]++', |
|
11 | + '*' => '.+?', |
|
12 | + '**' => '.++', |
|
13 | + '' => '[^/\.]++' |
|
14 | + ]; |
|
15 | + |
|
16 | + /** |
|
17 | + * Create router in one call from config. |
|
18 | + * |
|
19 | + * @param array $matchTypes |
|
20 | + */ |
|
21 | + public function __construct($matchTypes = []) |
|
22 | + { |
|
23 | + $this->setMatchTypes($matchTypes); |
|
24 | + } |
|
25 | + |
|
26 | + /** |
|
27 | + * Add named match types. It uses array_merge so keys can be overwritten. |
|
28 | + * |
|
29 | + * @param array $matchTypes The key is the name and the value is the regex. |
|
30 | + */ |
|
31 | + public function setMatchTypes($matchTypes) |
|
32 | + { |
|
33 | + $this->matchTypes = array_merge($this->matchTypes, $matchTypes); |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Get the url from a route name |
|
38 | + * |
|
39 | + * @param string $basePath |
|
40 | + * @param string $route |
|
41 | + * @param array $params |
|
42 | + * |
|
43 | + * @return string |
|
44 | + */ |
|
45 | + public function generateUrl($basePath, $route, array $params) |
|
46 | + { |
|
47 | + $url = $basePath . $route; |
|
48 | + |
|
49 | + if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
50 | + foreach ($matches as $match) { |
|
51 | + $pre = $match[1]; |
|
52 | + $param = $match[3]; |
|
53 | + $block = $pre ? substr($match[0], 1) : $match[0]; |
|
54 | + |
|
55 | + if (isset($params[$param])) { |
|
56 | + $url = str_replace($block, $params[$param], $url); |
|
57 | + } elseif ($match[4]) { |
|
58 | + $url = str_replace($pre . $block, '', $url); |
|
59 | + } |
|
60 | + } |
|
61 | + } |
|
62 | + |
|
63 | + return $url; |
|
64 | + } |
|
65 | + |
|
66 | + /** |
|
67 | + * @param string $method |
|
68 | + * @param string $requestMethod |
|
69 | + * @param string $routeString |
|
70 | + * @param string $requestUrl |
|
71 | + * |
|
72 | + * @return mixed |
|
73 | + */ |
|
74 | + public function methodMatch($method, $requestMethod, $routeString, $requestUrl) |
|
75 | + { |
|
76 | + $methods = explode('|', $method); |
|
77 | + |
|
78 | + if (preg_grep("/{$requestMethod}/i", $methods)) { |
|
79 | + if ($routeString == '*') { |
|
80 | + return true; |
|
81 | + } elseif (isset($routeString[0]) && $routeString[0] == '@') { |
|
82 | + return preg_match('`' . substr($routeString, 1) . '`u', $requestUrl, $this->params); |
|
83 | + } elseif (($position = strpos($routeString, '[')) === false) { |
|
84 | + return strcmp($requestUrl, $routeString) === 0; |
|
85 | + } |
|
86 | + if (strncmp($requestUrl, $routeString, $position) !== 0) { |
|
87 | + return false; |
|
88 | + } |
|
89 | + |
|
90 | + return preg_match($this->compileRoute($routeString, $requestUrl), $requestUrl, $this->params); |
|
91 | + } |
|
92 | + |
|
93 | + return false; |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Compile the regex for a given route (EXPENSIVE) |
|
98 | + * |
|
99 | + * @param $routeString |
|
100 | + * @param $requestUrl |
|
101 | + * |
|
102 | + * @return string |
|
103 | + */ |
|
104 | + private function compileRoute($routeString, $requestUrl) |
|
105 | + { |
|
106 | + $route = $this->getRoute($routeString, $requestUrl); |
|
107 | + |
|
108 | + if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
109 | + $matchTypes = $this->matchTypes; |
|
110 | + foreach ($matches as $match) { |
|
111 | + $pattern = $this->getRoutePattern($matchTypes, $match[1], $match[2], $match[3], $match[4]); |
|
112 | + $route = str_replace($match[0], $pattern, $route); |
|
113 | + } |
|
114 | + } |
|
115 | + |
|
116 | + return "`^$route$`u"; |
|
117 | + } |
|
118 | + |
|
119 | + /** |
|
120 | + * @param $matchTypes |
|
121 | + * @param $pre |
|
122 | + * @param $type |
|
123 | + * @param $param |
|
124 | + * @param $optional |
|
125 | + * |
|
126 | + * @return string |
|
127 | + */ |
|
128 | + private function getRoutePattern($matchTypes, $pre, $type, $param, $optional) |
|
129 | + { |
|
130 | + if (isset($matchTypes[$type])) { |
|
131 | + $type = $matchTypes[$type]; |
|
132 | + } |
|
133 | + if ($pre === '.') { |
|
134 | + $pre = '\.'; |
|
135 | + } |
|
136 | + |
|
137 | + //Older versions of PCRE require the 'P' in (?P<named>) |
|
138 | + return '(?:' |
|
139 | + . (!empty($pre) ? $pre : null) |
|
140 | + . '(' |
|
141 | + . (!empty($param) ? "?P<$param>" : null) |
|
142 | + . $type |
|
143 | + . '))' |
|
144 | + . (!empty($optional) ? '?' : null); |
|
145 | + } |
|
146 | + |
|
147 | + /** |
|
148 | + * @param $routeString |
|
149 | + * @param $requestUrl |
|
150 | + * |
|
151 | + * @return bool|string |
|
152 | + */ |
|
153 | + private function getRoute($routeString, $requestUrl) |
|
154 | + { |
|
155 | + $iPointer = $jPointer = 0; |
|
156 | + $nPointer = isset($routeString[0]) ? $routeString[0] : null; |
|
157 | + $regex = $route = false; |
|
158 | + |
|
159 | + // Find the longest non-regex substring and match it against the URI |
|
160 | + while (true) { |
|
161 | + if (!isset($routeString[$iPointer])) { |
|
162 | + break; |
|
163 | + } |
|
164 | + if ($regex === false) { |
|
165 | + if (!$this->isValidRouteRegex($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) { |
|
166 | + continue; |
|
167 | + } |
|
168 | + $jPointer++; |
|
169 | + } |
|
170 | + $route .= $routeString[$iPointer++]; |
|
171 | + } |
|
172 | + |
|
173 | + return $route; |
|
174 | + } |
|
175 | + |
|
176 | + /** |
|
177 | + * @param $nPointer |
|
178 | + * @param $jPointer |
|
179 | + * @param $iPointer |
|
180 | + * @param $routeString |
|
181 | + * @param $requestUrl |
|
182 | + * |
|
183 | + * @return bool |
|
184 | + */ |
|
185 | + private function isValidRouteRegex($nPointer, $jPointer, $iPointer, $routeString, $requestUrl) |
|
186 | + { |
|
187 | + $cPointer = $nPointer; |
|
188 | + $regex = in_array($cPointer, array('[', '(', '.')); |
|
189 | + if (!$regex && isset($routeString[$iPointer+1])) { |
|
190 | + $nPointer = $routeString[$iPointer + 1]; |
|
191 | + $regex = in_array($nPointer, array('?', '+', '*', '{')); |
|
192 | + } |
|
193 | + if (!$regex && $cPointer !== '/' && (!isset($requestUrl[$jPointer]) || $cPointer !== $requestUrl[$jPointer])) { |
|
194 | + return false; |
|
195 | + } |
|
196 | + return true; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * @return array |
|
201 | + */ |
|
202 | + public function getParams() |
|
203 | + { |
|
204 | + return $this->params; |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * @return array |
|
209 | + */ |
|
210 | + public function getMatchTypes() |
|
211 | + { |
|
212 | + return $this->matchTypes; |
|
213 | + } |
|
214 | 214 | } |
@@ -5,31 +5,31 @@ |
||
5 | 5 | |
6 | 6 | class SimpleTraversable implements Iterator |
7 | 7 | { |
8 | - protected $position = 0; |
|
8 | + protected $position = 0; |
|
9 | 9 | |
10 | - protected $data = array( |
|
11 | - array('GET', '/foo', 'foo_action', null), |
|
12 | - array('POST', '/bar', 'bar_action', 'second_route') |
|
13 | - ); |
|
10 | + protected $data = array( |
|
11 | + array('GET', '/foo', 'foo_action', null), |
|
12 | + array('POST', '/bar', 'bar_action', 'second_route') |
|
13 | + ); |
|
14 | 14 | |
15 | - public function current() |
|
16 | - { |
|
17 | - return $this->data[$this->position]; |
|
18 | - } |
|
19 | - public function key() |
|
20 | - { |
|
21 | - return $this->position; |
|
22 | - } |
|
23 | - public function next() |
|
24 | - { |
|
25 | - ++$this->position; |
|
26 | - } |
|
27 | - public function rewind() |
|
28 | - { |
|
29 | - $this->position = 0; |
|
30 | - } |
|
31 | - public function valid() |
|
32 | - { |
|
33 | - return isset($this->data[$this->position]); |
|
34 | - } |
|
15 | + public function current() |
|
16 | + { |
|
17 | + return $this->data[$this->position]; |
|
18 | + } |
|
19 | + public function key() |
|
20 | + { |
|
21 | + return $this->position; |
|
22 | + } |
|
23 | + public function next() |
|
24 | + { |
|
25 | + ++$this->position; |
|
26 | + } |
|
27 | + public function rewind() |
|
28 | + { |
|
29 | + $this->position = 0; |
|
30 | + } |
|
31 | + public function valid() |
|
32 | + { |
|
33 | + return isset($this->data[$this->position]); |
|
34 | + } |
|
35 | 35 | } |
@@ -3,34 +3,34 @@ |
||
3 | 3 | |
4 | 4 | interface RouterParserInterface |
5 | 5 | { |
6 | - /** |
|
7 | - * Get the route parameters |
|
8 | - * |
|
9 | - * @return array |
|
10 | - */ |
|
11 | - public function getParams(); |
|
6 | + /** |
|
7 | + * Get the route parameters |
|
8 | + * |
|
9 | + * @return array |
|
10 | + */ |
|
11 | + public function getParams(); |
|
12 | 12 | |
13 | - /** |
|
14 | - * Get the url from a route name |
|
15 | - * |
|
16 | - * @param string $basePath |
|
17 | - * @param string $route |
|
18 | - * @param array $params |
|
19 | - * |
|
20 | - * @return string |
|
21 | - */ |
|
22 | - public function generateUrl($basePath, $route, array $params); |
|
13 | + /** |
|
14 | + * Get the url from a route name |
|
15 | + * |
|
16 | + * @param string $basePath |
|
17 | + * @param string $route |
|
18 | + * @param array $params |
|
19 | + * |
|
20 | + * @return string |
|
21 | + */ |
|
22 | + public function generateUrl($basePath, $route, array $params); |
|
23 | 23 | |
24 | - /** |
|
25 | - * Check if the request method match the route methods |
|
26 | - * Update the $params |
|
27 | - * |
|
28 | - * @param string $method |
|
29 | - * @param string $requestMethod |
|
30 | - * @param string $routeString |
|
31 | - * @param string $requestUrl |
|
32 | - * |
|
33 | - * @return mixed |
|
34 | - */ |
|
35 | - public function methodMatch($method, $requestMethod, $routeString, $requestUrl); |
|
24 | + /** |
|
25 | + * Check if the request method match the route methods |
|
26 | + * Update the $params |
|
27 | + * |
|
28 | + * @param string $method |
|
29 | + * @param string $requestMethod |
|
30 | + * @param string $routeString |
|
31 | + * @param string $requestUrl |
|
32 | + * |
|
33 | + * @return mixed |
|
34 | + */ |
|
35 | + public function methodMatch($method, $requestMethod, $routeString, $requestUrl); |
|
36 | 36 | } |
@@ -29,7 +29,7 @@ |
||
29 | 29 | $this->router = new Router($parser, [], '', []); |
30 | 30 | $this->param1 = ['controller' => 'test', 'action' => 'someaction']; |
31 | 31 | $this->param2 = ['controller' => 'test', 'action' => 'someaction', 'type' => 'json']; |
32 | - $this->closure = function () { |
|
32 | + $this->closure = function() { |
|
33 | 33 | }; |
34 | 34 | } |
35 | 35 |
@@ -11,330 +11,330 @@ |
||
11 | 11 | |
12 | 12 | class RouterTest extends PHPUnit_Framework_TestCase |
13 | 13 | { |
14 | - /** |
|
15 | - * @var Router |
|
16 | - */ |
|
17 | - protected $router; |
|
18 | - protected $closure; |
|
19 | - protected $param1; |
|
20 | - protected $param2; |
|
14 | + /** |
|
15 | + * @var Router |
|
16 | + */ |
|
17 | + protected $router; |
|
18 | + protected $closure; |
|
19 | + protected $param1; |
|
20 | + protected $param2; |
|
21 | 21 | |
22 | - /** |
|
23 | - * Sets up the fixture, for example, opens a network connection. |
|
24 | - * This method is called before a test is executed. |
|
25 | - */ |
|
26 | - protected function setUp() |
|
27 | - { |
|
28 | - $parser = new RouterParser(); |
|
29 | - $this->router = new Router($parser, [], '', []); |
|
30 | - $this->param1 = ['controller' => 'test', 'action' => 'someaction']; |
|
31 | - $this->param2 = ['controller' => 'test', 'action' => 'someaction', 'type' => 'json']; |
|
32 | - $this->closure = function () { |
|
33 | - }; |
|
34 | - } |
|
22 | + /** |
|
23 | + * Sets up the fixture, for example, opens a network connection. |
|
24 | + * This method is called before a test is executed. |
|
25 | + */ |
|
26 | + protected function setUp() |
|
27 | + { |
|
28 | + $parser = new RouterParser(); |
|
29 | + $this->router = new Router($parser, [], '', []); |
|
30 | + $this->param1 = ['controller' => 'test', 'action' => 'someaction']; |
|
31 | + $this->param2 = ['controller' => 'test', 'action' => 'someaction', 'type' => 'json']; |
|
32 | + $this->closure = function () { |
|
33 | + }; |
|
34 | + } |
|
35 | 35 | |
36 | - /** |
|
37 | - * @covers Router::getRoutes |
|
38 | - */ |
|
39 | - public function testMapAndGetRoutes() |
|
40 | - { |
|
41 | - $route = ['POST', '/[:controller]/[:action]', $this->closure, null]; |
|
36 | + /** |
|
37 | + * @covers Router::getRoutes |
|
38 | + */ |
|
39 | + public function testMapAndGetRoutes() |
|
40 | + { |
|
41 | + $route = ['POST', '/[:controller]/[:action]', $this->closure, null]; |
|
42 | 42 | |
43 | - call_user_func_array([$this->router, 'map'], $route); |
|
43 | + call_user_func_array([$this->router, 'map'], $route); |
|
44 | 44 | |
45 | - $routes = $this->router->getRoutes(); |
|
45 | + $routes = $this->router->getRoutes(); |
|
46 | 46 | |
47 | - $this->assertInternalType('array', $routes); |
|
48 | - $this->assertEquals([$route], $routes); |
|
49 | - } |
|
47 | + $this->assertInternalType('array', $routes); |
|
48 | + $this->assertEquals([$route], $routes); |
|
49 | + } |
|
50 | 50 | |
51 | - /** |
|
52 | - * @covers Router::setRoutes |
|
53 | - */ |
|
54 | - public function testAddRoutesWithArrayArgument() |
|
55 | - { |
|
56 | - $route1 = ['POST', '/[:controller]/[:action]', $this->closure, null]; |
|
57 | - $route2 = ['POST', '/[:controller]/[:action]', $this->closure, 'second_route']; |
|
51 | + /** |
|
52 | + * @covers Router::setRoutes |
|
53 | + */ |
|
54 | + public function testAddRoutesWithArrayArgument() |
|
55 | + { |
|
56 | + $route1 = ['POST', '/[:controller]/[:action]', $this->closure, null]; |
|
57 | + $route2 = ['POST', '/[:controller]/[:action]', $this->closure, 'second_route']; |
|
58 | 58 | |
59 | - $this->router->setRoutes([$route1, $route2]); |
|
59 | + $this->router->setRoutes([$route1, $route2]); |
|
60 | 60 | |
61 | - $routes = $this->router->getRoutes(); |
|
61 | + $routes = $this->router->getRoutes(); |
|
62 | 62 | |
63 | - $this->assertEquals($route1, $routes[0]); |
|
64 | - $this->assertEquals($route2, $routes[1]); |
|
65 | - } |
|
63 | + $this->assertEquals($route1, $routes[0]); |
|
64 | + $this->assertEquals($route2, $routes[1]); |
|
65 | + } |
|
66 | 66 | |
67 | - /** |
|
68 | - * @covers Router::setRoutes |
|
69 | - */ |
|
70 | - public function testAddRoutesWithTraversableArgument() |
|
71 | - { |
|
72 | - $traversable = new SimpleTraversable(); |
|
73 | - $this->router->setRoutes($traversable); |
|
67 | + /** |
|
68 | + * @covers Router::setRoutes |
|
69 | + */ |
|
70 | + public function testAddRoutesWithTraversableArgument() |
|
71 | + { |
|
72 | + $traversable = new SimpleTraversable(); |
|
73 | + $this->router->setRoutes($traversable); |
|
74 | 74 | |
75 | - $traversable->rewind(); |
|
75 | + $traversable->rewind(); |
|
76 | 76 | |
77 | - $first = $traversable->current(); |
|
78 | - $traversable->next(); |
|
79 | - $second = $traversable->current(); |
|
77 | + $first = $traversable->current(); |
|
78 | + $traversable->next(); |
|
79 | + $second = $traversable->current(); |
|
80 | 80 | |
81 | - $routes = $this->router->getRoutes(); |
|
81 | + $routes = $this->router->getRoutes(); |
|
82 | 82 | |
83 | - $this->assertEquals($first, $routes[0]); |
|
84 | - $this->assertEquals($second, $routes[1]); |
|
85 | - } |
|
83 | + $this->assertEquals($first, $routes[0]); |
|
84 | + $this->assertEquals($second, $routes[1]); |
|
85 | + } |
|
86 | 86 | |
87 | - /** |
|
88 | - * @covers Router::setRoutes |
|
89 | - */ |
|
90 | - public function testAddRoutesWidthInvalidArgument() |
|
91 | - { |
|
92 | - try { |
|
93 | - $this->router->setRoutes(new stdClass); |
|
94 | - } catch (RouterException $e) { |
|
95 | - $this->assertEquals('Routes should be an array or an instance of Iterator', $e->getMessage()); |
|
96 | - } |
|
97 | - } |
|
87 | + /** |
|
88 | + * @covers Router::setRoutes |
|
89 | + */ |
|
90 | + public function testAddRoutesWidthInvalidArgument() |
|
91 | + { |
|
92 | + try { |
|
93 | + $this->router->setRoutes(new stdClass); |
|
94 | + } catch (RouterException $e) { |
|
95 | + $this->assertEquals('Routes should be an array or an instance of Iterator', $e->getMessage()); |
|
96 | + } |
|
97 | + } |
|
98 | 98 | |
99 | - /** |
|
100 | - * @covers Router::setBasePath |
|
101 | - */ |
|
102 | - public function testSetBasePath() |
|
103 | - { |
|
104 | - $this->router->setBasePath('/some/path'); |
|
105 | - $this->assertEquals('/some/path', $this->router->getBasePath()); |
|
106 | - } |
|
99 | + /** |
|
100 | + * @covers Router::setBasePath |
|
101 | + */ |
|
102 | + public function testSetBasePath() |
|
103 | + { |
|
104 | + $this->router->setBasePath('/some/path'); |
|
105 | + $this->assertEquals('/some/path', $this->router->getBasePath()); |
|
106 | + } |
|
107 | 107 | |
108 | - /** |
|
109 | - * @covers Router::map |
|
110 | - */ |
|
111 | - public function testMapWithName() |
|
112 | - { |
|
113 | - $route = ['POST', '/[:controller]/[:action]', $this->closure, 'my_route']; |
|
108 | + /** |
|
109 | + * @covers Router::map |
|
110 | + */ |
|
111 | + public function testMapWithName() |
|
112 | + { |
|
113 | + $route = ['POST', '/[:controller]/[:action]', $this->closure, 'my_route']; |
|
114 | 114 | |
115 | - call_user_func_array([$this->router, 'map'], $route); |
|
115 | + call_user_func_array([$this->router, 'map'], $route); |
|
116 | 116 | |
117 | - $routes = $this->router->getRoutes(); |
|
118 | - $namedRoutes = $this->router->getNamedRoutes(); |
|
117 | + $routes = $this->router->getRoutes(); |
|
118 | + $namedRoutes = $this->router->getNamedRoutes(); |
|
119 | 119 | |
120 | - $this->assertInternalType('array', $routes); |
|
121 | - $this->assertEquals($route, $routes[0]); |
|
120 | + $this->assertInternalType('array', $routes); |
|
121 | + $this->assertEquals($route, $routes[0]); |
|
122 | 122 | |
123 | - $this->assertInternalType('array', $namedRoutes); |
|
124 | - $this->assertEquals('/[:controller]/[:action]', $namedRoutes['my_route']); |
|
125 | - } |
|
123 | + $this->assertInternalType('array', $namedRoutes); |
|
124 | + $this->assertEquals('/[:controller]/[:action]', $namedRoutes['my_route']); |
|
125 | + } |
|
126 | 126 | |
127 | - /** |
|
128 | - * @covers Router::map |
|
129 | - */ |
|
130 | - public function testMapWithDuplicatedRouteName() |
|
131 | - { |
|
132 | - try { |
|
133 | - $route = ['POST', '/[:controller]/[:action]', $this->closure, 'my_route']; |
|
134 | - call_user_func_array([$this->router, 'map'], $route); |
|
135 | - call_user_func_array([$this->router, 'map'], $route); |
|
136 | - } catch (Exception $e) { |
|
137 | - $this->assertEquals("Can not redeclare route 'my_route'", $e->getMessage()); |
|
138 | - } |
|
139 | - } |
|
127 | + /** |
|
128 | + * @covers Router::map |
|
129 | + */ |
|
130 | + public function testMapWithDuplicatedRouteName() |
|
131 | + { |
|
132 | + try { |
|
133 | + $route = ['POST', '/[:controller]/[:action]', $this->closure, 'my_route']; |
|
134 | + call_user_func_array([$this->router, 'map'], $route); |
|
135 | + call_user_func_array([$this->router, 'map'], $route); |
|
136 | + } catch (Exception $e) { |
|
137 | + $this->assertEquals("Can not redeclare route 'my_route'", $e->getMessage()); |
|
138 | + } |
|
139 | + } |
|
140 | 140 | |
141 | - /** |
|
142 | - * @covers Router::generate |
|
143 | - */ |
|
144 | - public function testGenerate() |
|
145 | - { |
|
146 | - $this->router->map('GET', '/[:controller]/[:action]', $this->closure, 'foo_route'); |
|
141 | + /** |
|
142 | + * @covers Router::generate |
|
143 | + */ |
|
144 | + public function testGenerate() |
|
145 | + { |
|
146 | + $this->router->map('GET', '/[:controller]/[:action]', $this->closure, 'foo_route'); |
|
147 | 147 | |
148 | - $this->assertEquals('/test/someaction', $this->router->generate('foo_route', $this->param1)); |
|
149 | - $this->assertEquals('/test/someaction', $this->router->generate('foo_route', $this->param2)); |
|
150 | - } |
|
148 | + $this->assertEquals('/test/someaction', $this->router->generate('foo_route', $this->param1)); |
|
149 | + $this->assertEquals('/test/someaction', $this->router->generate('foo_route', $this->param2)); |
|
150 | + } |
|
151 | 151 | |
152 | - public function testGenerateWithOptionalUrlParts() |
|
153 | - { |
|
154 | - $this->router->map('GET', '/[:controller]/[:action].[:type]?', $this->closure, 'bar_route'); |
|
152 | + public function testGenerateWithOptionalUrlParts() |
|
153 | + { |
|
154 | + $this->router->map('GET', '/[:controller]/[:action].[:type]?', $this->closure, 'bar_route'); |
|
155 | 155 | |
156 | - $this->assertEquals('/test/someaction', $this->router->generate('bar_route', $this->param1)); |
|
157 | - $this->assertEquals('/test/someaction.json', $this->router->generate('bar_route', $this->param2)); |
|
158 | - } |
|
156 | + $this->assertEquals('/test/someaction', $this->router->generate('bar_route', $this->param1)); |
|
157 | + $this->assertEquals('/test/someaction.json', $this->router->generate('bar_route', $this->param2)); |
|
158 | + } |
|
159 | 159 | |
160 | - public function testGenerateWithNonExistingRoute() |
|
161 | - { |
|
162 | - try { |
|
163 | - $this->router->generate('non_existing_route'); |
|
164 | - } catch (Exception $e) { |
|
165 | - $this->assertEquals("Route 'non_existing_route' does not exist.", $e->getMessage()); |
|
166 | - } |
|
167 | - } |
|
160 | + public function testGenerateWithNonExistingRoute() |
|
161 | + { |
|
162 | + try { |
|
163 | + $this->router->generate('non_existing_route'); |
|
164 | + } catch (Exception $e) { |
|
165 | + $this->assertEquals("Route 'non_existing_route' does not exist.", $e->getMessage()); |
|
166 | + } |
|
167 | + } |
|
168 | 168 | |
169 | - /** |
|
170 | - * @covers Router::match |
|
171 | - * @covers Router::compileRoute |
|
172 | - */ |
|
173 | - public function testMatch() |
|
174 | - { |
|
175 | - $params = [ |
|
176 | - 'target' => 'foo_action', |
|
177 | - 'params' => ['controller' => 'test', 'action' => 'do'], |
|
178 | - 'name' => 'foo_route' |
|
179 | - ]; |
|
169 | + /** |
|
170 | + * @covers Router::match |
|
171 | + * @covers Router::compileRoute |
|
172 | + */ |
|
173 | + public function testMatch() |
|
174 | + { |
|
175 | + $params = [ |
|
176 | + 'target' => 'foo_action', |
|
177 | + 'params' => ['controller' => 'test', 'action' => 'do'], |
|
178 | + 'name' => 'foo_route' |
|
179 | + ]; |
|
180 | 180 | |
181 | - $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
181 | + $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
182 | 182 | |
183 | - $this->assertEquals($params, $this->router->match('/foo/test/do', 'GET')); |
|
184 | - $this->assertSame(false, $this->router->match('/foo/test/do', 'POST')); |
|
185 | - $this->assertEquals($params, $this->router->match('/foo/test/do?param=value', 'GET')); |
|
186 | - } |
|
183 | + $this->assertEquals($params, $this->router->match('/foo/test/do', 'GET')); |
|
184 | + $this->assertSame(false, $this->router->match('/foo/test/do', 'POST')); |
|
185 | + $this->assertEquals($params, $this->router->match('/foo/test/do?param=value', 'GET')); |
|
186 | + } |
|
187 | 187 | |
188 | - public function testMatchWithFixedParamValues() |
|
189 | - { |
|
190 | - $params = [ |
|
191 | - 'target' => 'usersController#doAction', |
|
192 | - 'params' => ['id' => 1, 'action' => 'delete'], |
|
193 | - 'name' => 'users_do' |
|
194 | - ]; |
|
188 | + public function testMatchWithFixedParamValues() |
|
189 | + { |
|
190 | + $params = [ |
|
191 | + 'target' => 'usersController#doAction', |
|
192 | + 'params' => ['id' => 1, 'action' => 'delete'], |
|
193 | + 'name' => 'users_do' |
|
194 | + ]; |
|
195 | 195 | |
196 | - $this->router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do'); |
|
196 | + $this->router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do'); |
|
197 | 197 | |
198 | - $this->assertEquals($params, $this->router->match('/users/1/delete', 'POST')); |
|
199 | - $this->assertFalse($this->router->match('/users/1/delete', 'GET')); |
|
200 | - $this->assertFalse($this->router->match('/users/abc/delete', 'POST')); |
|
201 | - $this->assertFalse($this->router->match('/users/1/create', 'GET')); |
|
202 | - } |
|
198 | + $this->assertEquals($params, $this->router->match('/users/1/delete', 'POST')); |
|
199 | + $this->assertFalse($this->router->match('/users/1/delete', 'GET')); |
|
200 | + $this->assertFalse($this->router->match('/users/abc/delete', 'POST')); |
|
201 | + $this->assertFalse($this->router->match('/users/1/create', 'GET')); |
|
202 | + } |
|
203 | 203 | |
204 | - public function testMatchWithServerVars() |
|
205 | - { |
|
206 | - $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
204 | + public function testMatchWithServerVars() |
|
205 | + { |
|
206 | + $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
207 | 207 | |
208 | - $this->router->setServer([ |
|
209 | - 'REQUEST_URI' => '/foo/test/do', |
|
210 | - 'REQUEST_METHOD' => 'GET' |
|
211 | - ]); |
|
208 | + $this->router->setServer([ |
|
209 | + 'REQUEST_URI' => '/foo/test/do', |
|
210 | + 'REQUEST_METHOD' => 'GET' |
|
211 | + ]); |
|
212 | 212 | |
213 | - $this->assertEquals(array( |
|
214 | - 'target' => 'foo_action', |
|
215 | - 'params' => array( |
|
216 | - 'controller' => 'test', |
|
217 | - 'action' => 'do' |
|
218 | - ), |
|
219 | - 'name' => 'foo_route' |
|
220 | - ), $this->router->match()); |
|
221 | - } |
|
213 | + $this->assertEquals(array( |
|
214 | + 'target' => 'foo_action', |
|
215 | + 'params' => array( |
|
216 | + 'controller' => 'test', |
|
217 | + 'action' => 'do' |
|
218 | + ), |
|
219 | + 'name' => 'foo_route' |
|
220 | + ), $this->router->match()); |
|
221 | + } |
|
222 | 222 | |
223 | - public function testMatchWithOptionalUrlParts() |
|
224 | - { |
|
225 | - $this->router->map('GET', '/bar/[:controller]/[:action].[:type]?', 'bar_action', 'bar_route'); |
|
223 | + public function testMatchWithOptionalUrlParts() |
|
224 | + { |
|
225 | + $this->router->map('GET', '/bar/[:controller]/[:action].[:type]?', 'bar_action', 'bar_route'); |
|
226 | 226 | |
227 | - $this->assertEquals(array( |
|
228 | - 'target' => 'bar_action', |
|
229 | - 'params' => array( |
|
230 | - 'controller' => 'test', |
|
231 | - 'action' => 'do', |
|
232 | - 'type' => 'json' |
|
233 | - ), |
|
234 | - 'name' => 'bar_route' |
|
235 | - ), $this->router->match('/bar/test/do.json', 'GET')); |
|
236 | - } |
|
227 | + $this->assertEquals(array( |
|
228 | + 'target' => 'bar_action', |
|
229 | + 'params' => array( |
|
230 | + 'controller' => 'test', |
|
231 | + 'action' => 'do', |
|
232 | + 'type' => 'json' |
|
233 | + ), |
|
234 | + 'name' => 'bar_route' |
|
235 | + ), $this->router->match('/bar/test/do.json', 'GET')); |
|
236 | + } |
|
237 | 237 | |
238 | - public function testMatchWithWildcard() |
|
239 | - { |
|
240 | - $this->router->map('GET', '/a', 'foo_action', 'foo_route'); |
|
241 | - $this->router->map('GET', '*', 'bar_action', 'bar_route'); |
|
238 | + public function testMatchWithWildcard() |
|
239 | + { |
|
240 | + $this->router->map('GET', '/a', 'foo_action', 'foo_route'); |
|
241 | + $this->router->map('GET', '*', 'bar_action', 'bar_route'); |
|
242 | 242 | |
243 | - $this->assertEquals(array( |
|
244 | - 'target' => 'bar_action', |
|
245 | - 'params' => array(), |
|
246 | - 'name' => 'bar_route' |
|
247 | - ), $this->router->match('/everything', 'GET')); |
|
248 | - } |
|
243 | + $this->assertEquals(array( |
|
244 | + 'target' => 'bar_action', |
|
245 | + 'params' => array(), |
|
246 | + 'name' => 'bar_route' |
|
247 | + ), $this->router->match('/everything', 'GET')); |
|
248 | + } |
|
249 | 249 | |
250 | - public function testMatchWithCustomRegexp() |
|
251 | - { |
|
252 | - $this->router->map('GET', '@^/[a-z]*$', 'bar_action', 'bar_route'); |
|
250 | + public function testMatchWithCustomRegexp() |
|
251 | + { |
|
252 | + $this->router->map('GET', '@^/[a-z]*$', 'bar_action', 'bar_route'); |
|
253 | 253 | |
254 | - $this->assertEquals(array( |
|
255 | - 'target' => 'bar_action', |
|
256 | - 'params' => array(), |
|
257 | - 'name' => 'bar_route' |
|
258 | - ), $this->router->match('/everything', 'GET')); |
|
254 | + $this->assertEquals(array( |
|
255 | + 'target' => 'bar_action', |
|
256 | + 'params' => array(), |
|
257 | + 'name' => 'bar_route' |
|
258 | + ), $this->router->match('/everything', 'GET')); |
|
259 | 259 | |
260 | - $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
261 | - } |
|
260 | + $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
261 | + } |
|
262 | 262 | |
263 | - public function testMatchWithUnicodeRegex() |
|
264 | - { |
|
265 | - $pattern = '/(?<path>[^'; |
|
266 | - // Arabic characters |
|
267 | - $pattern .= '\x{0600}-\x{06FF}'; |
|
268 | - $pattern .= '\x{FB50}-\x{FDFD}'; |
|
269 | - $pattern .= '\x{FE70}-\x{FEFF}'; |
|
270 | - $pattern .= '\x{0750}-\x{077F}'; |
|
271 | - // Alphanumeric, /, _, - and space characters |
|
272 | - $pattern .= 'a-zA-Z0-9\/_\-\s'; |
|
273 | - // 'ZERO WIDTH NON-JOINER' |
|
274 | - $pattern .= '\x{200C}'; |
|
275 | - $pattern .= ']+)'; |
|
263 | + public function testMatchWithUnicodeRegex() |
|
264 | + { |
|
265 | + $pattern = '/(?<path>[^'; |
|
266 | + // Arabic characters |
|
267 | + $pattern .= '\x{0600}-\x{06FF}'; |
|
268 | + $pattern .= '\x{FB50}-\x{FDFD}'; |
|
269 | + $pattern .= '\x{FE70}-\x{FEFF}'; |
|
270 | + $pattern .= '\x{0750}-\x{077F}'; |
|
271 | + // Alphanumeric, /, _, - and space characters |
|
272 | + $pattern .= 'a-zA-Z0-9\/_\-\s'; |
|
273 | + // 'ZERO WIDTH NON-JOINER' |
|
274 | + $pattern .= '\x{200C}'; |
|
275 | + $pattern .= ']+)'; |
|
276 | 276 | |
277 | - $this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route'); |
|
277 | + $this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route'); |
|
278 | 278 | |
279 | - $this->assertEquals(array( |
|
280 | - 'target' => 'unicode_action', |
|
281 | - 'name' => 'unicode_route', |
|
282 | - 'params' => array( |
|
283 | - 'path' => '大家好' |
|
284 | - ) |
|
285 | - ), $this->router->match('/大家好', 'GET')); |
|
279 | + $this->assertEquals(array( |
|
280 | + 'target' => 'unicode_action', |
|
281 | + 'name' => 'unicode_route', |
|
282 | + 'params' => array( |
|
283 | + 'path' => '大家好' |
|
284 | + ) |
|
285 | + ), $this->router->match('/大家好', 'GET')); |
|
286 | 286 | |
287 | - $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
288 | - } |
|
287 | + $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
288 | + } |
|
289 | 289 | |
290 | - /** |
|
291 | - * @covers Router::setMatchTypes |
|
292 | - */ |
|
293 | - public function testMatchWithCustomNamedRegex() |
|
294 | - { |
|
295 | - $this->router->getParser()->setMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?')); |
|
296 | - $this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route'); |
|
290 | + /** |
|
291 | + * @covers Router::setMatchTypes |
|
292 | + */ |
|
293 | + public function testMatchWithCustomNamedRegex() |
|
294 | + { |
|
295 | + $this->router->getParser()->setMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?')); |
|
296 | + $this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route'); |
|
297 | 297 | |
298 | - $this->assertEquals(array( |
|
299 | - 'target' => 'bar_action', |
|
300 | - 'params' => array( |
|
301 | - 'customId' => 'AB1', |
|
302 | - ), |
|
303 | - 'name' => 'bar_route' |
|
304 | - ), $this->router->match('/bar/AB1', 'GET')); |
|
298 | + $this->assertEquals(array( |
|
299 | + 'target' => 'bar_action', |
|
300 | + 'params' => array( |
|
301 | + 'customId' => 'AB1', |
|
302 | + ), |
|
303 | + 'name' => 'bar_route' |
|
304 | + ), $this->router->match('/bar/AB1', 'GET')); |
|
305 | 305 | |
306 | - $this->assertEquals(array( |
|
307 | - 'target' => 'bar_action', |
|
308 | - 'params' => array( |
|
309 | - 'customId' => 'AB1_0123456789', |
|
310 | - ), |
|
311 | - 'name' => 'bar_route' |
|
312 | - ), $this->router->match('/bar/AB1_0123456789', 'GET')); |
|
306 | + $this->assertEquals(array( |
|
307 | + 'target' => 'bar_action', |
|
308 | + 'params' => array( |
|
309 | + 'customId' => 'AB1_0123456789', |
|
310 | + ), |
|
311 | + 'name' => 'bar_route' |
|
312 | + ), $this->router->match('/bar/AB1_0123456789', 'GET')); |
|
313 | 313 | |
314 | - $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
315 | - } |
|
314 | + $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
315 | + } |
|
316 | 316 | |
317 | - public function testMatchWithCustomNamedUnicodeRegex() |
|
318 | - { |
|
319 | - $pattern = '[^'; |
|
320 | - // Arabic characters |
|
321 | - $pattern .= '\x{0600}-\x{06FF}'; |
|
322 | - $pattern .= '\x{FB50}-\x{FDFD}'; |
|
323 | - $pattern .= '\x{FE70}-\x{FEFF}'; |
|
324 | - $pattern .= '\x{0750}-\x{077F}'; |
|
325 | - $pattern .= ']+'; |
|
317 | + public function testMatchWithCustomNamedUnicodeRegex() |
|
318 | + { |
|
319 | + $pattern = '[^'; |
|
320 | + // Arabic characters |
|
321 | + $pattern .= '\x{0600}-\x{06FF}'; |
|
322 | + $pattern .= '\x{FB50}-\x{FDFD}'; |
|
323 | + $pattern .= '\x{FE70}-\x{FEFF}'; |
|
324 | + $pattern .= '\x{0750}-\x{077F}'; |
|
325 | + $pattern .= ']+'; |
|
326 | 326 | |
327 | - $this->router->getParser()->setMatchTypes(array('nonArabic' => $pattern)); |
|
328 | - $this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route'); |
|
327 | + $this->router->getParser()->setMatchTypes(array('nonArabic' => $pattern)); |
|
328 | + $this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route'); |
|
329 | 329 | |
330 | - $this->assertEquals(array( |
|
331 | - 'target' => 'non_arabic_action', |
|
332 | - 'name' => 'non_arabic_route', |
|
333 | - 'params' => array( |
|
334 | - 'string' => 'some-path' |
|
335 | - ) |
|
336 | - ), $this->router->match('/bar/some-path', 'GET')); |
|
330 | + $this->assertEquals(array( |
|
331 | + 'target' => 'non_arabic_action', |
|
332 | + 'name' => 'non_arabic_route', |
|
333 | + 'params' => array( |
|
334 | + 'string' => 'some-path' |
|
335 | + ) |
|
336 | + ), $this->router->match('/bar/some-path', 'GET')); |
|
337 | 337 | |
338 | - $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
339 | - } |
|
338 | + $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
339 | + } |
|
340 | 340 | } |