misuzu/src/RoutingContext.php

37 lines
1 KiB
PHP
Raw Normal View History

2023-09-10 00:04:53 +00:00
<?php
namespace Misuzu;
2024-03-30 03:14:03 +00:00
use Index\Http\Routing\{HttpRouter,IRouter,IRouteHandler};
use Misuzu\URLs\{IURLSource,URLInfo,URLRegistry};
2023-09-10 00:04:53 +00:00
class RoutingContext {
private URLRegistry $urls;
2024-03-30 03:14:03 +00:00
private HttpRouter $router;
2023-09-10 00:04:53 +00:00
public function __construct() {
$this->urls = new URLRegistry;
2024-03-30 03:14:03 +00:00
$this->router = new HttpRouter(errorHandler: new RoutingErrorHandler);
2023-09-10 00:04:53 +00:00
$this->router->use('/', fn($resp) => $resp->setPoweredBy('Misuzu'));
}
public function getURLs(): URLRegistry {
return $this->urls;
}
public function getRouter(): IRouter {
return $this->router;
}
public function register(IRouteHandler|IURLSource $handler): void {
if($handler instanceof IRouteHandler)
$this->router->register($handler);
if($handler instanceof IURLSource)
$handler->registerURLs($this->urls);
URLInfo::handleAttributes($this->urls, $handler);
}
2024-03-30 03:14:03 +00:00
public function dispatch(...$args): void {
$this->router->dispatch(...$args);
2023-09-10 00:04:53 +00:00
}
}