Files
laravel-manager-packagist/src/Modules/IriksIT/IriksItModule.php
T
2026-12-06 12:23:34 +00:00

190 lines
6.6 KiB
PHP

<?php
namespace IriksIT\Manager\Modules\IriksIT;
use IriksIT\Manager\Modules\BaseModule;
use IriksIT\Manager\Modules\Costs\CostsModule;
use IriksIT\Manager\Modules\Numbers\NumbersModule;
use IriksIT\Manager\Modules\Support\SupportModule;
use IriksIT\Manager\Modules\Updates\UpdatesModule;
use IriksIT\Manager\Support\IriksStorage\Client;
use IriksIT\Manager\Support\ModuleRegistry;
/**
* Iriks-IT (ADR-0017): what a customer of a project that Iriks-IT builds and hosts gets to see, for clarity and transparency. One module with
* four parts (what it cost, the numbers in plain words, the updates per version, and help), and one option: storage at Iriks-IT.
*
* It has one switch. When it is off, the heading "Iriks-IT" is gone from the menu, and so are its routes, rights and screens; its tables stay
* (ADR-0004). The parts are the modules that were separate before v0.10.0 (`CostsModule`, `NumbersModule`, `UpdatesModule`, `SupportModule`); they
* are not modules of their own any more, and a site that still lists them gets this module instead (see `ModuleRegistry`).
*
* Which parts are on: a part is on when `manager.iriksit.parts.<part>` says so, else when the old switch `manager.modules.<part>` says so. When
* nothing says anything about any part, every part is on (help shows only where an API account is filled in), so switching the module on shows the lot.
*/
class IriksItModule extends BaseModule
{
/** The parts, by name: the class that knows its screens. */
public const PARTS = [
'costs' => CostsModule::class,
'numbers' => NumbersModule::class,
'updates' => UpdatesModule::class,
'support' => SupportModule::class,
];
/** @var array<string, BaseModule>|null */
private ?array $built = null;
public function key(): string
{
return 'iriksit';
}
public function label(): string
{
return 'Iriks-IT';
}
/** On by default for a site that already used one of the parts, or that has an API account (it can then ask for help). */
public function enabledByDefault(): bool
{
foreach (array_keys(self::PARTS) as $part) {
if (self::said($part) === true) {
return true;
}
}
return self::accountFilledIn();
}
/** Whether the IriksIT API account of this site is filled in. */
public static function accountFilledIn(): bool
{
return (new Client((array) config('manager.media.iriksit', [])))->isConfigured();
}
/** What the site says about one part: true, false, or null when it says nothing. */
public static function said(string $part): ?bool
{
$value = config('manager.iriksit.parts.'.$part) ?? config('manager.modules.'.$part);
return $value === null ? null : filter_var($value, FILTER_VALIDATE_BOOLEAN);
}
/** Whether a part is on, when the module itself is. */
public static function partOn(string $part): bool
{
if (! isset(self::PARTS[$part])) {
return false;
}
$said = self::said($part);
if ($said !== null) {
return $said;
}
// Help was on unless a site said no; it shows only where an account is filled in.
if ($part === 'support') {
return true;
}
// Nothing is said about any part: all of them are on. Something is said about another one: this one is left out, as it always was.
return array_filter(array_keys(self::PARTS), fn (string $other) => self::said($other) !== null) === [];
}
/**
* The parts that are on. The numbers need the statistics (they are its plain-words view), so without them that part is left out.
*
* @return array<string, BaseModule>
*/
public function parts(): array
{
if ($this->built === null) {
$this->built = [];
foreach (self::PARTS as $name => $class) {
$on = self::partOn($name) && ($name !== 'numbers' || app(ModuleRegistry::class)->enabled('statistics'));
$on && $this->built[$name] = new $class;
}
}
return $this->built;
}
public function tables(): array
{
return $this->merge('tables');
}
public function permissions(): array
{
return $this->merge('permissions');
}
public function navigation(): array
{
return $this->merge('navigation');
}
public function commands(): array
{
return $this->merge('commands');
}
public function tutorial(): array
{
return $this->merge('tutorial');
}
public function actions(): array
{
return $this->mergeKeyed('actions');
}
public function views(): array
{
return $this->mergeKeyed('views');
}
public function boot(): void
{
foreach ($this->parts() as $part) {
$part->boot();
}
}
/**
* What a site may set about this module on the settings screen: which parts are on, and the account at the IriksIT API that serves the help
* and the storage. Only there while the module is on.
*
* @return array<string, array<string, mixed>>
*/
public function settings(): array
{
$parts = [];
foreach (['costs' => 'What it cost', 'numbers' => 'The numbers in plain words', 'updates' => 'Updates per version', 'support' => 'Help'] as $part => $label) {
$parts['manager.iriksit.parts.'.$part] = ['label' => $label, 'type' => 'toggle', 'group' => 'Iriks-IT', 'env' => 'MANAGER_'.strtoupper($part)];
}
return [
...$parts,
'manager.media.iriksit.url' => ['label' => 'IriksIT API: address', 'type' => 'url', 'group' => 'IriksIT API', 'sensitive' => true, 'help' => 'With its version, for example https://api.iriks-it.nl/v1', 'env' => 'MANAGER_IRIKSIT_URL'],
'manager.media.iriksit.email' => ['label' => 'IriksIT API: account', 'type' => 'text', 'group' => 'IriksIT API', 'sensitive' => true, 'env' => 'MANAGER_IRIKSIT_EMAIL'],
'manager.media.iriksit.password' => ['label' => 'IriksIT API: password', 'type' => 'secret', 'group' => 'IriksIT API', 'env' => 'MANAGER_IRIKSIT_PASSWORD'],
];
}
/** @return list<mixed> */
private function merge(string $method): array
{
return array_values(array_merge(...array_map(fn (BaseModule $part) => $part->{$method}(), array_values($this->parts())) ?: [[]]));
}
/** @return array<string, mixed> */
private function mergeKeyed(string $method): array
{
return array_merge(...array_map(fn (BaseModule $part) => $part->{$method}(), array_values($this->parts())) ?: [[]]);
}
}