PR CI / flow (pull_request) Successful in 4s
PR CI / ledger (pull_request) Successful in 4s
PR CI / test (stable) (pull_request) Successful in 35s
PR CI / test (lowest) (pull_request) Successful in 40s
PR CI / security (pull_request) Successful in 51s
PR CI / pr-ci (pull_request) Successful in 1s
PR CI / release-automerge (pull_request) Successful in 4s
186 lines
5.0 KiB
PHP
186 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace IriksIT\Manager\Support;
|
|
|
|
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;
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
/** @return list<array{label: string, route: string, permission: string}> */
|
|
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<string, array{0: string, 1?: list<string>}>
|
|
*/
|
|
public function actions(): array
|
|
{
|
|
$actions = [];
|
|
|
|
foreach ($this->active() as $module) {
|
|
$actions = [...$actions, ...$module->actions()];
|
|
}
|
|
|
|
return $actions;
|
|
}
|
|
|
|
/** @return array<string, string> */
|
|
public function views(): array
|
|
{
|
|
$views = ['dashboard' => 'Opened the dashboard'];
|
|
|
|
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;
|
|
}
|
|
}
|