@@ -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 { |
@@ -6,199 +6,199 @@ |
||
| 6 | 6 | |
| 7 | 7 | class Router |
| 8 | 8 | { |
| 9 | - protected $routes = []; |
|
| 10 | - protected $namedRoutes = []; |
|
| 11 | - protected $basePath = ''; |
|
| 12 | - protected $all = ['get', 'post']; |
|
| 13 | - protected $server; |
|
| 14 | - /** |
|
| 15 | - * @var RouterParser |
|
| 16 | - */ |
|
| 17 | - private $parser; |
|
| 18 | - |
|
| 19 | - /** |
|
| 20 | - * Create router in one call from config. |
|
| 21 | - * |
|
| 22 | - * @param RouterParser $parser |
|
| 23 | - * @param array $routes |
|
| 24 | - * @param string $basePath |
|
| 25 | - * @param array|null $server |
|
| 26 | - */ |
|
| 27 | - public function __construct(RouterParser $parser, $routes = [], $basePath = '', $server = null) |
|
| 28 | - { |
|
| 29 | - $this->setRoutes($routes); |
|
| 30 | - $this->setBasePath($basePath); |
|
| 31 | - $this->parser = $parser; |
|
| 32 | - $this->server = $server; |
|
| 33 | - } |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * Map a route to a target |
|
| 37 | - * |
|
| 38 | - * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE) |
|
| 39 | - * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id] |
|
| 40 | - * @param mixed $target The target where this route should point to. Can be anything. |
|
| 41 | - * @param string $routeName Optional name of this route. Supply if you want to reverse route this url in your application. |
|
| 42 | - */ |
|
| 43 | - public function map($method, $route, $target, $routeName = null) |
|
| 44 | - { |
|
| 45 | - if (is_string($routeName)) { |
|
| 46 | - $this->handleException($routeName, "Can not redeclare route '%s'", true); |
|
| 47 | - $this->namedRoutes[$routeName] = $route; |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - $this->routes[] = array($method, $route, $target, $routeName); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Reversed routing |
|
| 55 | - * Generate the URL for a named route. Replace regexes with supplied parameters |
|
| 56 | - * |
|
| 57 | - * @param string $routeName The name of the route. |
|
| 58 | - * @param array $params Associative array of parameters to replace placeholders with. |
|
| 59 | - * |
|
| 60 | - * @return string The URL of the route with named parameters in place. |
|
| 61 | - */ |
|
| 62 | - public function generate($routeName, array $params = []) |
|
| 63 | - { |
|
| 64 | - $this->handleException($routeName, "Route '%s' does not exist.", false); |
|
| 65 | - |
|
| 66 | - $route = $this->namedRoutes[$routeName]; |
|
| 67 | - |
|
| 68 | - return $this->parser->generate($this->basePath, $route, $params); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * Match a given Request Url against stored routes |
|
| 73 | - * @param string $requestUrl |
|
| 74 | - * @param string $requestMethod |
|
| 75 | - * |
|
| 76 | - * @return array|boolean Array with route information on success, false on failure (no match). |
|
| 77 | - */ |
|
| 78 | - public function match($requestUrl = null, $requestMethod = null) |
|
| 79 | - { |
|
| 80 | - $requestUrl = $this->getRequestUrl($requestUrl); |
|
| 81 | - |
|
| 82 | - // set Request Method if it isn't passed as a parameter |
|
| 83 | - if (is_null($requestMethod)) { |
|
| 84 | - $requestMethod = $this->server['REQUEST_METHOD']; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - foreach ($this->routes as $handler) { |
|
| 88 | - if (!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) { |
|
| 89 | - continue; |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - return array( |
|
| 93 | - 'target' => $handler[2], |
|
| 94 | - 'params' => array_filter($this->parser->getParams(), function ($k) { |
|
| 95 | - return !is_numeric($k); |
|
| 96 | - }, ARRAY_FILTER_USE_KEY), |
|
| 97 | - 'name' => $handler[3] |
|
| 98 | - ); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return false; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * @param $method |
|
| 106 | - * @param $arguments |
|
| 107 | - * |
|
| 108 | - * @throws RouterException |
|
| 109 | - */ |
|
| 110 | - public function __call($method, $arguments) |
|
| 111 | - { |
|
| 112 | - if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
|
| 113 | - throw new RouterException($method . ' not exist in the '. __CLASS__); |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - $methods = $method == 'all' ? implode('|', $this->all) : $method; |
|
| 117 | - |
|
| 118 | - $route = array_merge([$methods], $arguments); |
|
| 119 | - |
|
| 120 | - call_user_func_array([$this, 'map'], $route); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Retrieves all routes. |
|
| 125 | - * Useful if you want to process or display routes. |
|
| 126 | - * |
|
| 127 | - * @return array All routes. |
|
| 128 | - */ |
|
| 129 | - public function getRoutes() |
|
| 130 | - { |
|
| 131 | - return $this->routes; |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Add multiple routes at once from array in the following format: |
|
| 136 | - * |
|
| 137 | - * $routes = array( |
|
| 138 | - * array($method, $route, $target, $name) |
|
| 139 | - * ); |
|
| 140 | - * |
|
| 141 | - * @param array|Traversable $routes |
|
| 142 | - * |
|
| 143 | - * @return void |
|
| 144 | - */ |
|
| 145 | - public function setRoutes($routes) |
|
| 146 | - { |
|
| 147 | - if (!is_array($routes) && !$routes instanceof Traversable) { |
|
| 148 | - throw new RouterException('Routes should be an array or an instance of Traversable'); |
|
| 149 | - } |
|
| 150 | - if (!empty($routes)) { |
|
| 151 | - foreach ($routes as $route) { |
|
| 152 | - call_user_func_array(array($this, 'map'), $route); |
|
| 153 | - } |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Set the base path. |
|
| 159 | - * Useful if you are running your application from a subdirectory. |
|
| 160 | - * |
|
| 161 | - * @param $basePath |
|
| 162 | - */ |
|
| 163 | - public function setBasePath($basePath) |
|
| 164 | - { |
|
| 165 | - $this->basePath = $basePath; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * @param $routeName |
|
| 170 | - * @param $message |
|
| 171 | - * @param bool $cmpTo |
|
| 172 | - * |
|
| 173 | - * @throws RouterException |
|
| 174 | - */ |
|
| 175 | - private function handleException($routeName, $message, $cmpTo) |
|
| 176 | - { |
|
| 177 | - if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) { |
|
| 178 | - throw new RouterException(sprintf($message, $routeName)); |
|
| 179 | - } |
|
| 180 | - } |
|
| 181 | - |
|
| 182 | - /** |
|
| 183 | - * @param $requestUrl |
|
| 184 | - * |
|
| 185 | - * @return mixed |
|
| 186 | - */ |
|
| 187 | - private function getRequestUrl($requestUrl = null) |
|
| 188 | - { |
|
| 189 | - // set Request Url if it isn't passed as parameter |
|
| 190 | - if (is_null($requestUrl)) { |
|
| 191 | - $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - return str_replace($this->basePath, '', strtok($requestUrl, '?')); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - /** |
|
| 198 | - * @return RouterParser |
|
| 199 | - */ |
|
| 200 | - public function getParser() |
|
| 201 | - { |
|
| 202 | - return $this->parser; |
|
| 203 | - } |
|
| 9 | + protected $routes = []; |
|
| 10 | + protected $namedRoutes = []; |
|
| 11 | + protected $basePath = ''; |
|
| 12 | + protected $all = ['get', 'post']; |
|
| 13 | + protected $server; |
|
| 14 | + /** |
|
| 15 | + * @var RouterParser |
|
| 16 | + */ |
|
| 17 | + private $parser; |
|
| 18 | + |
|
| 19 | + /** |
|
| 20 | + * Create router in one call from config. |
|
| 21 | + * |
|
| 22 | + * @param RouterParser $parser |
|
| 23 | + * @param array $routes |
|
| 24 | + * @param string $basePath |
|
| 25 | + * @param array|null $server |
|
| 26 | + */ |
|
| 27 | + public function __construct(RouterParser $parser, $routes = [], $basePath = '', $server = null) |
|
| 28 | + { |
|
| 29 | + $this->setRoutes($routes); |
|
| 30 | + $this->setBasePath($basePath); |
|
| 31 | + $this->parser = $parser; |
|
| 32 | + $this->server = $server; |
|
| 33 | + } |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * Map a route to a target |
|
| 37 | + * |
|
| 38 | + * @param string $method One of 5 HTTP Methods, or a pipe-separated list of multiple HTTP Methods (GET|POST|PATCH|PUT|DELETE) |
|
| 39 | + * @param string $route The route regex, custom regex must start with an @. You can use multiple pre-set regex filters, like [i:id] |
|
| 40 | + * @param mixed $target The target where this route should point to. Can be anything. |
|
| 41 | + * @param string $routeName Optional name of this route. Supply if you want to reverse route this url in your application. |
|
| 42 | + */ |
|
| 43 | + public function map($method, $route, $target, $routeName = null) |
|
| 44 | + { |
|
| 45 | + if (is_string($routeName)) { |
|
| 46 | + $this->handleException($routeName, "Can not redeclare route '%s'", true); |
|
| 47 | + $this->namedRoutes[$routeName] = $route; |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + $this->routes[] = array($method, $route, $target, $routeName); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Reversed routing |
|
| 55 | + * Generate the URL for a named route. Replace regexes with supplied parameters |
|
| 56 | + * |
|
| 57 | + * @param string $routeName The name of the route. |
|
| 58 | + * @param array $params Associative array of parameters to replace placeholders with. |
|
| 59 | + * |
|
| 60 | + * @return string The URL of the route with named parameters in place. |
|
| 61 | + */ |
|
| 62 | + public function generate($routeName, array $params = []) |
|
| 63 | + { |
|
| 64 | + $this->handleException($routeName, "Route '%s' does not exist.", false); |
|
| 65 | + |
|
| 66 | + $route = $this->namedRoutes[$routeName]; |
|
| 67 | + |
|
| 68 | + return $this->parser->generate($this->basePath, $route, $params); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * Match a given Request Url against stored routes |
|
| 73 | + * @param string $requestUrl |
|
| 74 | + * @param string $requestMethod |
|
| 75 | + * |
|
| 76 | + * @return array|boolean Array with route information on success, false on failure (no match). |
|
| 77 | + */ |
|
| 78 | + public function match($requestUrl = null, $requestMethod = null) |
|
| 79 | + { |
|
| 80 | + $requestUrl = $this->getRequestUrl($requestUrl); |
|
| 81 | + |
|
| 82 | + // set Request Method if it isn't passed as a parameter |
|
| 83 | + if (is_null($requestMethod)) { |
|
| 84 | + $requestMethod = $this->server['REQUEST_METHOD']; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + foreach ($this->routes as $handler) { |
|
| 88 | + if (!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) { |
|
| 89 | + continue; |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + return array( |
|
| 93 | + 'target' => $handler[2], |
|
| 94 | + 'params' => array_filter($this->parser->getParams(), function ($k) { |
|
| 95 | + return !is_numeric($k); |
|
| 96 | + }, ARRAY_FILTER_USE_KEY), |
|
| 97 | + 'name' => $handler[3] |
|
| 98 | + ); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return false; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * @param $method |
|
| 106 | + * @param $arguments |
|
| 107 | + * |
|
| 108 | + * @throws RouterException |
|
| 109 | + */ |
|
| 110 | + public function __call($method, $arguments) |
|
| 111 | + { |
|
| 112 | + if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
|
| 113 | + throw new RouterException($method . ' not exist in the '. __CLASS__); |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + $methods = $method == 'all' ? implode('|', $this->all) : $method; |
|
| 117 | + |
|
| 118 | + $route = array_merge([$methods], $arguments); |
|
| 119 | + |
|
| 120 | + call_user_func_array([$this, 'map'], $route); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Retrieves all routes. |
|
| 125 | + * Useful if you want to process or display routes. |
|
| 126 | + * |
|
| 127 | + * @return array All routes. |
|
| 128 | + */ |
|
| 129 | + public function getRoutes() |
|
| 130 | + { |
|
| 131 | + return $this->routes; |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Add multiple routes at once from array in the following format: |
|
| 136 | + * |
|
| 137 | + * $routes = array( |
|
| 138 | + * array($method, $route, $target, $name) |
|
| 139 | + * ); |
|
| 140 | + * |
|
| 141 | + * @param array|Traversable $routes |
|
| 142 | + * |
|
| 143 | + * @return void |
|
| 144 | + */ |
|
| 145 | + public function setRoutes($routes) |
|
| 146 | + { |
|
| 147 | + if (!is_array($routes) && !$routes instanceof Traversable) { |
|
| 148 | + throw new RouterException('Routes should be an array or an instance of Traversable'); |
|
| 149 | + } |
|
| 150 | + if (!empty($routes)) { |
|
| 151 | + foreach ($routes as $route) { |
|
| 152 | + call_user_func_array(array($this, 'map'), $route); |
|
| 153 | + } |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Set the base path. |
|
| 159 | + * Useful if you are running your application from a subdirectory. |
|
| 160 | + * |
|
| 161 | + * @param $basePath |
|
| 162 | + */ |
|
| 163 | + public function setBasePath($basePath) |
|
| 164 | + { |
|
| 165 | + $this->basePath = $basePath; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * @param $routeName |
|
| 170 | + * @param $message |
|
| 171 | + * @param bool $cmpTo |
|
| 172 | + * |
|
| 173 | + * @throws RouterException |
|
| 174 | + */ |
|
| 175 | + private function handleException($routeName, $message, $cmpTo) |
|
| 176 | + { |
|
| 177 | + if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) { |
|
| 178 | + throw new RouterException(sprintf($message, $routeName)); |
|
| 179 | + } |
|
| 180 | + } |
|
| 181 | + |
|
| 182 | + /** |
|
| 183 | + * @param $requestUrl |
|
| 184 | + * |
|
| 185 | + * @return mixed |
|
| 186 | + */ |
|
| 187 | + private function getRequestUrl($requestUrl = null) |
|
| 188 | + { |
|
| 189 | + // set Request Url if it isn't passed as parameter |
|
| 190 | + if (is_null($requestUrl)) { |
|
| 191 | + $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + return str_replace($this->basePath, '', strtok($requestUrl, '?')); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + /** |
|
| 198 | + * @return RouterParser |
|
| 199 | + */ |
|
| 200 | + public function getParser() |
|
| 201 | + { |
|
| 202 | + return $this->parser; |
|
| 203 | + } |
|
| 204 | 204 | } |
@@ -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; |
@@ -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 generate($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 generate($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 | } |
@@ -10,51 +10,51 @@ discard block |
||
| 10 | 10 | |
| 11 | 11 | class RouterDebug extends Router |
| 12 | 12 | { |
| 13 | - public function getNamedRoutes() |
|
| 14 | - { |
|
| 15 | - return $this->namedRoutes; |
|
| 16 | - } |
|
| 13 | + public function getNamedRoutes() |
|
| 14 | + { |
|
| 15 | + return $this->namedRoutes; |
|
| 16 | + } |
|
| 17 | 17 | |
| 18 | - public function getBasePath() |
|
| 19 | - { |
|
| 20 | - return $this->basePath; |
|
| 21 | - } |
|
| 18 | + public function getBasePath() |
|
| 19 | + { |
|
| 20 | + return $this->basePath; |
|
| 21 | + } |
|
| 22 | 22 | |
| 23 | - public function setServer($server) |
|
| 24 | - { |
|
| 25 | - $this->server = $server; |
|
| 26 | - } |
|
| 23 | + public function setServer($server) |
|
| 24 | + { |
|
| 25 | + $this->server = $server; |
|
| 26 | + } |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | 29 | class SimpleTraversable implements Iterator |
| 30 | 30 | { |
| 31 | - protected $_position = 0; |
|
| 31 | + protected $_position = 0; |
|
| 32 | 32 | |
| 33 | - protected $_data = array( |
|
| 34 | - array('GET', '/foo', 'foo_action', null), |
|
| 35 | - array('POST', '/bar', 'bar_action', 'second_route') |
|
| 36 | - ); |
|
| 33 | + protected $_data = array( |
|
| 34 | + array('GET', '/foo', 'foo_action', null), |
|
| 35 | + array('POST', '/bar', 'bar_action', 'second_route') |
|
| 36 | + ); |
|
| 37 | 37 | |
| 38 | - public function current() |
|
| 39 | - { |
|
| 40 | - return $this->_data[$this->_position]; |
|
| 41 | - } |
|
| 42 | - public function key() |
|
| 43 | - { |
|
| 44 | - return $this->_position; |
|
| 45 | - } |
|
| 46 | - public function next() |
|
| 47 | - { |
|
| 48 | - ++$this->_position; |
|
| 49 | - } |
|
| 50 | - public function rewind() |
|
| 51 | - { |
|
| 52 | - $this->_position = 0; |
|
| 53 | - } |
|
| 54 | - public function valid() |
|
| 55 | - { |
|
| 56 | - return isset($this->_data[$this->_position]); |
|
| 57 | - } |
|
| 38 | + public function current() |
|
| 39 | + { |
|
| 40 | + return $this->_data[$this->_position]; |
|
| 41 | + } |
|
| 42 | + public function key() |
|
| 43 | + { |
|
| 44 | + return $this->_position; |
|
| 45 | + } |
|
| 46 | + public function next() |
|
| 47 | + { |
|
| 48 | + ++$this->_position; |
|
| 49 | + } |
|
| 50 | + public function rewind() |
|
| 51 | + { |
|
| 52 | + $this->_position = 0; |
|
| 53 | + } |
|
| 54 | + public function valid() |
|
| 55 | + { |
|
| 56 | + return isset($this->_data[$this->_position]); |
|
| 57 | + } |
|
| 58 | 58 | } |
| 59 | 59 | |
| 60 | 60 | /** |
@@ -62,399 +62,399 @@ discard block |
||
| 62 | 62 | */ |
| 63 | 63 | class AltoRouterTest extends PHPUnit_Framework_TestCase |
| 64 | 64 | { |
| 65 | - /** |
|
| 66 | - * @var RouterDebug |
|
| 67 | - */ |
|
| 68 | - protected $router; |
|
| 65 | + /** |
|
| 66 | + * @var RouterDebug |
|
| 67 | + */ |
|
| 68 | + protected $router; |
|
| 69 | 69 | |
| 70 | - /** |
|
| 71 | - * Sets up the fixture, for example, opens a network connection. |
|
| 72 | - * This method is called before a test is executed. |
|
| 73 | - */ |
|
| 74 | - protected function setUp() |
|
| 75 | - { |
|
| 76 | - $parser = new RouterParser(); |
|
| 77 | - $this->router = new RouterDebug($parser, [], '', $_SERVER); |
|
| 78 | - } |
|
| 70 | + /** |
|
| 71 | + * Sets up the fixture, for example, opens a network connection. |
|
| 72 | + * This method is called before a test is executed. |
|
| 73 | + */ |
|
| 74 | + protected function setUp() |
|
| 75 | + { |
|
| 76 | + $parser = new RouterParser(); |
|
| 77 | + $this->router = new RouterDebug($parser, [], '', $_SERVER); |
|
| 78 | + } |
|
| 79 | 79 | |
| 80 | - /** |
|
| 81 | - * @covers Router::getRoutes |
|
| 82 | - */ |
|
| 83 | - public function testGetRoutes() |
|
| 84 | - { |
|
| 85 | - $method = 'POST'; |
|
| 86 | - $route = '/[:controller]/[:action]'; |
|
| 87 | - $target = function () { |
|
| 88 | - }; |
|
| 80 | + /** |
|
| 81 | + * @covers Router::getRoutes |
|
| 82 | + */ |
|
| 83 | + public function testGetRoutes() |
|
| 84 | + { |
|
| 85 | + $method = 'POST'; |
|
| 86 | + $route = '/[:controller]/[:action]'; |
|
| 87 | + $target = function () { |
|
| 88 | + }; |
|
| 89 | 89 | |
| 90 | - $this->assertInternalType('array', $this->router->getRoutes()); |
|
| 91 | - $this->router->map($method, $route, $target); |
|
| 92 | - $this->assertEquals(array(array($method, $route, $target, null)), $this->router->getRoutes()); |
|
| 93 | - } |
|
| 90 | + $this->assertInternalType('array', $this->router->getRoutes()); |
|
| 91 | + $this->router->map($method, $route, $target); |
|
| 92 | + $this->assertEquals(array(array($method, $route, $target, null)), $this->router->getRoutes()); |
|
| 93 | + } |
|
| 94 | 94 | |
| 95 | - /** |
|
| 96 | - * @covers Router::setRoutes |
|
| 97 | - */ |
|
| 98 | - public function testAddRoutes() |
|
| 99 | - { |
|
| 100 | - $method = 'POST'; |
|
| 101 | - $route = '/[:controller]/[:action]'; |
|
| 102 | - $target = function () { |
|
| 103 | - }; |
|
| 104 | - |
|
| 105 | - $this->router->setRoutes(array( |
|
| 106 | - array($method, $route, $target), |
|
| 107 | - array($method, $route, $target, 'second_route') |
|
| 108 | - )); |
|
| 109 | - |
|
| 110 | - $routes = $this->router->getRoutes(); |
|
| 111 | - |
|
| 112 | - $this->assertEquals(array($method, $route, $target, null), $routes[0]); |
|
| 113 | - $this->assertEquals(array($method, $route, $target, 'second_route'), $routes[1]); |
|
| 114 | - } |
|
| 95 | + /** |
|
| 96 | + * @covers Router::setRoutes |
|
| 97 | + */ |
|
| 98 | + public function testAddRoutes() |
|
| 99 | + { |
|
| 100 | + $method = 'POST'; |
|
| 101 | + $route = '/[:controller]/[:action]'; |
|
| 102 | + $target = function () { |
|
| 103 | + }; |
|
| 104 | + |
|
| 105 | + $this->router->setRoutes(array( |
|
| 106 | + array($method, $route, $target), |
|
| 107 | + array($method, $route, $target, 'second_route') |
|
| 108 | + )); |
|
| 109 | + |
|
| 110 | + $routes = $this->router->getRoutes(); |
|
| 111 | + |
|
| 112 | + $this->assertEquals(array($method, $route, $target, null), $routes[0]); |
|
| 113 | + $this->assertEquals(array($method, $route, $target, 'second_route'), $routes[1]); |
|
| 114 | + } |
|
| 115 | 115 | |
| 116 | - /** |
|
| 117 | - * @covers Router::setRoutes |
|
| 118 | - */ |
|
| 119 | - public function testAddRoutesAcceptsTraverable() |
|
| 120 | - { |
|
| 121 | - $traversable = new SimpleTraversable(); |
|
| 122 | - $this->router->setRoutes($traversable); |
|
| 116 | + /** |
|
| 117 | + * @covers Router::setRoutes |
|
| 118 | + */ |
|
| 119 | + public function testAddRoutesAcceptsTraverable() |
|
| 120 | + { |
|
| 121 | + $traversable = new SimpleTraversable(); |
|
| 122 | + $this->router->setRoutes($traversable); |
|
| 123 | 123 | |
| 124 | - $traversable->rewind(); |
|
| 124 | + $traversable->rewind(); |
|
| 125 | 125 | |
| 126 | - $first = $traversable->current(); |
|
| 127 | - $traversable->next(); |
|
| 128 | - $second = $traversable->current(); |
|
| 126 | + $first = $traversable->current(); |
|
| 127 | + $traversable->next(); |
|
| 128 | + $second = $traversable->current(); |
|
| 129 | 129 | |
| 130 | - $routes = $this->router->getRoutes(); |
|
| 130 | + $routes = $this->router->getRoutes(); |
|
| 131 | 131 | |
| 132 | - $this->assertEquals($first, $routes[0]); |
|
| 133 | - $this->assertEquals($second, $routes[1]); |
|
| 134 | - } |
|
| 132 | + $this->assertEquals($first, $routes[0]); |
|
| 133 | + $this->assertEquals($second, $routes[1]); |
|
| 134 | + } |
|
| 135 | 135 | |
| 136 | - /** |
|
| 137 | - * @covers Router::setRoutes |
|
| 138 | - * @expectedException Exception |
|
| 139 | - */ |
|
| 140 | - public function testAddRoutesThrowsExceptionOnInvalidArgument() |
|
| 141 | - { |
|
| 142 | - $this->router->setRoutes(new stdClass); |
|
| 143 | - } |
|
| 136 | + /** |
|
| 137 | + * @covers Router::setRoutes |
|
| 138 | + * @expectedException Exception |
|
| 139 | + */ |
|
| 140 | + public function testAddRoutesThrowsExceptionOnInvalidArgument() |
|
| 141 | + { |
|
| 142 | + $this->router->setRoutes(new stdClass); |
|
| 143 | + } |
|
| 144 | 144 | |
| 145 | - /** |
|
| 146 | - * @covers AltoRouter::setBasePath |
|
| 147 | - */ |
|
| 148 | - public function testSetBasePath() |
|
| 149 | - { |
|
| 150 | - $this->router->setBasePath('/some/path'); |
|
| 151 | - $this->assertEquals('/some/path', $this->router->getBasePath()); |
|
| 152 | - |
|
| 153 | - $this->router->setBasePath('/some/path'); |
|
| 154 | - $this->assertEquals('/some/path', $this->router->getBasePath()); |
|
| 155 | - } |
|
| 145 | + /** |
|
| 146 | + * @covers AltoRouter::setBasePath |
|
| 147 | + */ |
|
| 148 | + public function testSetBasePath() |
|
| 149 | + { |
|
| 150 | + $this->router->setBasePath('/some/path'); |
|
| 151 | + $this->assertEquals('/some/path', $this->router->getBasePath()); |
|
| 152 | + |
|
| 153 | + $this->router->setBasePath('/some/path'); |
|
| 154 | + $this->assertEquals('/some/path', $this->router->getBasePath()); |
|
| 155 | + } |
|
| 156 | 156 | |
| 157 | - /** |
|
| 158 | - * @covers Router::map |
|
| 159 | - */ |
|
| 160 | - public function testMap() |
|
| 161 | - { |
|
| 162 | - $method = 'POST'; |
|
| 163 | - $route = '/[:controller]/[:action]'; |
|
| 164 | - $target = function () { |
|
| 165 | - }; |
|
| 166 | - |
|
| 167 | - $this->router->map($method, $route, $target); |
|
| 168 | - |
|
| 169 | - $routes = $this->router->getRoutes(); |
|
| 170 | - |
|
| 171 | - $this->assertEquals(array($method, $route, $target, null), $routes[0]); |
|
| 172 | - } |
|
| 157 | + /** |
|
| 158 | + * @covers Router::map |
|
| 159 | + */ |
|
| 160 | + public function testMap() |
|
| 161 | + { |
|
| 162 | + $method = 'POST'; |
|
| 163 | + $route = '/[:controller]/[:action]'; |
|
| 164 | + $target = function () { |
|
| 165 | + }; |
|
| 166 | + |
|
| 167 | + $this->router->map($method, $route, $target); |
|
| 168 | + |
|
| 169 | + $routes = $this->router->getRoutes(); |
|
| 170 | + |
|
| 171 | + $this->assertEquals(array($method, $route, $target, null), $routes[0]); |
|
| 172 | + } |
|
| 173 | 173 | |
| 174 | - /** |
|
| 175 | - * @covers Router::map |
|
| 176 | - */ |
|
| 177 | - public function testMapWithName() |
|
| 178 | - { |
|
| 179 | - $method = 'POST'; |
|
| 180 | - $route = '/[:controller]/[:action]'; |
|
| 181 | - $target = function () { |
|
| 182 | - }; |
|
| 183 | - $name = 'myroute'; |
|
| 184 | - |
|
| 185 | - $this->router->map($method, $route, $target, $name); |
|
| 186 | - |
|
| 187 | - $routes = $this->router->getRoutes(); |
|
| 188 | - $this->assertEquals(array($method, $route, $target, $name), $routes[0]); |
|
| 189 | - |
|
| 190 | - $named_routes = $this->router->getNamedRoutes(); |
|
| 191 | - $this->assertEquals($route, $named_routes[$name]); |
|
| 192 | - |
|
| 193 | - try { |
|
| 194 | - $this->router->map($method, $route, $target, $name); |
|
| 195 | - $this->fail('Should not be able to add existing named route'); |
|
| 196 | - } catch (Exception $e) { |
|
| 197 | - $this->assertEquals("Can not redeclare route '{$name}'", $e->getMessage()); |
|
| 198 | - } |
|
| 199 | - } |
|
| 174 | + /** |
|
| 175 | + * @covers Router::map |
|
| 176 | + */ |
|
| 177 | + public function testMapWithName() |
|
| 178 | + { |
|
| 179 | + $method = 'POST'; |
|
| 180 | + $route = '/[:controller]/[:action]'; |
|
| 181 | + $target = function () { |
|
| 182 | + }; |
|
| 183 | + $name = 'myroute'; |
|
| 184 | + |
|
| 185 | + $this->router->map($method, $route, $target, $name); |
|
| 186 | + |
|
| 187 | + $routes = $this->router->getRoutes(); |
|
| 188 | + $this->assertEquals(array($method, $route, $target, $name), $routes[0]); |
|
| 189 | + |
|
| 190 | + $named_routes = $this->router->getNamedRoutes(); |
|
| 191 | + $this->assertEquals($route, $named_routes[$name]); |
|
| 192 | + |
|
| 193 | + try { |
|
| 194 | + $this->router->map($method, $route, $target, $name); |
|
| 195 | + $this->fail('Should not be able to add existing named route'); |
|
| 196 | + } catch (Exception $e) { |
|
| 197 | + $this->assertEquals("Can not redeclare route '{$name}'", $e->getMessage()); |
|
| 198 | + } |
|
| 199 | + } |
|
| 200 | 200 | |
| 201 | 201 | |
| 202 | - /** |
|
| 203 | - * @covers Router::generate |
|
| 204 | - */ |
|
| 205 | - public function testGenerate() |
|
| 206 | - { |
|
| 207 | - $params = array( |
|
| 208 | - 'controller' => 'test', |
|
| 209 | - 'action' => 'someaction' |
|
| 210 | - ); |
|
| 202 | + /** |
|
| 203 | + * @covers Router::generate |
|
| 204 | + */ |
|
| 205 | + public function testGenerate() |
|
| 206 | + { |
|
| 207 | + $params = array( |
|
| 208 | + 'controller' => 'test', |
|
| 209 | + 'action' => 'someaction' |
|
| 210 | + ); |
|
| 211 | 211 | |
| 212 | - $this->router->map('GET', '/[:controller]/[:action]', function () { |
|
| 213 | - }, 'foo_route'); |
|
| 212 | + $this->router->map('GET', '/[:controller]/[:action]', function () { |
|
| 213 | + }, 'foo_route'); |
|
| 214 | 214 | |
| 215 | - $this->assertEquals( |
|
| 215 | + $this->assertEquals( |
|
| 216 | 216 | |
| 217 | - '/test/someaction', |
|
| 218 | - $this->router->generate('foo_route', $params) |
|
| 217 | + '/test/someaction', |
|
| 218 | + $this->router->generate('foo_route', $params) |
|
| 219 | 219 | |
| 220 | - ); |
|
| 220 | + ); |
|
| 221 | 221 | |
| 222 | - $params = array( |
|
| 223 | - 'controller' => 'test', |
|
| 224 | - 'action' => 'someaction', |
|
| 225 | - 'type' => 'json' |
|
| 226 | - ); |
|
| 222 | + $params = array( |
|
| 223 | + 'controller' => 'test', |
|
| 224 | + 'action' => 'someaction', |
|
| 225 | + 'type' => 'json' |
|
| 226 | + ); |
|
| 227 | 227 | |
| 228 | - $this->assertEquals( |
|
| 228 | + $this->assertEquals( |
|
| 229 | 229 | |
| 230 | - '/test/someaction', |
|
| 231 | - $this->router->generate('foo_route', $params) |
|
| 230 | + '/test/someaction', |
|
| 231 | + $this->router->generate('foo_route', $params) |
|
| 232 | 232 | |
| 233 | - ); |
|
| 234 | - } |
|
| 233 | + ); |
|
| 234 | + } |
|
| 235 | 235 | |
| 236 | - public function testGenerateWithOptionalUrlParts() |
|
| 237 | - { |
|
| 238 | - $this->router->map('GET', '/[:controller]/[:action].[:type]?', function () { |
|
| 239 | - }, 'bar_route'); |
|
| 236 | + public function testGenerateWithOptionalUrlParts() |
|
| 237 | + { |
|
| 238 | + $this->router->map('GET', '/[:controller]/[:action].[:type]?', function () { |
|
| 239 | + }, 'bar_route'); |
|
| 240 | 240 | |
| 241 | - $params = array( |
|
| 242 | - 'controller' => 'test', |
|
| 243 | - 'action' => 'someaction' |
|
| 244 | - ); |
|
| 241 | + $params = array( |
|
| 242 | + 'controller' => 'test', |
|
| 243 | + 'action' => 'someaction' |
|
| 244 | + ); |
|
| 245 | 245 | |
| 246 | - $this->assertEquals( |
|
| 246 | + $this->assertEquals( |
|
| 247 | 247 | |
| 248 | - '/test/someaction', |
|
| 249 | - $this->router->generate('bar_route', $params) |
|
| 248 | + '/test/someaction', |
|
| 249 | + $this->router->generate('bar_route', $params) |
|
| 250 | 250 | |
| 251 | - ); |
|
| 251 | + ); |
|
| 252 | 252 | |
| 253 | - $params = array( |
|
| 254 | - 'controller' => 'test', |
|
| 255 | - 'action' => 'someaction', |
|
| 256 | - 'type' => 'json' |
|
| 257 | - ); |
|
| 253 | + $params = array( |
|
| 254 | + 'controller' => 'test', |
|
| 255 | + 'action' => 'someaction', |
|
| 256 | + 'type' => 'json' |
|
| 257 | + ); |
|
| 258 | 258 | |
| 259 | - $this->assertEquals( |
|
| 259 | + $this->assertEquals( |
|
| 260 | 260 | |
| 261 | - '/test/someaction.json', |
|
| 262 | - $this->router->generate('bar_route', $params) |
|
| 261 | + '/test/someaction.json', |
|
| 262 | + $this->router->generate('bar_route', $params) |
|
| 263 | 263 | |
| 264 | - ); |
|
| 265 | - } |
|
| 264 | + ); |
|
| 265 | + } |
|
| 266 | 266 | |
| 267 | - public function testGenerateWithNonexistingRoute() |
|
| 268 | - { |
|
| 269 | - try { |
|
| 270 | - $this->router->generate('nonexisting_route'); |
|
| 271 | - $this->fail('Should trigger an exception on nonexisting named route'); |
|
| 272 | - } catch (Exception $e) { |
|
| 273 | - $this->assertEquals("Route 'nonexisting_route' does not exist.", $e->getMessage()); |
|
| 274 | - } |
|
| 275 | - } |
|
| 267 | + public function testGenerateWithNonexistingRoute() |
|
| 268 | + { |
|
| 269 | + try { |
|
| 270 | + $this->router->generate('nonexisting_route'); |
|
| 271 | + $this->fail('Should trigger an exception on nonexisting named route'); |
|
| 272 | + } catch (Exception $e) { |
|
| 273 | + $this->assertEquals("Route 'nonexisting_route' does not exist.", $e->getMessage()); |
|
| 274 | + } |
|
| 275 | + } |
|
| 276 | 276 | |
| 277 | - /** |
|
| 278 | - * @covers Router::match |
|
| 279 | - * @covers Router::compileRoute |
|
| 280 | - */ |
|
| 281 | - public function testMatch() |
|
| 282 | - { |
|
| 283 | - $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
| 284 | - |
|
| 285 | - $this->assertEquals(array( |
|
| 286 | - 'target' => 'foo_action', |
|
| 287 | - 'params' => array( |
|
| 288 | - 'controller' => 'test', |
|
| 289 | - 'action' => 'do' |
|
| 290 | - ), |
|
| 291 | - 'name' => 'foo_route' |
|
| 292 | - ), $this->router->match('/foo/test/do', 'GET')); |
|
| 293 | - |
|
| 294 | - $this->assertFalse($this->router->match('/foo/test/do', 'POST')); |
|
| 295 | - |
|
| 296 | - $this->assertEquals(array( |
|
| 297 | - 'target' => 'foo_action', |
|
| 298 | - 'params' => array( |
|
| 299 | - 'controller' => 'test', |
|
| 300 | - 'action' => 'do' |
|
| 301 | - ), |
|
| 302 | - 'name' => 'foo_route' |
|
| 303 | - ), $this->router->match('/foo/test/do?param=value', 'GET')); |
|
| 304 | - } |
|
| 277 | + /** |
|
| 278 | + * @covers Router::match |
|
| 279 | + * @covers Router::compileRoute |
|
| 280 | + */ |
|
| 281 | + public function testMatch() |
|
| 282 | + { |
|
| 283 | + $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
| 284 | + |
|
| 285 | + $this->assertEquals(array( |
|
| 286 | + 'target' => 'foo_action', |
|
| 287 | + 'params' => array( |
|
| 288 | + 'controller' => 'test', |
|
| 289 | + 'action' => 'do' |
|
| 290 | + ), |
|
| 291 | + 'name' => 'foo_route' |
|
| 292 | + ), $this->router->match('/foo/test/do', 'GET')); |
|
| 293 | + |
|
| 294 | + $this->assertFalse($this->router->match('/foo/test/do', 'POST')); |
|
| 295 | + |
|
| 296 | + $this->assertEquals(array( |
|
| 297 | + 'target' => 'foo_action', |
|
| 298 | + 'params' => array( |
|
| 299 | + 'controller' => 'test', |
|
| 300 | + 'action' => 'do' |
|
| 301 | + ), |
|
| 302 | + 'name' => 'foo_route' |
|
| 303 | + ), $this->router->match('/foo/test/do?param=value', 'GET')); |
|
| 304 | + } |
|
| 305 | 305 | |
| 306 | - public function testMatchWithFixedParamValues() |
|
| 307 | - { |
|
| 308 | - $this->router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do'); |
|
| 309 | - |
|
| 310 | - $this->assertEquals(array( |
|
| 311 | - 'target' => 'usersController#doAction', |
|
| 312 | - 'params' => array( |
|
| 313 | - 'id' => 1, |
|
| 314 | - 'action' => 'delete' |
|
| 315 | - ), |
|
| 316 | - 'name' => 'users_do' |
|
| 317 | - ), $this->router->match('/users/1/delete', 'POST')); |
|
| 318 | - |
|
| 319 | - $this->assertFalse($this->router->match('/users/1/delete', 'GET')); |
|
| 320 | - $this->assertFalse($this->router->match('/users/abc/delete', 'POST')); |
|
| 321 | - $this->assertFalse($this->router->match('/users/1/create', 'GET')); |
|
| 322 | - } |
|
| 306 | + public function testMatchWithFixedParamValues() |
|
| 307 | + { |
|
| 308 | + $this->router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do'); |
|
| 309 | + |
|
| 310 | + $this->assertEquals(array( |
|
| 311 | + 'target' => 'usersController#doAction', |
|
| 312 | + 'params' => array( |
|
| 313 | + 'id' => 1, |
|
| 314 | + 'action' => 'delete' |
|
| 315 | + ), |
|
| 316 | + 'name' => 'users_do' |
|
| 317 | + ), $this->router->match('/users/1/delete', 'POST')); |
|
| 318 | + |
|
| 319 | + $this->assertFalse($this->router->match('/users/1/delete', 'GET')); |
|
| 320 | + $this->assertFalse($this->router->match('/users/abc/delete', 'POST')); |
|
| 321 | + $this->assertFalse($this->router->match('/users/1/create', 'GET')); |
|
| 322 | + } |
|
| 323 | 323 | |
| 324 | - public function testMatchWithServerVars() |
|
| 325 | - { |
|
| 326 | - $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
| 324 | + public function testMatchWithServerVars() |
|
| 325 | + { |
|
| 326 | + $this->router->map('GET', '/foo/[:controller]/[:action]', 'foo_action', 'foo_route'); |
|
| 327 | 327 | |
| 328 | - $_SERVER['REQUEST_URI'] = '/foo/test/do'; |
|
| 329 | - $_SERVER['REQUEST_METHOD'] = 'GET'; |
|
| 328 | + $_SERVER['REQUEST_URI'] = '/foo/test/do'; |
|
| 329 | + $_SERVER['REQUEST_METHOD'] = 'GET'; |
|
| 330 | 330 | |
| 331 | - $this->router->setServer($_SERVER); |
|
| 332 | - |
|
| 333 | - $this->assertEquals(array( |
|
| 334 | - 'target' => 'foo_action', |
|
| 335 | - 'params' => array( |
|
| 336 | - 'controller' => 'test', |
|
| 337 | - 'action' => 'do' |
|
| 338 | - ), |
|
| 339 | - 'name' => 'foo_route' |
|
| 340 | - ), $this->router->match()); |
|
| 341 | - } |
|
| 331 | + $this->router->setServer($_SERVER); |
|
| 332 | + |
|
| 333 | + $this->assertEquals(array( |
|
| 334 | + 'target' => 'foo_action', |
|
| 335 | + 'params' => array( |
|
| 336 | + 'controller' => 'test', |
|
| 337 | + 'action' => 'do' |
|
| 338 | + ), |
|
| 339 | + 'name' => 'foo_route' |
|
| 340 | + ), $this->router->match()); |
|
| 341 | + } |
|
| 342 | 342 | |
| 343 | - public function testMatchWithOptionalUrlParts() |
|
| 344 | - { |
|
| 345 | - $this->router->map('GET', '/bar/[:controller]/[:action].[:type]?', 'bar_action', 'bar_route'); |
|
| 346 | - |
|
| 347 | - $this->assertEquals(array( |
|
| 348 | - 'target' => 'bar_action', |
|
| 349 | - 'params' => array( |
|
| 350 | - 'controller' => 'test', |
|
| 351 | - 'action' => 'do', |
|
| 352 | - 'type' => 'json' |
|
| 353 | - ), |
|
| 354 | - 'name' => 'bar_route' |
|
| 355 | - ), $this->router->match('/bar/test/do.json', 'GET')); |
|
| 356 | - } |
|
| 343 | + public function testMatchWithOptionalUrlParts() |
|
| 344 | + { |
|
| 345 | + $this->router->map('GET', '/bar/[:controller]/[:action].[:type]?', 'bar_action', 'bar_route'); |
|
| 346 | + |
|
| 347 | + $this->assertEquals(array( |
|
| 348 | + 'target' => 'bar_action', |
|
| 349 | + 'params' => array( |
|
| 350 | + 'controller' => 'test', |
|
| 351 | + 'action' => 'do', |
|
| 352 | + 'type' => 'json' |
|
| 353 | + ), |
|
| 354 | + 'name' => 'bar_route' |
|
| 355 | + ), $this->router->match('/bar/test/do.json', 'GET')); |
|
| 356 | + } |
|
| 357 | 357 | |
| 358 | - public function testMatchWithWildcard() |
|
| 359 | - { |
|
| 360 | - $this->router->map('GET', '/a', 'foo_action', 'foo_route'); |
|
| 361 | - $this->router->map('GET', '*', 'bar_action', 'bar_route'); |
|
| 362 | - |
|
| 363 | - $this->assertEquals(array( |
|
| 364 | - 'target' => 'bar_action', |
|
| 365 | - 'params' => array(), |
|
| 366 | - 'name' => 'bar_route' |
|
| 367 | - ), $this->router->match('/everything', 'GET')); |
|
| 368 | - } |
|
| 358 | + public function testMatchWithWildcard() |
|
| 359 | + { |
|
| 360 | + $this->router->map('GET', '/a', 'foo_action', 'foo_route'); |
|
| 361 | + $this->router->map('GET', '*', 'bar_action', 'bar_route'); |
|
| 362 | + |
|
| 363 | + $this->assertEquals(array( |
|
| 364 | + 'target' => 'bar_action', |
|
| 365 | + 'params' => array(), |
|
| 366 | + 'name' => 'bar_route' |
|
| 367 | + ), $this->router->match('/everything', 'GET')); |
|
| 368 | + } |
|
| 369 | 369 | |
| 370 | - public function testMatchWithCustomRegexp() |
|
| 371 | - { |
|
| 372 | - $this->router->map('GET', '@^/[a-z]*$', 'bar_action', 'bar_route'); |
|
| 373 | - |
|
| 374 | - $this->assertEquals(array( |
|
| 375 | - 'target' => 'bar_action', |
|
| 376 | - 'params' => array(), |
|
| 377 | - 'name' => 'bar_route' |
|
| 378 | - ), $this->router->match('/everything', 'GET')); |
|
| 379 | - |
|
| 380 | - $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
| 381 | - } |
|
| 370 | + public function testMatchWithCustomRegexp() |
|
| 371 | + { |
|
| 372 | + $this->router->map('GET', '@^/[a-z]*$', 'bar_action', 'bar_route'); |
|
| 373 | + |
|
| 374 | + $this->assertEquals(array( |
|
| 375 | + 'target' => 'bar_action', |
|
| 376 | + 'params' => array(), |
|
| 377 | + 'name' => 'bar_route' |
|
| 378 | + ), $this->router->match('/everything', 'GET')); |
|
| 379 | + |
|
| 380 | + $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
| 381 | + } |
|
| 382 | 382 | |
| 383 | - public function testMatchWithUnicodeRegex() |
|
| 384 | - { |
|
| 385 | - $pattern = '/(?<path>[^'; |
|
| 386 | - // Arabic characters |
|
| 387 | - $pattern .= '\x{0600}-\x{06FF}'; |
|
| 388 | - $pattern .= '\x{FB50}-\x{FDFD}'; |
|
| 389 | - $pattern .= '\x{FE70}-\x{FEFF}'; |
|
| 390 | - $pattern .= '\x{0750}-\x{077F}'; |
|
| 391 | - // Alphanumeric, /, _, - and space characters |
|
| 392 | - $pattern .= 'a-zA-Z0-9\/_\-\s'; |
|
| 393 | - // 'ZERO WIDTH NON-JOINER' |
|
| 394 | - $pattern .= '\x{200C}'; |
|
| 395 | - $pattern .= ']+)'; |
|
| 396 | - |
|
| 397 | - $this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route'); |
|
| 398 | - |
|
| 399 | - $this->assertEquals(array( |
|
| 400 | - 'target' => 'unicode_action', |
|
| 401 | - 'name' => 'unicode_route', |
|
| 402 | - 'params' => array( |
|
| 403 | - 'path' => '大家好' |
|
| 404 | - ) |
|
| 405 | - ), $this->router->match('/大家好', 'GET')); |
|
| 406 | - |
|
| 407 | - $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
| 408 | - } |
|
| 383 | + public function testMatchWithUnicodeRegex() |
|
| 384 | + { |
|
| 385 | + $pattern = '/(?<path>[^'; |
|
| 386 | + // Arabic characters |
|
| 387 | + $pattern .= '\x{0600}-\x{06FF}'; |
|
| 388 | + $pattern .= '\x{FB50}-\x{FDFD}'; |
|
| 389 | + $pattern .= '\x{FE70}-\x{FEFF}'; |
|
| 390 | + $pattern .= '\x{0750}-\x{077F}'; |
|
| 391 | + // Alphanumeric, /, _, - and space characters |
|
| 392 | + $pattern .= 'a-zA-Z0-9\/_\-\s'; |
|
| 393 | + // 'ZERO WIDTH NON-JOINER' |
|
| 394 | + $pattern .= '\x{200C}'; |
|
| 395 | + $pattern .= ']+)'; |
|
| 396 | + |
|
| 397 | + $this->router->map('GET', '@' . $pattern, 'unicode_action', 'unicode_route'); |
|
| 398 | + |
|
| 399 | + $this->assertEquals(array( |
|
| 400 | + 'target' => 'unicode_action', |
|
| 401 | + 'name' => 'unicode_route', |
|
| 402 | + 'params' => array( |
|
| 403 | + 'path' => '大家好' |
|
| 404 | + ) |
|
| 405 | + ), $this->router->match('/大家好', 'GET')); |
|
| 406 | + |
|
| 407 | + $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
| 408 | + } |
|
| 409 | 409 | |
| 410 | - /** |
|
| 411 | - * @covers Router::setMatchTypes |
|
| 412 | - */ |
|
| 413 | - public function testMatchWithCustomNamedRegex() |
|
| 414 | - { |
|
| 415 | - $this->router->getParser()->setMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?')); |
|
| 416 | - $this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route'); |
|
| 417 | - |
|
| 418 | - $this->assertEquals(array( |
|
| 419 | - 'target' => 'bar_action', |
|
| 420 | - 'params' => array( |
|
| 421 | - 'customId' => 'AB1', |
|
| 422 | - ), |
|
| 423 | - 'name' => 'bar_route' |
|
| 424 | - ), $this->router->match('/bar/AB1', 'GET')); |
|
| 410 | + /** |
|
| 411 | + * @covers Router::setMatchTypes |
|
| 412 | + */ |
|
| 413 | + public function testMatchWithCustomNamedRegex() |
|
| 414 | + { |
|
| 415 | + $this->router->getParser()->setMatchTypes(array('cId' => '[a-zA-Z]{2}[0-9](?:_[0-9]++)?')); |
|
| 416 | + $this->router->map('GET', '/bar/[cId:customId]', 'bar_action', 'bar_route'); |
|
| 417 | + |
|
| 418 | + $this->assertEquals(array( |
|
| 419 | + 'target' => 'bar_action', |
|
| 420 | + 'params' => array( |
|
| 421 | + 'customId' => 'AB1', |
|
| 422 | + ), |
|
| 423 | + 'name' => 'bar_route' |
|
| 424 | + ), $this->router->match('/bar/AB1', 'GET')); |
|
| 425 | 425 | |
| 426 | - $this->assertEquals(array( |
|
| 427 | - 'target' => 'bar_action', |
|
| 428 | - 'params' => array( |
|
| 429 | - 'customId' => 'AB1_0123456789', |
|
| 430 | - ), |
|
| 431 | - 'name' => 'bar_route' |
|
| 432 | - ), $this->router->match('/bar/AB1_0123456789', 'GET')); |
|
| 433 | - |
|
| 434 | - $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
| 435 | - } |
|
| 426 | + $this->assertEquals(array( |
|
| 427 | + 'target' => 'bar_action', |
|
| 428 | + 'params' => array( |
|
| 429 | + 'customId' => 'AB1_0123456789', |
|
| 430 | + ), |
|
| 431 | + 'name' => 'bar_route' |
|
| 432 | + ), $this->router->match('/bar/AB1_0123456789', 'GET')); |
|
| 433 | + |
|
| 434 | + $this->assertFalse($this->router->match('/some-other-thing', 'GET')); |
|
| 435 | + } |
|
| 436 | 436 | |
| 437 | - public function testMatchWithCustomNamedUnicodeRegex() |
|
| 438 | - { |
|
| 439 | - $pattern = '[^'; |
|
| 440 | - // Arabic characters |
|
| 441 | - $pattern .= '\x{0600}-\x{06FF}'; |
|
| 442 | - $pattern .= '\x{FB50}-\x{FDFD}'; |
|
| 443 | - $pattern .= '\x{FE70}-\x{FEFF}'; |
|
| 444 | - $pattern .= '\x{0750}-\x{077F}'; |
|
| 445 | - $pattern .= ']+'; |
|
| 446 | - |
|
| 447 | - $this->router->getParser()->setMatchTypes(array('nonArabic' => $pattern)); |
|
| 448 | - $this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route'); |
|
| 437 | + public function testMatchWithCustomNamedUnicodeRegex() |
|
| 438 | + { |
|
| 439 | + $pattern = '[^'; |
|
| 440 | + // Arabic characters |
|
| 441 | + $pattern .= '\x{0600}-\x{06FF}'; |
|
| 442 | + $pattern .= '\x{FB50}-\x{FDFD}'; |
|
| 443 | + $pattern .= '\x{FE70}-\x{FEFF}'; |
|
| 444 | + $pattern .= '\x{0750}-\x{077F}'; |
|
| 445 | + $pattern .= ']+'; |
|
| 446 | + |
|
| 447 | + $this->router->getParser()->setMatchTypes(array('nonArabic' => $pattern)); |
|
| 448 | + $this->router->map('GET', '/bar/[nonArabic:string]', 'non_arabic_action', 'non_arabic_route'); |
|
| 449 | 449 | |
| 450 | - $this->assertEquals(array( |
|
| 451 | - 'target' => 'non_arabic_action', |
|
| 452 | - 'name' => 'non_arabic_route', |
|
| 453 | - 'params' => array( |
|
| 454 | - 'string' => 'some-path' |
|
| 455 | - ) |
|
| 456 | - ), $this->router->match('/bar/some-path', 'GET')); |
|
| 457 | - |
|
| 458 | - $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
| 459 | - } |
|
| 450 | + $this->assertEquals(array( |
|
| 451 | + 'target' => 'non_arabic_action', |
|
| 452 | + 'name' => 'non_arabic_route', |
|
| 453 | + 'params' => array( |
|
| 454 | + 'string' => 'some-path' |
|
| 455 | + ) |
|
| 456 | + ), $this->router->match('/bar/some-path', 'GET')); |
|
| 457 | + |
|
| 458 | + $this->assertFalse($this->router->match('/﷽', 'GET')); |
|
| 459 | + } |
|
| 460 | 460 | } |
@@ -84,7 +84,7 @@ discard block |
||
| 84 | 84 | { |
| 85 | 85 | $method = 'POST'; |
| 86 | 86 | $route = '/[:controller]/[:action]'; |
| 87 | - $target = function () { |
|
| 87 | + $target = function() { |
|
| 88 | 88 | }; |
| 89 | 89 | |
| 90 | 90 | $this->assertInternalType('array', $this->router->getRoutes()); |
@@ -99,7 +99,7 @@ discard block |
||
| 99 | 99 | { |
| 100 | 100 | $method = 'POST'; |
| 101 | 101 | $route = '/[:controller]/[:action]'; |
| 102 | - $target = function () { |
|
| 102 | + $target = function() { |
|
| 103 | 103 | }; |
| 104 | 104 | |
| 105 | 105 | $this->router->setRoutes(array( |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | { |
| 162 | 162 | $method = 'POST'; |
| 163 | 163 | $route = '/[:controller]/[:action]'; |
| 164 | - $target = function () { |
|
| 164 | + $target = function() { |
|
| 165 | 165 | }; |
| 166 | 166 | |
| 167 | 167 | $this->router->map($method, $route, $target); |
@@ -178,7 +178,7 @@ discard block |
||
| 178 | 178 | { |
| 179 | 179 | $method = 'POST'; |
| 180 | 180 | $route = '/[:controller]/[:action]'; |
| 181 | - $target = function () { |
|
| 181 | + $target = function() { |
|
| 182 | 182 | }; |
| 183 | 183 | $name = 'myroute'; |
| 184 | 184 | |
@@ -209,7 +209,7 @@ discard block |
||
| 209 | 209 | 'action' => 'someaction' |
| 210 | 210 | ); |
| 211 | 211 | |
| 212 | - $this->router->map('GET', '/[:controller]/[:action]', function () { |
|
| 212 | + $this->router->map('GET', '/[:controller]/[:action]', function() { |
|
| 213 | 213 | }, 'foo_route'); |
| 214 | 214 | |
| 215 | 215 | $this->assertEquals( |
@@ -235,7 +235,7 @@ discard block |
||
| 235 | 235 | |
| 236 | 236 | public function testGenerateWithOptionalUrlParts() |
| 237 | 237 | { |
| 238 | - $this->router->map('GET', '/[:controller]/[:action].[:type]?', function () { |
|
| 238 | + $this->router->map('GET', '/[:controller]/[:action].[:type]?', function() { |
|
| 239 | 239 | }, 'bar_route'); |
| 240 | 240 | |
| 241 | 241 | $params = array( |
@@ -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 generate($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 generate($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 | } |
@@ -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 | } |