diff options
author | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-10-15 20:22:33 +0200 |
---|---|---|
committer | Ferdinand Thiessen <opensource@fthiessen.de> | 2024-10-15 20:33:45 +0200 |
commit | 9a7e1bb2276f4a4635cd330fbc2a385ebaeed5ee (patch) | |
tree | e33bb863c02fc890f596a64354fc440278e67b30 /lib/private/Settings/DeclarativeManager.php | |
parent | cd3dc1719b8e84526f2c99634f0dc8bf16380579 (diff) | |
download | nextcloud-server-9a7e1bb2276f4a4635cd330fbc2a385ebaeed5ee.tar.gz nextcloud-server-9a7e1bb2276f4a4635cd330fbc2a385ebaeed5ee.zip |
feat(DeclarativeSettings): Allow to implement value getter and setter directly in Form
Instead of implementing the form class, a setter event listener and a getter event listener,
this allows to simply write a basic class that provides `getSchema`, `setValue` and `getValue` functions.
Signed-off-by: Ferdinand Thiessen <opensource@fthiessen.de>
Diffstat (limited to 'lib/private/Settings/DeclarativeManager.php')
-rw-r--r-- | lib/private/Settings/DeclarativeManager.php | 53 |
1 files changed, 43 insertions, 10 deletions
diff --git a/lib/private/Settings/DeclarativeManager.php b/lib/private/Settings/DeclarativeManager.php index 5c2b868f417..ee253b86449 100644 --- a/lib/private/Settings/DeclarativeManager.php +++ b/lib/private/Settings/DeclarativeManager.php @@ -22,6 +22,7 @@ use OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent; use OCP\Settings\Events\DeclarativeSettingsSetValueEvent; use OCP\Settings\IDeclarativeManager; use OCP\Settings\IDeclarativeSettingsForm; +use OCP\Settings\IDeclarativeSettingsFormWithHandlers; use Psr\Log\LoggerInterface; /** @@ -32,6 +33,15 @@ use Psr\Log\LoggerInterface; * @psalm-import-type DeclarativeSettingsFormSchemaWithoutValues from IDeclarativeSettingsForm */ class DeclarativeManager implements IDeclarativeManager { + + /** @var array<string, list<IDeclarativeSettingsForm>> */ + private array $declarativeForms = []; + + /** + * @var array<string, list<DeclarativeSettingsFormSchemaWithoutValues>> + */ + private array $appSchemas = []; + public function __construct( private IEventDispatcher $eventDispatcher, private IGroupManager $groupManager, @@ -43,11 +53,6 @@ class DeclarativeManager implements IDeclarativeManager { } /** - * @var array<string, list<DeclarativeSettingsFormSchemaWithoutValues>> - */ - private array $appSchemas = []; - - /** * @inheritdoc */ public function registerSchema(string $app, array $schema): void { @@ -77,11 +82,15 @@ class DeclarativeManager implements IDeclarativeManager { * @inheritdoc */ public function loadSchemas(): void { - $declarativeSettings = $this->coordinator->getRegistrationContext()->getDeclarativeSettings(); - foreach ($declarativeSettings as $declarativeSetting) { - /** @var IDeclarativeSettingsForm $declarativeSettingObject */ - $declarativeSettingObject = Server::get($declarativeSetting->getService()); - $this->registerSchema($declarativeSetting->getAppId(), $declarativeSettingObject->getSchema()); + if (empty($this->declarativeForms)) { + $declarativeSettings = $this->coordinator->getRegistrationContext()->getDeclarativeSettings(); + foreach ($declarativeSettings as $declarativeSetting) { + $app = $declarativeSetting->getAppId(); + /** @var IDeclarativeSettingsForm $declarativeForm */ + $declarativeForm = Server::get($declarativeSetting->getService()); + $this->registerSchema($app, $declarativeForm->getSchema()); + $this->declarativeForms[$app][] = $declarativeForm; + } } $this->eventDispatcher->dispatchTyped(new DeclarativeSettingsRegisterFormEvent($this)); @@ -224,6 +233,10 @@ class DeclarativeManager implements IDeclarativeManager { $storageType = $this->getStorageType($app, $fieldId); switch ($storageType) { case DeclarativeSettingsTypes::STORAGE_TYPE_EXTERNAL: + $form = $this->getForm($app, $formId); + if ($form !== null && $form instanceof IDeclarativeSettingsFormWithHandlers) { + return $form->getValue($fieldId, $user); + } $event = new DeclarativeSettingsGetValueEvent($user, $app, $formId, $fieldId); $this->eventDispatcher->dispatchTyped($event); return $event->getValue(); @@ -244,6 +257,12 @@ class DeclarativeManager implements IDeclarativeManager { $storageType = $this->getStorageType($app, $fieldId); switch ($storageType) { case DeclarativeSettingsTypes::STORAGE_TYPE_EXTERNAL: + $form = $this->getForm($app, $formId); + if ($form !== null && $form instanceof IDeclarativeSettingsFormWithHandlers) { + $form->setValue($fieldId, $value, $user); + break; + } + // fall back to event handling $this->eventDispatcher->dispatchTyped(new DeclarativeSettingsSetValueEvent($user, $app, $formId, $fieldId, $value)); break; case DeclarativeSettingsTypes::STORAGE_TYPE_INTERNAL: @@ -254,6 +273,20 @@ class DeclarativeManager implements IDeclarativeManager { } } + /** + * If a declarative setting was registered as a form and not just a schema + * then this will yield the registering form. + */ + private function getForm(string $app, string $formId): ?IDeclarativeSettingsForm { + $allForms = $this->declarativeForms[$app] ?? []; + foreach ($allForms as $form) { + if ($form->getSchema()['id'] === $formId) { + return $form; + } + } + return null; + } + private function getInternalValue(IUser $user, string $app, string $formId, string $fieldId): mixed { $sectionType = $this->getSectionType($app, $fieldId); $defaultValue = $this->getDefaultValue($app, $formId, $fieldId); |