@@ -25,22 +25,22 @@ |
||
| 25 | 25 | <h3>Current request: </h3> |
| 26 | 26 | <pre> |
| 27 | 27 | <?php |
| 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 | - } |
|
| 42 | - } |
|
| 43 | - ?> |
|
| 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 | + } |
|
| 42 | + } |
|
| 43 | + ?> |
|
| 44 | 44 | </pre> |
| 45 | 45 | |
| 46 | 46 | <h3>Try these requests: </h3> |
@@ -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 { |
@@ -3,195 +3,195 @@ |
||
| 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 | - $block = $match[0]; |
|
| 52 | - $pre = $match[1]; |
|
| 53 | - $param = $match[3]; |
|
| 54 | - |
|
| 55 | - if ($pre) { |
|
| 56 | - $block = substr($block, 1); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - if (isset($params[$param])) { |
|
| 60 | - $url = str_replace($block, $params[$param], $url); |
|
| 61 | - } elseif ($match[4]) { |
|
| 62 | - $url = str_replace($pre . $block, '', $url); |
|
| 63 | - } |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - return $url; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * @param $method |
|
| 72 | - * @param $requestMethod |
|
| 73 | - * @param string $routeString |
|
| 74 | - * @param $requestUrl |
|
| 75 | - * |
|
| 76 | - * @return mixed |
|
| 77 | - */ |
|
| 78 | - public function methodMatch($method, $requestMethod, $routeString, $requestUrl) |
|
| 79 | - { |
|
| 80 | - $method = strtolower($method); |
|
| 81 | - $requestMethod = strtolower($requestMethod); |
|
| 82 | - $methods = explode('|', $method); |
|
| 83 | - |
|
| 84 | - if (in_array($requestMethod, $methods)) { |
|
| 85 | - if ($routeString == '*') { |
|
| 86 | - return true; |
|
| 87 | - } elseif (isset($routeString[0]) && $routeString[0] == '@') { |
|
| 88 | - $match = preg_match('`' . substr($routeString, 1) . '`u', $requestUrl, $this->params); |
|
| 89 | - return $match; |
|
| 90 | - } elseif (($position = strpos($routeString, '[')) === false) { |
|
| 91 | - return strcmp($requestUrl, $routeString) === 0; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - if (strncmp($requestUrl, $routeString, $position) !== 0) { |
|
| 95 | - return false; |
|
| 96 | - } |
|
| 97 | - $regex = $this->compileRoute($routeString, $requestUrl); |
|
| 98 | - return preg_match($regex, $requestUrl, $this->params); |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return false; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Compile the regex for a given route (EXPENSIVE) |
|
| 106 | - */ |
|
| 107 | - private function compileRoute($routeString, $requestUrl) |
|
| 108 | - { |
|
| 109 | - $route = $this->getRoute($routeString, $requestUrl); |
|
| 110 | - |
|
| 111 | - if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 112 | - $matchTypes = $this->matchTypes; |
|
| 113 | - foreach ($matches as $match) { |
|
| 114 | - list($block, $pre, $type, $param, $optional) = $match; |
|
| 115 | - $pattern = $this->getRoutePattern($matchTypes, $pre, $type, $param, $optional); |
|
| 116 | - $route = str_replace($block, $pattern, $route); |
|
| 117 | - } |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - return "`^$route$`u"; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - private function getRoutePattern($matchTypes, $pre, $type, $param, $optional) |
|
| 124 | - { |
|
| 125 | - if (isset($matchTypes[$type])) { |
|
| 126 | - $type = $matchTypes[$type]; |
|
| 127 | - } |
|
| 128 | - if ($pre === '.') { |
|
| 129 | - $pre = '\.'; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - //Older versions of PCRE require the 'P' in (?P<named>) |
|
| 133 | - return '(?:' |
|
| 134 | - . ($pre !== '' ? $pre : null) |
|
| 135 | - . '(' |
|
| 136 | - . ($param !== '' ? "?P<$param>" : null) |
|
| 137 | - . $type |
|
| 138 | - . '))' |
|
| 139 | - . ($optional !== '' ? '?' : null); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - /** |
|
| 143 | - * @param $routeString |
|
| 144 | - * @param $requestUrl |
|
| 145 | - * |
|
| 146 | - * @return bool|string |
|
| 147 | - */ |
|
| 148 | - private function getRoute($routeString, $requestUrl) |
|
| 149 | - { |
|
| 150 | - $iPointer = $jPointer = 0; |
|
| 151 | - $nPointer = isset($routeString[0]) ? $routeString[0] : null; |
|
| 152 | - $regex = $route = false; |
|
| 153 | - |
|
| 154 | - // Find the longest non-regex substring and match it against the URI |
|
| 155 | - while (true) { |
|
| 156 | - if (!isset($routeString[$iPointer])) { |
|
| 157 | - break; |
|
| 158 | - } |
|
| 159 | - if ($regex === false) { |
|
| 160 | - if (!$this->getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) { |
|
| 161 | - continue; |
|
| 162 | - } |
|
| 163 | - $jPointer++; |
|
| 164 | - } |
|
| 165 | - $route .= $routeString[$iPointer++]; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return $route; |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - private function getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl) |
|
| 172 | - { |
|
| 173 | - $cPointer = $nPointer; |
|
| 174 | - $regex = in_array($cPointer, array('[', '(', '.')); |
|
| 175 | - if (!$regex && isset($routeString[$iPointer+1])) { |
|
| 176 | - $nPointer = $routeString[$iPointer + 1]; |
|
| 177 | - $regex = in_array($nPointer, array('?', '+', '*', '{')); |
|
| 178 | - } |
|
| 179 | - if (!$regex && $cPointer !== '/' && (!isset($requestUrl[$jPointer]) || $cPointer !== $requestUrl[$jPointer])) { |
|
| 180 | - return false; |
|
| 181 | - } |
|
| 182 | - return true; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * @return array |
|
| 187 | - */ |
|
| 188 | - public function getParams() |
|
| 189 | - { |
|
| 190 | - return $this->params; |
|
| 191 | - } |
|
| 192 | - |
|
| 193 | - public function getMatchTypes() |
|
| 194 | - { |
|
| 195 | - return $this->matchTypes; |
|
| 196 | - } |
|
| 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 | + $block = $match[0]; |
|
| 52 | + $pre = $match[1]; |
|
| 53 | + $param = $match[3]; |
|
| 54 | + |
|
| 55 | + if ($pre) { |
|
| 56 | + $block = substr($block, 1); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + if (isset($params[$param])) { |
|
| 60 | + $url = str_replace($block, $params[$param], $url); |
|
| 61 | + } elseif ($match[4]) { |
|
| 62 | + $url = str_replace($pre . $block, '', $url); |
|
| 63 | + } |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + return $url; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * @param $method |
|
| 72 | + * @param $requestMethod |
|
| 73 | + * @param string $routeString |
|
| 74 | + * @param $requestUrl |
|
| 75 | + * |
|
| 76 | + * @return mixed |
|
| 77 | + */ |
|
| 78 | + public function methodMatch($method, $requestMethod, $routeString, $requestUrl) |
|
| 79 | + { |
|
| 80 | + $method = strtolower($method); |
|
| 81 | + $requestMethod = strtolower($requestMethod); |
|
| 82 | + $methods = explode('|', $method); |
|
| 83 | + |
|
| 84 | + if (in_array($requestMethod, $methods)) { |
|
| 85 | + if ($routeString == '*') { |
|
| 86 | + return true; |
|
| 87 | + } elseif (isset($routeString[0]) && $routeString[0] == '@') { |
|
| 88 | + $match = preg_match('`' . substr($routeString, 1) . '`u', $requestUrl, $this->params); |
|
| 89 | + return $match; |
|
| 90 | + } elseif (($position = strpos($routeString, '[')) === false) { |
|
| 91 | + return strcmp($requestUrl, $routeString) === 0; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + if (strncmp($requestUrl, $routeString, $position) !== 0) { |
|
| 95 | + return false; |
|
| 96 | + } |
|
| 97 | + $regex = $this->compileRoute($routeString, $requestUrl); |
|
| 98 | + return preg_match($regex, $requestUrl, $this->params); |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return false; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Compile the regex for a given route (EXPENSIVE) |
|
| 106 | + */ |
|
| 107 | + private function compileRoute($routeString, $requestUrl) |
|
| 108 | + { |
|
| 109 | + $route = $this->getRoute($routeString, $requestUrl); |
|
| 110 | + |
|
| 111 | + if (preg_match_all('`(/|\.|)\[([^:\]]*+)(?::([^:\]]*+))?\](\?|)`', $route, $matches, PREG_SET_ORDER)) { |
|
| 112 | + $matchTypes = $this->matchTypes; |
|
| 113 | + foreach ($matches as $match) { |
|
| 114 | + list($block, $pre, $type, $param, $optional) = $match; |
|
| 115 | + $pattern = $this->getRoutePattern($matchTypes, $pre, $type, $param, $optional); |
|
| 116 | + $route = str_replace($block, $pattern, $route); |
|
| 117 | + } |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + return "`^$route$`u"; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + private function getRoutePattern($matchTypes, $pre, $type, $param, $optional) |
|
| 124 | + { |
|
| 125 | + if (isset($matchTypes[$type])) { |
|
| 126 | + $type = $matchTypes[$type]; |
|
| 127 | + } |
|
| 128 | + if ($pre === '.') { |
|
| 129 | + $pre = '\.'; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + //Older versions of PCRE require the 'P' in (?P<named>) |
|
| 133 | + return '(?:' |
|
| 134 | + . ($pre !== '' ? $pre : null) |
|
| 135 | + . '(' |
|
| 136 | + . ($param !== '' ? "?P<$param>" : null) |
|
| 137 | + . $type |
|
| 138 | + . '))' |
|
| 139 | + . ($optional !== '' ? '?' : null); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + /** |
|
| 143 | + * @param $routeString |
|
| 144 | + * @param $requestUrl |
|
| 145 | + * |
|
| 146 | + * @return bool|string |
|
| 147 | + */ |
|
| 148 | + private function getRoute($routeString, $requestUrl) |
|
| 149 | + { |
|
| 150 | + $iPointer = $jPointer = 0; |
|
| 151 | + $nPointer = isset($routeString[0]) ? $routeString[0] : null; |
|
| 152 | + $regex = $route = false; |
|
| 153 | + |
|
| 154 | + // Find the longest non-regex substring and match it against the URI |
|
| 155 | + while (true) { |
|
| 156 | + if (!isset($routeString[$iPointer])) { |
|
| 157 | + break; |
|
| 158 | + } |
|
| 159 | + if ($regex === false) { |
|
| 160 | + if (!$this->getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl)) { |
|
| 161 | + continue; |
|
| 162 | + } |
|
| 163 | + $jPointer++; |
|
| 164 | + } |
|
| 165 | + $route .= $routeString[$iPointer++]; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return $route; |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + private function getRouteRegexCheck($nPointer, $jPointer, $iPointer, $routeString, $requestUrl) |
|
| 172 | + { |
|
| 173 | + $cPointer = $nPointer; |
|
| 174 | + $regex = in_array($cPointer, array('[', '(', '.')); |
|
| 175 | + if (!$regex && isset($routeString[$iPointer+1])) { |
|
| 176 | + $nPointer = $routeString[$iPointer + 1]; |
|
| 177 | + $regex = in_array($nPointer, array('?', '+', '*', '{')); |
|
| 178 | + } |
|
| 179 | + if (!$regex && $cPointer !== '/' && (!isset($requestUrl[$jPointer]) || $cPointer !== $requestUrl[$jPointer])) { |
|
| 180 | + return false; |
|
| 181 | + } |
|
| 182 | + return true; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * @return array |
|
| 187 | + */ |
|
| 188 | + public function getParams() |
|
| 189 | + { |
|
| 190 | + return $this->params; |
|
| 191 | + } |
|
| 192 | + |
|
| 193 | + public function getMatchTypes() |
|
| 194 | + { |
|
| 195 | + return $this->matchTypes; |
|
| 196 | + } |
|
| 197 | 197 | } |
@@ -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 | } |
@@ -6,190 +6,190 @@ |
||
| 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 | - * |
|
| 56 | - * Generate the URL for a named route. Replace regexes with supplied parameters |
|
| 57 | - * |
|
| 58 | - * @param string $routeName The name of the route. |
|
| 59 | - * @param array $params Associative array of parameters to replace placeholders with. |
|
| 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 | - * @return array|boolean Array with route information on success, false on failure (no match). |
|
| 76 | - */ |
|
| 77 | - public function match($requestUrl = null, $requestMethod = null) |
|
| 78 | - { |
|
| 79 | - $requestUrl = $this->getRequestUrl($requestUrl); |
|
| 80 | - |
|
| 81 | - // set Request Method if it isn't passed as a parameter |
|
| 82 | - if (is_null($requestMethod)) { |
|
| 83 | - $requestMethod = $this->server['REQUEST_METHOD']; |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - foreach ($this->routes as $handler) { |
|
| 87 | - if (!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) { |
|
| 88 | - continue; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - return array( |
|
| 92 | - 'target' => $handler[2], |
|
| 93 | - 'params' => array_filter($this->parser->getParams(), function ($k) { |
|
| 94 | - return !is_numeric($k); |
|
| 95 | - }, ARRAY_FILTER_USE_KEY), |
|
| 96 | - 'name' => $handler[3] |
|
| 97 | - ); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - return false; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * @param $method |
|
| 105 | - * @param $arguments |
|
| 106 | - * @throws RouterException |
|
| 107 | - */ |
|
| 108 | - public function __call($method, $arguments) |
|
| 109 | - { |
|
| 110 | - if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
|
| 111 | - throw new RouterException($method . ' not exist in the '. __CLASS__); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - $methods = $method == 'all' ? implode('|', $this->all) : $method; |
|
| 115 | - |
|
| 116 | - $route = array_merge([$methods], $arguments); |
|
| 117 | - |
|
| 118 | - call_user_func_array([$this, 'map'], $route); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - /** |
|
| 122 | - * Retrieves all routes. |
|
| 123 | - * Useful if you want to process or display routes. |
|
| 124 | - * @return array All routes. |
|
| 125 | - */ |
|
| 126 | - public function getRoutes() |
|
| 127 | - { |
|
| 128 | - return $this->routes; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - /** |
|
| 132 | - * Add multiple routes at once from array in the following format: |
|
| 133 | - * |
|
| 134 | - * $routes = array( |
|
| 135 | - * array($method, $route, $target, $name) |
|
| 136 | - * ); |
|
| 137 | - * |
|
| 138 | - * @param array|Traversable $routes |
|
| 139 | - * @return void |
|
| 140 | - * @author Koen Punt |
|
| 141 | - */ |
|
| 142 | - public function setRoutes($routes) |
|
| 143 | - { |
|
| 144 | - if (!is_array($routes) && !$routes instanceof Traversable) { |
|
| 145 | - throw new RouterException('Routes should be an array or an instance of Traversable'); |
|
| 146 | - } |
|
| 147 | - if (!empty($routes)) { |
|
| 148 | - foreach ($routes as $route) { |
|
| 149 | - call_user_func_array(array($this, 'map'), $route); |
|
| 150 | - } |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - |
|
| 154 | - /** |
|
| 155 | - * Set the base path. |
|
| 156 | - * Useful if you are running your application from a subdirectory. |
|
| 157 | - */ |
|
| 158 | - public function setBasePath($basePath) |
|
| 159 | - { |
|
| 160 | - $this->basePath = $basePath; |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * @param $routeName |
|
| 165 | - * @param $message |
|
| 166 | - * @param bool $cmpTo |
|
| 167 | - * @throws RouterException |
|
| 168 | - */ |
|
| 169 | - private function handleException($routeName, $message, $cmpTo) |
|
| 170 | - { |
|
| 171 | - if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) { |
|
| 172 | - throw new RouterException(sprintf($message, $routeName)); |
|
| 173 | - } |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - /** |
|
| 177 | - * @param $requestUrl |
|
| 178 | - * |
|
| 179 | - * @return mixed |
|
| 180 | - */ |
|
| 181 | - private function getRequestUrl($requestUrl = null) |
|
| 182 | - { |
|
| 183 | - // set Request Url if it isn't passed as parameter |
|
| 184 | - if (is_null($requestUrl)) { |
|
| 185 | - $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - return str_replace($this->basePath, '', strtok($requestUrl, '?')); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - public function getParser() |
|
| 192 | - { |
|
| 193 | - return $this->parser; |
|
| 194 | - } |
|
| 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 | + * |
|
| 56 | + * Generate the URL for a named route. Replace regexes with supplied parameters |
|
| 57 | + * |
|
| 58 | + * @param string $routeName The name of the route. |
|
| 59 | + * @param array $params Associative array of parameters to replace placeholders with. |
|
| 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 | + * @return array|boolean Array with route information on success, false on failure (no match). |
|
| 76 | + */ |
|
| 77 | + public function match($requestUrl = null, $requestMethod = null) |
|
| 78 | + { |
|
| 79 | + $requestUrl = $this->getRequestUrl($requestUrl); |
|
| 80 | + |
|
| 81 | + // set Request Method if it isn't passed as a parameter |
|
| 82 | + if (is_null($requestMethod)) { |
|
| 83 | + $requestMethod = $this->server['REQUEST_METHOD']; |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + foreach ($this->routes as $handler) { |
|
| 87 | + if (!$this->parser->methodMatch($handler[0], $requestMethod, $handler[1], $requestUrl)) { |
|
| 88 | + continue; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + return array( |
|
| 92 | + 'target' => $handler[2], |
|
| 93 | + 'params' => array_filter($this->parser->getParams(), function ($k) { |
|
| 94 | + return !is_numeric($k); |
|
| 95 | + }, ARRAY_FILTER_USE_KEY), |
|
| 96 | + 'name' => $handler[3] |
|
| 97 | + ); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + return false; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * @param $method |
|
| 105 | + * @param $arguments |
|
| 106 | + * @throws RouterException |
|
| 107 | + */ |
|
| 108 | + public function __call($method, $arguments) |
|
| 109 | + { |
|
| 110 | + if (!in_array($method, array('get', 'post', 'delete', 'put', 'patch', 'update', 'all'))) { |
|
| 111 | + throw new RouterException($method . ' not exist in the '. __CLASS__); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + $methods = $method == 'all' ? implode('|', $this->all) : $method; |
|
| 115 | + |
|
| 116 | + $route = array_merge([$methods], $arguments); |
|
| 117 | + |
|
| 118 | + call_user_func_array([$this, 'map'], $route); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + /** |
|
| 122 | + * Retrieves all routes. |
|
| 123 | + * Useful if you want to process or display routes. |
|
| 124 | + * @return array All routes. |
|
| 125 | + */ |
|
| 126 | + public function getRoutes() |
|
| 127 | + { |
|
| 128 | + return $this->routes; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + /** |
|
| 132 | + * Add multiple routes at once from array in the following format: |
|
| 133 | + * |
|
| 134 | + * $routes = array( |
|
| 135 | + * array($method, $route, $target, $name) |
|
| 136 | + * ); |
|
| 137 | + * |
|
| 138 | + * @param array|Traversable $routes |
|
| 139 | + * @return void |
|
| 140 | + * @author Koen Punt |
|
| 141 | + */ |
|
| 142 | + public function setRoutes($routes) |
|
| 143 | + { |
|
| 144 | + if (!is_array($routes) && !$routes instanceof Traversable) { |
|
| 145 | + throw new RouterException('Routes should be an array or an instance of Traversable'); |
|
| 146 | + } |
|
| 147 | + if (!empty($routes)) { |
|
| 148 | + foreach ($routes as $route) { |
|
| 149 | + call_user_func_array(array($this, 'map'), $route); |
|
| 150 | + } |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + |
|
| 154 | + /** |
|
| 155 | + * Set the base path. |
|
| 156 | + * Useful if you are running your application from a subdirectory. |
|
| 157 | + */ |
|
| 158 | + public function setBasePath($basePath) |
|
| 159 | + { |
|
| 160 | + $this->basePath = $basePath; |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * @param $routeName |
|
| 165 | + * @param $message |
|
| 166 | + * @param bool $cmpTo |
|
| 167 | + * @throws RouterException |
|
| 168 | + */ |
|
| 169 | + private function handleException($routeName, $message, $cmpTo) |
|
| 170 | + { |
|
| 171 | + if (array_key_exists($routeName, $this->namedRoutes) === $cmpTo) { |
|
| 172 | + throw new RouterException(sprintf($message, $routeName)); |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + /** |
|
| 177 | + * @param $requestUrl |
|
| 178 | + * |
|
| 179 | + * @return mixed |
|
| 180 | + */ |
|
| 181 | + private function getRequestUrl($requestUrl = null) |
|
| 182 | + { |
|
| 183 | + // set Request Url if it isn't passed as parameter |
|
| 184 | + if (is_null($requestUrl)) { |
|
| 185 | + $requestUrl = parse_url($this->server['REQUEST_URI'], PHP_URL_PATH); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + return str_replace($this->basePath, '', strtok($requestUrl, '?')); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + public function getParser() |
|
| 192 | + { |
|
| 193 | + return $this->parser; |
|
| 194 | + } |
|
| 195 | 195 | } |
@@ -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; |