2016-10-20 22:09:36 -04:00
|
|
|
<?php declare(strict_types=1);
|
2015-06-16 11:11:35 -04:00
|
|
|
/**
|
2017-02-16 11:09:37 -05:00
|
|
|
* Hummingbird Anime List Client
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2018-08-22 13:48:27 -04:00
|
|
|
* An API client for Kitsu to manage anime and manga watch lists
|
2015-11-16 11:40:01 -05:00
|
|
|
*
|
2021-02-04 11:57:01 -05:00
|
|
|
* PHP version 8
|
2016-08-30 10:01:18 -04:00
|
|
|
*
|
2022-03-04 15:50:35 -05:00
|
|
|
* @copyright 2015 - 2022 Timothy J. Warren <tim@timshome.page>
|
2016-08-30 10:01:18 -04:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License
|
2020-12-10 17:06:50 -05:00
|
|
|
* @version 5.2
|
2022-03-04 15:50:35 -05:00
|
|
|
* @link https://git.timshome.page/timw4mail/HummingBirdAnimeClient
|
2015-06-16 11:11:35 -04:00
|
|
|
*/
|
2016-10-20 22:09:36 -04:00
|
|
|
|
2015-09-15 13:19:29 -04:00
|
|
|
namespace Aviat\AnimeClient;
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2021-02-11 19:54:22 -05:00
|
|
|
use Aura\Router\{
|
|
|
|
Map,
|
|
|
|
Matcher,
|
|
|
|
Route,
|
|
|
|
Rule,
|
|
|
|
};
|
2017-04-05 13:01:51 -04:00
|
|
|
use Aviat\AnimeClient\API\FailedResponseException;
|
2022-03-03 17:26:09 -05:00
|
|
|
use Aviat\AnimeClient\Enum\EventType;
|
2015-09-17 23:11:18 -04:00
|
|
|
use Aviat\Ion\Di\ContainerInterface;
|
2020-04-10 20:01:46 -04:00
|
|
|
use Aviat\Ion\Type\StringType;
|
2022-03-03 17:26:09 -05:00
|
|
|
use Aviat\Ion\{Event, Friend, Json};
|
2019-12-09 14:34:23 -05:00
|
|
|
use LogicException;
|
|
|
|
use ReflectionException;
|
|
|
|
|
2021-02-11 19:54:22 -05:00
|
|
|
use function Aviat\Ion\_dir;
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Basic routing/ dispatch
|
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
final class Dispatcher extends RoutingBase
|
|
|
|
{
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* The route-matching object
|
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
protected Map $router;
|
2015-06-11 16:44:52 -04:00
|
|
|
|
2016-02-16 16:28:44 -05:00
|
|
|
/**
|
|
|
|
* The route matcher
|
|
|
|
*/
|
2020-04-10 20:01:46 -04:00
|
|
|
protected Matcher $matcher;
|
2016-02-16 16:28:44 -05:00
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
/**
|
2019-01-07 09:08:00 -05:00
|
|
|
* Routing array
|
2015-06-30 13:03:20 -04:00
|
|
|
*/
|
2022-03-03 13:25:10 -05:00
|
|
|
protected array $routes = [];
|
2015-06-30 13:03:20 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Routes added to router
|
|
|
|
*/
|
2022-03-03 13:25:10 -05:00
|
|
|
protected array $outputRoutes = [];
|
2015-06-30 13:03:20 -04:00
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*/
|
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);
|
2018-11-09 10:38:35 -05:00
|
|
|
$router = $this->container->get('aura-router');
|
|
|
|
$this->router = $router->getMap();
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2018-11-09 10:38:35 -05:00
|
|
|
$this->matcher = $router->getMatcher();
|
2019-01-07 09:08:00 -05:00
|
|
|
$this->routes = $this->config->get('routes');
|
2017-02-16 14:30:06 -05:00
|
|
|
$this->outputRoutes = $this->setupRoutes();
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the current route object, if one matches
|
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
public function getRoute(): Route|false
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2018-01-18 16:21:45 -05:00
|
|
|
$logger = $this->container->getLogger();
|
2015-06-11 16:44:52 -04:00
|
|
|
|
2017-02-16 14:30:06 -05:00
|
|
|
$rawRoute = $this->request->getUri()->getPath();
|
2018-01-16 14:58:07 -05:00
|
|
|
$routePath = '/' . trim($rawRoute, '/');
|
2015-06-11 16:44:52 -04:00
|
|
|
|
2018-01-18 16:21:45 -05:00
|
|
|
if ($logger !== NULL)
|
|
|
|
{
|
|
|
|
$logger->info('Dispatcher - Routing data from get_route method');
|
|
|
|
$logger->info(print_r([
|
2022-03-03 17:26:09 -05:00
|
|
|
'route_path' => $routePath,
|
2018-01-18 16:21:45 -05:00
|
|
|
], TRUE));
|
|
|
|
}
|
2015-06-11 16:44:52 -04:00
|
|
|
|
2016-02-16 16:28:44 -05:00
|
|
|
return $this->matcher->match($this->request);
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
/**
|
|
|
|
* Get list of routes applied
|
|
|
|
*
|
2022-03-03 13:25:10 -05:00
|
|
|
* @return mixed[]
|
2015-06-30 13:03:20 -04:00
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
public function getOutputRoutes(): array
|
2015-06-30 13:03:20 -04:00
|
|
|
{
|
2017-02-16 14:30:06 -05:00
|
|
|
return $this->outputRoutes;
|
2015-06-30 13:03:20 -04:00
|
|
|
}
|
|
|
|
|
2015-05-22 12:36:26 -04:00
|
|
|
/**
|
|
|
|
* Handle the current route
|
|
|
|
*
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws ReflectionException
|
2015-05-22 12:36:26 -04:00
|
|
|
*/
|
2022-03-03 17:26:09 -05:00
|
|
|
public function __invoke(?object $route = NULL): void
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2018-01-18 16:21:45 -05:00
|
|
|
$logger = $this->container->getLogger();
|
2015-05-27 09:03:42 -04:00
|
|
|
|
2018-01-18 16:21:45 -05:00
|
|
|
if ($route === NULL)
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2016-12-20 12:58:37 -05:00
|
|
|
$route = $this->getRoute();
|
2016-01-11 14:39:53 -05:00
|
|
|
|
2018-01-18 16:21:45 -05:00
|
|
|
if ($logger !== NULL)
|
|
|
|
{
|
|
|
|
$logger->info('Dispatcher - Route invoke arguments');
|
|
|
|
$logger->info(print_r($route, TRUE));
|
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2020-03-16 15:06:55 -04:00
|
|
|
if ( ! $route)
|
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
|
2022-01-07 12:33:01 -05:00
|
|
|
$errorRoute = $this->getErrorParams();
|
2017-02-08 15:48:20 -05:00
|
|
|
$controllerName = DEFAULT_CONTROLLER;
|
2017-02-16 14:30:06 -05:00
|
|
|
$actionMethod = $errorRoute['action_method'];
|
|
|
|
$params = $errorRoute['params'];
|
2020-03-16 15:06:55 -04:00
|
|
|
$this->call($controllerName, $actionMethod, $params);
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2020-03-16 15:06:55 -04:00
|
|
|
return;
|
2016-01-08 16:39:18 -05:00
|
|
|
}
|
2017-02-08 15:48:20 -05:00
|
|
|
|
2020-03-16 15:06:55 -04:00
|
|
|
$parsed = $this->processRoute(new Friend($route));
|
|
|
|
$controllerName = $parsed['controller_name'];
|
|
|
|
$actionMethod = $parsed['action_method'];
|
|
|
|
$params = $parsed['params'];
|
|
|
|
|
2017-02-08 15:48:20 -05:00
|
|
|
$this->call($controllerName, $actionMethod, $params);
|
2016-01-08 16:39:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse out the arguments for the appropriate controller for
|
|
|
|
* the current route
|
|
|
|
*
|
2019-12-09 14:34:23 -05:00
|
|
|
* @throws LogicException
|
2022-03-03 13:25:10 -05:00
|
|
|
* @return array<string, mixed>
|
2016-01-08 16:39:18 -05:00
|
|
|
*/
|
2021-02-11 19:54:22 -05:00
|
|
|
protected function processRoute(Friend $route): array
|
2016-01-08 16:39:18 -05:00
|
|
|
{
|
2020-03-16 15:06:55 -04:00
|
|
|
if ( ! array_key_exists('controller', $route->attributes))
|
2016-01-08 16:39:18 -05:00
|
|
|
{
|
2020-03-16 15:06:55 -04:00
|
|
|
throw new LogicException('Missing controller');
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
2020-03-16 15:06:55 -04:00
|
|
|
|
2020-05-06 13:16:40 -04:00
|
|
|
$controllerName = $route->attributes['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
|
2021-02-11 19:54:22 -05:00
|
|
|
if ( ! str_contains($controllerName, '\\'))
|
2016-01-08 16:39:18 -05:00
|
|
|
{
|
2016-12-20 12:58:37 -05:00
|
|
|
$map = $this->getControllerList();
|
2017-02-16 14:30:06 -05:00
|
|
|
$controllerName = $map[$controllerName];
|
2016-01-08 16:39:18 -05:00
|
|
|
}
|
2015-10-01 16:30:46 -04:00
|
|
|
|
2018-11-09 10:38:35 -05:00
|
|
|
$actionMethod = array_key_exists('action', $route->attributes)
|
2016-02-16 16:28:44 -05:00
|
|
|
? $route->attributes['action']
|
2017-02-08 15:48:20 -05:00
|
|
|
: 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'));
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2016-03-03 16:53:17 -05:00
|
|
|
foreach ($tokens as $param)
|
2015-06-16 11:11:35 -04:00
|
|
|
{
|
2016-03-03 16:53:17 -05:00
|
|
|
if (array_key_exists($param, $route->attributes))
|
2015-06-16 11:11:35 -04:00
|
|
|
{
|
2016-03-03 16:53:17 -05:00
|
|
|
$params[$param] = $route->attributes[$param];
|
2015-06-16 11:11:35 -04:00
|
|
|
}
|
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2018-01-18 16:21:45 -05:00
|
|
|
$logger = $this->container->getLogger();
|
|
|
|
if ($logger !== NULL)
|
|
|
|
{
|
2021-02-11 19:54:22 -05:00
|
|
|
$logger->info(Json::encode($params));
|
2018-01-18 16:21:45 -05:00
|
|
|
}
|
2015-05-22 12:36:26 -04:00
|
|
|
|
2016-01-08 16:39:18 -05:00
|
|
|
return [
|
2017-02-16 14:30:06 -05:00
|
|
|
'controller_name' => $controllerName,
|
|
|
|
'action_method' => $actionMethod,
|
2022-03-03 17:26:09 -05:00
|
|
|
'params' => $params,
|
2016-01-08 16:39:18 -05:00
|
|
|
];
|
2015-05-22 12:36:26 -04:00
|
|
|
}
|
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
/**
|
|
|
|
* Get the type of route, to select the current controller
|
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
public function getController(): string
|
2015-06-30 13:03:20 -04:00
|
|
|
{
|
2018-11-09 10:38:35 -05:00
|
|
|
$routeType = $this->config->get('default_list');
|
2017-02-16 14:30:06 -05:00
|
|
|
$requestUri = $this->request->getUri()->getPath();
|
|
|
|
$path = trim($requestUri, '/');
|
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
|
|
|
|
2018-01-18 16:21:45 -05:00
|
|
|
$logger = $this->container->getLogger();
|
|
|
|
if ($logger !== NULL)
|
|
|
|
{
|
|
|
|
$logger->info('Controller: ' . $controller);
|
|
|
|
}
|
2017-09-14 15:32:53 -04:00
|
|
|
|
2015-09-14 16:14:02 -04:00
|
|
|
if (empty($controller))
|
|
|
|
{
|
2017-02-16 14:30:06 -05:00
|
|
|
$controller = $routeType;
|
2015-09-14 16:14:02 -04:00
|
|
|
}
|
2015-06-30 13:03:20 -04:00
|
|
|
|
2018-11-09 10:38:35 -05: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
|
|
|
|
*
|
2022-03-03 13:25:10 -05:00
|
|
|
* @return mixed[]
|
2015-09-16 12:25:35 -04:00
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
public function getControllerList(): array
|
2015-09-16 12:25:35 -04:00
|
|
|
{
|
2017-02-16 14:30:06 -05:00
|
|
|
$defaultNamespace = DEFAULT_CONTROLLER_NAMESPACE;
|
2018-11-09 10:38:35 -05:00
|
|
|
$find = ['\\', 'Aviat/AnimeClient/'];
|
|
|
|
$replace = ['/', ''];
|
|
|
|
|
|
|
|
$path = str_replace($find, $replace, $defaultNamespace);
|
2015-09-16 12:25:35 -04:00
|
|
|
$path = trim($path, '/');
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2017-02-16 14:30:06 -05:00
|
|
|
$actualPath = realpath(_dir(SRC_DIR, $path));
|
|
|
|
$classFiles = glob("{$actualPath}/*.php");
|
2021-02-11 19:54:22 -05:00
|
|
|
if ($classFiles === FALSE)
|
|
|
|
{
|
|
|
|
return [];
|
|
|
|
}
|
2015-09-16 12:25:35 -04:00
|
|
|
|
|
|
|
$controllers = [];
|
|
|
|
|
2017-02-16 14:30:06 -05:00
|
|
|
foreach ($classFiles as $file)
|
2015-09-16 12:25:35 -04:00
|
|
|
{
|
2018-01-18 16:21:45 -05:00
|
|
|
$rawClassName = basename(str_replace('.php', '', $file));
|
2022-03-03 17:26:09 -05:00
|
|
|
$path = (string) StringType::from($rawClassName)->dasherize();
|
2017-02-16 14:30:06 -05:00
|
|
|
$className = trim($defaultNamespace . '\\' . $rawClassName, '\\');
|
2015-09-16 12:25:35 -04:00
|
|
|
|
2017-02-16 14:30:06 -05:00
|
|
|
$controllers[$path] = $className;
|
2015-09-16 12:25:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $controllers;
|
|
|
|
}
|
|
|
|
|
2016-01-08 15:54:21 -05:00
|
|
|
/**
|
|
|
|
* Create the controller object and call the appropriate
|
|
|
|
* method
|
|
|
|
*
|
2022-03-03 17:26:09 -05:00
|
|
|
* @param string $controllerName - The full namespace of the controller class
|
2016-01-08 15:54:21 -05:00
|
|
|
*/
|
2021-02-03 09:45:18 -05:00
|
|
|
protected function call(string $controllerName, string $method, array $params): void
|
2016-01-08 15:54:21 -05:00
|
|
|
{
|
2022-01-07 12:33:01 -05:00
|
|
|
$logger = $this->container->getLogger();
|
2016-01-08 15:54:21 -05:00
|
|
|
|
2017-04-05 13:01:51 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
$controller = new $controllerName($this->container);
|
|
|
|
|
|
|
|
// Run the appropriate controller method
|
2022-01-07 12:33:01 -05:00
|
|
|
$logger?->debug('Dispatcher - controller arguments', $params);
|
2016-01-08 15:54:21 -05:00
|
|
|
|
2021-02-11 19:54:22 -05:00
|
|
|
$params = array_values($params);
|
2022-03-03 17:26:09 -05:00
|
|
|
$controller->{$method}(...$params);
|
2017-04-05 13:01:51 -04:00
|
|
|
}
|
2021-02-11 19:54:22 -05:00
|
|
|
catch (FailedResponseException)
|
2017-04-05 13:01:51 -04:00
|
|
|
{
|
|
|
|
$controllerName = DEFAULT_CONTROLLER;
|
|
|
|
$controller = new $controllerName($this->container);
|
2022-03-03 17:26:09 -05:00
|
|
|
$controller->errorPage(
|
|
|
|
500,
|
2017-04-05 13:01:51 -04:00
|
|
|
'API request timed out',
|
2022-03-03 17:26:09 -05:00
|
|
|
'Failed to retrieve data from API (╯°□°)╯︵ ┻━┻'
|
|
|
|
);
|
2017-04-05 13:01:51 -04:00
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2020-05-08 19:17:11 -04:00
|
|
|
/* finally
|
2020-05-06 13:16:40 -04:00
|
|
|
{
|
|
|
|
// Log out on session/api token expiration
|
|
|
|
Event::on(EventType::UNAUTHORIZED, static function () {
|
|
|
|
$controllerName = DEFAULT_CONTROLLER;
|
|
|
|
(new $controllerName($this->container))->logout();
|
|
|
|
});
|
2020-05-08 19:17:11 -04:00
|
|
|
} */
|
2016-01-08 15:54:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the appropriate params for the error page
|
2019-12-09 14:34:23 -05:00
|
|
|
* passed on the failed route
|
2022-03-03 13:25:10 -05:00
|
|
|
* @return mixed[][]
|
2016-01-08 15:54:21 -05:00
|
|
|
*/
|
2021-02-26 14:42:07 -05:00
|
|
|
protected function getErrorParams(): array
|
2016-01-08 15:54:21 -05:00
|
|
|
{
|
2018-01-18 16:21:45 -05:00
|
|
|
$logger = $this->container->getLogger();
|
2016-02-17 11:36:37 -05:00
|
|
|
$failure = $this->matcher->getFailedRoute();
|
2016-01-11 14:39:53 -05:00
|
|
|
|
2018-01-18 16:21:45 -05:00
|
|
|
if ($logger !== NULL)
|
|
|
|
{
|
|
|
|
$logger->info('Dispatcher - failed route');
|
|
|
|
$logger->info(print_r($failure, TRUE));
|
|
|
|
}
|
2016-01-11 14:39:53 -05:00
|
|
|
|
2017-02-16 14:30:06 -05:00
|
|
|
$actionMethod = ERROR_MESSAGE_METHOD;
|
2016-01-08 15:54:21 -05:00
|
|
|
|
|
|
|
$params = [];
|
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
switch ($failure->failedRule) {
|
2018-10-05 14:32:05 -04:00
|
|
|
case Rule\Allows::class:
|
2016-02-17 11:36:37 -05:00
|
|
|
$params = [
|
|
|
|
'http_code' => 405,
|
|
|
|
'title' => '405 Method Not Allowed',
|
2022-03-03 17:26:09 -05:00
|
|
|
'message' => 'Invalid HTTP Verb',
|
2016-02-17 11:36:37 -05:00
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
2018-10-05 14:32:05 -04:00
|
|
|
case Rule\Accepts::class:
|
2016-02-17 11:36:37 -05:00
|
|
|
$params = [
|
|
|
|
'http_code' => 406,
|
|
|
|
'title' => '406 Not Acceptable',
|
2022-03-03 17:26:09 -05:00
|
|
|
'message' => 'Unacceptable content type',
|
2016-02-17 11:36:37 -05:00
|
|
|
];
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Fall back to a 404 message
|
2017-02-16 14:30:06 -05:00
|
|
|
$actionMethod = NOT_FOUND_METHOD;
|
2016-02-17 11:36:37 -05:00
|
|
|
break;
|
2016-01-08 15:54:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'params' => $params,
|
2022-03-03 17:26:09 -05:00
|
|
|
'action_method' => $actionMethod,
|
2016-01-08 15:54:21 -05:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-06-11 16:44:52 -04:00
|
|
|
/**
|
2022-01-07 12:33:01 -05:00
|
|
|
* Select controller based on the current url, and apply its relevant routes
|
2015-06-11 16:44:52 -04:00
|
|
|
*
|
2022-03-03 13:25:10 -05:00
|
|
|
* @return mixed[]
|
2015-06-11 16:44:52 -04:00
|
|
|
*/
|
2018-11-09 10:38:35 -05:00
|
|
|
protected function setupRoutes(): array
|
2015-05-22 12:36:26 -04:00
|
|
|
{
|
2017-02-16 14:30:06 -05:00
|
|
|
$routeType = $this->getController();
|
2015-06-30 13:03:20 -04:00
|
|
|
|
2015-06-24 16:01:35 -04:00
|
|
|
// Add routes
|
2016-01-06 11:08:56 -05:00
|
|
|
$routes = [];
|
2022-03-03 17:26:09 -05:00
|
|
|
|
2016-01-06 11:08:56 -05:00
|
|
|
foreach ($this->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
|
|
|
|
2017-02-16 14:30:06 -05:00
|
|
|
$controllerMap = $this->getControllerList();
|
2018-11-09 10:38:35 -05:00
|
|
|
$controllerClass = array_key_exists($routeType, $controllerMap)
|
2017-02-16 14:30:06 -05:00
|
|
|
? $controllerMap[$routeType]
|
2017-02-08 15:48:20 -05:00
|
|
|
: DEFAULT_CONTROLLER;
|
2016-01-06 17:06:30 -05:00
|
|
|
|
2018-11-01 22:15:20 -04:00
|
|
|
// If there's an explicit controller, try to find
|
|
|
|
// the full namespaced class name
|
|
|
|
if (array_key_exists('controller', $route))
|
2015-11-13 11:33:27 -05:00
|
|
|
{
|
2018-11-01 22:15:20 -04:00
|
|
|
$controllerKey = $route['controller'];
|
|
|
|
if (array_key_exists($controllerKey, $controllerMap))
|
|
|
|
{
|
|
|
|
$controllerClass = $controllerMap[$controllerKey];
|
|
|
|
}
|
2015-11-13 11:33:27 -05:00
|
|
|
}
|
2015-09-14 10:54:50 -04:00
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
// Prepend the controller to the route parameters
|
2017-02-16 14:30:06 -05:00
|
|
|
$route['controller'] = $controllerClass;
|
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
|
2021-02-03 09:45:18 -05:00
|
|
|
$verb = array_key_exists('verb', $route)
|
2016-02-16 16:28:44 -05:00
|
|
|
? strtolower($route['verb'])
|
2018-01-18 16:21:45 -05:00
|
|
|
: 'get';
|
2015-06-17 08:50:01 -04:00
|
|
|
|
2015-06-30 13:03:20 -04:00
|
|
|
// Add the route to the router object
|
2016-02-17 11:36:37 -05:00
|
|
|
if ( ! array_key_exists('tokens', $route))
|
2015-06-30 13:03:20 -04:00
|
|
|
{
|
2022-03-03 17:26:09 -05:00
|
|
|
$routes[] = $this->router->{$verb}($name, $path)->defaults($route);
|
|
|
|
|
2020-03-16 15:06:55 -04:00
|
|
|
continue;
|
2015-06-30 13:03:20 -04:00
|
|
|
}
|
2015-06-17 08:50:01 -04:00
|
|
|
|
2020-03-16 15:06:55 -04:00
|
|
|
$tokens = $route['tokens'];
|
|
|
|
unset($route['tokens']);
|
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
$routes[] = $this->router->{$verb}($name, $path)
|
2020-03-16 15:06:55 -04:00
|
|
|
->defaults($route)
|
|
|
|
->tokens($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
|
|
|
}
|
|
|
|
}
|
2022-03-03 13:25:10 -05:00
|
|
|
|
2022-03-03 17:26:09 -05:00
|
|
|
// End of Dispatcher.php
|