refactoring routing and comments.

This commit is contained in:
Taylor Otwell
2011-10-15 14:04:11 -05:00
parent a44ca9d53b
commit a6eaa06981
20 changed files with 368 additions and 265 deletions

View File

@@ -59,25 +59,18 @@ abstract class Controller {
$response = call_user_func_array(array($controller, $method), $parameters);
}
$filters = array_merge($controller->filters('after'), array('after'));
// The after filter and the framework expects all responses to
// be instances of the Response class. If the route did not
// return an instsance of Response, we will make on now.
if ( ! $response instanceof Response) $response = new Response($response);
$filters = array_merge($this->filters('after'), array('after'));
Filter::run($filters, array($response));
return $response;
}
/**
* Determine if a given controller method is callable.
*
* @param string $method
* @return bool
*/
protected static function hidden($method)
{
return $method == 'before' or $method == 'after' or strncmp($method, '_', 1) == 0;
}
/**
* Resolve a controller name to a controller instance.
*
@@ -123,6 +116,17 @@ abstract class Controller {
return false;
}
/**
* Determine if a given controller method is callable.
*
* @param string $method
* @return bool
*/
protected static function hidden($method)
{
return $method == 'before' or $method == 'after' or strncmp($method, '_', 1) == 0;
}
/**
* Get an array of filter names defined for the destination.
*

View File

@@ -50,25 +50,28 @@ class Loader {
{
$routes = (file_exists($path = $this->base.'routes'.EXT)) ? require $path : array();
return array_merge($this->nested(Arr::without(explode('/', $uri), array(''))), $routes);
$segments = Arr::without(explode('/', $uri), '');
return array_merge($this->nested($segments), $routes);
}
/**
* Get the appropriate routes from the routes directory for a given URI.
*
* This method works backwards through the URI segments until we find the
* deepest possible matching route directory. Once the deepest directory
* is found, all of the applicable routes will be returend.
*
* @param array $segments
* @return array
*/
protected function nested($segments)
{
// Work backwards through the URI segments until we find the deepest possible
// matching route directory. Once we find it, we will return those routes.
foreach (array_reverse($segments, true) as $key => $value)
{
if (file_exists($path = $this->nest.implode('/', array_slice($segments, 0, $key + 1)).EXT))
{
return require $path;
}
$path = $this->nest.implode('/', array_slice($segments, 0, $key + 1)).EXT;
if (file_exists($path)) return require $path;
}
return array();
@@ -77,6 +80,10 @@ class Loader {
/**
* Get every route defined for the application.
*
* The entire routes directory will be searched recursively to gather
* every route for the application. Of course, the routes in the root
* routes file will be returned as well.
*
* @return array
*/
public function everything()
@@ -90,8 +97,8 @@ class Loader {
$routes = array_merge($routes, require $path);
}
// Since route files can be nested deep within the route directory,
// we need to recursively spin through each directory
if ( ! is_dir($this->nest)) return $routes;
$iterator = new Iterator(new DirectoryIterator($this->nest), Iterator::SELF_FIRST);
foreach ($iterator as $file)

View File

@@ -104,19 +104,24 @@ class Route {
{
if ($response instanceof Delegate)
{
return $response;
return Controller::call($response, $this->parameters);
}
else
{
// The after filter and the framework expects all responses to
// be instances of the Response class. If the route did not
// return an instsance of Response, we will make on now.
if ( ! $response instanceof Response) $response = new Response($response);
$filters = array_merge($this->filters('after'), array('after'));
$filters = array_merge($this->filters('after'), array('after'));
Filter::run($filters, array($response));
Filter::run($filters, array($response));
return $response;
}
else
{
return Response::error('404');
return $response;
}
}
return Response::error('404');
}
/**

View File

@@ -88,13 +88,8 @@ class Router {
*/
public function find($name)
{
// First we will check the cache of route names. If we have already found the given route,
// we will simply return that route from the cache to improve performance.
if (array_key_exists($name, $this->names)) return $this->names[$name];
// Spin through every route defined for the application searching for a route that has
// a name matching the name passed to the method. If the route is found, it will be
// cached in the array of named routes and returned.
foreach ($this->loader->everything() as $key => $value)
{
if (is_array($value) and isset($value['name']) and $value['name'] === $name)
@@ -116,11 +111,10 @@ class Router {
$routes = $this->loader->load($uri);
// Put the request method and URI in route form. Routes begin with
// the request method and a forward slash.
// the request method and a forward slash followed by the URI.
$destination = $method.' /'.trim($uri, '/');
// Check for a literal route match first. If we find one, there is
// no need to spin through all of the routes.
// Check for a literal route match first...
if (isset($routes[$destination]))
{
return Request::$route = new Route($destination, $routes[$destination], array());
@@ -130,22 +124,42 @@ class Router {
{
// Only check routes that have multiple URIs or wildcards since other
// routes would have been caught by the check for literal matches.
if (strpos($keys, '(') !== false or strpos($keys, ',') !== false )
if (strpos($keys, '(') !== false or strpos($keys, ',') !== false)
{
foreach (explode(', ', $keys) as $key)
if ( ! is_null($route = $this->match($destination, $keys, $callback)))
{
// Append the provided formats to the route as an optional regular expression.
if ( ! is_null($formats = $this->provides($callback))) $key .= '(\.('.implode('|', $formats).'))?';
if (preg_match('#^'.$this->translate_wildcards($key).'$#', $destination))
{
return Request::$route = new Route($keys, $callback, $this->parameters($destination, $key));
}
}
return Request::$route = $route;
}
}
}
return Request::$route = $this->route_to_controller($method, $uri, $destination);
return Request::$route = $this->controller($method, $uri, $destination);
}
/**
* Attempt to match a given route destination to a given route.
*
* The destination's methods and URIs will be compared against the route's.
* If there is a match, the Route instance will be returned, otherwise null
* will be returned by the method.
*
* @param string $destination
* @param array $keys
* @param mixed $callback
* @return mixed
*/
protected function match($destination, $keys, $callback)
{
foreach (explode(', ', $keys) as $key)
{
// Append the provided formats to the route as an optional regular expression.
if ( ! is_null($formats = $this->provides($callback))) $key .= '(\.('.implode('|', $formats).'))?';
if (preg_match('#^'.$this->wildcards($key).'$#', $destination))
{
return new Route($keys, $callback, $this->parameters($destination, $key));
}
}
}
/**
@@ -156,10 +170,11 @@ class Router {
* @param string $destination
* @return Route
*/
protected function route_to_controller($method, $uri, $destination)
protected function controller($method, $uri, $destination)
{
// If the request is to the root of the application, an ad-hoc route will be generated
// to the home controller's "index" method, making it the default controller method.
// If the request is to the root of the application, an ad-hoc route
// will be generated to the home controller's "index" method, making
// it the default controller method.
if ($uri === '/') return new Route($method.' /', 'home@index');
$segments = explode('/', trim($uri, '/'));
@@ -228,7 +243,7 @@ class Router {
* @param string $key
* @return string
*/
protected function translate_wildcards($key)
protected function wildcards($key)
{
$replacements = 0;