- Add DISCOVERY_LOG_LEVEL=debug - Add DISCOVERY_SHOW_PROGRESS=true - Temporary changes for debugging InitializerProcessor fixes on production
130 lines
3.4 KiB
PHP
130 lines
3.4 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Framework\Router;
|
|
|
|
use App\Framework\Core\Route;
|
|
use App\Framework\Http\Method;
|
|
|
|
final readonly class CompiledRoutes
|
|
{
|
|
/**
|
|
* @param array<string, array<string, array<string, Route>>> $staticRoutes - [method][subdomain][path] => Route
|
|
* @param array<string, array<string, CompiledPattern|null>> $dynamicPatterns - [method][subdomain] => CompiledPattern
|
|
*/
|
|
public function __construct(
|
|
private array $staticRoutes,
|
|
private array $dynamicPatterns,
|
|
private array $namedRoutes,
|
|
) {
|
|
}
|
|
|
|
public function getStaticRoute(Method $method, string $path, string $subdomainKey = 'default'): ?Route
|
|
{
|
|
return $this->staticRoutes[$method->value][$subdomainKey][$path] ?? null;
|
|
}
|
|
|
|
public function getCompiledPattern(Method $method, string $subdomainKey = 'default'): ?CompiledPattern
|
|
{
|
|
return $this->dynamicPatterns[$method->value][$subdomainKey] ?? null;
|
|
}
|
|
|
|
/**
|
|
* Get static routes for a specific subdomain
|
|
*/
|
|
public function getStaticRoutesForSubdomain(Method $method, string $subdomainKey): array
|
|
{
|
|
return $this->staticRoutes[$method->value][$subdomainKey] ?? [];
|
|
}
|
|
|
|
/**
|
|
* Get all subdomain keys for a method
|
|
*/
|
|
public function getSubdomainKeys(Method $method): array
|
|
{
|
|
return array_keys($this->staticRoutes[$method->value] ?? []);
|
|
}
|
|
|
|
public function getNamedRoute(string $name): ?Route
|
|
{
|
|
return $this->namedRoutes[$name] ?? null;
|
|
}
|
|
|
|
public function hasNamedRoute(string $name): bool
|
|
{
|
|
return isset($this->namedRoutes[$name]);
|
|
}
|
|
|
|
public function getAllNamedRoutes(): array
|
|
{
|
|
return $this->namedRoutes;
|
|
}
|
|
|
|
/**
|
|
* Generator version for memory-efficient iteration over named routes
|
|
*
|
|
* @return \Generator<string, Route>
|
|
*/
|
|
public function getAllNamedRoutesGenerator(): \Generator
|
|
{
|
|
foreach ($this->namedRoutes as $name => $route) {
|
|
yield $name => $route;
|
|
}
|
|
}
|
|
|
|
public function getStaticRoutes(): array
|
|
{
|
|
return $this->staticRoutes;
|
|
}
|
|
|
|
public function generateUrl(string $name, array $params = []): ?string
|
|
{
|
|
$route = $this->getNamedRoute($name);
|
|
if (! $route) {
|
|
return null;
|
|
}
|
|
|
|
return $this->buildUrlFromRoute($route, $params);
|
|
}
|
|
|
|
public function getStats(): array
|
|
{
|
|
$staticCount = 0;
|
|
$dynamicCount = 0;
|
|
$subdomainCount = 0;
|
|
|
|
foreach ($this->staticRoutes as $methodRoutes) {
|
|
foreach ($methodRoutes as $subdomainRoutes) {
|
|
$staticCount += count($subdomainRoutes);
|
|
$subdomainCount++;
|
|
}
|
|
}
|
|
|
|
foreach ($this->dynamicPatterns as $methodPatterns) {
|
|
$dynamicCount += count(array_filter($methodPatterns));
|
|
}
|
|
|
|
$namedCount = count($this->namedRoutes);
|
|
|
|
return [
|
|
'static_routes' => $staticCount,
|
|
'dynamic_patterns' => $dynamicCount,
|
|
'named_routes' => $namedCount,
|
|
'subdomain_groups' => $subdomainCount,
|
|
'total_methods' => count($this->staticRoutes),
|
|
];
|
|
}
|
|
|
|
private function buildUrlFromRoute(Route $route, array $params): string
|
|
{
|
|
$path = $route->path;
|
|
|
|
foreach ($params as $key => $value) {
|
|
$path = str_replace("{{$key}}", (string)$value, $path);
|
|
}
|
|
|
|
return $path;
|
|
}
|
|
}
|