misuzu/src/RoutingContext.php

35 lines
993 B
PHP

<?php
namespace Misuzu;
use Index\Http\Routing\{HttpRouter,Router,RouteHandler};
use Index\Urls\{ArrayUrlRegistry,UrlFormat,UrlRegistry,UrlSource};
class RoutingContext {
private UrlRegistry $urls;
private 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 getUrls(): UrlRegistry {
return $this->urls;
}
public function getRouter(): Router {
return $this->router;
}
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);
}
}