v0.9.6
This commit is contained in:
commit
1396974b03
286 files changed
+19411
No files matched your search
@@ -0,0 +1,205 @@
|
||||
<?php
|
||||
|
||||
namespace IriksIT\Manager\Support\Settings;
|
||||
|
||||
use IriksIT\Manager\Support\Features;
|
||||
use IriksIT\Manager\Support\ModuleRegistry;
|
||||
|
||||
/**
|
||||
* What can be set from the settings screen. A definition is keyed by the config path it overrides (`manager.modules.reviews`)
|
||||
* and has: `label`, `type` (toggle, text, url, number, select, secret), `group`, optional `help`, `options` (select: value => label),
|
||||
* `env` (the .env variable that is written along when the site allows it) and `permission` (a right on top of `settings.manage`).
|
||||
* A secret is never sent back to a browser.
|
||||
*
|
||||
* They come from three places: the parts of the manager, the integrations the package knows (Turnstile, the IriksIT API) and
|
||||
* the site itself (`manager.settings.definitions`, and `settings()` of a module of its own). Nothing else can be set: a key that
|
||||
* is not defined is refused, so the screen can never write an arbitrary setting or .env line.
|
||||
*/
|
||||
class Definitions
|
||||
{
|
||||
public const TYPES = ['toggle', 'text', 'url', 'number', 'select', 'secret'];
|
||||
|
||||
public function __construct(private readonly ModuleRegistry $modules) {}
|
||||
|
||||
/** @return array<string, array<string, mixed>> keyed by config path */
|
||||
public function all(): array
|
||||
{
|
||||
$all = [];
|
||||
|
||||
foreach ([...$this->parts(), ...$this->features(), ...$this->frontend(), ...$this->integrations(), ...$this->fromModules(), ...(array) config('manager.settings.definitions', [])] as $key => $definition) {
|
||||
$key = is_string($key) ? $key : (string) ($definition['config'] ?? '');
|
||||
$definition = $this->normalise($key, (array) $definition);
|
||||
|
||||
if ($definition !== null) {
|
||||
$all[$key] = $definition;
|
||||
}
|
||||
}
|
||||
|
||||
return $all;
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns what was typed into the value of the setting, or says it is no good.
|
||||
*
|
||||
* @param array<string, mixed> $definition
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public function cast(array $definition, mixed $raw): mixed
|
||||
{
|
||||
$bad = fn () => throw new \InvalidArgumentException('This value is not valid.');
|
||||
|
||||
return match ($definition['type']) {
|
||||
'toggle' => is_bool($raw) || in_array($raw, [0, 1, '0', '1', 'true', 'false'], true) ? filter_var($raw, FILTER_VALIDATE_BOOLEAN) : $bad(),
|
||||
'number' => is_numeric($raw) ? $raw + 0 : $bad(),
|
||||
'select' => is_string($raw) && array_key_exists($raw, $definition['options']) ? $raw : $bad(),
|
||||
'url' => is_string($raw) && mb_strlen($raw) <= 500 && preg_match('#^https?://[^\s<>"\']+$#i', $raw) === 1 ? $raw : $bad(),
|
||||
default => is_string($raw) && mb_strlen($raw) <= 1000 && preg_match('/[\r\n\0]/', $raw) !== 1 ? trim($raw) : $bad(),
|
||||
};
|
||||
}
|
||||
|
||||
/** @return array<string, mixed>|null */
|
||||
public function find(string $key): ?array
|
||||
{
|
||||
return $this->all()[$key] ?? null;
|
||||
}
|
||||
|
||||
/** @return array<string, array<string, mixed>> */
|
||||
private function parts(): array
|
||||
{
|
||||
$parts = [];
|
||||
|
||||
foreach ($this->modules->all() as $module) {
|
||||
// Users and media are the core: without them there is no manager.
|
||||
if (in_array($module->key(), ['users', 'media', 'settings'], true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// A feature of the site with the name of the module is its switch: it has its own toggle.
|
||||
if (isset(Features::all()[$module->key()])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$parts['manager.modules.'.$module->key()] = [
|
||||
'label' => $module->label(),
|
||||
'type' => 'toggle',
|
||||
'group' => 'Parts of the manager',
|
||||
'env' => 'MANAGER_'.strtoupper($module->key()),
|
||||
];
|
||||
}
|
||||
|
||||
return $parts;
|
||||
}
|
||||
|
||||
/** @return array<string, array<string, mixed>> */
|
||||
private function features(): array
|
||||
{
|
||||
$features = [];
|
||||
|
||||
foreach (Features::all() as $key => $feature) {
|
||||
$features['manager.features.'.$key.'.enabled'] = [
|
||||
'label' => $feature['label'],
|
||||
'type' => 'toggle',
|
||||
'group' => 'Features of the site',
|
||||
'help' => $feature['hint'],
|
||||
'env' => $feature['env'],
|
||||
];
|
||||
}
|
||||
|
||||
return $features;
|
||||
}
|
||||
|
||||
/**
|
||||
* Who draws the site, in words an owner can follow. Only there when the pages module is on: without pages there is nothing to draw.
|
||||
*
|
||||
* @return array<string, array<string, mixed>>
|
||||
*/
|
||||
private function frontend(): array
|
||||
{
|
||||
if (array_filter($this->modules->active(), fn ($module) => $module->key() === 'pages') === []) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return [
|
||||
'manager.frontend.mode' => [
|
||||
'label' => 'Who shows the website',
|
||||
'type' => 'select',
|
||||
'group' => 'The website',
|
||||
'help' => 'The website itself (it has its own code, the usual choice), this manager (it draws every page made of blocks at its address, nothing else is needed), or another website that reads the pages from here (headless).',
|
||||
'options' => ['site' => 'The website itself', 'package' => 'This manager', 'headless' => 'Another website reads it (headless)'],
|
||||
'env' => 'MANAGER_FRONTEND',
|
||||
],
|
||||
'manager.blocks.preview.enabled' => [
|
||||
'label' => 'Show the real page next to the editor',
|
||||
'type' => 'toggle',
|
||||
'group' => 'The website',
|
||||
'help' => 'While you build a page you see how it looks on a phone, tablet and computer, before you save.',
|
||||
'env' => 'MANAGER_BLOCKS_PREVIEW',
|
||||
],
|
||||
'manager.blocks.api.enabled' => [
|
||||
'label' => 'Let another website read the pages',
|
||||
'type' => 'toggle',
|
||||
'group' => 'The website',
|
||||
'help' => 'The pages go out as data (JSON) for a website or app elsewhere. Always on when the website is "headless".',
|
||||
'env' => 'MANAGER_BLOCKS_API',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string, array<string, mixed>> */
|
||||
private function integrations(): array
|
||||
{
|
||||
$disks = array_keys((array) config('filesystems.disks', []));
|
||||
|
||||
return [
|
||||
'manager.media.disk' => ['label' => 'Disk for new media files', 'type' => 'select', 'group' => 'Media', 'sensitive' => true, 'options' => array_combine($disks, $disks), 'env' => 'MANAGER_MEDIA_DISK'],
|
||||
'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'],
|
||||
'manager.login.turnstile.site_key' => ['label' => 'Turnstile: site key', 'type' => 'text', 'group' => 'Login', 'sensitive' => true, 'env' => 'MANAGER_TURNSTILE_SITE_KEY'],
|
||||
'manager.login.turnstile.secret' => ['label' => 'Turnstile: secret key', 'type' => 'secret', 'group' => 'Login', 'env' => 'MANAGER_TURNSTILE_SECRET'],
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string, array<string, mixed>> */
|
||||
private function fromModules(): array
|
||||
{
|
||||
$found = [];
|
||||
|
||||
foreach ($this->modules->active() as $module) {
|
||||
if (method_exists($module, 'settings')) {
|
||||
foreach ((array) $module->settings() as $key => $definition) {
|
||||
$found[$key] = $definition;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $found;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed>|null null when the definition is no good: it is left out, never half used */
|
||||
private function normalise(string $key, array $definition): ?array
|
||||
{
|
||||
$type = $definition['type'] ?? 'text';
|
||||
|
||||
if (preg_match('/^[a-z][a-z0-9_]*(\.[a-zA-Z0-9_-]+)+$/', $key) !== 1 || ! in_array($type, self::TYPES, true) || ! is_string($definition['label'] ?? null)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$env = $definition['env'] ?? null;
|
||||
|
||||
return [
|
||||
'key' => $key,
|
||||
'config' => $key,
|
||||
'label' => $definition['label'],
|
||||
'type' => $type,
|
||||
'group' => is_string($definition['group'] ?? null) ? $definition['group'] : 'Site',
|
||||
'help' => is_string($definition['help'] ?? null) ? $definition['help'] : null,
|
||||
'options' => $type === 'select' ? (array) ($definition['options'] ?? []) : [],
|
||||
'env' => is_string($env) && preg_match('/^[A-Z][A-Z0-9_]*$/', $env) === 1 ? $env : null,
|
||||
'permission' => is_string($definition['permission'] ?? null) ? $definition['permission'] : null,
|
||||
// What can send a password or a file somewhere else (an address, an account, a disk) needs the right for secrets too, and the password of whoever saves it.
|
||||
'sensitive' => $type === 'secret' || (bool) ($definition['sensitive'] ?? false),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user