Skip to content

Internals Router Manager RouterManager

Stefan Crkvenjakov edited this page Oct 7, 2026 · 1 revision

router\manager\RouterManager

Framework Internals · Subsystem overview · Developer guide

Source: awt_src/classes/router/manager/RouterManager.php · Snapshot 8411f4287706.

Routing separates definition, HTTP request snapshot, matching, method policy, execution and error responses. Collaborators can be replaced through constructor injection.

Imports and dependencies

use event\EventDispatcher;
use redirect\Redirect;
use response\Response;
use router\http\GlobalsRequestProvider;
use router\interface\IRequest;
use router\interface\IRequestProvider;
use router\interface\IRoute;
use router\interface\IRouterManager;
use router\interface\IRouteResolver;
use router\interface\IRouteResponseFactory;
use router\matching\RouteResolver;
use router\response\RouteResponseFactory;
use view\View;

RouterManager

Declaration and architectural role

final class RouterManager implements IRouterManager

Source description (behavioral corrections are recorded in Known discrepancies):

Registers route contracts and coordinates replaceable resolution and responses.

Constants, enum cases, traits and properties

Declarations below preserve visibility, type, default and modifiers exactly. A typed property without an initializer remains uninitialized until its constructor or another method assigns it. Mutable fields belong to this instance unless marked static; readonly fields can be initialized once. Constructor-promoted properties are also listed below.

Member Declaration Purpose or source contract
$routesName private array $routesName = []; @var array<string, IRoute>
$eventDispatcher public EventDispatcher $eventDispatcher; Used by __construct(), addEventDispatcher(), addRouter().
Constructor-promoted private readonly IRequestProvider $requests = new GlobalsRequestProvider() Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private readonly IRouteResolver $resolver = new RouteResolver() Initialized from the constructor argument; default expression is evaluated when the argument is omitted.
Constructor-promoted private readonly IRouteResponseFactory $responses = new RouteResponseFactory() Initialized from the constructor argument; default expression is evaluated when the argument is omitted.

Method index

Method Signature
__construct public function __construct( private readonly IRequestProvider $requests = new GlobalsRequestProvider(), private readonly IRouteResolver $resolver = new RouteResolver(), private readonly IRouteResponseFactory $responses = new RouteResponseFactory(), ?EventDispatcher $eventDispatcher = null, )
addEventDispatcher public function addEventDispatcher(EventDispatcher $eventDispatcher): self
addRouter public function addRouter(IRoute $router): void
loadRouters public function loadRouters(array $routers): void
getRoutes public function getRoutes(): array
getRouters public function getRouters(): array
getRouteByName public function getRouteByName(string $name): ?IRoute
getRouteByPath public function getRouteByPath(string $path, string $method = 'GET'): ?IRoute
startRouter public function startRouter(?IRequest $request = null): View|Redirect|Response

__construct

Source line 26

public function __construct(
        private readonly IRequestProvider $requests = new GlobalsRequestProvider(),
        private readonly IRouteResolver $resolver = new RouteResolver(),
        private readonly IRouteResponseFactory $responses = new RouteResponseFactory(),
        ?EventDispatcher $eventDispatcher = null,
    )

Parameters

Parameter Type, modifiers and default Purpose
$requests private readonly IRequestProvider $requests = new GlobalsRequestProvider() Operation input; the exact use, validation and propagation are shown in the implementation trace.
$resolver private readonly IRouteResolver $resolver = new RouteResolver() Operation input; the exact use, validation and propagation are shown in the implementation trace.
$responses private readonly IRouteResponseFactory $responses = new RouteResponseFactory() Operation input; the exact use, validation and propagation are shown in the implementation trace.
$eventDispatcher ?EventDispatcher $eventDispatcher = null Dispatcher used for synchronous listener registration/dispatch and context propagation.

State writes: $this->eventDispatcher.

Implementation, validation guards and interaction order
public function __construct(
        private readonly IRequestProvider $requests = new GlobalsRequestProvider(),
        private readonly IRouteResolver $resolver = new RouteResolver(),
        private readonly IRouteResponseFactory $responses = new RouteResponseFactory(),
        ?EventDispatcher $eventDispatcher = null,
    ) {
        $this->eventDispatcher = $eventDispatcher ?? new EventDispatcher();
    }

