refactoring for dependency injection and testability.
This commit is contained in:
@@ -1,56 +0,0 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
class Filter {
|
||||
|
||||
/**
|
||||
* The loaded route filters.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $filters = array();
|
||||
|
||||
/**
|
||||
* Register a set of route filters.
|
||||
*
|
||||
* @param array $filters
|
||||
* @return void
|
||||
*/
|
||||
public static function register($filters)
|
||||
{
|
||||
static::$filters = array_merge(static::$filters, $filters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Clear all of the registered route filters.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public static function clear()
|
||||
{
|
||||
static::$filters = array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a set of route filters.
|
||||
*
|
||||
* @param string $filter
|
||||
* @param array $parameters
|
||||
* @param bool $override
|
||||
* @return mixed
|
||||
*/
|
||||
public static function call($filters, $parameters = array(), $override = false)
|
||||
{
|
||||
foreach (explode(', ', $filters) as $filter)
|
||||
{
|
||||
if ( ! isset(static::$filters[$filter])) continue;
|
||||
|
||||
$response = call_user_func_array(static::$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,40 +0,0 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
class Finder {
|
||||
|
||||
/**
|
||||
* The named routes that have been found so far.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $names = array();
|
||||
|
||||
/**
|
||||
* Find a named route in a given array of routes.
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $routes
|
||||
* @return array
|
||||
*/
|
||||
public static function find($name, $routes)
|
||||
{
|
||||
if (array_key_exists($name, static::$names)) return static::$names[$name];
|
||||
|
||||
$arrayIterator = new \RecursiveArrayIterator($routes);
|
||||
|
||||
$recursiveIterator = new \RecursiveIteratorIterator($arrayIterator);
|
||||
|
||||
// Since routes can be nested deep within sub-directories, we need to recursively
|
||||
// iterate through each directory and gather all of the routes.
|
||||
foreach ($recursiveIterator as $iterator)
|
||||
{
|
||||
$route = $recursiveIterator->getSubIterator();
|
||||
|
||||
if (isset($route['name']) and $route['name'] == $name)
|
||||
{
|
||||
return static::$names[$name] = array($arrayIterator->key() => iterator_to_array($route));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
192
laravel/routing/handler.php
Normal file
192
laravel/routing/handler.php
Normal file
@@ -0,0 +1,192 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Closure;
|
||||
use Laravel\IoC;
|
||||
use Laravel\Error;
|
||||
use Laravel\Request;
|
||||
use Laravel\Response;
|
||||
|
||||
class Handler {
|
||||
|
||||
/**
|
||||
* The active request instance.
|
||||
*
|
||||
* @var Request
|
||||
*/
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* The route filter manager.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $filters;
|
||||
|
||||
/**
|
||||
* Create a new route handler instance.
|
||||
*
|
||||
* @param array $filters
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Request $request, $filters)
|
||||
{
|
||||
$this->request = $request;
|
||||
$this->filters = $filters;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a given route and return the response.
|
||||
*
|
||||
* @param Route $route
|
||||
* @return Response
|
||||
*/
|
||||
public function handle(Route $route)
|
||||
{
|
||||
$this->validate($route);
|
||||
|
||||
if ( ! is_null($response = $this->filter(array_merge($route->before(), array('before')), array($this->request), true)))
|
||||
{
|
||||
return $this->finish($route, $response);
|
||||
}
|
||||
|
||||
$closure = ( ! $route->callback instanceof Closure) ? $this->find_route_closure($route) : $route->callback;
|
||||
|
||||
if ( ! is_null($closure)) return $this->handle_closure($route, $closure);
|
||||
|
||||
return $this->finish($route, new Error('404'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a given route is callable.
|
||||
*
|
||||
* @param Route $route
|
||||
* @return void
|
||||
*/
|
||||
protected function validate(Route $route)
|
||||
{
|
||||
if ( ! $route->callback instanceof Closure and ! is_array($route->callback))
|
||||
{
|
||||
throw new \Exception('Invalid route defined for URI ['.$route->key.']');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the route closure from the route.
|
||||
*
|
||||
* If a "do" index is specified on the callback, that is the handler.
|
||||
* Otherwise, we will return the first callable array value.
|
||||
*
|
||||
* @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 (is_callable($value)) return $value; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a route closure.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param Closure $closure
|
||||
* @return mixed
|
||||
*/
|
||||
protected function handle_closure(Route $route, Closure $closure)
|
||||
{
|
||||
$response = call_user_func_array($closure, $route->parameters);
|
||||
|
||||
if (is_array($response))
|
||||
{
|
||||
$response = $this->delegate($response[0], $response[1], $route->parameters);
|
||||
}
|
||||
|
||||
return $this->finish($route, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the delegation of a route to a controller method.
|
||||
*
|
||||
* @param string $controller
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
* @return Response
|
||||
*/
|
||||
protected function delegate($controller, $method, $parameters)
|
||||
{
|
||||
if ( ! file_exists($path = CONTROLLER_PATH.strtolower(str_replace('.', '/', $controller)).EXT))
|
||||
{
|
||||
throw new \Exception("Controller [$controller] is not defined.");
|
||||
}
|
||||
|
||||
require $path;
|
||||
|
||||
$controller = $this->resolve($controller);
|
||||
|
||||
$response = $controller->before($this->request);
|
||||
|
||||
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)
|
||||
{
|
||||
if (IoC::container()->registered('controllers.'.$controller))
|
||||
{
|
||||
return IoC::container()->resolve('controllers.'.$controller);
|
||||
}
|
||||
|
||||
$controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
|
||||
|
||||
return new $controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* Call a filter or set of filters.
|
||||
*
|
||||
* @param array $filters
|
||||
* @param array $parameters
|
||||
* @param bool $override
|
||||
* @return mixed
|
||||
*/
|
||||
protected 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param mixed $response
|
||||
* @return Response
|
||||
*/
|
||||
protected function finish(Route $route, $response)
|
||||
{
|
||||
if ( ! $response instanceof Response) $response = new Response($response);
|
||||
|
||||
$this->filter(array_merge($route->after(), array('after')), array($this->request, $response));
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,116 +0,0 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Laravel\Config;
|
||||
|
||||
class Loader {
|
||||
|
||||
/**
|
||||
* All of the routes for the application.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
private static $routes;
|
||||
|
||||
/**
|
||||
* The path where the routes are located.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $path;
|
||||
|
||||
/**
|
||||
* Create a new route loader instance.
|
||||
*
|
||||
* @param string $path
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($path)
|
||||
{
|
||||
$this->path = $path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the appropriate routes for the request URI.
|
||||
*
|
||||
* @param string
|
||||
* @return array
|
||||
*/
|
||||
public function load($uri)
|
||||
{
|
||||
$base = (file_exists($path = $this->path.'routes'.EXT)) ? require $path : array();
|
||||
|
||||
return array_merge($this->load_nested_routes(explode('/', $uri)), $base);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the appropriate routes from the routes directory.
|
||||
*
|
||||
* @param array $segments
|
||||
* @return array
|
||||
*/
|
||||
private function load_nested_routes($segments)
|
||||
{
|
||||
// If the request URI only more than one segment, and the last segment contains a dot, we will
|
||||
// assume the request is for a specific format (users.json or users.xml) and strip off
|
||||
// everything after the dot so we can load the appropriate file.
|
||||
if (count($segments) > 0 and strpos(end($segments), '.') !== false)
|
||||
{
|
||||
$segment = array_pop($segments);
|
||||
|
||||
array_push($segments, substr($segment, 0, strpos($segment, '.')));
|
||||
}
|
||||
|
||||
// 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->path.'routes/'.implode('/', array_slice($segments, 0, $key + 1)).EXT))
|
||||
{
|
||||
return require $path;
|
||||
}
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all of the routes for the application.
|
||||
*
|
||||
* To improve performance, this operation will only be performed once. The routes
|
||||
* will be cached and returned on every subsequent call.
|
||||
*
|
||||
* @param bool $reload
|
||||
* @return array
|
||||
*/
|
||||
public static function all($path = APP_PATH, $reload = false)
|
||||
{
|
||||
if ( ! is_null(static::$routes) and ! $reload) return static::$routes;
|
||||
|
||||
$routes = array();
|
||||
|
||||
if (file_exists($path.'routes'.EXT))
|
||||
{
|
||||
$routes = array_merge($routes, require $path.'routes'.EXT);
|
||||
}
|
||||
|
||||
if (is_dir($path.'routes'))
|
||||
{
|
||||
// Since route files can be nested deep within the route directory, we need to
|
||||
// recursively spin through the directory to find every file.
|
||||
$directoryIterator = new \RecursiveDirectoryIterator($path.'routes');
|
||||
|
||||
$recursiveIterator = new \RecursiveIteratorIterator($directoryIterator, \RecursiveIteratorIterator::SELF_FIRST);
|
||||
|
||||
foreach ($recursiveIterator as $file)
|
||||
{
|
||||
if (filetype($file) === 'file' and strpos($file, EXT) !== false)
|
||||
{
|
||||
$routes = array_merge($routes, require $file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return static::$routes = $routes;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,8 +1,5 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Laravel\Package;
|
||||
use Laravel\Response;
|
||||
|
||||
class Route {
|
||||
|
||||
/**
|
||||
@@ -42,64 +39,39 @@ class Route {
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the route function.
|
||||
* Get all of the "before" filters defined for the route.
|
||||
*
|
||||
* @param mixed $route
|
||||
* @param array $parameters
|
||||
* @return Response
|
||||
* @return array
|
||||
*/
|
||||
public function call()
|
||||
public function before()
|
||||
{
|
||||
$response = null;
|
||||
|
||||
// The callback may be in array form, meaning it has attached filters or is named and we
|
||||
// will need to evaluate it further to determine what to do. If the callback is just a
|
||||
// closure, we can execute it now and return the result.
|
||||
if (is_callable($this->callback))
|
||||
{
|
||||
$response = call_user_func_array($this->callback, $this->parameters);
|
||||
}
|
||||
elseif (is_array($this->callback))
|
||||
{
|
||||
if (isset($this->callback['needs']))
|
||||
{
|
||||
Package::load(explode(', ', $this->callback['needs']));
|
||||
}
|
||||
|
||||
$response = isset($this->callback['before']) ? Filter::call($this->callback['before'], array(), true) : null;
|
||||
|
||||
if (is_null($response) and ! is_null($handler = $this->find_route_function()))
|
||||
{
|
||||
$response = call_user_func_array($handler, $this->parameters);
|
||||
}
|
||||
}
|
||||
|
||||
$response = Response::prepare($response);
|
||||
|
||||
if (is_array($this->callback) and isset($this->callback['after']))
|
||||
{
|
||||
Filter::call($this->callback['after'], array($response));
|
||||
}
|
||||
|
||||
return $response;
|
||||
return $this->filters('before');
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract the route function from the route.
|
||||
* Get all of the "after" filters defined 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.
|
||||
*
|
||||
* @return Closure
|
||||
* @return array
|
||||
*/
|
||||
private function find_route_function()
|
||||
public function after()
|
||||
{
|
||||
if (isset($this->callback['do'])) return $this->callback['do'];
|
||||
return $this->filters('after');
|
||||
}
|
||||
|
||||
foreach ($this->callback as $value)
|
||||
{
|
||||
if (is_callable($value)) return $value;
|
||||
}
|
||||
/**
|
||||
* Get an array of filters defined for the route.
|
||||
*
|
||||
* <code>
|
||||
* // Get all of the "before" filters defined for the route.
|
||||
* $filters = $route->filters('before');
|
||||
* </code>
|
||||
*
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
private function filters($name)
|
||||
{
|
||||
return (is_array($this->callback) and isset($this->callback[$name])) ? explode(', ', $this->callback[$name]) : array();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -5,14 +5,7 @@ use Laravel\Request;
|
||||
class Router {
|
||||
|
||||
/**
|
||||
* The request method and URI.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $destination;
|
||||
|
||||
/**
|
||||
* All of the loaded routes.
|
||||
* All of the routes available to the router.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
@@ -23,58 +16,81 @@ class Router {
|
||||
*
|
||||
* @var Request
|
||||
*/
|
||||
private $request;
|
||||
protected $request;
|
||||
|
||||
/**
|
||||
* The route loader instance.
|
||||
* The named routes that have been found so far.
|
||||
*
|
||||
* @var Loader
|
||||
* @var array
|
||||
*/
|
||||
private $loader;
|
||||
protected $names = array();
|
||||
|
||||
/**
|
||||
* Create a new router for a request method and URI.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Loader $loader
|
||||
* @param array $routes
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Request $request, Loader $loader)
|
||||
public function __construct(Request $request, $routes)
|
||||
{
|
||||
$this->loader = $loader;
|
||||
$this->routes = $routes;
|
||||
$this->request = $request;
|
||||
|
||||
// Put the request method and URI in route form. Routes begin with
|
||||
// the request method and a forward slash.
|
||||
$this->destination = $request->method().' /'.trim($request->uri(), '/');
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new router for a request method and URI.
|
||||
* Find a route by name.
|
||||
*
|
||||
* @param Request $request
|
||||
* @param Loader $loader
|
||||
* @return Router
|
||||
* The returned array will be identical the array defined in the routes.php file.
|
||||
*
|
||||
* <code>
|
||||
* // Find the "login" named route
|
||||
* $route = $router->find('login');
|
||||
*
|
||||
* // Find the "login" named route through the IoC container
|
||||
* $route = IoC::resolve('laravel.routing.router')->find('login');
|
||||
* </code>
|
||||
*
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
public static function make(Request $request, Loader $loader)
|
||||
public function find($name)
|
||||
{
|
||||
return new static($request, $loader);
|
||||
if (array_key_exists($name, $this->names)) return $this->names[$name];
|
||||
|
||||
$arrayIterator = new \RecursiveArrayIterator($this->routes);
|
||||
|
||||
$recursiveIterator = new \RecursiveIteratorIterator($arrayIterator);
|
||||
|
||||
foreach ($recursiveIterator as $iterator)
|
||||
{
|
||||
$route = $recursiveIterator->getSubIterator();
|
||||
|
||||
if (isset($route['name']) and $route['name'] === $name)
|
||||
{
|
||||
return $this->names[$name] = array($arrayIterator->key() => iterator_to_array($route));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search a set of routes for the route matching a method and URI.
|
||||
* Search the routes for the route matching a method and URI.
|
||||
*
|
||||
* If no route can be found, the application controllers will be searched.
|
||||
*
|
||||
* @return Route
|
||||
*/
|
||||
public function route()
|
||||
{
|
||||
if (is_null($this->routes)) $this->routes = $this->loader->load($this->request->uri());
|
||||
// Put the request method and URI in route form. Routes begin with
|
||||
// the request method and a forward slash.
|
||||
$destination = $this->request->method().' /'.trim($this->request->uri(), '/');
|
||||
|
||||
// Check for a literal route match first. If we find one, there is
|
||||
// no need to spin through all of the routes.
|
||||
if (isset($this->routes[$this->destination]))
|
||||
if (isset($this->routes[$destination]))
|
||||
{
|
||||
return $this->request->route = new Route($this->destination, $this->routes[$this->destination]);
|
||||
return $this->request->route = new Route($destination, $this->routes[$destination]);
|
||||
}
|
||||
|
||||
foreach ($this->routes as $keys => $callback)
|
||||
@@ -85,13 +101,75 @@ class Router {
|
||||
{
|
||||
foreach (explode(', ', $keys) as $key)
|
||||
{
|
||||
if (preg_match('#^'.$this->translate_wildcards($key).'$#', $this->destination))
|
||||
if (preg_match('#^'.$this->translate_wildcards($key).'$#', $destination))
|
||||
{
|
||||
return $this->request->route = new Route($keys, $callback, $this->parameters($this->destination, $key));
|
||||
return $this->request->route = new Route($keys, $callback, $this->parameters($destination, $key));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $this->route_to_controller();
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to find a controller for the incoming request.
|
||||
*
|
||||
* If no corresponding controller can be found, NULL will be returned.
|
||||
*
|
||||
* @return Route
|
||||
*/
|
||||
protected function route_to_controller()
|
||||
{
|
||||
$segments = explode('/', trim($this->request->uri(), '/'));
|
||||
|
||||
if ( ! is_null($key = $this->controller_key($segments)))
|
||||
{
|
||||
// Create the controller name for the current request. This controller
|
||||
// name will be returned by the anonymous route we will create. Instead
|
||||
// of using directory slashes, dots will be used to specify the controller
|
||||
// location with the controllers directory.
|
||||
$controller = implode('.', array_slice($segments, 0, $key));
|
||||
|
||||
// Now that we have the controller path and name, we can slice the controller
|
||||
// section of the URI from the array of segments.
|
||||
$segments = array_slice($segments, $key);
|
||||
|
||||
// Extract the controller method from the URI segments. If no more segments
|
||||
// are remaining after slicing off the controller, the "index" method will
|
||||
// be used as the default controller method.
|
||||
$method = (count($segments) > 0) ? array_shift($segments) : 'index';
|
||||
|
||||
// Now we're ready to dummy up a controller delegating route callback. This
|
||||
// callback will look exactly like the callback the developer would create
|
||||
// were they to code the controller delegation manually.
|
||||
$callback = function() use ($controller, $method) { return array($controller, $method); };
|
||||
|
||||
return new Route($controller, $callback, $segments);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Search the controllers for the application and determine if an applicable
|
||||
* controller exists for the current request.
|
||||
*
|
||||
* If a controller is found, the array key for the controller name in the URI
|
||||
* segments will be returned by the method, otherwise NULL will be returned.
|
||||
*
|
||||
* @param array $segments
|
||||
* @return int
|
||||
*/
|
||||
protected function controller_key($segments)
|
||||
{
|
||||
// Work backwards through the URI segments until we find the deepest possible
|
||||
// matching controller. Once we find it, we will return those routes.
|
||||
foreach (array_reverse($segments, true) as $key => $value)
|
||||
{
|
||||
if (file_exists($path = CONTROLLER_PATH.implode('/', array_slice($segments, 0, $key + 1)).EXT))
|
||||
{
|
||||
return $key + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -100,7 +178,7 @@ class Router {
|
||||
* @param string $key
|
||||
* @return string
|
||||
*/
|
||||
private function translate_wildcards($key)
|
||||
protected function translate_wildcards($key)
|
||||
{
|
||||
$replacements = 0;
|
||||
|
||||
@@ -123,7 +201,7 @@ class Router {
|
||||
* @param string $route
|
||||
* @return array
|
||||
*/
|
||||
private function parameters($uri, $route)
|
||||
protected function parameters($uri, $route)
|
||||
{
|
||||
return array_values(array_intersect_key(explode('/', $uri), preg_grep('/\(.+\)/', explode('/', $route))));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user