more refactoring and dependency injection.
This commit is contained in:
@@ -14,23 +14,32 @@ class Caller {
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* The path to the application controllers.
|
||||
* The route filterer instance.
|
||||
*
|
||||
* @var string
|
||||
* @var Filterer
|
||||
*/
|
||||
protected $controller_path;
|
||||
protected $filterer;
|
||||
|
||||
/**
|
||||
* The route delegator instance.
|
||||
*
|
||||
* @var Delegator
|
||||
*/
|
||||
protected $delegator;
|
||||
|
||||
/**
|
||||
* Create a new route caller instance.
|
||||
*
|
||||
* @param Container $container
|
||||
* @param string $controller_path
|
||||
* @param Filterer $filterer
|
||||
* @param Delegator $delegator
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Container $container, $controller_path)
|
||||
public function __construct(Container $container, Filterer $filterer, Delegator $delegator)
|
||||
{
|
||||
$this->filterer = $filterer;
|
||||
$this->container = $container;
|
||||
$this->controller_path = $controller_path;
|
||||
$this->delegator = $delegator;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,116 +55,41 @@ class Caller {
|
||||
throw new \Exception('Invalid route defined for URI ['.$route->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.
|
||||
$before = array_merge($route->before(), array('before'));
|
||||
|
||||
if ( ! is_null($response = $this->filter($route, $before, array(), true)))
|
||||
if ( ! is_null($response = $this->before($route)))
|
||||
{
|
||||
return $this->finish($route, $response);
|
||||
}
|
||||
|
||||
$closure = ( ! $route->callback instanceof Closure) ? $this->find_route_closure($route) : $route->callback;
|
||||
if ( ! is_null($response = $route->call($this->container)))
|
||||
{
|
||||
if (is_array($response)) $response = $this->delegator->delegate($route, $response);
|
||||
|
||||
if ( ! is_null($closure)) return $this->handle_closure($route, $closure);
|
||||
return $this->finish($route, $response);
|
||||
}
|
||||
|
||||
return $this->finish($route, $this->container->resolve('laravel.response')->error('404'));
|
||||
return $this->finish($route, $this->container->response->error('404'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the route closure from the route.
|
||||
* Run the "before" filters for the route.
|
||||
*
|
||||
* If a "do" index is specified on the callback, that is the handler.
|
||||
* Otherwise, we will return the first callable array value.
|
||||
* 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.
|
||||
*
|
||||
* @param Route $route
|
||||
* @return Closure
|
||||
*/
|
||||
protected function find_route_closure(Route $route)
|
||||
{
|
||||
if (isset($route->callback['do'])) return $route->callback['do'];
|
||||
|
||||
foreach ($route->callback as $value) { if ($value instanceof Closure) return $value; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a route closure.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param Closure $closure
|
||||
* @param Route $route
|
||||
* @return mixed
|
||||
*/
|
||||
protected function handle_closure(Route $route, Closure $closure)
|
||||
protected function before(Route $route)
|
||||
{
|
||||
$response = call_user_func_array($closure, $route->parameters);
|
||||
$before = array_merge(array('before'), $route->filters('before'));
|
||||
|
||||
// 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))
|
||||
{
|
||||
$response = $this->delegate($route, $response[0], $response[1], $route->parameters);
|
||||
}
|
||||
|
||||
return $this->finish($route, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the delegation of a route to a controller method.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param string $controller
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
* @return Response
|
||||
*/
|
||||
protected function delegate(Route $route, $controller, $method, $parameters)
|
||||
{
|
||||
if ( ! file_exists($path = $this->controller_path.strtolower(str_replace('.', '/', $controller)).EXT))
|
||||
{
|
||||
throw new \Exception("Controller [$controller] does not exist.");
|
||||
}
|
||||
|
||||
require $path;
|
||||
|
||||
$controller = $this->resolve_controller($controller);
|
||||
|
||||
if ($method == 'before' or strncmp($method, '_', 1) === 0)
|
||||
{
|
||||
$response = $this->container->resolve('laravel.response')->error('404');
|
||||
}
|
||||
else
|
||||
{
|
||||
$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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a controller name to a controller instance.
|
||||
*
|
||||
* @param string $controller
|
||||
* @return Controller
|
||||
*/
|
||||
protected function resolve_controller($controller)
|
||||
{
|
||||
if ($this->container->registered('controllers.'.$controller)) return $this->container->resolve('controllers.'.$controller);
|
||||
|
||||
$controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
|
||||
|
||||
return new $controller;
|
||||
return $this->filterer->filter($before, array($this->container), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finish the route handling for the request.
|
||||
*
|
||||
* The route response will be converted to a Response instance and the "after" filters
|
||||
* defined for the route will be executed.
|
||||
* The route response will be converted to a Response instance and the "after" filters will be run.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param mixed $response
|
||||
@@ -165,33 +99,9 @@ class Caller {
|
||||
{
|
||||
if ( ! $response instanceof Response) $response = new Response($response);
|
||||
|
||||
$this->filter($route, array_merge($route->after(), array('after')), array($response));
|
||||
$this->filterer->filter(array_merge($route->filters('after'), array('after')), array($this->container, $response));
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a filter or set of filters.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param array $filters
|
||||
* @param array $parameters
|
||||
* @param bool $override
|
||||
* @return mixed
|
||||
*/
|
||||
protected function filter(Route $route, $filters, $parameters = array(), $override = false)
|
||||
{
|
||||
foreach ((array) $filters as $filter)
|
||||
{
|
||||
if ( ! isset($route->filters[$filter])) continue;
|
||||
|
||||
$response = call_user_func_array($route->filters[$filter], $parameters);
|
||||
|
||||
// "Before" filters may override the request cycle. For example, an authentication
|
||||
// filter may redirect a user to a login view if they are not logged in. Because of
|
||||
// this, we will return the first filter response if overriding is enabled.
|
||||
if ( ! is_null($response) and $override) return $response;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
117
laravel/routing/delegator.php
Normal file
117
laravel/routing/delegator.php
Normal file
@@ -0,0 +1,117 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Laravel\Container;
|
||||
|
||||
class Delegator {
|
||||
|
||||
/**
|
||||
* The IoC container instance.
|
||||
*
|
||||
* @var Container
|
||||
*/
|
||||
protected $container;
|
||||
|
||||
/**
|
||||
* The path to the application controllers.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $path;
|
||||
|
||||
/**
|
||||
* Create a new route delegator instance.
|
||||
*
|
||||
* @param Container $container
|
||||
* @param string $path
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Container $container, $path)
|
||||
{
|
||||
$this->path = $path;
|
||||
$this->container = $container;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the delegation of a route to a controller method.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param array $delegate
|
||||
* @return mixed
|
||||
*/
|
||||
public function delegate(Route $route, $delegate)
|
||||
{
|
||||
list($controller, $method) = array($delegate[0], $delegate[1]);
|
||||
|
||||
$controller = $this->resolve($controller);
|
||||
|
||||
// If the controller doesn't exist or the request is to an invalid method, we will
|
||||
// return the 404 error response. The "before" method and any method beginning with
|
||||
// an underscore are not publicly available.
|
||||
if (is_null($controller) or ($method == 'before' or strncmp($method, '_', 1) === 0))
|
||||
{
|
||||
return $this->container->response->error('404');
|
||||
}
|
||||
|
||||
$controller->container = $this->container;
|
||||
|
||||
// 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.
|
||||
$response = $controller->before();
|
||||
|
||||
return (is_null($response)) ? call_user_func_array(array($controller, $method), $route->parameters) : $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a controller name to a controller instance.
|
||||
*
|
||||
* @param string $controller
|
||||
* @return Controller
|
||||
*/
|
||||
protected function resolve($controller)
|
||||
{
|
||||
if ( ! $this->load($controller)) return;
|
||||
|
||||
if ($this->container->registered('controllers.'.$controller))
|
||||
{
|
||||
return $this->container->resolve('controllers.'.$controller);
|
||||
}
|
||||
|
||||
$controller = $this->format($controller);
|
||||
|
||||
return new $controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the file for a given controller.
|
||||
*
|
||||
* @param string $controller
|
||||
* @return bool
|
||||
*/
|
||||
protected function load($controller)
|
||||
{
|
||||
if (file_exists($path = $this->path.strtolower(str_replace('.', '/', $controller)).EXT))
|
||||
{
|
||||
require $path;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Format a controller name to its class name.
|
||||
*
|
||||
* All controllers are suffixed with "_Controller" to avoid namespacing. It gives the developer
|
||||
* a more convenient environment since the controller sits in the global namespace.
|
||||
*
|
||||
* @param string $controller
|
||||
* @return string
|
||||
*/
|
||||
protected function format($controller)
|
||||
{
|
||||
return str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
|
||||
}
|
||||
|
||||
}
|
||||
46
laravel/routing/filterer.php
Normal file
46
laravel/routing/filterer.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
class Filterer {
|
||||
|
||||
/**
|
||||
* All of the route filters for the application.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $filters = array();
|
||||
|
||||
/**
|
||||
* Create a new route filterer instance.
|
||||
*
|
||||
* @param array $filters
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($filters)
|
||||
{
|
||||
$this->filters = $filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a filter or set of filters.
|
||||
*
|
||||
* @param array $filters
|
||||
* @param array $parameters
|
||||
* @param bool $override
|
||||
* @return mixed
|
||||
*/
|
||||
public function filter($filters, $parameters = array(), $override = false)
|
||||
{
|
||||
foreach ((array) $filters as $filter)
|
||||
{
|
||||
if ( ! isset($this->filters[$filter])) continue;
|
||||
|
||||
$response = call_user_func_array($this->filters[$filter], $parameters);
|
||||
|
||||
// "Before" filters may override the request cycle. For example, an authentication
|
||||
// filter may redirect a user to a login view if they are not logged in. Because of
|
||||
// this, we will return the first filter response if overriding is enabled.
|
||||
if ( ! is_null($response) and $override) return $response;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Closure;
|
||||
use Laravel\Container;
|
||||
|
||||
class Route {
|
||||
|
||||
/**
|
||||
@@ -30,13 +33,6 @@ class Route {
|
||||
*/
|
||||
public $parameters;
|
||||
|
||||
/**
|
||||
* The route filters for the application.
|
||||
*
|
||||
* @param array $filters
|
||||
*/
|
||||
public $filters = array();
|
||||
|
||||
/**
|
||||
* Create a new Route instance.
|
||||
*
|
||||
@@ -45,51 +41,120 @@ class Route {
|
||||
* @param array $parameters
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($key, $callback, $parameters)
|
||||
public function __construct($key, $callback, $parameters = array())
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->callback = $callback;
|
||||
$this->parameters = $parameters;
|
||||
|
||||
// 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;
|
||||
}
|
||||
$this->uris = $this->parse($key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the "before" filters defined for the route.
|
||||
* Call the route closure.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function before()
|
||||
{
|
||||
return $this->filters('before');
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the "after" filters defined for the route.
|
||||
* If no closure is defined for the route, null will be returned. The IoC container instance will be
|
||||
* passed to the route closure so it has access to all of the framework components.
|
||||
*
|
||||
* @return array
|
||||
* @param Container $container
|
||||
* @return mixed
|
||||
*/
|
||||
public function after()
|
||||
public function call(Container $container)
|
||||
{
|
||||
return $this->filters('after');
|
||||
if (is_null($closure = $this->find_closure())) return;
|
||||
|
||||
return call_user_func_array($closure, array_merge($this->parameters, array($container)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of filters defined for the route.
|
||||
* Extract the route closure from the route.
|
||||
*
|
||||
* @return Closure|null
|
||||
*/
|
||||
protected function find_closure()
|
||||
{
|
||||
if ($this->callback instanceof Closure) return $this->callback;
|
||||
|
||||
if (isset($this->callback['do'])) return $this->callback['do'];
|
||||
|
||||
foreach ($this->callback as $value) { if ($value instanceof Closure) return $value; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of filter names defined for a route.
|
||||
*
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
private function filters($name)
|
||||
public function filters($name)
|
||||
{
|
||||
return (is_array($this->callback) and isset($this->callback[$name])) ? explode(', ', $this->callback[$name]) : array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the route handling has a given name.
|
||||
*
|
||||
* @param string $name
|
||||
* @return bool
|
||||
*/
|
||||
public function is($name)
|
||||
{
|
||||
if ( ! is_array($this->callback) or ! isset($this->callback['name'])) return false;
|
||||
|
||||
return $this->callback['name'] === $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the route handles a given URI.
|
||||
*
|
||||
* @param string $uri
|
||||
* @return bool
|
||||
*/
|
||||
public function handles($uri)
|
||||
{
|
||||
return in_array($uri, $this->uris);
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the route key and return an array of URIs the route responds to.
|
||||
*
|
||||
* @param string $key
|
||||
* @return array
|
||||
*/
|
||||
protected function parse($key)
|
||||
{
|
||||
if (strpos($key, ', ') === false) return array($this->extract($key));
|
||||
|
||||
foreach (explode(', ', $key) as $segment)
|
||||
{
|
||||
$uris[] = $this->extract($segment);
|
||||
}
|
||||
|
||||
return $uris;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the URI from a route destination.
|
||||
*
|
||||
* Route destinations include the request method the route responds to, so this method
|
||||
* will only remove it from the string. Unless the URI is root, the forward slash will
|
||||
* be removed to make searching the URIs more convenient.
|
||||
*
|
||||
* @param string $segment
|
||||
* @return string
|
||||
*/
|
||||
protected function extract($segment)
|
||||
{
|
||||
$segment = substr($segment, strpos($segment, ' ') + 1);
|
||||
|
||||
return ($segment !== '/') ? trim($segment, '/') : $segment;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic Method to handle dynamic method calls to determine the name of the route.
|
||||
*/
|
||||
public function __call($method, $parameters)
|
||||
{
|
||||
if (strpos($method, 'is_') === 0) { return $this->is(substr($method, 3)); }
|
||||
}
|
||||
|
||||
}
|
||||
@@ -111,7 +111,7 @@ class Router {
|
||||
}
|
||||
}
|
||||
|
||||
return $this->route_to_controller();
|
||||
return $this->request->route = $this->route_to_controller();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -123,6 +123,8 @@ class Router {
|
||||
*/
|
||||
protected function route_to_controller()
|
||||
{
|
||||
if ($this->request->uri() === '/') return new Route($this->request->method().' /', function() { return array('home', 'index'); });
|
||||
|
||||
$segments = explode('/', trim($this->request->uri(), '/'));
|
||||
|
||||
if ( ! is_null($key = $this->controller_key($segments)))
|
||||
@@ -147,7 +149,7 @@ class Router {
|
||||
// were they to code the controller delegation manually.
|
||||
$callback = function() use ($controller, $method) { return array($controller, $method); };
|
||||
|
||||
return new Route($controller, $callback, $segments);
|
||||
return new Route($this->request->method().' /'.$this->request->uri(), $callback, $segments);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user