*/ private array $definitions; /** @param array $definitions */ public function __construct( private readonly ExtensionSettings $settings, array $definitions, ) { $this->definitions = []; foreach ($definitions as $definition) { $this->definitions[$definition->key] = $definition; } } public function forUser(User $user): self { return new self($this->settings->forUser($user), array_values($this->definitions)); } public function forServer(Server $server): self { return new self($this->settings->forServer($server), array_values($this->definitions)); } /** The id of the extension these settings belong to. */ public function extension(): string { return $this->settings->extension(); } /** * The setting's current value. A file setting reads as the public URL of * its uploaded file, or its default while nothing is uploaded. */ public function get(string $key): mixed { $definition = $this->definitions[$key] ?? null; if ($definition === null) { throw new InvalidArgumentException(sprintf('Unknown extension setting "%s".', $key)); } if ($definition->isFile()) { $name = $this->storedFile($definition); return $definition->normalize($name === null ? $definition->default : ExtensionSettingFiles::url($this->extension(), $name)); } return $definition->normalize($this->settings->get($definition->key, $definition->default)); } /** @return array */ public function publicSettings(): array { $values = []; foreach ($this->definitions as $definition) { $values[$definition->publicName()] = $definition->serializePublic($this->get($definition->key)); } return $values; } /** * Rules for a partial update: every input is optional and, when submitted, must pass * its definition's rules and those of its field type. File inputs are prohibited: * files are uploaded and cleared through replaceFile(). * * @return NormalizedValidationRules */ public function validationRules(): array { $rules = []; foreach ($this->definitions as $definition) { $rules = [...$rules, ...$definition->validationRules()]; } return $rules; } /** @param array $input */ public function update(array $input): void { $input = $this->withoutBlankSecrets($input); $values = []; $secretKeys = []; foreach ($this->definitions as $definition) { if ($definition->isFile() || ! array_key_exists($definition->input, $input)) { continue; } $values[$definition->key] = $definition->normalize($input[$definition->input]); if ($definition->isSecret()) { $secretKeys[] = $definition->key; } } if ($secretKeys === []) { $this->settings->setMany($values); return; } $this->settings->setManySecrets($values, $secretKeys); } /** * Drops secret inputs that would replace the stored value with nothing or with * its own mask, whatever the field type, so they keep the stored value. * * @param array $input * @return array */ public function withoutBlankSecrets(array $input): array { foreach ($this->definitions as $definition) { if ($definition->isSecret() && in_array($input[$definition->input] ?? null, ['', null, ExtensionSettingDefinition::MASK], true)) { unset($input[$definition->input]); } } return $input; } /** * Field schema for the auto-rendered admin settings form, in definition * order, with each entry carrying its current public value. * * @return list */ public function schema(): array { return array_values(array_map( fn (ExtensionSettingDefinition $definition): array => $definition->describe($this->get($definition->key)), $this->definitions, )); } /** The file setting behind an input name, or null when the input is not a file setting. */ public function fileField(string $input): ?ExtensionSettingDefinition { return array_find($this->definitions, fn (ExtensionSettingDefinition $definition): bool => $definition->input === $input && $definition->isFile()); } /** * Point a file setting at a newly stored file, or clear it with null. * * @return string|null the stored name it pointed at before, which the caller deletes */ public function replaceFile(string $input, ?string $name): ?string { $definition = $this->fileField($input); throw_unless($definition instanceof ExtensionSettingDefinition, InvalidArgumentException::class, sprintf('Extension setting "%s" is not a file setting.', $input)); throw_if($name !== null && ExtensionSettingValueGuard::fileReference($name) === null, InvalidArgumentException::class, 'Invalid stored file name.'); $previous = $this->storedFile($definition); if ($name === null) { $this->settings->forget($definition->key); } else { $this->settings->set($definition->key, $name); } return $previous; } /** * Values exposed to the extension's frontend bundle as ctx.config: only * definitions explicitly marked ->frontend(), keyed by public name. These * are visible to every logged-in user. With $publicOnly, only those also * marked ->public(), which is what visitors who are not signed in receive. * * @return array */ public function frontendConfig(bool $publicOnly = false): array { $config = []; foreach ($this->definitions as $definition) { if (! $definition->isFrontend() || ($publicOnly && ! $definition->isPublic())) { continue; } $value = $definition->serializePublic($this->get($definition->key)); ExtensionSettingValueGuard::assertFrontendType($value, $definition->declaredFrontendType()); $config[$definition->publicName()] = $value; } return $config; } /** @return array */ public function frontendConfigTypes(): array { $types = []; foreach ($this->definitions as $definition) { if ($definition->isFrontend()) { $types[$definition->publicName()] = $definition->declaredFrontendType(); } } return $types; } /** @return list public names of the frontend settings that guests receive too */ public function publicConfigKeys(): array { return array_values(array_map( fn (ExtensionSettingDefinition $definition): string => $definition->publicName(), array_filter($this->definitions, fn (ExtensionSettingDefinition $definition): bool => $definition->isPublic()), )); } private function storedFile(ExtensionSettingDefinition $definition): ?string { return ExtensionSettingValueGuard::fileReference($this->settings->get($definition->key)); } }