szurubooru/src/Dispatcher.php

44 lines
877 B
PHP
Raw Normal View History

2014-08-30 15:04:33 +02:00
<?php
namespace Szurubooru;
//if script fails prematurely, mark it as fail from advance
http_response_code(500);
2014-08-30 15:04:33 +02:00
final class Dispatcher
{
private $router;
public function __construct(
\Szurubooru\Router $router,
\Szurubooru\ControllerRepository $controllerRepository)
{
$this->router = $router;
foreach ($controllerRepository->getControllers() as $controller)
$controller->registerRoutes($router);
}
public function run()
{
global $start;
try
{
2014-08-30 22:16:00 +02:00
$code = 200;
2014-08-30 15:04:33 +02:00
$json = $this->router->handle($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI']);
}
catch (\Exception $e)
{
2014-08-30 22:16:00 +02:00
$code = 400;
2014-08-30 15:04:33 +02:00
$json = [
'error' => $e->getMessage(),
'trace' => $e->getTrace(),
];
}
$end = microtime(true);
$json['__time'] = $end - $start;
2014-08-30 22:16:00 +02:00
http_response_code($code);
2014-08-30 15:04:33 +02:00
header('Content-Type: application/json');
echo json_encode($json);
}
}