refactor(config): remove runtime config files and centralize route/bootstrap

This commit is contained in:
2026-04-01 20:27:42 +02:00
parent dba589b495
commit 5699bc6c5b
20 changed files with 487 additions and 118 deletions

View File

@@ -22,9 +22,18 @@ class AccessControl
private array $configuredPublicPaths;
private IntendedUrlService $intendedUrlService;
public function __construct(?array $publicPaths = null, ?IntendedUrlService $intendedUrlService = null)
public function __construct(array $publicPaths = [], ?IntendedUrlService $intendedUrlService = null)
{
$this->configuredPublicPaths = $publicPaths ?? (defined('APP_PUBLIC_PATHS') ? APP_PUBLIC_PATHS : []);
$normalizedPaths = [];
foreach ($publicPaths as $path) {
$rawPath = trim((string) $path);
if ($rawPath === '') {
continue;
}
$normalizedPaths[] = $rawPath === '/' ? '/' : trim($rawPath, '/');
}
$this->configuredPublicPaths = array_values(array_unique($normalizedPaths));
$this->intendedUrlService = $intendedUrlService ?? new IntendedUrlService();
}

100
lib/Http/RouteCatalog.php Normal file
View File

@@ -0,0 +1,100 @@
<?php
namespace MintyPHP\Http;
use RuntimeException;
/**
* Loads and validates core route declarations from config/routes.php.
*/
final class RouteCatalog
{
/** @var list<array{path: string, target: string, public: bool}> */
private array $coreRoutes;
/** @var list<string> */
private array $corePublicPaths;
/**
* @param list<array{path: string, target: string, public: bool}> $coreRoutes
* @param list<string> $corePublicPaths
*/
private function __construct(array $coreRoutes, array $corePublicPaths)
{
$this->coreRoutes = $coreRoutes;
$this->corePublicPaths = $corePublicPaths;
}
public static function fromConfigFile(string $routesFile): self
{
if (!is_file($routesFile)) {
throw new RuntimeException("Core route config not found: {$routesFile}");
}
$loaded = include $routesFile;
if (!is_array($loaded)) {
throw new RuntimeException("Core route config '{$routesFile}' must return an array.");
}
return self::fromArray($loaded, $routesFile);
}
/**
* @param array<mixed> $routes
*/
public static function fromArray(array $routes, string $source = 'config/routes.php'): self
{
$normalized = [];
$publicPaths = [];
$seenPaths = [];
foreach (array_values($routes) as $index => $route) {
if (!is_array($route)) {
throw new RuntimeException("Invalid route definition at {$source}[{$index}]: expected array.");
}
$path = trim((string) ($route['path'] ?? ''));
$target = trim((string) ($route['target'] ?? ''));
if ($path === '' || $target === '') {
throw new RuntimeException("Invalid route definition at {$source}[{$index}]: non-empty path and target are required.");
}
if (isset($seenPaths[$path])) {
throw new RuntimeException("Core route path conflict: '{$path}' is defined multiple times in {$source}.");
}
$seenPaths[$path] = true;
$isPublic = (bool) ($route['public'] ?? false);
$normalized[] = [
'path' => $path,
'target' => $target,
'public' => $isPublic,
];
if ($isPublic) {
$publicPaths[] = $path;
}
}
$publicPaths = array_values(array_unique($publicPaths));
sort($publicPaths);
return new self($normalized, $publicPaths);
}
/**
* @return list<array{path: string, target: string, public: bool}>
*/
public function coreRoutes(): array
{
return $this->coreRoutes;
}
/**
* @return list<string>
*/
public function corePublicPaths(): array
{
return $this->corePublicPaths;
}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace MintyPHP\Http;
use MintyPHP\App\Module\ModuleRegistry;
use MintyPHP\Router;
use RuntimeException;
/**
* Registers core + module routes into the runtime router.
*/
final class RouteRegistrar
{
public function __construct(private readonly ModuleRegistry $moduleRegistry)
{
}
public function register(RouteCatalog $catalog): void
{
$coreRoutePaths = [];
foreach ($catalog->coreRoutes() as $route) {
$path = trim((string) ($route['path'] ?? ''));
$target = trim((string) ($route['target'] ?? ''));
if ($path === '' || $target === '') {
continue;
}
$coreRoutePaths[$path] = $target;
Router::addRoute($path, $target);
}
$modulePaths = [];
foreach ($this->moduleRegistry->getRoutes() as $route) {
$path = trim((string) ($route['path'] ?? ''));
$target = trim((string) ($route['target'] ?? ''));
$moduleId = trim((string) ($route['module_id'] ?? ''));
if ($path === '' || $target === '') {
continue;
}
if (isset($coreRoutePaths[$path])) {
throw new RuntimeException(
"Module route path conflict: '{$path}' from module '{$moduleId}' collides with core route target '{$coreRoutePaths[$path]}'."
);
}
if (isset($modulePaths[$path])) {
throw new RuntimeException(
"Module route path conflict: '{$path}' from module '{$moduleId}' collides with module '{$modulePaths[$path]}'."
);
}
$modulePaths[$path] = $moduleId;
Router::addRoute($path, $target);
}
}
}