` says so, else when the old switch `manager.modules.` 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|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 */ 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> */ 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 */ private function merge(string $method): array { return array_values(array_merge(...array_map(fn (BaseModule $part) => $part->{$method}(), array_values($this->parts())) ?: [[]])); } /** @return array */ private function mergeKeyed(string $method): array { return array_merge(...array_map(fn (BaseModule $part) => $part->{$method}(), array_values($this->parts())) ?: [[]]); } }