2015-05-22 12:36:26 -04:00
|
|
|
<?php
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
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
|
|
|
|
* @link https://github.com/timw4mail/HummingBirdAnimeClient
|
|
|
|
* @license MIT
|
2015-06-16 11:11:35 -04:00
|
|
|
*/
|
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;
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Basic routing/ dispatch
|
|
|
|
*/
|
2015-10-09 14:34:55 -04:00
|
|
|
class Dispatcher extends RoutingBase {
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* The route-matching object
|
|
|
|
* @var object $router
|
|
|
|
*/
|
|
|
|
protected $router;
|
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
/**
|
|
|
|
* Class wrapper for input superglobals
|
|
|
|
* @var object
|
|
|
|
*/
|
|
|
|
protected $request;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Routes added to router
|
|
|
|
* @var array $output_routes
|
|
|
|
*/
|
|
|
|
protected $output_routes;
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
2015-06-29 09:46:49 -04:00
|
|
|
*
|
2015-10-06 10:24:48 -04:00
|
|
|
* @param ContainerInterface $container
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
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);
|
2015-09-14 10:54:50 -04:00
|
|
|
$this->router = $container->get('aura-router');
|
|
|
|
$this->request = $container->get('request');
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
$this->output_routes = $this->_setup_routes();
|
2015-10-01 16:30:46 -04:00
|
|
|
$this->generate_convention_routes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate routes based on controller methods
|
|
|
|
*
|
2015-10-06 12:15:19 -04:00
|
|
|
* @return void
|
2015-10-01 16:30:46 -04:00
|
|
|
*/
|
|
|
|
protected function generate_convention_routes()
|
|
|
|
{
|
2015-11-13 11:33:27 -05:00
|
|
|
$default_controller = $this->routes['convention']['default_controller'];
|
|
|
|
|
|
|
|
$this->output_routes[] = $this->router->addGet('login', '/{controller}/login')
|
2015-10-20 16:41:51 -04:00
|
|
|
->setValues([
|
2015-11-13 11:33:27 -05:00
|
|
|
'controller' => $default_controller,
|
|
|
|
'action' => 'login'
|
2015-10-20 16:41:51 -04:00
|
|
|
]);
|
|
|
|
|
2015-11-13 11:33:27 -05:00
|
|
|
$this->output_routes[] = $this->router->addPost('login_post', '/{controller}/login')
|
2015-10-01 16:30:46 -04:00
|
|
|
->setValues([
|
2015-11-13 11:33:27 -05:00
|
|
|
'controller' => $default_controller,
|
|
|
|
'action' => 'login_action'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->output_routes[] = $this->router->addGet('logout', '/{controller}/logout')
|
|
|
|
->setValues([
|
|
|
|
'controller' => $default_controller,
|
|
|
|
'action' => 'logout'
|
2015-10-01 16:30:46 -04:00
|
|
|
]);
|
|
|
|
|
2015-11-13 11:33:27 -05:00
|
|
|
$this->output_routes[] = $this->router->addPost('update', '/{controller}/update')
|
2015-10-01 16:30:46 -04:00
|
|
|
->setValues([
|
2015-11-13 11:33:27 -05:00
|
|
|
'controller' => $default_controller,
|
|
|
|
'action' => 'update'
|
|
|
|
])->setTokens([
|
|
|
|
'controller' => '[a-z_]+'
|
|
|
|
]);
|
|
|
|
|
2016-01-04 10:53:03 -05:00
|
|
|
$this->output_routes[] = $this->router->addPost('update_form', '/{controller}/update_form')
|
|
|
|
->setValues([
|
|
|
|
'controller' => $default_controller,
|
|
|
|
'action' => 'form_update'
|
|
|
|
])->setTokens([
|
|
|
|
'controller' => '[a-z_]+'
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->output_routes[] = $this->router->addGet('edit', '/{controller}/edit/{id}/{status}')
|
|
|
|
->setValues([
|
|
|
|
'controller' => $default_controller,
|
|
|
|
'action' => 'edit'
|
|
|
|
])->setTokens([
|
|
|
|
'id' => '[0-9a-z_]+',
|
|
|
|
'status' => '[a-z\-]+',
|
|
|
|
]);
|
|
|
|
|
2015-11-13 11:33:27 -05:00
|
|
|
$this->output_routes[] = $this->router->addGet('list', '/{controller}/{type}{/view}')
|
|
|
|
->setValues([
|
|
|
|
'controller' => $default_controller,
|
2015-10-01 16:30:46 -04:00
|
|
|
'action' => $this->routes['convention']['default_method'],
|
2015-11-04 16:36:54 -05:00
|
|
|
])->setTokens([
|
|
|
|
'type' => '[a-z_]+',
|
|
|
|
'view' => '[a-z_]+'
|
2015-10-01 16:30:46 -04:00
|
|
|
]);
|
2015-11-13 11:33:27 -05:00
|
|
|
|
|
|
|
$this->output_routes[] = $this->router->addGet('index_redirect', '/')
|
|
|
|
->setValues([
|
|
|
|
'controller' => 'Aviat\\AnimeClient\\Controller',
|
|
|
|
'action' => 'redirect_to_default'
|
|
|
|
]);
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current route object, if one matches
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function get_route()
|
|
|
|
{
|
2015-09-14 10:54:50 -04:00
|
|
|
$error_handler = $this->container->get('error-handler');
|
2015-06-11 16:44:52 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
$raw_route = $this->request->server->get('PATH_INFO');
|
|
|
|
$route_path = "/" . trim($raw_route, '/');
|
2015-06-11 16:44:52 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
$error_handler->addDataTable('Route Info', [
|
2015-06-11 16:44:52 -04:00
|
|
|
'route_path' => $route_path
|
|
|
|
]);
|
|
|
|
|
2015-11-05 10:41:46 -05:00
|
|
|
return $this->router->match($route_path, $_SERVER);
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2015-06-30 13:03:20 -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
|
|
|
|
*
|
2015-06-30 13:03:20 -04:00
|
|
|
* @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
|
|
|
{
|
2015-09-14 10:54:50 -04:00
|
|
|
$error_handler = $this->container->get('error-handler');
|
2015-11-13 11:33:27 -05:00
|
|
|
$controller_name = $this->routes['convention']['default_controller'];
|
|
|
|
$action_method = $this->routes['convention']['404_method'];
|
|
|
|
$params = [];
|
2015-05-27 09:03:42 -04:00
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
if (is_null($route))
|
|
|
|
{
|
|
|
|
$route = $this->get_route();
|
2015-09-14 10:54:50 -04:00
|
|
|
$error_handler->addDataTable('route_args', (array)$route);
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $route)
|
|
|
|
{
|
2015-05-27 09:03:42 -04:00
|
|
|
$failure = $this->router->getFailedRoute();
|
2015-09-14 10:54:50 -04:00
|
|
|
$error_handler->addDataTable('failed_route', (array)$failure);
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-11-13 11:33:27 -05:00
|
|
|
if (isset($route->params['controller']))
|
|
|
|
{
|
|
|
|
$controller_name = $route->params['controller'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($route->params['action']))
|
|
|
|
{
|
|
|
|
$action_method = $route->params['action'];
|
|
|
|
}
|
2015-10-01 16:30:46 -04:00
|
|
|
|
2015-09-17 23:11:18 -04:00
|
|
|
if (is_null($controller_name))
|
|
|
|
{
|
|
|
|
throw new \LogicException("Missing controller");
|
|
|
|
}
|
2015-10-01 16:30:46 -04:00
|
|
|
|
|
|
|
if (strpos($controller_name, '\\') === FALSE)
|
|
|
|
{
|
|
|
|
$map = $this->get_controller_list();
|
|
|
|
$controller_name = $map[$controller_name];
|
|
|
|
}
|
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
$params = (isset($route->params['params'])) ? $route->params['params'] : [];
|
2015-06-16 11:11:35 -04:00
|
|
|
|
|
|
|
if ( ! empty($route->tokens))
|
|
|
|
{
|
2015-10-06 13:35:42 -04:00
|
|
|
foreach ($route->tokens as $key => $v)
|
2015-06-16 11:11:35 -04:00
|
|
|
{
|
|
|
|
if (array_key_exists($key, $route->params))
|
|
|
|
{
|
|
|
|
$params[$key] = $route->params[$key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
$controller = new $controller_name($this->container);
|
2015-05-22 12:36:26 -04:00
|
|
|
|
|
|
|
// Run the appropriate controller method
|
2015-09-14 10:54:50 -04:00
|
|
|
$error_handler->addDataTable('controller_args', $params);
|
2015-05-22 12:36:26 -04:00
|
|
|
call_user_func_array([$controller, $action_method], $params);
|
|
|
|
}
|
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
/**
|
|
|
|
* Get the type of route, to select the current controller
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-09-14 10:54:50 -04:00
|
|
|
public function get_controller()
|
2015-06-30 13:03:20 -04:00
|
|
|
{
|
2015-09-14 15:49:20 -04:00
|
|
|
$route_type = $this->__get('default_list');
|
2015-09-14 10:54:50 -04:00
|
|
|
$request_uri = $this->request->server->get('PATH_INFO');
|
2015-06-30 13:03:20 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
$path = trim($request_uri, '/');
|
2015-06-30 13:03:20 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
$segments = explode('/', $path);
|
2015-09-14 15:49:20 -04:00
|
|
|
$controller = reset($segments);
|
2015-06-30 13:03:20 -04:00
|
|
|
|
2015-09-14 16:14:02 -04:00
|
|
|
if (empty($controller))
|
|
|
|
{
|
|
|
|
$controller = $route_type;
|
|
|
|
}
|
2015-06-30 13:03:20 -04:00
|
|
|
|
2015-09-14 15:49:20 -04:00
|
|
|
return $controller;
|
2015-06-30 13:03:20 -04:00
|
|
|
}
|
|
|
|
|
2015-09-16 12:25:35 -04:00
|
|
|
/**
|
|
|
|
* Get the list of controllers in the default namespace
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_controller_list()
|
|
|
|
{
|
|
|
|
$convention_routing = $this->routes['convention'];
|
|
|
|
$default_namespace = $convention_routing['default_namespace'];
|
|
|
|
$path = str_replace('\\', '/', $default_namespace);
|
|
|
|
$path = trim($path, '/');
|
|
|
|
$actual_path = \_dir(SRC_DIR, $path);
|
|
|
|
|
|
|
|
$class_files = glob("{$actual_path}/*.php");
|
|
|
|
|
|
|
|
$controllers = [];
|
|
|
|
|
2015-10-06 13:35:42 -04:00
|
|
|
foreach ($class_files as $file)
|
2015-09-16 12:25:35 -04:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Select controller based on the current url, and apply its relevent routes
|
|
|
|
*
|
2015-06-30 13:03:20 -04:00
|
|
|
* @return array
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2015-06-30 13:03:20 -04:00
|
|
|
public function _setup_routes()
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-11-11 14:53:09 -05:00
|
|
|
$routes = [];
|
2015-06-30 13:03:20 -04:00
|
|
|
|
2015-09-14 10:54:50 -04:00
|
|
|
$route_type = $this->get_controller();
|
2015-06-30 13:03:20 -04:00
|
|
|
|
|
|
|
// Return early if invalid route array
|
2015-11-05 10:41:46 -05:00
|
|
|
if ( ! array_key_exists($route_type, $this->routes))
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2016-01-04 10:53:03 -05:00
|
|
|
$applied_routes = $this->routes[$route_type];
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-24 16:01:35 -04:00
|
|
|
// Add routes
|
2015-10-06 13:35:42 -04:00
|
|
|
foreach ($applied_routes as $name => &$route)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2015-06-30 13:03:20 -04:00
|
|
|
$path = $route['path'];
|
|
|
|
unset($route['path']);
|
2015-06-16 11:11:35 -04:00
|
|
|
|
2015-09-16 12:25:35 -04:00
|
|
|
$controller_map = $this->get_controller_list();
|
2015-11-13 11:33:27 -05:00
|
|
|
if (array_key_exists($route_type, $controller_map))
|
|
|
|
{
|
|
|
|
$controller_class = $controller_map[$route_type];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$controller_class = $this->routes['convention']['default_controller'];
|
|
|
|
}
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
// 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
|
|
|
|
2015-06-30 13:03:20 -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))
|
|
|
|
? "add" . ucfirst(strtolower($route['verb'])) : "addGet";
|
2015-06-17 08:50:01 -04:00
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
// Add the route to the router object
|
|
|
|
if ( ! array_key_exists('tokens', $route))
|
|
|
|
{
|
2015-11-11 14:53:09 -05:00
|
|
|
$routes[] = $this->router->$add($name, $path)->addValues($route);
|
2015-06-30 13:03:20 -04:00
|
|
|
}
|
|
|
|
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)
|
2015-06-30 13:03:20 -04:00
|
|
|
->addValues($route)
|
|
|
|
->addTokens($tokens);
|
2015-06-17 08:50:01 -04:00
|
|
|
}
|
|
|
|
}
|
2015-06-30 13:03:20 -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
|