Refactor Router (remove getUri method. All logic moved to getRoute method). Modified RouterTest.

This commit is contained in:
Alexander Demidov
2012-05-25 19:03:42 +04:00
parent 6e4a9ef23a
commit 9ef37d003b
2 changed files with 38 additions and 65 deletions

View File

@ -57,18 +57,29 @@ class Router
}
/**
* @param null $name
* @return Route
* @throws ErrorException
*/
public function getRoute()
public function getRoute($name = null)
{
return $this->route;
if (is_null($name)) {
return $this->route;
} else {
if ($this->routeIsExists($name)) {
return $this->getRouteByName($name);
} else {
$btrace = debug_backtrace();
throw new ErrorException('Unknown route name: "' . $name . '". ' . 'Call from "' . $btrace[0]['file'] . '" on line ' . $btrace[0]['line'] . '.');
}
}
}
/**
* @param $name
* @return bool
*/
protected function routeIsExists($name)
public function routeIsExists($name)
{
return array_key_exists($name, $this->routes);
}
@ -81,24 +92,4 @@ class Router
{
return $this->routes[$name];
}
/**
* @param null $route_name
* @return string
* @throws ErrorException
*/
public function getUri($route_name = null)
{
if (is_null($route_name)) {
$route = $this->getRoute();
} else {
if ($this->routeIsExists($route_name)) {
$route = $this->getRouteByName($route_name);
} else {
$btrace = debug_backtrace();
throw new ErrorException('Unknown route name: "' . $route_name . '". ' . 'Call from "' . $btrace[0]['file'] . '" on line ' . $btrace[0]['line'] . '.');
}
}
return $route->getUri();
}
}