2014-08-30 15:04:33 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Bootstrap;
|
|
|
|
use Szurubooru\Config;
|
|
|
|
use Szurubooru\ControllerRepository;
|
|
|
|
use Szurubooru\DatabaseConnection;
|
|
|
|
use Szurubooru\Helpers\HttpHelper;
|
|
|
|
use Szurubooru\Router;
|
|
|
|
use Szurubooru\Services\AuthService;
|
|
|
|
use Szurubooru\Services\TokenService;
|
2014-08-30 15:04:33 +02:00
|
|
|
|
|
|
|
final class Dispatcher
|
|
|
|
{
|
|
|
|
private $router;
|
2014-10-05 20:25:11 +02:00
|
|
|
private $config;
|
2014-10-04 13:28:04 +02:00
|
|
|
private $databaseConnection;
|
2014-09-04 19:21:18 +02:00
|
|
|
private $authService;
|
2014-09-09 19:38:16 +02:00
|
|
|
private $tokenService;
|
2014-08-30 15:04:33 +02:00
|
|
|
|
|
|
|
public function __construct(
|
2014-10-08 14:47:47 +02:00
|
|
|
Router $router,
|
|
|
|
Config $config,
|
|
|
|
DatabaseConnection $databaseConnection,
|
|
|
|
HttpHelper $httpHelper,
|
|
|
|
AuthService $authService,
|
|
|
|
TokenService $tokenService,
|
2014-11-19 22:00:50 +01:00
|
|
|
RouteRepository $routeRepository,
|
2014-10-08 14:47:47 +02:00
|
|
|
ControllerRepository $controllerRepository)
|
2014-08-30 15:04:33 +02:00
|
|
|
{
|
|
|
|
$this->router = $router;
|
2014-10-05 20:25:11 +02:00
|
|
|
$this->config = $config;
|
2014-10-04 13:28:04 +02:00
|
|
|
$this->databaseConnection = $databaseConnection;
|
2014-08-31 16:56:00 +02:00
|
|
|
$this->httpHelper = $httpHelper;
|
2014-10-08 19:47:21 +02:00
|
|
|
$this->authService = $authService;
|
|
|
|
$this->tokenService = $tokenService;
|
2014-08-31 16:56:00 +02:00
|
|
|
|
|
|
|
//if script fails prematurely, mark it as fail from advance
|
|
|
|
$this->httpHelper->setResponseCode(500);
|
|
|
|
|
2014-08-30 15:04:33 +02:00
|
|
|
foreach ($controllerRepository->getControllers() as $controller)
|
|
|
|
$controller->registerRoutes($router);
|
2014-11-19 22:00:50 +01:00
|
|
|
|
|
|
|
$routeRepository->injectRoutes($router);
|
2014-08-30 15:04:33 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 14:32:26 +02:00
|
|
|
public function run($requestMethod, $requestUri)
|
2014-08-30 15:04:33 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-08-30 22:16:00 +02:00
|
|
|
$code = 200;
|
2014-09-04 19:21:18 +02:00
|
|
|
$this->authorizeFromRequestHeader();
|
2014-09-17 14:32:26 +02:00
|
|
|
$json = (array) $this->router->handle($requestMethod, $requestUri);
|
2014-08-30 15:04:33 +02:00
|
|
|
}
|
|
|
|
catch (\Exception $e)
|
|
|
|
{
|
2014-08-30 22:16:00 +02:00
|
|
|
$code = 400;
|
2014-09-16 14:42:46 +02:00
|
|
|
$trace = $e->getTrace();
|
|
|
|
foreach ($trace as &$item)
|
|
|
|
unset($item['args']);
|
2014-08-30 15:04:33 +02:00
|
|
|
$json = [
|
|
|
|
'error' => $e->getMessage(),
|
2014-09-16 14:42:46 +02:00
|
|
|
'trace' => $trace,
|
2014-08-30 15:04:33 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
$end = microtime(true);
|
2014-10-08 14:47:47 +02:00
|
|
|
$json['__time'] = $end - Bootstrap::getStartTime();
|
2014-10-05 20:25:11 +02:00
|
|
|
if ($this->config->misc->dumpSqlIntoQueries)
|
|
|
|
{
|
|
|
|
$json['__queries'] = $this->databaseConnection->getPDO()->getQueryCount();
|
|
|
|
$json['__statements'] = $this->databaseConnection->getPDO()->getStatements();
|
|
|
|
}
|
2014-08-30 22:16:00 +02:00
|
|
|
|
2014-08-31 16:56:00 +02:00
|
|
|
$this->httpHelper->setResponseCode($code);
|
|
|
|
$this->httpHelper->setHeader('Content-Type', 'application/json');
|
|
|
|
$this->httpHelper->outputJSON($json);
|
|
|
|
|
|
|
|
return $json;
|
2014-08-30 15:04:33 +02:00
|
|
|
}
|
2014-09-04 19:21:18 +02:00
|
|
|
|
|
|
|
private function authorizeFromRequestHeader()
|
|
|
|
{
|
2014-09-09 19:38:16 +02:00
|
|
|
$loginTokenName = $this->httpHelper->getRequestHeader('X-Authorization-Token');
|
|
|
|
if ($loginTokenName)
|
|
|
|
{
|
|
|
|
$token = $this->tokenService->getByName($loginTokenName);
|
|
|
|
$this->authService->loginFromToken($token);
|
|
|
|
}
|
2014-09-04 19:21:18 +02:00
|
|
|
}
|
2014-08-30 15:04:33 +02:00
|
|
|
}
|