misuzu/src/RoutingContext.php

36 lines
993 B
PHP
Raw Normal View History

2023-09-10 00:04:53 +00:00
<?php
namespace Misuzu;
2024-10-05 02:40:29 +00:00
use Index\Http\Routing\{HttpRouter,Router,RouteHandler};
use Index\Urls\{ArrayUrlRegistry,UrlFormat,UrlRegistry,UrlSource};
2023-09-10 00:04:53 +00:00
class RoutingContext {
2024-10-05 02:40:29 +00:00
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() {
2024-10-05 02:40:29 +00:00
$this->urls = new ArrayUrlRegistry;
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'));
}
2024-10-05 02:40:29 +00:00
public function getUrls(): UrlRegistry {
2023-09-10 00:04:53 +00:00
return $this->urls;
}
2024-10-05 02:40:29 +00:00
public function getRouter(): Router {
2023-09-10 00:04:53 +00:00
return $this->router;
}
2024-10-05 02:40:29 +00:00
public function register(RouteHandler|UrlSource $handler): void {
if($handler instanceof RouteHandler)
2023-09-10 00:04:53 +00:00
$this->router->register($handler);
2024-10-05 02:40:29 +00:00
if($handler instanceof UrlSource)
$this->urls->register($handler);
2023-09-10 00:04:53 +00:00
}
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
}
}