This commit is contained in:
deploy-bot committed 2026-12-04 15:50:10 +00:00
commit 5b230c75aa
278 files changed
+18390

No files matched your search

+262
View File
@@ -0,0 +1,262 @@
<?php
namespace IriksIT\Manager\Support;
use Illuminate\Support\Str;
use IriksIT\Manager\Contracts\Module;
use IriksIT\Manager\Exceptions\ModuleException;
use IriksIT\Manager\Http\Controllers\MascotController;
/**
* 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;
}
// A feature of the site with the name of the module is its one switch (see `Features`).
$feature = Features::all()[$key] ?? null;
if ($feature !== null) {
return $feature['enabled'];
}
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['simple_label']) && $item['simple_label'] !== null ? ['simple_label' => __($item['simple_label'])] : []),
...(isset($item['section']) ? ['section' => __($item['section'])] : []),
...(isset($item['hint']) && $item['hint'] !== null ? ['hint' => __($item['hint'])] : []),
'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);
}
/**
* The steps the modules add to the tour, for an account with these permissions.
*
* @param list<string> $allowed
* @return list<array{title: string, text: string, image: ?string, href: ?string}>
*/
public function tutorial(array $allowed): array
{
$steps = [];
foreach ($this->active() as $module) {
foreach (method_exists($module, 'tutorial') ? $module->tutorial() : [] as $step) {
if (! isset($step['title'], $step['text']) || (isset($step['permission']) && ! in_array($step['permission'], $allowed, true))) {
continue;
}
$steps[] = [
'title' => __($step['title']),
'text' => __($step['text']),
'image' => isset($step['image']) && in_array($step['image'], MascotController::NAMES, true) ? $step['image'] : null,
'href' => isset($step['menu']) ? route(Names::route($step['menu']), [], false) : null,
];
}
}
return $steps;
}
/**
* 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'], 'profile.tutorial' => ['Finished or skipped the tour']];
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;
}
}