Merge pull request #235 from rootcoma/reports

Formatting for readability
This commit is contained in:
tannn 2013-12-07 09:09:42 -08:00
commit ed6f38afdc
2 changed files with 22 additions and 24 deletions

View File

@ -16,7 +16,9 @@ class Route
$url = (string) $url;
// make sure that the URL is suffixed with a forward slash
if(substr($url,-1) !== '/') $url .= '/';
if(substr($url,-1) !== '/') {
$url .= '/';
}
$this->url = $url;
}
@ -55,10 +57,10 @@ class Route
private function substituteFilter($matches) {
if (isset($matches[1]) && isset($this->filters[$matches[1]])) {
return $this->filters[$matches[1]];
}
return "([\w-]+)";
return $this->filters[$matches[1]];
}
return "([\w-]+)";
}
public function getParameters() {

View File

@ -11,9 +11,7 @@ class Router
public function map($routeUrl, $target = '', array $args = array()) {
$route = new Route();
$route->setUrl($this->basePath . $routeUrl);
$route->setTarget($target);
if(isset($args['methods'])) {
@ -36,11 +34,16 @@ class Router
}
public function matchCurrentRequest() {
$requestMethod = (isset($_POST['_method']) && ($_method = strtoupper($_POST['_method'])) && in_array($_method,array('PUT','DELETE'))) ? $_method : $_SERVER['REQUEST_METHOD'];
$requestMethod = $_SERVER['REQUEST_METHOD'];
if(isset($_POST['_method']) && in_array(strtoupper($_POST['_method']),array('PUT','DELETE'))) {
$requestMethod = strtoupper($_POST['_method']);
}
$requestUrl = $_SERVER['REQUEST_URI'];
// strip GET variables from URL
if(($pos = strpos($requestUrl, '?')) !== false) {
$pos = strpos($requestUrl, '?');
if($pos !== false) {
$requestUrl = substr($requestUrl, 0, $pos);
}
@ -50,17 +53,18 @@ class Router
public function match($requestUrl, $requestMethod = 'GET') {
foreach($this->routes as $route) {
// compare server request method with route's allowed http methods
if(!in_array($requestMethod, $route->getMethods())) continue;
if(!in_array($requestMethod, $route->getMethods())) {
continue;
}
// check if request url matches route regex. if not, return false.
if (!preg_match("@^".$route->getRegex()."*$@i", $requestUrl, $matches)) continue;
if (!preg_match("@^".$route->getRegex()."*$@i", $requestUrl, $matches)) {
continue;
}
$params = array();
if (preg_match_all("/:([\w-]+)/", $route->getUrl(), $argument_keys)) {
// grab array with matches
$argument_keys = $argument_keys[1];
@ -71,31 +75,24 @@ class Router
}
}
}
$route->setParameters($params);
return $route;
}
return false;
}
public function generate($routeName, array $params = array()) {
// Check if route exists
if (!isset($this->namedRoutes[$routeName]))
if (!isset($this->namedRoutes[$routeName])) {
throw new Exception("No route with the name $routeName has been found.");
}
$route = $this->namedRoutes[$routeName];
$url = $route->getUrl();
// replace route url with given parameters
if ($params && preg_match_all("/:(\w+)/", $url, $param_keys)) {
// grab array with matches
$param_keys = $param_keys[1];
// loop trough parameter names, store matching value in $params array
foreach ($param_keys as $i => $key) {
if (isset($params[$key])) {
@ -103,7 +100,6 @@ class Router
}
}
}
return $url;
}
}