2015-05-22 12:36:26 -04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
use Aura\Router\RouterFactory;
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Basic routing/ dispatch
|
|
|
|
*/
|
2015-05-22 12:36:26 -04:00
|
|
|
class Router {
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* The route-matching object
|
|
|
|
* @var object $router
|
|
|
|
*/
|
|
|
|
protected $router;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The global configuration object
|
|
|
|
* @var object $config
|
|
|
|
*/
|
|
|
|
protected $config;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
2015-05-22 12:36:26 -04:00
|
|
|
public function __construct()
|
|
|
|
{
|
2015-06-11 16:44:52 -04:00
|
|
|
global $config;
|
|
|
|
$this->config = $config;
|
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
$router_factory = new RouterFactory();
|
|
|
|
$router = $router_factory->newInstance();
|
|
|
|
$this->router = $router_factory->newInstance();
|
|
|
|
|
|
|
|
$this->_setup_routes();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current route object, if one matches
|
|
|
|
*
|
|
|
|
* @return object
|
|
|
|
*/
|
|
|
|
public function get_route()
|
|
|
|
{
|
2015-06-11 16:44:52 -04:00
|
|
|
global $defaultHandler;
|
|
|
|
|
|
|
|
$raw_route = $_SERVER['REQUEST_URI'];
|
|
|
|
$route_path = str_replace([$this->config->anime_path, $this->config->manga_path], '', $raw_route);
|
|
|
|
$route_path = "/" . trim($route_path, '/');
|
|
|
|
|
|
|
|
$defaultHandler->addDataTable('Route Info', [
|
|
|
|
'route_path' => $route_path
|
|
|
|
]);
|
|
|
|
|
|
|
|
$route = $this->router->match($route_path, $_SERVER);
|
2015-05-22 12:36:26 -04:00
|
|
|
|
|
|
|
return $route;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle the current route
|
|
|
|
*
|
|
|
|
* @param [object] $route
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function dispatch($route = NULL)
|
|
|
|
{
|
2015-05-27 09:03:42 -04:00
|
|
|
global $defaultHandler;
|
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
if (is_null($route))
|
|
|
|
{
|
|
|
|
$route = $this->get_route();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $route)
|
|
|
|
{
|
2015-05-27 09:03:42 -04:00
|
|
|
$failure = $this->router->getFailedRoute();
|
|
|
|
$defaultHandler->addDataTable('failed_route', (array)$failure);
|
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
$controller_name = 'BaseController';
|
|
|
|
$action_method = 'outputHTML';
|
|
|
|
$params = [
|
|
|
|
'template' => '404',
|
|
|
|
'data' => [
|
|
|
|
'title' => 'Page Not Found'
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$controller_name = $route->params['controller'];
|
|
|
|
$action_method = $route->params['action'];
|
|
|
|
$params = (isset($route->params['params'])) ? $route->params['params'] : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$controller = new $controller_name();
|
|
|
|
|
|
|
|
// Run the appropriate controller method
|
|
|
|
call_user_func_array([$controller, $action_method], $params);
|
|
|
|
}
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Select controller based on the current url, and apply its relevent routes
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2015-05-22 12:36:26 -04:00
|
|
|
private function _setup_routes()
|
|
|
|
{
|
2015-06-11 16:44:52 -04:00
|
|
|
$route_type = "anime";
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
if ($this->config->manga_host !== "" && strpos($_SERVER['HTTP_HOST'], $this->config->manga_host) !== FALSE)
|
|
|
|
{
|
|
|
|
$route_type = "manga";
|
|
|
|
}
|
|
|
|
else if ($this->config->manga_path !== "" && strpos($_SERVER['REQUEST_URI'], $this->config->manga_path) !== FALSE)
|
|
|
|
{
|
|
|
|
$route_type = "manga";
|
2015-05-27 09:03:42 -04:00
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
$routes = $this->config->routes;
|
2015-05-22 12:36:26 -04:00
|
|
|
|
|
|
|
// Add routes by the configuration file
|
2015-05-27 09:03:42 -04:00
|
|
|
foreach($routes[$route_type] as $name => $route)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
|
|
|
$path = $route['path'];
|
|
|
|
unset($route['path']);
|
|
|
|
$this->router->add($name, $path)->addValues($route);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// End of Router.php
|