37 lines
1.3 KiB
PHP
37 lines
1.3 KiB
PHP
|
<?php
|
||
|
namespace Satori;
|
||
|
|
||
|
use Index\Http\HttpFx;
|
||
|
use Index\Http\HttpRequest;
|
||
|
use Index\Routing\IRouter;
|
||
|
use Index\Routing\IRouteHandler;
|
||
|
|
||
|
class RoutingContext {
|
||
|
private HttpFx $router;
|
||
|
|
||
|
public function __construct() {
|
||
|
$this->router = new HttpFx;
|
||
|
$this->router->use('/', fn($resp) => $resp->setPoweredBy('Satori'));
|
||
|
}
|
||
|
|
||
|
public function getRouter(): IRouter {
|
||
|
return $this->router;
|
||
|
}
|
||
|
|
||
|
public function registerDefaultErrorPages(): void {
|
||
|
$this->router->addErrorHandler(401, fn($resp) => $resp->setContent(file_get_contents(SAT_DIR_PUBLIC . '/404.html')));
|
||
|
$this->router->addErrorHandler(403, fn($resp) => $resp->setContent(file_get_contents(SAT_DIR_PUBLIC . '/404.html')));
|
||
|
$this->router->addErrorHandler(404, fn($resp) => $resp->setContent(file_get_contents(SAT_DIR_PUBLIC . '/404.html')));
|
||
|
$this->router->addErrorHandler(500, fn($resp) => $resp->setContent(file_get_contents(SAT_DIR_PUBLIC . '/404.html')));
|
||
|
$this->router->addErrorHandler(503, fn($resp) => $resp->setContent(file_get_contents(SAT_DIR_PUBLIC . '/404.html')));
|
||
|
}
|
||
|
|
||
|
public function register(IRouteHandler $handler): void {
|
||
|
$this->router->register($handler);
|
||
|
}
|
||
|
|
||
|
public function dispatch(?HttpRequest $request = null): void {
|
||
|
$this->router->dispatch($request);
|
||
|
}
|
||
|
}
|