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 $route = $dispatcher->dispatch($this->request
->getRequestMethod(),
28 $this->baseUrl
->pathinfo($this->request
->getRequestUrl()));
31 case Dispatcher
::NOT_FOUND
:
32 $this->display(404, null, 404);
35 case Dispatcher
::METHOD_NOT_ALLOWED
:
36 $this->display(405, null, 405, ["Allowed" => $route[1]]);
39 case Dispatcher
::FOUND
:
40 list(, $handler, $args) = $route;
42 $handler(array_map("urldecode", $args));
43 } catch (\Exception
$exception) {
45 $this->display(500, compact("exception"), 500, ["X-Exception", get_class($exception)]);
50 $this->response
->send();
53 function display($view, array $data = null, $code = null, array $headers = []) {
55 $this->response
->setResponseCode($code);
58 $this->response
->addHeaders($headers);
60 $this->response
->getBody()->append(
61 $this->view
->render($view, (array) $data));
64 function redirect($url, $code = 302) {
65 $this->response
->setResponseCode($code);
66 $this->response
->setHeader("Location", $url);
69 function getBaseUrl() {
70 return $this->baseUrl
;
77 function getRequest() {
78 return $this->request
;
81 function getResponse() {
82 return $this->response
;
85 static function cleanBuffers() {
86 while (ob_get_level()) {
87 if (!@ob_end_clean
()) {