misuzu/src/RoutingContext.php

27 lines
858 B
PHP

<?php
namespace Misuzu;
use Index\Http\Routing\{HttpRouter,Router,RouteHandler};
use Index\Urls\{ArrayUrlRegistry,UrlFormat,UrlRegistry,UrlSource};
class RoutingContext {
public private(set) UrlRegistry $urls;
public private(set) HttpRouter $router;
public function __construct() {
$this->urls = new ArrayUrlRegistry;
$this->router = new HttpRouter(errorHandler: new RoutingErrorHandler);
$this->router->use('/', fn($resp) => $resp->setPoweredBy('Misuzu'));
}
public function register(RouteHandler|UrlSource $handler): void {
if($handler instanceof RouteHandler)
$this->router->register($handler);
if($handler instanceof UrlSource)
$this->urls->register($handler);
}
public function dispatch(...$args): void {
$this->router->dispatch(...$args);
}
}