refactor(config): remove runtime config files and centralize route/bootstrap
This commit is contained in:
56
lib/Http/RouteRegistrar.php
Normal file
56
lib/Http/RouteRegistrar.php
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user