*/ private array $all = []; /** * @param list> $available * @param array $switches */ public function __construct(array $available, private readonly array $switches) { foreach ($available as $class) { $module = new $class; $this->all[$module->key()] = $module; } } /** @return array every known module */ public function all(): array { return $this->all; } public function has(string $key): bool { return isset($this->all[$key]); } public function enabled(string $key): bool { if (! $this->has($key)) { return false; } return (bool) ($this->switches[$key] ?? $this->all[$key]->enabledByDefault()); } /** @return array the modules that are on */ public function active(): array { return array_filter($this->all, fn (Module $module) => $this->enabled($module->key())); } /** * A module that is on while one it needs is off is a configuration mistake: fail loudly at boot. */ public function validate(): void { foreach ($this->active() as $module) { foreach ($module->requires() as $needed) { if (! $this->enabled($needed)) { throw new ModuleException("Manager module '{$module->key()}' needs module '{$needed}', which is off."); } } } } /** @return array permission value => permission, of the active modules only */ public function permissions(): array { $permissions = []; foreach ($this->active() as $module) { foreach ($module->permissions() as $permission) { $permissions[$permission->value] = $permission; } } return $permissions; } /** @return list */ public function permissionValues(): array { return array_keys($this->permissions()); } /** * The given values without what is no permission (any more), completed with what they * need, in the order of the permissions. * * @param array $values * @return list */ public function complete(array $values): array { $known = $this->permissions(); $wanted = []; foreach ($values as $value) { if (is_string($value) && isset($known[$value])) { $wanted[$value] = true; foreach ($known[$value]->needs as $needed) { $wanted[$needed] = true; } } } return array_values(array_filter(array_keys($known), fn (string $value) => isset($wanted[$value]))); } /** * The list for the group form, per section. * * @return list}>}> */ public function catalogue(): array { $sections = []; foreach ($this->permissions() as $permission) { $sections[$permission->section][] = ['value' => $permission->value, 'label' => $permission->label, 'needs' => $permission->needs]; } $catalogue = []; foreach ($sections as $section => $permissions) { $catalogue[] = ['section' => $section, 'permissions' => $permissions]; } return $catalogue; } /** @return list */ public function navigation(): array { $items = []; foreach ($this->active() as $module) { array_push($items, ...$module->navigation()); } return $items; } /** * Audit-log names of what is done: the modules' own plus the core's. * * @return array}> */ public function actions(): array { $actions = []; foreach ($this->active() as $module) { $actions = [...$actions, ...$module->actions()]; } return $actions; } /** @return array */ public function views(): array { $views = ['dashboard' => 'Opened the dashboard']; foreach ($this->active() as $module) { $views = [...$views, ...$module->views()]; } return $views; } /** @return list middleware of the active modules for `before` or `after` the site's own */ public function middleware(string $position): array { $middleware = []; foreach ($this->active() as $module) { array_push($middleware, ...($module->middleware()[$position] ?? [])); } return $middleware; } }