Commit 5e40e296 authored by Taylor Otwell's avatar Taylor Otwell

refactoring routing and adding comments.

parent 30514d90
...@@ -14,6 +14,13 @@ class Route { ...@@ -14,6 +14,13 @@ class Route {
*/ */
public $key; public $key;
/**
* The URIs the route responds to.
*
* @var array
*/
public $uris;
/** /**
* The route callback or array. * The route callback or array.
* *
...@@ -56,6 +63,15 @@ class Route { ...@@ -56,6 +63,15 @@ class Route {
$this->callback = $callback; $this->callback = $callback;
$this->parameters = $parameters; $this->parameters = $parameters;
$this->controller_path = $controller_path; $this->controller_path = $controller_path;
// Extract each URI handled by the URI. These will be used to find the route by
// URI when requested. The leading slash will be removed for convenience.
foreach (explode(', ', $key) as $segment)
{
$segment = substr($segment, strpos($segment, ' ') + 1);
$this->uris[] = ($segment !== '/') ? trim($segment, '/') : $segment;
}
} }
/** /**
...@@ -66,8 +82,14 @@ class Route { ...@@ -66,8 +82,14 @@ class Route {
*/ */
public function call(Application $application) public function call(Application $application)
{ {
$this->validate(); if ( ! $this->callback instanceof Closure and ! is_array($this->callback))
{
throw new \Exception('Invalid route defined for URI ['.$this->key.']');
}
// Run the "before" filters for the route. If a before filter returns a value, that value
// will be considered the response to the request and the route function / controller will
// not be used to handle the request.
if ( ! is_null($response = $this->filter(array_merge($this->before(), array('before')), array($application), true))) if ( ! is_null($response = $this->filter(array_merge($this->before(), array('before')), array($application), true)))
{ {
return $this->finish($application, $response); return $this->finish($application, $response);
...@@ -80,19 +102,6 @@ class Route { ...@@ -80,19 +102,6 @@ class Route {
return $this->finish($application, $application->responder->error('404')); return $this->finish($application, $application->responder->error('404'));
} }
/**
* Validate that a given route is callable.
*
* @return void
*/
protected function validate()
{
if ( ! $this->callback instanceof Closure and ! is_array($this->callback))
{
throw new \Exception('Invalid route defined for URI ['.$this->key.']');
}
}
/** /**
* Extract the route closure from the route. * Extract the route closure from the route.
* *
...@@ -121,6 +130,9 @@ class Route { ...@@ -121,6 +130,9 @@ class Route {
$response = call_user_func_array($closure, $this->parameters); $response = call_user_func_array($closure, $this->parameters);
// If the route closure returns an array, we assume that they are returning a
// reference to a controller and method and will use the given controller method
// to handle the request to the application.
if (is_array($response)) if (is_array($response))
{ {
$response = $this->delegate($application, $response[0], $response[1], $this->parameters); $response = $this->delegate($application, $response[0], $response[1], $this->parameters);
...@@ -142,7 +154,7 @@ class Route { ...@@ -142,7 +154,7 @@ class Route {
{ {
if ( ! file_exists($path = $this->controller_path.strtolower(str_replace('.', '/', $controller)).EXT)) if ( ! file_exists($path = $this->controller_path.strtolower(str_replace('.', '/', $controller)).EXT))
{ {
throw new \Exception("Controller [$controller] is not defined."); throw new \Exception("Controller [$controller] does not exist.");
} }
require $path; require $path;
...@@ -158,6 +170,9 @@ class Route { ...@@ -158,6 +170,9 @@ class Route {
$response = $controller->before(); $response = $controller->before();
} }
// Again, as was the case with route closures, if the controller "before" method returns
// a response, it will be considered the response to the request and the controller method
// will not be used to handle the request to the application.
return (is_null($response)) ? call_user_func_array(array($controller, $method), $parameters) : $response; return (is_null($response)) ? call_user_func_array(array($controller, $method), $parameters) : $response;
} }
...@@ -170,10 +185,7 @@ class Route { ...@@ -170,10 +185,7 @@ class Route {
*/ */
protected function resolve(Container $container, $controller) protected function resolve(Container $container, $controller)
{ {
if ($container->registered('controllers.'.$controller)) if ($container->registered('controllers.'.$controller)) return $container->resolve('controllers.'.$controller);
{
return $container->resolve('controllers.'.$controller);
}
$controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller'; $controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
......
...@@ -93,6 +93,18 @@ class View_Factory { ...@@ -93,6 +93,18 @@ class View_Factory {
$this->composer = $composer; $this->composer = $composer;
} }
/**
* Create a new view instance.
*
* @param string $view
* @param array $data
* @return View
*/
public function make($view, $data = array())
{
return new View($view, $data, $this->path($view), $this->composer, $this);
}
/** /**
* Create a new view instance from a view name. * Create a new view instance from a view name.
* *
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment