Files
laravel-manager/src/Support/ModuleRegistry.php
T
Matthijs Iriks 3345797141
PR CI / ledger (pull_request) Successful in 4s
PR CI / flow (pull_request) Successful in 4s
PR CI / ui (pull_request) Successful in 16s
PR CI / test (lowest) (pull_request) Successful in 44s
PR CI / test (stable) (pull_request) Successful in 49s
PR CI / security (pull_request) Successful in 50s
PR CI / pr-ci (pull_request) Successful in 2s
PR CI / release-automerge (pull_request) Successful in 4s
Add resources defined in config and a bundle of the site's own screens
2026-12-04 05:26:37 +01:00

225 lines
7.1 KiB
PHP

<?php
namespace IriksIT\Manager\Support;
use Illuminate\Support\Str;
use IriksIT\Manager\Contracts\Module;
use IriksIT\Manager\Exceptions\ModuleException;
/**
* Knows which modules exist and which of them are on for this site.
*/
final class ModuleRegistry
{
/** @var array<string, Module> */
private array $all = [];
/**
* @param list<class-string<Module>> $available
* @param array<string, bool> $switches
*/
public function __construct(array $available, private readonly array $switches)
{
foreach ($available as $class) {
$module = new $class;
$this->all[$module->key()] = $module;
}
}
/** @return array<string, Module> 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<string, Module> 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<string, Permission> 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<string> */
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<array-key, mixed> $values
* @return list<string>
*/
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<array{section: string, permissions: list<array{value: string, label: string, needs: list<string>}>}>
*/
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<array{label: string, route?: string, url?: string, permission: ?string, section?: string, icon?: string, order: int, simple: bool, new_tab?: bool}>
*/
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<string, array{0: string, 1?: list<string>}>
*/
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<string, string> */
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<string> 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;
}
}