refactor(config): remove runtime config files and centralize route/bootstrap
This commit is contained in:
@@ -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
100
lib/Http/RouteCatalog.php
Normal 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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,12 +4,23 @@ namespace MintyPHP\Service\Settings;
|
||||
|
||||
class SettingCacheService
|
||||
{
|
||||
private const HOT_PATH_KEYS = [
|
||||
SettingKeys::APP_TITLE_KEY,
|
||||
SettingKeys::APP_LOCALE_KEY,
|
||||
SettingKeys::APP_THEME_KEY,
|
||||
SettingKeys::APP_THEME_USER_KEY,
|
||||
SettingKeys::APP_REGISTRATION_KEY,
|
||||
SettingKeys::APP_PRIMARY_COLOR_KEY,
|
||||
];
|
||||
|
||||
private ?array $settings = null;
|
||||
private ?string $cacheFilePath;
|
||||
private ?SettingsMetadataGateway $settingsMetadataGateway;
|
||||
|
||||
public function __construct(?string $cacheFilePath = null)
|
||||
public function __construct(?string $cacheFilePath = null, ?SettingsMetadataGateway $settingsMetadataGateway = null)
|
||||
{
|
||||
$this->cacheFilePath = $cacheFilePath;
|
||||
$this->settingsMetadataGateway = $settingsMetadataGateway;
|
||||
}
|
||||
|
||||
public function get(string $key): ?string
|
||||
@@ -31,7 +42,7 @@ class SettingCacheService
|
||||
|
||||
$file = $this->filePath();
|
||||
if (!is_file($file)) {
|
||||
$this->settings = [];
|
||||
$this->settings = $this->hydrateColdStartCache();
|
||||
return $this->settings;
|
||||
}
|
||||
|
||||
@@ -90,7 +101,35 @@ class SettingCacheService
|
||||
if (is_string($this->cacheFilePath) && $this->cacheFilePath !== '') {
|
||||
return $this->cacheFilePath;
|
||||
}
|
||||
return dirname(__DIR__, 3) . '/config/settings.php';
|
||||
|
||||
$storagePath = defined('APP_STORAGE_PATH')
|
||||
? (string) APP_STORAGE_PATH
|
||||
: dirname(__DIR__, 3) . '/storage';
|
||||
|
||||
return rtrim($storagePath, '/') . '/runtime/settings.php';
|
||||
}
|
||||
|
||||
private function hydrateColdStartCache(): array
|
||||
{
|
||||
if (!$this->settingsMetadataGateway instanceof SettingsMetadataGateway) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$hydrated = [];
|
||||
foreach (self::HOT_PATH_KEYS as $key) {
|
||||
$value = $this->settingsMetadataGateway->getValue($key);
|
||||
if ($value === null) {
|
||||
continue;
|
||||
}
|
||||
$hydrated[$key] = (string) $value;
|
||||
}
|
||||
|
||||
if ($this->write($hydrated)) {
|
||||
return $this->settings ?? $hydrated;
|
||||
}
|
||||
|
||||
$this->settings = $hydrated;
|
||||
return $hydrated;
|
||||
}
|
||||
|
||||
private function exportValue(mixed $value, int $depth = 0): string
|
||||
|
||||
@@ -102,7 +102,10 @@ class SettingServicesFactory
|
||||
|
||||
public function createSettingCacheService(): SettingCacheService
|
||||
{
|
||||
return $this->settingCacheService ??= new SettingCacheService();
|
||||
return $this->settingCacheService ??= new SettingCacheService(
|
||||
null,
|
||||
$this->createSettingsMetadataGateway()
|
||||
);
|
||||
}
|
||||
|
||||
public function createThemeConfigGateway(): ThemeConfigGateway
|
||||
|
||||
@@ -186,7 +186,7 @@ function appLogoUrlAbsolute(int $size = 128): string
|
||||
}
|
||||
|
||||
/**
|
||||
* Read one cached app setting from config/settings.php.
|
||||
* Read one cached app setting from storage/runtime/settings.php.
|
||||
*/
|
||||
function appSetting(string $key): ?string
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user