index/src/Http/Routing/ScopedRouter.php

43 lines
1.1 KiB
PHP

<?php
// ScopedRouter.php
// Created: 2024-03-28
// Updated: 2024-03-28
namespace Index\Http\Routing;
class ScopedRouter implements IRouter {
use RouterTrait;
private IRouter $router;
private string $prefix;
public function __construct(IRouter $router, string $prefix) {
if($router instanceof ScopedRouter)
$router = $router->getParentRouter();
$this->router = $router;
// todo: cleanup
$this->prefix = $prefix;
}
private function getParentRouter(): IRouter {
return $this->router;
}
public function scopeTo(string $prefix): IRouter {
return $this->router->scopeTo($this->prefix . $prefix);
}
public function add(string $method, string $path, callable $handler): void {
$this->router->add($method, $this->prefix . $path, $handler);
}
public function use(string $path, callable $handler): void {
$this->router->use($this->prefix . $path, $handler);
}
public function resolve(string $method, string $path): ResolvedRouteInfo {
return $this->router->resolve($method, $this->prefix . $path);
}
}