@@ -11,28 +11,28 @@ discard block |
||
| 11 | 11 | protected $basePath = ''; |
| 12 | 12 | protected $all = ['get', 'post']; |
| 13 | 13 | private $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 null $server |
|
| 26 | - */ |
|
| 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 null $server |
|
| 26 | + */ |
|
| 27 | 27 | public function __construct(RouterParser $parser, $routes = [], $basePath = '', $server = null) |
| 28 | 28 | { |
| 29 | 29 | $this->setRoutes($routes); |
| 30 | 30 | $this->setBasePath($basePath); |
| 31 | - $this->parser = $parser; |
|
| 31 | + $this->parser = $parser; |
|
| 32 | 32 | if(!$server) { |
| 33 | 33 | $this->server = $_SERVER; |
| 34 | 34 | } |
| 35 | - } |
|
| 35 | + } |
|
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | 38 | * Map a route to a target |
@@ -45,7 +45,7 @@ discard block |
||
| 45 | 45 | public function map($method, $route, $target, $routeName = null) |
| 46 | 46 | { |
| 47 | 47 | if (is_string($routeName)) { |
| 48 | - $this->handleException($routeName, "Can not redeclare route '%s'"); |
|
| 48 | + $this->handleException($routeName, "Can not redeclare route '%s'"); |
|
| 49 | 49 | $this->namedRoutes[$routeName] = $route; |
| 50 | 50 | } |
| 51 | 51 | |
@@ -63,11 +63,11 @@ discard block |
||
| 63 | 63 | */ |
| 64 | 64 | public function generate($routeName, array $params = []) |
| 65 | 65 | { |
| 66 | - $this->handleException($routeName, "Can not redeclare route '%s'", false); |
|
| 66 | + $this->handleException($routeName, "Can not redeclare route '%s'", false); |
|
| 67 | 67 | |
| 68 | 68 | $route = $this->namedRoutes[$routeName]; |
| 69 | 69 | |
| 70 | - return $this->parser->generate($this->basePath, $route, $params); |
|
| 70 | + return $this->parser->generate($this->basePath, $route, $params); |
|
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | /** |
@@ -90,21 +90,21 @@ discard block |
||
| 90 | 90 | if(!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) continue; |
| 91 | 91 | |
| 92 | 92 | return array( |
| 93 | - 'target' => $handler[2], |
|
| 94 | - 'params' => array_filter($this->parser->getParams(), function ($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY), |
|
| 95 | - 'name' => $handler[3] |
|
| 96 | - ); |
|
| 93 | + 'target' => $handler[2], |
|
| 94 | + 'params' => array_filter($this->parser->getParams(), function ($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY), |
|
| 95 | + 'name' => $handler[3] |
|
| 96 | + ); |
|
| 97 | 97 | } |
| 98 | 98 | |
| 99 | 99 | return false; |
| 100 | 100 | } |
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * @param $method |
|
| 104 | - * @param $arguments |
|
| 105 | - * @throws RouterException |
|
| 106 | - */ |
|
| 107 | - public function __call($method, $arguments) |
|
| 102 | + /** |
|
| 103 | + * @param $method |
|
| 104 | + * @param $arguments |
|
| 105 | + * @throws RouterException |
|
| 106 | + */ |
|
| 107 | + public function __call($method, $arguments) |
|
| 108 | 108 | { |
| 109 | 109 | if(!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
| 110 | 110 | throw new RouterException($method . ' not exist in the '. __CLASS__); |
@@ -117,83 +117,83 @@ discard block |
||
| 117 | 117 | call_user_func_array([$this, 'map'], $route); |
| 118 | 118 | } |
| 119 | 119 | |
| 120 | - /** |
|
| 121 | - * Retrieves all routes. |
|
| 122 | - * Useful if you want to process or display routes. |
|
| 123 | - * @return array All routes. |
|
| 124 | - */ |
|
| 125 | - public function getRoutes() |
|
| 126 | - { |
|
| 127 | - return $this->routes; |
|
| 128 | - } |
|
| 129 | - |
|
| 130 | - /** |
|
| 131 | - * Add multiple routes at once from array in the following format: |
|
| 132 | - * |
|
| 133 | - * $routes = array( |
|
| 134 | - * array($method, $route, $target, $name) |
|
| 135 | - * ); |
|
| 136 | - * |
|
| 137 | - * @param array $routes |
|
| 138 | - * @return void |
|
| 139 | - * @author Koen Punt |
|
| 140 | - */ |
|
| 141 | - public function setRoutes($routes) |
|
| 142 | - { |
|
| 143 | - if (!is_array($routes) && !$routes instanceof Traversable) { |
|
| 144 | - throw new RouterException('Routes should be an array or an instance of Traversable'); |
|
| 145 | - } |
|
| 146 | - if(!empty($routes)) { |
|
| 147 | - foreach ($routes as $route) { |
|
| 148 | - call_user_func_array(array($this, 'map'), $route); |
|
| 149 | - } |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * Set the base path. |
|
| 155 | - * Useful if you are running your application from a subdirectory. |
|
| 156 | - */ |
|
| 157 | - public function setBasePath($basePath) |
|
| 158 | - { |
|
| 159 | - $this->basePath = $basePath; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Add named match types. It uses array_merge so keys can be overwritten. |
|
| 164 | - * |
|
| 165 | - * @param array $matchTypes The key is the name and the value is the regex. |
|
| 166 | - */ |
|
| 167 | - public function setMatchTypes($matchTypes) |
|
| 168 | - { |
|
| 169 | - $this->matchTypes = array_merge($this->matchTypes, $matchTypes); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - /** |
|
| 173 | - * @param $routeName |
|
| 174 | - * @param $message |
|
| 175 | - * @param bool $cmpTo |
|
| 176 | - * @throws RouterException |
|
| 177 | - */ |
|
| 178 | - private function handleException($routeName, $message, $cmpTo = true) |
|
| 179 | - { |
|
| 180 | - if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) { |
|
| 181 | - throw new RouterException(sprintf($message, $routeName)); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @param $requestUrl |
|
| 187 | - * |
|
| 188 | - * @return mixed |
|
| 189 | - */ |
|
| 190 | - private function getRequestUrl($requestUrl = null) |
|
| 191 | - { |
|
| 192 | - // set Request Url if it isn't passed as parameter |
|
| 193 | - if (is_null($requestUrl)) { |
|
| 194 | - $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - return str_replace($this->basePath, '', $requestUrl); |
|
| 198 | - } |
|
| 120 | + /** |
|
| 121 | + * Retrieves all routes. |
|
| 122 | + * Useful if you want to process or display routes. |
|
| 123 | + * @return array All routes. |
|
| 124 | + */ |
|
| 125 | + public function getRoutes() |
|
| 126 | + { |
|
| 127 | + return $this->routes; |
|
| 128 | + } |
|
| 129 | + |
|
| 130 | + /** |
|
| 131 | + * Add multiple routes at once from array in the following format: |
|
| 132 | + * |
|
| 133 | + * $routes = array( |
|
| 134 | + * array($method, $route, $target, $name) |
|
| 135 | + * ); |
|
| 136 | + * |
|
| 137 | + * @param array $routes |
|
| 138 | + * @return void |
|
| 139 | + * @author Koen Punt |
|
| 140 | + */ |
|
| 141 | + public function setRoutes($routes) |
|
| 142 | + { |
|
| 143 | + if (!is_array($routes) && !$routes instanceof Traversable) { |
|
| 144 | + throw new RouterException('Routes should be an array or an instance of Traversable'); |
|
| 145 | + } |
|
| 146 | + if(!empty($routes)) { |
|
| 147 | + foreach ($routes as $route) { |
|
| 148 | + call_user_func_array(array($this, 'map'), $route); |
|
| 149 | + } |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * Set the base path. |
|
| 155 | + * Useful if you are running your application from a subdirectory. |
|
| 156 | + */ |
|
| 157 | + public function setBasePath($basePath) |
|
| 158 | + { |
|
| 159 | + $this->basePath = $basePath; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Add named match types. It uses array_merge so keys can be overwritten. |
|
| 164 | + * |
|
| 165 | + * @param array $matchTypes The key is the name and the value is the regex. |
|
| 166 | + */ |
|
| 167 | + public function setMatchTypes($matchTypes) |
|
| 168 | + { |
|
| 169 | + $this->matchTypes = array_merge($this->matchTypes, $matchTypes); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + /** |
|
| 173 | + * @param $routeName |
|
| 174 | + * @param $message |
|
| 175 | + * @param bool $cmpTo |
|
| 176 | + * @throws RouterException |
|
| 177 | + */ |
|
| 178 | + private function handleException($routeName, $message, $cmpTo = true) |
|
| 179 | + { |
|
| 180 | + if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) { |
|
| 181 | + throw new RouterException(sprintf($message, $routeName)); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @param $requestUrl |
|
| 187 | + * |
|
| 188 | + * @return mixed |
|
| 189 | + */ |
|
| 190 | + private function getRequestUrl($requestUrl = null) |
|
| 191 | + { |
|
| 192 | + // set Request Url if it isn't passed as parameter |
|
| 193 | + if (is_null($requestUrl)) { |
|
| 194 | + $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + return str_replace($this->basePath, '', $requestUrl); |
|
| 198 | + } |
|
| 199 | 199 | } |
@@ -29,7 +29,7 @@ discard block |
||
| 29 | 29 | $this->setRoutes($routes); |
| 30 | 30 | $this->setBasePath($basePath); |
| 31 | 31 | $this->parser = $parser; |
| 32 | - if(!$server) { |
|
| 32 | + if (!$server) { |
|
| 33 | 33 | $this->server = $_SERVER; |
| 34 | 34 | } |
| 35 | 35 | } |
@@ -87,11 +87,11 @@ discard block |
||
| 87 | 87 | |
| 88 | 88 | foreach ($this->routes as $handler) { |
| 89 | 89 | |
| 90 | - if(!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) continue; |
|
| 90 | + if (!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) continue; |
|
| 91 | 91 | |
| 92 | 92 | return array( |
| 93 | 93 | 'target' => $handler[2], |
| 94 | - 'params' => array_filter($this->parser->getParams(), function ($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY), |
|
| 94 | + 'params' => array_filter($this->parser->getParams(), function($k) { return !is_numeric($k); }, ARRAY_FILTER_USE_KEY), |
|
| 95 | 95 | 'name' => $handler[3] |
| 96 | 96 | ); |
| 97 | 97 | } |
@@ -106,8 +106,8 @@ discard block |
||
| 106 | 106 | */ |
| 107 | 107 | public function __call($method, $arguments) |
| 108 | 108 | { |
| 109 | - if(!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
|
| 110 | - throw new RouterException($method . ' not exist in the '. __CLASS__); |
|
| 109 | + if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
|
| 110 | + throw new RouterException($method . ' not exist in the ' . __CLASS__); |
|
| 111 | 111 | } |
| 112 | 112 | |
| 113 | 113 | $methods = $method == 'all' ? implode('|', $this->all) : $method; |
@@ -143,7 +143,7 @@ discard block |
||
| 143 | 143 | if (!is_array($routes) && !$routes instanceof Traversable) { |
| 144 | 144 | throw new RouterException('Routes should be an array or an instance of Traversable'); |
| 145 | 145 | } |
| 146 | - if(!empty($routes)) { |
|
| 146 | + if (!empty($routes)) { |
|
| 147 | 147 | foreach ($routes as $route) { |
| 148 | 148 | call_user_func_array(array($this, 'map'), $route); |
| 149 | 149 | } |
@@ -87,7 +87,9 @@ |
||
| 87 | 87 | |
| 88 | 88 | foreach ($this->routes as $handler) { |
| 89 | 89 | |
| 90 | - if(!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) continue; |
|
| 90 | + if(!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) { |
|
| 91 | + continue; |
|
| 92 | + } |
|
| 91 | 93 | |
| 92 | 94 | return array( |
| 93 | 95 | 'target' => $handler[2], |
@@ -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 array $route |
|
| 18 | - * @param array $params |
|
| 19 | - * |
|
| 20 | - * @return string |
|
| 21 | - */ |
|
| 22 | - public function generate($basePath, array $route, array $params); |
|
| 13 | + /** |
|
| 14 | + * Get the url from a route name |
|
| 15 | + * |
|
| 16 | + * @param string $basePath |
|
| 17 | + * @param array $route |
|
| 18 | + * @param array $params |
|
| 19 | + * |
|
| 20 | + * @return string |
|
| 21 | + */ |
|
| 22 | + public function generate($basePath, array $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 | } |
@@ -3,189 +3,189 @@ |
||
| 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 | - * @param $basePath |
|
| 38 | - * @param $route |
|
| 39 | - * @param $params |
|
| 40 | - * @return mixed|string |
|
| 41 | - */ |
|
| 42 | - public function generate($basePath, $route, $params) |
|
| 43 | - { |
|
| 44 | - $url = $basePath . $route; |
|
| 45 | - |
|
| 46 | - if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 47 | - foreach ($matches as $match) { |
|
| 48 | - $block = $match[0]; |
|
| 49 | - $pre = $match[1]; |
|
| 50 | - $param = $match[3]; |
|
| 51 | - |
|
| 52 | - if ($pre) { |
|
| 53 | - $block = substr($block, 1); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - if (isset($params[$param])) { |
|
| 57 | - $url = str_replace($block, $params[$param], $url); |
|
| 58 | - } elseif ($match[4]) { |
|
| 59 | - $url = str_replace($pre . $block, '', $url); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - return $url; |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * @param $method |
|
| 69 | - * @param $requestMethod |
|
| 70 | - * @param string|array $routeString |
|
| 71 | - * @param $requestUrl |
|
| 72 | - * |
|
| 73 | - * @return mixed |
|
| 74 | - */ |
|
| 75 | - public function methodMatch($method, $requestMethod, $routeString, $requestUrl) |
|
| 76 | - { |
|
| 77 | - $method = strtolower($method); |
|
| 78 | - $requestMethod = strtolower($requestMethod); |
|
| 79 | - $methods = explode('|', $method); |
|
| 80 | - |
|
| 81 | - if(in_array($requestMethod, $methods)) { |
|
| 82 | - if($routeString == '*') { |
|
| 83 | - return true; |
|
| 84 | - } |
|
| 85 | - elseif(isset($routeString[0]) && $routeString[0] == '@') { |
|
| 86 | - return preg_match('`' . substr($routeString[0], 1) . '`u', $requestUrl, $this->params); |
|
| 87 | - } |
|
| 88 | - elseif (($position = strpos($routeString, '[')) === false) { |
|
| 89 | - return strcmp($requestUrl, $routeString) === 0; |
|
| 90 | - } |
|
| 91 | - else { |
|
| 92 | - if (strncmp($requestUrl, $routeString, $position) !== 0) { |
|
| 93 | - return false; |
|
| 94 | - } |
|
| 95 | - $regex = $this->compileRoute($routeString, $requestUrl); |
|
| 96 | - return preg_match($regex, $requestUrl, $this->params); |
|
| 97 | - } |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - return false; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Compile the regex for a given route (EXPENSIVE) |
|
| 105 | - */ |
|
| 106 | - private function compileRoute($routeString, $requestUrl) |
|
| 107 | - { |
|
| 108 | - $route = $this->getRoute($routeString, $requestUrl); |
|
| 109 | - |
|
| 110 | - if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 111 | - $matchTypes = $this->matchTypes; |
|
| 112 | - foreach ($matches as $match) { |
|
| 113 | - list($block, $pre, $type, $param, $optional) = $match; |
|
| 114 | - $pattern = $this->getRoutePattern($matchTypes, $pre, $type, $param, $optional); |
|
| 115 | - $route = str_replace($block, $pattern, $route); |
|
| 116 | - } |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - return "`^$route$`u"; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - private function getRoutePattern($matchTypes, $pre, $type, $param, $optional) |
|
| 123 | - { |
|
| 124 | - if (isset($matchTypes[$type])) { |
|
| 125 | - $type = $matchTypes[$type]; |
|
| 126 | - } |
|
| 127 | - if ($pre === '.') { |
|
| 128 | - $pre = '\.'; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - //Older versions of PCRE require the 'P' in (?P<named>) |
|
| 132 | - return '(?:' |
|
| 133 | - . ($pre !== '' ? $pre : null) |
|
| 134 | - . '(' |
|
| 135 | - . ($param !== '' ? "?P<$param>" : null) |
|
| 136 | - . $type |
|
| 137 | - . '))' |
|
| 138 | - . ($optional !== '' ? '?' : null); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - /** |
|
| 142 | - * @param $routeString |
|
| 143 | - * @param $requestUrl |
|
| 144 | - * |
|
| 145 | - * @return bool|string |
|
| 146 | - */ |
|
| 147 | - private function getRoute($routeString, $requestUrl) |
|
| 148 | - { |
|
| 149 | - $iPointer = $jPointer = 0; |
|
| 150 | - $nPointer = isset($routeString[0]) ? $routeString[0] : null; |
|
| 151 | - $regex = $route = false; |
|
| 152 | - |
|
| 153 | - // Find the longest non-regex substring and match it against the URI |
|
| 154 | - while (true) { |
|
| 155 | - if (!isset($routeString[$iPointer])) { |
|
| 156 | - break; |
|
| 157 | - } |
|
| 158 | - if ($regex === false) { |
|
| 159 | - if(!$this->getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) { |
|
| 160 | - continue; |
|
| 161 | - } |
|
| 162 | - $jPointer++; |
|
| 163 | - } |
|
| 164 | - $route .= $routeString[$iPointer++]; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return $route; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - private function getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl) |
|
| 171 | - { |
|
| 172 | - $cPointer = $nPointer; |
|
| 173 | - $regex = in_array($cPointer, array('[', '(', '.')); |
|
| 174 | - if (!$regex && isset($routeString[$iPointer+1])) { |
|
| 175 | - $nPointer = $routeString[$iPointer + 1]; |
|
| 176 | - $regex = in_array($nPointer, array('?', '+', '*', '{')); |
|
| 177 | - } |
|
| 178 | - if (!$regex && $cPointer !== '/' && (!isset($requestUrl[$jPointer]) || $cPointer !== $requestUrl[$jPointer])) { |
|
| 179 | - return false; |
|
| 180 | - } |
|
| 181 | - return true; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * @return array |
|
| 186 | - */ |
|
| 187 | - public function getParams() |
|
| 188 | - { |
|
| 189 | - return $this->params; |
|
| 190 | - } |
|
| 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 | + * @param $basePath |
|
| 38 | + * @param $route |
|
| 39 | + * @param $params |
|
| 40 | + * @return mixed|string |
|
| 41 | + */ |
|
| 42 | + public function generate($basePath, $route, $params) |
|
| 43 | + { |
|
| 44 | + $url = $basePath . $route; |
|
| 45 | + |
|
| 46 | + if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 47 | + foreach ($matches as $match) { |
|
| 48 | + $block = $match[0]; |
|
| 49 | + $pre = $match[1]; |
|
| 50 | + $param = $match[3]; |
|
| 51 | + |
|
| 52 | + if ($pre) { |
|
| 53 | + $block = substr($block, 1); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + if (isset($params[$param])) { |
|
| 57 | + $url = str_replace($block, $params[$param], $url); |
|
| 58 | + } elseif ($match[4]) { |
|
| 59 | + $url = str_replace($pre . $block, '', $url); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + return $url; |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * @param $method |
|
| 69 | + * @param $requestMethod |
|
| 70 | + * @param string|array $routeString |
|
| 71 | + * @param $requestUrl |
|
| 72 | + * |
|
| 73 | + * @return mixed |
|
| 74 | + */ |
|
| 75 | + public function methodMatch($method, $requestMethod, $routeString, $requestUrl) |
|
| 76 | + { |
|
| 77 | + $method = strtolower($method); |
|
| 78 | + $requestMethod = strtolower($requestMethod); |
|
| 79 | + $methods = explode('|', $method); |
|
| 80 | + |
|
| 81 | + if(in_array($requestMethod, $methods)) { |
|
| 82 | + if($routeString == '*') { |
|
| 83 | + return true; |
|
| 84 | + } |
|
| 85 | + elseif(isset($routeString[0]) && $routeString[0] == '@') { |
|
| 86 | + return preg_match('`' . substr($routeString[0], 1) . '`u', $requestUrl, $this->params); |
|
| 87 | + } |
|
| 88 | + elseif (($position = strpos($routeString, '[')) === false) { |
|
| 89 | + return strcmp($requestUrl, $routeString) === 0; |
|
| 90 | + } |
|
| 91 | + else { |
|
| 92 | + if (strncmp($requestUrl, $routeString, $position) !== 0) { |
|
| 93 | + return false; |
|
| 94 | + } |
|
| 95 | + $regex = $this->compileRoute($routeString, $requestUrl); |
|
| 96 | + return preg_match($regex, $requestUrl, $this->params); |
|
| 97 | + } |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + return false; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Compile the regex for a given route (EXPENSIVE) |
|
| 105 | + */ |
|
| 106 | + private function compileRoute($routeString, $requestUrl) |
|
| 107 | + { |
|
| 108 | + $route = $this->getRoute($routeString, $requestUrl); |
|
| 109 | + |
|
| 110 | + if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 111 | + $matchTypes = $this->matchTypes; |
|
| 112 | + foreach ($matches as $match) { |
|
| 113 | + list($block, $pre, $type, $param, $optional) = $match; |
|
| 114 | + $pattern = $this->getRoutePattern($matchTypes, $pre, $type, $param, $optional); |
|
| 115 | + $route = str_replace($block, $pattern, $route); |
|
| 116 | + } |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + return "`^$route$`u"; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + private function getRoutePattern($matchTypes, $pre, $type, $param, $optional) |
|
| 123 | + { |
|
| 124 | + if (isset($matchTypes[$type])) { |
|
| 125 | + $type = $matchTypes[$type]; |
|
| 126 | + } |
|
| 127 | + if ($pre === '.') { |
|
| 128 | + $pre = '\.'; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + //Older versions of PCRE require the 'P' in (?P<named>) |
|
| 132 | + return '(?:' |
|
| 133 | + . ($pre !== '' ? $pre : null) |
|
| 134 | + . '(' |
|
| 135 | + . ($param !== '' ? "?P<$param>" : null) |
|
| 136 | + . $type |
|
| 137 | + . '))' |
|
| 138 | + . ($optional !== '' ? '?' : null); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + /** |
|
| 142 | + * @param $routeString |
|
| 143 | + * @param $requestUrl |
|
| 144 | + * |
|
| 145 | + * @return bool|string |
|
| 146 | + */ |
|
| 147 | + private function getRoute($routeString, $requestUrl) |
|
| 148 | + { |
|
| 149 | + $iPointer = $jPointer = 0; |
|
| 150 | + $nPointer = isset($routeString[0]) ? $routeString[0] : null; |
|
| 151 | + $regex = $route = false; |
|
| 152 | + |
|
| 153 | + // Find the longest non-regex substring and match it against the URI |
|
| 154 | + while (true) { |
|
| 155 | + if (!isset($routeString[$iPointer])) { |
|
| 156 | + break; |
|
| 157 | + } |
|
| 158 | + if ($regex === false) { |
|
| 159 | + if(!$this->getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) { |
|
| 160 | + continue; |
|
| 161 | + } |
|
| 162 | + $jPointer++; |
|
| 163 | + } |
|
| 164 | + $route .= $routeString[$iPointer++]; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return $route; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + private function getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl) |
|
| 171 | + { |
|
| 172 | + $cPointer = $nPointer; |
|
| 173 | + $regex = in_array($cPointer, array('[', '(', '.')); |
|
| 174 | + if (!$regex && isset($routeString[$iPointer+1])) { |
|
| 175 | + $nPointer = $routeString[$iPointer + 1]; |
|
| 176 | + $regex = in_array($nPointer, array('?', '+', '*', '{')); |
|
| 177 | + } |
|
| 178 | + if (!$regex && $cPointer !== '/' && (!isset($requestUrl[$jPointer]) || $cPointer !== $requestUrl[$jPointer])) { |
|
| 179 | + return false; |
|
| 180 | + } |
|
| 181 | + return true; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * @return array |
|
| 186 | + */ |
|
| 187 | + public function getParams() |
|
| 188 | + { |
|
| 189 | + return $this->params; |
|
| 190 | + } |
|
| 191 | 191 | } |
@@ -78,11 +78,11 @@ discard block |
||
| 78 | 78 | $requestMethod = strtolower($requestMethod); |
| 79 | 79 | $methods = explode('|', $method); |
| 80 | 80 | |
| 81 | - if(in_array($requestMethod, $methods)) { |
|
| 82 | - if($routeString == '*') { |
|
| 81 | + if (in_array($requestMethod, $methods)) { |
|
| 82 | + if ($routeString == '*') { |
|
| 83 | 83 | return true; |
| 84 | 84 | } |
| 85 | - elseif(isset($routeString[0]) && $routeString[0] == '@') { |
|
| 85 | + elseif (isset($routeString[0]) && $routeString[0] == '@') { |
|
| 86 | 86 | return preg_match('`' . substr($routeString[0], 1) . '`u', $requestUrl, $this->params); |
| 87 | 87 | } |
| 88 | 88 | elseif (($position = strpos($routeString, '[')) === false) { |
@@ -156,7 +156,7 @@ discard block |
||
| 156 | 156 | break; |
| 157 | 157 | } |
| 158 | 158 | if ($regex === false) { |
| 159 | - if(!$this->getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) { |
|
| 159 | + if (!$this->getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) { |
|
| 160 | 160 | continue; |
| 161 | 161 | } |
| 162 | 162 | $jPointer++; |
@@ -171,7 +171,7 @@ discard block |
||
| 171 | 171 | { |
| 172 | 172 | $cPointer = $nPointer; |
| 173 | 173 | $regex = in_array($cPointer, array('[', '(', '.')); |
| 174 | - if (!$regex && isset($routeString[$iPointer+1])) { |
|
| 174 | + if (!$regex && isset($routeString[$iPointer + 1])) { |
|
| 175 | 175 | $nPointer = $routeString[$iPointer + 1]; |
| 176 | 176 | $regex = in_array($nPointer, array('?', '+', '*', '{')); |
| 177 | 177 | } |
@@ -81,14 +81,11 @@ |
||
| 81 | 81 | if(in_array($requestMethod, $methods)) { |
| 82 | 82 | if($routeString == '*') { |
| 83 | 83 | return true; |
| 84 | - } |
|
| 85 | - elseif(isset($routeString[0]) && $routeString[0] == '@') { |
|
| 84 | + } elseif(isset($routeString[0]) && $routeString[0] == '@') { |
|
| 86 | 85 | return preg_match('`' . substr($routeString[0], 1) . '`u', $requestUrl, $this->params); |
| 87 | - } |
|
| 88 | - elseif (($position = strpos($routeString, '[')) === false) { |
|
| 86 | + } elseif (($position = strpos($routeString, '[')) === false) { |
|
| 89 | 87 | return strcmp($requestUrl, $routeString) === 0; |
| 90 | - } |
|
| 91 | - else { |
|
| 88 | + } else { |
|
| 92 | 89 | if (strncmp($requestUrl, $routeString, $position) !== 0) { |
| 93 | 90 | return false; |
| 94 | 91 | } |
@@ -19,21 +19,21 @@ |
||
| 19 | 19 | <h3>Current request: </h3> |
| 20 | 20 | <pre> |
| 21 | 21 | <?php |
| 22 | - foreach($match as $key => $value) { |
|
| 23 | - echo '<p>' . $key . ': '; |
|
| 24 | - if(is_array($value)) { |
|
| 25 | - echo '<ul>'; |
|
| 26 | - foreach($value as $k => $v) { |
|
| 27 | - echo '<li>'.$k.': '.$v.'</li>'; |
|
| 28 | - } |
|
| 29 | - echo '</ul>'; |
|
| 30 | - } |
|
| 31 | - else { |
|
| 32 | - echo $value; |
|
| 33 | - } |
|
| 34 | - echo '</p>'; |
|
| 35 | - } |
|
| 36 | - ?> |
|
| 22 | + foreach($match as $key => $value) { |
|
| 23 | + echo '<p>' . $key . ': '; |
|
| 24 | + if(is_array($value)) { |
|
| 25 | + echo '<ul>'; |
|
| 26 | + foreach($value as $k => $v) { |
|
| 27 | + echo '<li>'.$k.': '.$v.'</li>'; |
|
| 28 | + } |
|
| 29 | + echo '</ul>'; |
|
| 30 | + } |
|
| 31 | + else { |
|
| 32 | + echo $value; |
|
| 33 | + } |
|
| 34 | + echo '</p>'; |
|
| 35 | + } |
|
| 36 | + ?> |
|
| 37 | 37 | </pre> |
| 38 | 38 | |
| 39 | 39 | <h3>Try these requests: </h3> |
@@ -6,10 +6,10 @@ discard block |
||
| 6 | 6 | $parser = new \HakimCh\Http\RouterParser(); |
| 7 | 7 | $router = new \HakimCh\Http\Router($parser); |
| 8 | 8 | $router->setBasePath('/AltoRouter/examples/basic'); |
| 9 | -$router->map('GET|POST','/', 'home#index', 'home'); |
|
| 10 | -$router->map('GET','/users/', array('c' => 'UserController', 'a' => 'ListAction')); |
|
| 11 | -$router->map('GET','/users/[i:id]', 'users#show', 'users_show'); |
|
| 12 | -$router->map('POST','/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do'); |
|
| 9 | +$router->map('GET|POST', '/', 'home#index', 'home'); |
|
| 10 | +$router->map('GET', '/users/', array('c' => 'UserController', 'a' => 'ListAction')); |
|
| 11 | +$router->map('GET', '/users/[i:id]', 'users#show', 'users_show'); |
|
| 12 | +$router->map('POST', '/users/[i:id]/[delete|update:action]', 'usersController#doAction', 'users_do'); |
|
| 13 | 13 | |
| 14 | 14 | // match current request |
| 15 | 15 | $match = $router->match(); |
@@ -19,12 +19,12 @@ discard block |
||
| 19 | 19 | <h3>Current request: </h3> |
| 20 | 20 | <pre> |
| 21 | 21 | <?php |
| 22 | - foreach($match as $key => $value) { |
|
| 22 | + foreach ($match as $key => $value) { |
|
| 23 | 23 | echo '<p>' . $key . ': '; |
| 24 | - if(is_array($value)) { |
|
| 24 | + if (is_array($value)) { |
|
| 25 | 25 | echo '<ul>'; |
| 26 | - foreach($value as $k => $v) { |
|
| 27 | - echo '<li>'.$k.': '.$v.'</li>'; |
|
| 26 | + foreach ($value as $k => $v) { |
|
| 27 | + echo '<li>' . $k . ': ' . $v . '</li>'; |
|
| 28 | 28 | } |
| 29 | 29 | echo '</ul>'; |
| 30 | 30 | } |
@@ -27,8 +27,7 @@ |
||
| 27 | 27 | echo '<li>'.$k.': '.$v.'</li>'; |
| 28 | 28 | } |
| 29 | 29 | echo '</ul>'; |
| 30 | - } |
|
| 31 | - else { |
|
| 30 | + } else { |
|
| 32 | 31 | echo $value; |
| 33 | 32 | } |
| 34 | 33 | echo '</p>'; |