|null */ private ?array $disabledPaths = null; public function __construct(private readonly Router $router) {} public function enabled(string $module): bool { return (bool) config('modules.enabled.'.$module, false); } /** @return array */ public function states(): array { return array_map(fn ($value) => (bool) $value, (array) config('modules.enabled', [])); } /** O módulo a que a rota pertence, ou null quando é do núcleo. */ public function moduleFor(Route $route): ?string { $controller = ltrim((string) $route->getControllerClass(), '\\'); $uri = trim($route->uri(), '/'); foreach ((array) config('modules.controllers', []) as $module => $controllers) { if ($controller !== '' && in_array($controller, (array) $controllers, true)) { return (string) $module; } } foreach ((array) config('modules.paths', []) as $module => $prefixes) { foreach ((array) $prefixes as $prefix) { if ($uri === $prefix || str_starts_with($uri, $prefix.'/')) { return (string) $module; } } } return null; } public function allows(Route $route): bool { $module = $this->moduleFor($route); return $module === null || $this->enabled($module); } /** * Caminhos (parte fixa da URL) servidos só por módulos desligados, para esconder entradas de menu. * * @return list */ public function disabledPaths(): array { if ($this->disabledPaths !== null) { return $this->disabledPaths; } $disabled = []; $enabled = []; foreach ($this->router->getRoutes()->getRoutes() as $route) { if (! in_array('GET', $route->methods(), true)) { continue; } $path = '/'.trim(preg_replace('/\/?\{.*$/', '', $route->uri()) ?? '', '/'); if ($this->allows($route)) { $enabled[$path] = true; } else { $disabled[$path] = true; } } return $this->disabledPaths = array_values(array_diff(array_keys($disabled), array_keys($enabled))); } }