addEventDispatcher

Source line 35

public function addEventDispatcher(EventDispatcher $eventDispatcher): self

Parameters

Parameter Type, modifiers and default Purpose
$eventDispatcher EventDispatcher $eventDispatcher Dispatcher used for synchronous listener registration/dispatch and context propagation.

State writes: $this->eventDispatcher.

Return expressions (including local callbacks): $this.

Implementation, validation guards and interaction order
public function addEventDispatcher(EventDispatcher $eventDispatcher): self
    {
        $this->eventDispatcher = $eventDispatcher;
        foreach ($this->routesName as $route) {
            $route->addEventDispatcher($eventDispatcher);
        }
        return $this;
    }

addRouter

Source line 44

public function addRouter(IRoute $router): void

Parameters

Parameter Type, modifiers and default Purpose
$router IRoute $router Operation input; the exact use, validation and propagation are shown in the implementation trace.

State writes: $this->routesName[$router->getName()].

Implementation, validation guards and interaction order
public function addRouter(IRoute $router): void
    {
        $name = $router->getName();
        if ($name === null || trim($name) === '') {
            $index = count($this->routesName);
            while (isset($this->routesName[(string) $index])) {
                $index++;
            }
            $router->setName((string) $index);
        }
        $router->addEventDispatcher($this->eventDispatcher);
        $this->routesName[$router->getName()] = $router;
    }

loadRouters

Source line 58

public function loadRouters(array $routers): void

Parameters

Parameter Type, modifiers and default Purpose
$routers array $routers Operation input; the exact use, validation and propagation are shown in the implementation trace.

Delegates to addRouter().

Implementation, validation guards and interaction order
public function loadRouters(array $routers): void
    {
        foreach ($routers as $router) {
            $this->addRouter($router);
        }
    }

getRoutes

Source line 65

public function getRoutes(): array

Return expressions (including local callbacks): $this->routesName.

Implementation, validation guards and interaction order
public function getRoutes(): array { return $this->routesName; }

getRouters

Source line 66

public function getRouters(): array

Return expressions (including local callbacks): $this->getRoutes().

Implementation, validation guards and interaction order
public function getRouters(): array { return $this->getRoutes(); }

getRouteByName

Source line 68

public function getRouteByName(string $name): ?IRoute

Parameters

Parameter Type, modifiers and default Purpose
$name string $name Named key/label selected by this API; its exact scope is shown by the implementation trace.

Return expressions (including local callbacks): $this->routesName[$name] ?? null.

Implementation, validation guards and interaction order
public function getRouteByName(string $name): ?IRoute
    {
        return $this->routesName[$name] ?? null;
    }

getRouteByPath

Source line 73

public function getRouteByPath(string $path, string $method = 'GET'): ?IRoute

Parameters

Parameter Type, modifiers and default Purpose
$path string $path Filesystem or route path as defined by the owning component; no universal path normalization is implied.
$method string $method = 'GET' HTTP method token; validation/normalization follows the method policy.

Return expressions (including local callbacks): $route; null.

Implementation, validation guards and interaction order
public function getRouteByPath(string $path, string $method = 'GET'): ?IRoute
    {
        foreach ($this->routesName as $route) {
            if ($route->getPath() === $path && $route->getMethod() === strtoupper($method)) {
                return $route;
            }
        }
        return null;
    }

startRouter

Source line 83

public function startRouter(?IRequest $request = null): View|Redirect|Response

Parameters

Parameter Type, modifiers and default Purpose
$request ?IRequest $request = null Request snapshot consumed by matching, method and security checks.

Return expressions (including local callbacks): $match->route->route($match->params, $request); $match->allowed !== [] ? $this->responses->methodNotAllowed($match->allowed) : $this->responses->notFound().

Implementation, validation guards and interaction order
public function startRouter(?IRequest $request = null): View|Redirect|Response
    {
        $request ??= $this->requests->current();
        $match = $this->resolver->resolve($this->routesName, $request);
        if ($match->route !== null) {
            return $match->route->route($match->params, $request);
        }
        return $match->allowed !== []
            ? $this->responses->methodNotAllowed($match->allowed)
            : $this->responses->notFound();
    }

Related types

Clone this wiki locally