@@ -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], |
@@ -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 | } |
@@ -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>'; |