8 use FastRoute\Dispatcher
;
18 function __construct(BaseUrl
$baseUrl, Request
$request, Response
$response, Plates\Engine
$view) {
19 $this->baseUrl
= $baseUrl;
20 $this->request
= $request;
21 $this->response
= $response;
22 $this->view
= $view->addData(["location" => null]);
26 function __invoke(Dispatcher
$dispatcher) {
27 if (!file_exists("../config/maintenance")) {
28 $route = $dispatcher->dispatch($this->request
->getRequestMethod(),
29 $this->baseUrl
->pathinfo($this->request
->getRequestUrl()));
32 case Dispatcher
::NOT_FOUND
:
33 $this->display(404, null, 404);
36 case Dispatcher
::METHOD_NOT_ALLOWED
:
37 $this->display(405, null, 405, ["Allowed" => $route[1]]);
40 case Dispatcher
::FOUND
:
41 list(, $handler, $args) = $route;
43 $handler(array_map("urldecode", $args));
44 } catch (\Exception
$exception) {
46 $this->display(500, compact("exception"), 500, ["X-Exception", get_class($exception)]);
51 $this->display(503, null, 503);
54 $this->response
->send();
57 function display($view, array $data = null, $code = null, array $headers = []) {
59 $this->response
->setResponseCode($code);
62 $this->response
->addHeaders($headers);
64 $this->response
->getBody()->append(
65 $this->view
->render($view, (array) $data));
68 function redirect($url, $code = 302) {
69 $this->response
->setResponseCode($code);
70 $this->response
->setHeader("Location", $url);
73 function getBaseUrl() {
74 return $this->baseUrl
;
81 function getRequest() {
82 return $this->request
;
85 function getResponse() {
86 return $this->response
;
89 static function cleanBuffers() {
90 while (ob_get_level()) {
91 if (!@ob_end_clean
()) {