HummingBirdAnimeClient/src/Aviat/AnimeClient/Dispatcher.php

358 lines
8.0 KiB
PHP
Raw Normal View History

2015-05-22 12:36:26 -04:00
<?php
/**
2015-11-16 11:40:01 -05:00
* Hummingbird Anime Client
*
* An API client for Hummingbird to manage anime and manga watch lists
*
* @package HummingbirdAnimeClient
* @author Timothy J. Warren
* @copyright Copyright (c) 2015 - 2016
2015-11-16 11:40:01 -05:00
* @link https://github.com/timw4mail/HummingBirdAnimeClient
* @license MIT
*/
2015-09-15 13:19:29 -04:00
namespace Aviat\AnimeClient;
2015-05-22 12:36:26 -04:00
2015-09-14 19:54:34 -04:00
use Aura\Web\Request;
use Aura\Web\Response;
2015-06-26 16:39:10 -04:00
2015-09-17 23:11:18 -04:00
use Aviat\Ion\Di\ContainerInterface;
2016-03-03 16:53:17 -05:00
use Aviat\Ion\Friend;
2016-01-06 11:08:56 -05:00
use Aviat\AnimeClient\AnimeClient;
2015-09-17 23:11:18 -04:00
2016-03-03 16:53:17 -05:00
/**
* Basic routing/ dispatch
*/
2015-10-09 14:34:55 -04:00
class Dispatcher extends RoutingBase {
2015-05-22 12:36:26 -04:00
/**
* The route-matching object
* @var object $router
*/
protected $router;
/**
* The route matcher
* @var object $matcher
*/
protected $matcher;
/**
* Class wrapper for input superglobals
* @var object
*/
protected $request;
/**
* Routes added to router
* @var array $output_routes
*/
protected $output_routes;
/**
* Constructor
2015-06-29 09:46:49 -04:00
*
* @param ContainerInterface $container
*/
2015-09-17 23:11:18 -04:00
public function __construct(ContainerInterface $container)
2015-05-22 12:36:26 -04:00
{
2015-09-14 15:49:20 -04:00
parent::__construct($container);
$this->router = $container->get('aura-router')->getMap();
$this->matcher = $container->get('aura-router')->getMatcher();
$this->request = $container->get('request');
2015-05-22 12:36:26 -04:00
$this->output_routes = $this->_setup_routes();
2015-05-22 12:36:26 -04:00
}
/**
* Get the current route object, if one matches
*
* @return object
*/
public function get_route()
{
$logger = $this->container->getLogger('default');
$raw_route = $this->request->getUri()->getPath();
$route_path = "/" . trim($raw_route, '/');
$logger->debug('Dispatcher - Routing data from get_route method');
$logger->debug(print_r([
'route_path' => $route_path
], TRUE));
return $this->matcher->match($this->request);
2015-05-22 12:36:26 -04:00
}
/**
* Get list of routes applied
*
* @return array
*/
public function get_output_routes()
{
return $this->output_routes;
}
2015-05-22 12:36:26 -04:00
/**
* Handle the current route
*
* @codeCoverageIgnore
2015-10-12 14:27:20 -04:00
* @param object|null $route
2015-05-22 12:36:26 -04:00
* @return void
*/
2015-10-09 14:34:55 -04:00
public function __invoke($route = NULL)
2015-05-22 12:36:26 -04:00
{
$logger = $this->container->getLogger('default');
2015-05-27 09:03:42 -04:00
2015-05-22 12:36:26 -04:00
if (is_null($route))
{
$route = $this->get_route();
$logger->debug('Dispatcher - Route invoke arguments');
$logger->debug(print_r($route, TRUE));
2015-05-22 12:36:26 -04:00
}
2016-02-02 21:38:38 -05:00
if ($route)
2016-01-08 16:39:18 -05:00
{
2016-03-03 16:53:17 -05:00
$parsed = $this->process_route(new Friend($route));
2016-01-08 16:39:18 -05:00
$controller_name = $parsed['controller_name'];
$action_method = $parsed['action_method'];
$params = $parsed['params'];
}
else
2015-05-22 12:36:26 -04:00
{
2016-01-08 15:54:21 -05:00
// If not route was matched, return an appropriate http
// error message
$error_route = $this->get_error_params();
2016-01-08 16:39:18 -05:00
$controller_name = AnimeClient::DEFAULT_CONTROLLER;
2016-01-08 15:54:21 -05:00
$action_method = $error_route['action_method'];
2016-01-08 16:39:18 -05:00
$params = $error_route['params'];
}
// 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->attributes))
2016-01-08 16:39:18 -05:00
{
$controller_name = $route->attributes['controller'];
2015-05-22 12:36:26 -04:00
}
else
{
2016-01-08 16:39:18 -05:00
throw new \LogicException("Missing controller");
}
2015-11-13 11:33:27 -05:00
2016-01-08 16:39:18 -05:00
// Get the full namespace for a controller if a short name is given
if (strpos($controller_name, '\\') === FALSE)
{
$map = $this->get_controller_list();
$controller_name = $map[$controller_name];
}
2015-10-01 16:30:46 -04:00
$action_method = (array_key_exists('action', $route->attributes))
? $route->attributes['action']
2016-01-08 16:39:18 -05:00
: AnimeClient::NOT_FOUND_METHOD;
2015-10-01 16:30:46 -04:00
2016-03-03 16:53:17 -05:00
$params = [];
if ( ! empty($route->__get('tokens')))
2016-01-08 16:39:18 -05:00
{
2016-03-03 16:53:17 -05:00
$tokens = array_keys($route->__get('tokens'));
foreach ($tokens as $param)
{
2016-03-03 16:53:17 -05:00
if (array_key_exists($param, $route->attributes))
{
2016-03-03 16:53:17 -05:00
$params[$param] = $route->attributes[$param];
}
}
2015-05-22 12:36:26 -04:00
}
2016-03-03 16:53:17 -05:00
$logger = $this->container->getLogger('default');
$logger->info(json_encode($params));
2015-05-22 12:36:26 -04:00
2016-01-08 16:39:18 -05:00
return [
'controller_name' => $controller_name,
'action_method' => $action_method,
'params' => $params
];
2015-05-22 12:36:26 -04:00
}
/**
* Get the type of route, to select the current controller
*
* @return string
*/
public function get_controller()
{
2015-09-14 15:49:20 -04:00
$route_type = $this->__get('default_list');
$request_uri = $this->request->getUri()->getPath();
$path = trim($request_uri, '/');
$segments = explode('/', $path);
2015-09-14 15:49:20 -04:00
$controller = reset($segments);
if (empty($controller))
{
$controller = $route_type;
}
2015-09-14 15:49:20 -04:00
return $controller;
}
/**
* Get the list of controllers in the default namespace
*
* @return array
*/
public function get_controller_list()
{
2016-01-06 11:08:56 -05:00
$default_namespace = AnimeClient::DEFAULT_CONTROLLER_NAMESPACE;
$path = str_replace('\\', '/', $default_namespace);
$path = trim($path, '/');
2016-01-06 17:06:30 -05:00
$actual_path = realpath(\_dir(AnimeClient::SRC_DIR, $path));
$class_files = glob("{$actual_path}/*.php");
$controllers = [];
foreach ($class_files as $file)
{
$raw_class_name = basename(str_replace(".php", "", $file));
$path = strtolower(basename($raw_class_name));
$class_name = trim($default_namespace . '\\' . $raw_class_name, '\\');
$controllers[$path] = $class_name;
}
return $controllers;
}
2016-01-08 15:54:21 -05:00
/**
* Create the controller object and call the appropriate
* method
*
* @param string $controller_name - The full namespace of the controller class
* @param string $method
* @param array $params
* @return void
*/
protected function call($controller_name, $method, array $params)
{
$logger = $this->container->getLogger('default');
2016-01-08 15:54:21 -05:00
$controller = new $controller_name($this->container);
// Run the appropriate controller method
$logger->debug('Dispatcher - controller arguments');
$logger->debug(print_r($params, TRUE));
2016-01-08 16:39:18 -05:00
call_user_func_array([$controller, $method], $params);
2016-01-08 15:54:21 -05:00
}
/**
* Get the appropriate params for the error page
* pased on the failed route
*
* @return array|false
*/
protected function get_error_params()
{
$logger = $this->container->getLogger('default');
$failure = $this->matcher->getFailedRoute();
$logger->info('Dispatcher - failed route');
$logger->info(print_r($failure, TRUE));
2016-01-08 15:54:21 -05:00
$action_method = AnimeClient::ERROR_MESSAGE_METHOD;
$params = [];
switch($failure->failedRule) {
case 'Aura\Router\Rule\Alows':
$params = [
'http_code' => 405,
'title' => '405 Method Not Allowed',
'message' => 'Invalid HTTP Verb'
];
break;
case 'Aura\Router\Rule\Accepts':
$params = [
'http_code' => 406,
'title' => '406 Not Acceptable',
'message' => 'Unacceptable content type'
];
break;
default:
// Fall back to a 404 message
$action_method = AnimeClient::NOT_FOUND_METHOD;
break;
2016-01-08 15:54:21 -05:00
}
return [
'params' => $params,
'action_method' => $action_method
];
}
/**
* Select controller based on the current url, and apply its relevent routes
*
* @return array
*/
2016-01-06 11:08:56 -05:00
protected function _setup_routes()
2015-05-22 12:36:26 -04:00
{
$route_type = $this->get_controller();
// Add routes
2016-01-06 11:08:56 -05:00
$routes = [];
foreach ($this->routes as $name => &$route)
2015-05-22 12:36:26 -04:00
{
$path = $route['path'];
unset($route['path']);
$controller_map = $this->get_controller_list();
2016-01-06 17:06:30 -05:00
$controller_class = (array_key_exists($route_type, $controller_map))
? $controller_map[$route_type]
: AnimeClient::DEFAULT_CONTROLLER;
2015-11-13 11:33:27 -05:00
if (array_key_exists($route_type, $controller_map))
{
$controller_class = $controller_map[$route_type];
}
// Prepend the controller to the route parameters
2015-10-01 16:30:46 -04:00
$route['controller'] = $controller_class;
2015-06-17 08:50:01 -04:00
// Select the appropriate router method based on the http verb
2015-09-17 23:11:18 -04:00
$add = (array_key_exists('verb', $route))
? strtolower($route['verb'])
: "get";
2015-06-17 08:50:01 -04:00
// Add the route to the router object
if ( ! array_key_exists('tokens', $route))
{
$routes[] = $this->router->$add($name, $path)->defaults($route);
}
else
{
$tokens = $route['tokens'];
unset($route['tokens']);
2015-06-17 08:50:01 -04:00
2015-11-11 14:53:09 -05:00
$routes[] = $this->router->$add($name, $path)
->defaults($route)
->tokens($tokens);
}
2015-06-17 08:50:01 -04:00
}
2015-11-11 14:53:09 -05:00
return $routes;
2015-05-22 12:36:26 -04:00
}
}
2015-10-09 14:34:55 -04:00
// End of Dispatcher.php