Further refine Dispatcher

This commit is contained in:
Timothy Warren 2016-01-08 16:39:18 -05:00
parent 276a14a80c
commit 3981b8471a

View File

@ -94,8 +94,6 @@ class Dispatcher extends RoutingBase {
public function __invoke($route = NULL) public function __invoke($route = NULL)
{ {
$error_handler = $this->container->get('error-handler'); $error_handler = $this->container->get('error-handler');
$controller_name = AnimeClient::DEFAULT_CONTROLLER;
$action_method = AnimeClient::NOT_FOUND_METHOD;
if (is_null($route)) if (is_null($route))
{ {
@ -103,29 +101,41 @@ class Dispatcher extends RoutingBase {
$error_handler->addDataTable('route_args', (array)$route); $error_handler->addDataTable('route_args', (array)$route);
} }
if ( ! $route) if($route)
{
$parsed = $this->process_route($route);
$controller_name = $parsed['controller_name'];
$action_method = $parsed['action_method'];
$params = $parsed['params'];
}
else
{ {
// If not route was matched, return an appropriate http // If not route was matched, return an appropriate http
// error message // error message
$error_route = $this->get_error_params(); $error_route = $this->get_error_params();
$params = $error_route['params']; $controller_name = AnimeClient::DEFAULT_CONTROLLER;
$action_method = $error_route['action_method']; $action_method = $error_route['action_method'];
$params = $error_route['params'];
} }
else
{
$params = (isset($route->params['params'])) ? $route->params['params'] : [];
if (isset($route->params['controller'])) // Actually instantiate the controller
$this->call($controller_name, $action_method, $params);
}
/**
* Parse out the arguments for the appropriate controller for
* the current route
*
* @param \Aura\Router\Route $route
* @return array
*/
protected function process_route($route)
{
if (array_key_exists('controller', $route->params))
{ {
$controller_name = $route->params['controller']; $controller_name = $route->params['controller'];
} }
else
if (isset($route->params['action']))
{
$action_method = $route->params['action'];
}
if (is_null($controller_name))
{ {
throw new \LogicException("Missing controller"); throw new \LogicException("Missing controller");
} }
@ -137,6 +147,14 @@ class Dispatcher extends RoutingBase {
$controller_name = $map[$controller_name]; $controller_name = $map[$controller_name];
} }
$action_method = (array_key_exists('action', $route->params))
? $route->params['action']
: AnimeClient::NOT_FOUND_METHOD;
$params = (array_key_exists('params', $route->params))
? $route->params['params']
: [];
if ( ! empty($route->tokens)) if ( ! empty($route->tokens))
{ {
foreach ($route->tokens as $key => $v) foreach ($route->tokens as $key => $v)
@ -147,10 +165,12 @@ class Dispatcher extends RoutingBase {
} }
} }
} }
}
// Actually instantiate the controller return [
$this->call($controller_name, $method, $params); 'controller_name' => $controller_name,
'action_method' => $action_method,
'params' => $params
];
} }
/** /**
@ -219,7 +239,7 @@ class Dispatcher extends RoutingBase {
// Run the appropriate controller method // Run the appropriate controller method
$error_handler->addDataTable('controller_args', $params); $error_handler->addDataTable('controller_args', $params);
call_user_func_array([$controller, $action_method], $params); call_user_func_array([$controller, $method], $params);
} }
/** /**