*/ 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; } } if (config('manager.simple_view')) { $permissions['manager.full'] = new Permission('manager.full', 'See the whole manager, not only the simple screens', 'Manager'); $permissions['manager.switch'] = new Permission('manager.switch', 'Switch between the simple and the whole manager', 'Manager'); } 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; } /** * The menu: what the modules give, changed by config `manager.menu`, in order. An item has `label`, * `route` (or `url` for a link of the site's own) and `permission`, and may add `section` (a heading), * `icon`, `order` (lower comes first; default 100), `simple` (shown in the simple view) and `new_tab`. * * @return list */ public function navigation(): array { $changes = (array) config('manager.menu.items', []); $items = []; foreach ($this->active() as $module) { foreach ($module->navigation() as $item) { $short = $item['id'] ?? (isset($item['route']) ? Str::after($item['route'], Names::routePrefix()) : ''); $item = [...$item, ...(array) ($changes[$short] ?? [])]; if (($item['hidden'] ?? false) === true) { continue; } $items[] = $item; } } foreach ((array) config('manager.menu.links', []) as $link) { if (is_array($link) && isset($link['label'], $link['url'])) { $items[] = $link + ['permission' => null]; } } $items = array_map(fn (array $item) => [ ...$item, 'label' => __($item['label']), ...(isset($item['section']) ? ['section' => __($item['section'])] : []), 'order' => (int) ($item['order'] ?? 100), 'simple' => (bool) ($item['simple'] ?? false), ], $items); // PHP's sort is stable: items with the same order keep the order of the modules. usort($items, fn (array $a, array $b) => $a['order'] <=> $b['order']); return array_values($items); } /** * Audit-log names of what is done: the modules' own plus the core's. * * @return array}> */ public function actions(): array { $actions = ['profile.update' => ['Changed own account'], 'profile.view' => ['Switched between the simple and the whole manager']]; foreach ($this->active() as $module) { $actions = [...$actions, ...$module->actions()]; } return $actions; } /** @return array */ public function views(): array { $views = ['dashboard' => 'Opened the dashboard', 'profile.show' => 'Opened own account']; 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; } }