continued ioc refactoring.
This commit is contained in:
@@ -1,194 +0,0 @@
|
||||
<?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 ($value instanceof Closure) return $value; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a route closure.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param Closure $closure
|
||||
* @return mixed
|
||||
*/
|
||||
protected function handle_closure(Route $route, Closure $closure)
|
||||
{
|
||||
array_unshift($route->parameters, $this->request);
|
||||
|
||||
$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 = ($method == 'before' or strncmp($method, '_', 1) === 0) ? new Error('404') : $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,5 +1,10 @@
|
||||
<?php namespace Laravel\Routing;
|
||||
|
||||
use Closure;
|
||||
use Laravel\Response;
|
||||
use Laravel\Container;
|
||||
use Laravel\Application;
|
||||
|
||||
class Route {
|
||||
|
||||
/**
|
||||
@@ -23,19 +28,198 @@ class Route {
|
||||
*/
|
||||
public $parameters;
|
||||
|
||||
/**
|
||||
* The route filters for the application.
|
||||
*
|
||||
* @param array $filters
|
||||
*/
|
||||
public $filters = array();
|
||||
|
||||
/**
|
||||
* The path the application controllers.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $controller_path;
|
||||
|
||||
/**
|
||||
* Create a new Route instance.
|
||||
*
|
||||
* @param string $key
|
||||
* @param mixed $callback
|
||||
* @param array $parameters
|
||||
* @param string $key
|
||||
* @param mixed $callback
|
||||
* @param array $parameters
|
||||
* @return void
|
||||
*/
|
||||
public function __construct($key, $callback, $parameters = array())
|
||||
public function __construct($key, $callback, $parameters, $controller_path)
|
||||
{
|
||||
$this->key = $key;
|
||||
$this->callback = $callback;
|
||||
$this->parameters = $parameters;
|
||||
$this->controller_path = $controller_path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute the route for a given request to the application and return the response.
|
||||
*
|
||||
* @param Application $application
|
||||
* @return Response
|
||||
*/
|
||||
public function call(Application $application)
|
||||
{
|
||||
$this->validate();
|
||||
|
||||
if ( ! is_null($response = $this->filter(array_merge($this->before(), array('before')), array($application), true)))
|
||||
{
|
||||
return $this->finish($application, $response);
|
||||
}
|
||||
|
||||
$closure = ( ! $this->callback instanceof Closure) ? $this->find_route_closure() : $this->callback;
|
||||
|
||||
if ( ! is_null($closure)) return $this->handle_closure($application, $closure);
|
||||
|
||||
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.
|
||||
*
|
||||
* If a "do" index is specified on the callback, that is the handler.
|
||||
* Otherwise, we will return the first callable array value.
|
||||
*
|
||||
* @return Closure
|
||||
*/
|
||||
protected function find_route_closure()
|
||||
{
|
||||
if (isset($this->callback['do'])) return $this->callback['do'];
|
||||
|
||||
foreach ($this->callback as $value) { if ($value instanceof Closure) return $value; }
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a route closure.
|
||||
*
|
||||
* @param Route $route
|
||||
* @param Closure $closure
|
||||
* @return mixed
|
||||
*/
|
||||
protected function handle_closure(Application $application, Closure $closure)
|
||||
{
|
||||
array_unshift($this->parameters, $application);
|
||||
|
||||
$response = call_user_func_array($closure, $this->parameters);
|
||||
|
||||
if (is_array($response))
|
||||
{
|
||||
$response = $this->delegate($application, $response[0], $response[1], $this->parameters);
|
||||
}
|
||||
|
||||
return $this->finish($application, $response);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the delegation of a route to a controller method.
|
||||
*
|
||||
* @param Application $application
|
||||
* @param string $controller
|
||||
* @param string $method
|
||||
* @param array $parameters
|
||||
* @return Response
|
||||
*/
|
||||
protected function delegate(Application $application, $controller, $method, $parameters)
|
||||
{
|
||||
if ( ! file_exists($path = $this->controller_path.strtolower(str_replace('.', '/', $controller)).EXT))
|
||||
{
|
||||
throw new \Exception("Controller [$controller] is not defined.");
|
||||
}
|
||||
|
||||
require $path;
|
||||
|
||||
$controller = $this->resolve($application->container, $controller);
|
||||
|
||||
if ($method == 'before' or strncmp($method, '_', 1) === 0)
|
||||
{
|
||||
$response = $application->responder->error('404');
|
||||
}
|
||||
else
|
||||
{
|
||||
$response = $controller->before();
|
||||
}
|
||||
|
||||
return (is_null($response)) ? call_user_func_array(array($controller, $method), $parameters) : $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a controller name to a controller instance.
|
||||
*
|
||||
* @param Container $container
|
||||
* @param string $controller
|
||||
* @return Controller
|
||||
*/
|
||||
protected function resolve(Container $container, $controller)
|
||||
{
|
||||
if ($container->registered('controllers.'.$controller))
|
||||
{
|
||||
return $container->resolve('controllers.'.$controller);
|
||||
}
|
||||
|
||||
$controller = str_replace(' ', '_', ucwords(str_replace('.', ' ', $controller))).'_Controller';
|
||||
|
||||
return new $controller;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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(Application $application, $response)
|
||||
{
|
||||
if ( ! $response instanceof Response) $response = new Response($response);
|
||||
|
||||
$this->filter(array_merge($this->after(), array('after')), array($application, $response));
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -25,6 +25,13 @@ class Router {
|
||||
*/
|
||||
protected $names = array();
|
||||
|
||||
/**
|
||||
* The path the application controllers.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $controller_path;
|
||||
|
||||
/**
|
||||
* Create a new router for a request method and URI.
|
||||
*
|
||||
@@ -32,10 +39,11 @@ class Router {
|
||||
* @param array $routes
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(Request $request, $routes)
|
||||
public function __construct(Request $request, $routes, $controller_path)
|
||||
{
|
||||
$this->routes = $routes;
|
||||
$this->request = $request;
|
||||
$this->controller_path = $controller_path;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,13 +92,13 @@ class Router {
|
||||
{
|
||||
// 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(), '/');
|
||||
$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[$destination]))
|
||||
{
|
||||
return $this->request->route = new Route($destination, $this->routes[$destination]);
|
||||
return $this->request->route = new Route($destination, $this->routes[$destination], array(), $this->controller_path);
|
||||
}
|
||||
|
||||
foreach ($this->routes as $keys => $callback)
|
||||
@@ -103,7 +111,7 @@ class Router {
|
||||
{
|
||||
if (preg_match('#^'.$this->translate_wildcards($key).'$#', $destination))
|
||||
{
|
||||
return $this->request->route = new Route($keys, $callback, $this->parameters($destination, $key));
|
||||
return $this->request->route = new Route($keys, $callback, $this->parameters($destination, $key), $this->controller_path);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -121,7 +129,7 @@ class Router {
|
||||
*/
|
||||
protected function route_to_controller()
|
||||
{
|
||||
$segments = explode('/', trim($this->request->uri(), '/'));
|
||||
$segments = explode('/', trim($this->request->uri, '/'));
|
||||
|
||||
if ( ! is_null($key = $this->controller_key($segments)))
|
||||
{
|
||||
@@ -145,7 +153,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($controller, $callback, $segments, $this->controller_path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -165,7 +173,7 @@ class Router {
|
||||
// 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))
|
||||
if (file_exists($path = $this->controller_path.implode('/', array_slice($segments, 0, $key + 1)).EXT))
|
||||
{
|
||||
return $key + 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user