*/ public function installed(): Collection { return $this->extensions->discovered()->filter(fn (ExtensionManifest $manifest): bool => $manifest->isTheme()); } public function active(): string { return $this->active ??= rescue(fn (): ?string => Setting::fetch(self::SETTING), report: false) ?: self::CLASSIC; } public function setActive(string $id): void { if ($id === self::CLASSIC) { Setting::query()->where('key', self::SETTING)->delete(); } else { Setting::put(self::SETTING, $id); } $this->active = $id; } /** * The enabled themes a theme is built from, parent first and without classic. A missing, * disabled or circular parent makes the theme unusable. * * @return list * * @throws InvalidExtensionException */ public function chain(string $id): array { $chain = []; $child = null; throw_if($id !== self::CLASSIC && ! config('extensions.enabled'), InvalidExtensionException::class, 'Extensions are turned off on this panel.'); while ($id !== self::CLASSIC) { throw_if(isset($chain[$id]), InvalidExtensionException::class, sprintf('Theme "%s" extends itself through "%s".', $id, $child)); $manifest = $this->extensions->discovered()->get($id); throw_unless($manifest?->isTheme(), InvalidExtensionException::class, $child === null ? sprintf('"%s" is not an installed theme.', $id) : sprintf('Theme "%s" extends "%s", which is not an installed theme.', $child, $id)); throw_unless($this->extensions->enabled()->has($id), InvalidExtensionException::class, sprintf('Theme "%s" is not enabled or failed to load.', $id)); $chain[$id] = $manifest; $child = $id; $id = $manifest->theme->extends; } return array_reverse(array_values($chain)); } /** @throws InvalidExtensionException while the theme is the active one */ public function assertNotActive(string $id): void { throw_if($id === $this->active(), InvalidExtensionException::class, sprintf('Theme "%s" is active; activate another theme or reset to classic first.', $id)); } /** Why the theme cannot be activated, or null when it can. */ public function activationProblem(string $id): ?string { try { $this->chain($id); } catch (InvalidExtensionException $invalidExtensionException) { return $invalidExtensionException->getMessage(); } return null; } /** * The schemes of the nearest theme in the chain that declares them, else classic's. * * @param list $chain * @return non-empty-list<'dark'|'light'> */ public function schemes(array $chain): array { foreach (array_reverse($chain) as $manifest) { if ($manifest->theme?->schemes !== null && $manifest->theme->schemes !== []) { return $manifest->theme->schemes; } } return self::CLASSIC_SCHEMES; } /** Why the theme's stylesheet would be laid over the panel's instead of replacing it, or null. */ public function coreMismatch(ExtensionManifest $theme): ?string { if ($theme->theme?->stylesheet === null) { return null; } $built = ThemeBuild::of($theme)->core; $panel = $this->assets->coreClassesHash(); return $built !== null && $built === $panel ? null : sprintf( 'Theme "%s" was built against core classes "%s", but this panel has "%s". Its stylesheet loads after the panel\'s until it is rebuilt with this panel\'s SDK.', $theme->id, $built ?? 'unknown', $panel ?? 'unknown', ); } public function settings(ExtensionManifest $theme): ExtensionSettingsDefinition { return ($theme->theme ?? new ThemeDefinition)->settingsDefinition($this->extensions->settings($theme->id)); } }