Skip to content

Commit 8fab143

Browse files
authored
Merge pull request #48721 from nextcloud/feat/allow-getter-setter-decl-fors
2 parents 01ad23a + bcfb02f commit 8fab143

File tree

12 files changed

+183
-120
lines changed

12 files changed

+183
-120
lines changed

apps/files/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,6 @@
5858
'OCA\\Files\\Event\\LoadSidebar' => $baseDir . '/../lib/Event/LoadSidebar.php',
5959
'OCA\\Files\\Exception\\TransferOwnershipException' => $baseDir . '/../lib/Exception/TransferOwnershipException.php',
6060
'OCA\\Files\\Helper' => $baseDir . '/../lib/Helper.php',
61-
'OCA\\Files\\Listener\\DeclarativeSettingsGetValueEventListener' => $baseDir . '/../lib/Listener/DeclarativeSettingsGetValueEventListener.php',
62-
'OCA\\Files\\Listener\\DeclarativeSettingsRegisterFormEventListener' => $baseDir . '/../lib/Listener/DeclarativeSettingsRegisterFormEventListener.php',
63-
'OCA\\Files\\Listener\\DeclarativeSettingsSetValueEventListener' => $baseDir . '/../lib/Listener/DeclarativeSettingsSetValueEventListener.php',
6461
'OCA\\Files\\Listener\\LoadSearchPluginsListener' => $baseDir . '/../lib/Listener/LoadSearchPluginsListener.php',
6562
'OCA\\Files\\Listener\\LoadSidebarListener' => $baseDir . '/../lib/Listener/LoadSidebarListener.php',
6663
'OCA\\Files\\Listener\\RenderReferenceEventListener' => $baseDir . '/../lib/Listener/RenderReferenceEventListener.php',
@@ -79,5 +76,6 @@
7976
'OCA\\Files\\Service\\TagService' => $baseDir . '/../lib/Service/TagService.php',
8077
'OCA\\Files\\Service\\UserConfig' => $baseDir . '/../lib/Service/UserConfig.php',
8178
'OCA\\Files\\Service\\ViewConfig' => $baseDir . '/../lib/Service/ViewConfig.php',
79+
'OCA\\Files\\Settings\\DeclarativeAdminSettings' => $baseDir . '/../lib/Settings/DeclarativeAdminSettings.php',
8280
'OCA\\Files\\Settings\\PersonalSettings' => $baseDir . '/../lib/Settings/PersonalSettings.php',
8381
);

apps/files/composer/composer/autoload_static.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,6 @@ class ComposerStaticInitFiles
7373
'OCA\\Files\\Event\\LoadSidebar' => __DIR__ . '/..' . '/../lib/Event/LoadSidebar.php',
7474
'OCA\\Files\\Exception\\TransferOwnershipException' => __DIR__ . '/..' . '/../lib/Exception/TransferOwnershipException.php',
7575
'OCA\\Files\\Helper' => __DIR__ . '/..' . '/../lib/Helper.php',
76-
'OCA\\Files\\Listener\\DeclarativeSettingsGetValueEventListener' => __DIR__ . '/..' . '/../lib/Listener/DeclarativeSettingsGetValueEventListener.php',
77-
'OCA\\Files\\Listener\\DeclarativeSettingsRegisterFormEventListener' => __DIR__ . '/..' . '/../lib/Listener/DeclarativeSettingsRegisterFormEventListener.php',
78-
'OCA\\Files\\Listener\\DeclarativeSettingsSetValueEventListener' => __DIR__ . '/..' . '/../lib/Listener/DeclarativeSettingsSetValueEventListener.php',
7976
'OCA\\Files\\Listener\\LoadSearchPluginsListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSearchPluginsListener.php',
8077
'OCA\\Files\\Listener\\LoadSidebarListener' => __DIR__ . '/..' . '/../lib/Listener/LoadSidebarListener.php',
8178
'OCA\\Files\\Listener\\RenderReferenceEventListener' => __DIR__ . '/..' . '/../lib/Listener/RenderReferenceEventListener.php',
@@ -94,6 +91,7 @@ class ComposerStaticInitFiles
9491
'OCA\\Files\\Service\\TagService' => __DIR__ . '/..' . '/../lib/Service/TagService.php',
9592
'OCA\\Files\\Service\\UserConfig' => __DIR__ . '/..' . '/../lib/Service/UserConfig.php',
9693
'OCA\\Files\\Service\\ViewConfig' => __DIR__ . '/..' . '/../lib/Service/ViewConfig.php',
94+
'OCA\\Files\\Settings\\DeclarativeAdminSettings' => __DIR__ . '/..' . '/../lib/Settings/DeclarativeAdminSettings.php',
9795
'OCA\\Files\\Settings\\PersonalSettings' => __DIR__ . '/..' . '/../lib/Settings/PersonalSettings.php',
9896
);
9997

apps/files/lib/AppInfo/Application.php

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
use OCA\Files\DirectEditingCapabilities;
1717
use OCA\Files\Event\LoadSearchPlugins;
1818
use OCA\Files\Event\LoadSidebar;
19-
use OCA\Files\Listener\DeclarativeSettingsGetValueEventListener;
20-
use OCA\Files\Listener\DeclarativeSettingsRegisterFormEventListener;
21-
use OCA\Files\Listener\DeclarativeSettingsSetValueEventListener;
2219
use OCA\Files\Listener\LoadSearchPluginsListener;
2320
use OCA\Files\Listener\LoadSidebarListener;
2421
use OCA\Files\Listener\RenderReferenceEventListener;
@@ -28,6 +25,7 @@
2825
use OCA\Files\Service\TagService;
2926
use OCA\Files\Service\UserConfig;
3027
use OCA\Files\Service\ViewConfig;
28+
use OCA\Files\Settings\DeclarativeAdminSettings;
3129
use OCP\Activity\IManager as IActivityManager;
3230
use OCP\AppFramework\App;
3331
use OCP\AppFramework\Bootstrap\IBootContext;
@@ -49,9 +47,6 @@
4947
use OCP\IServerContainer;
5048
use OCP\ITagManager;
5149
use OCP\IUserSession;
52-
use OCP\Settings\Events\DeclarativeSettingsGetValueEvent;
53-
use OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent;
54-
use OCP\Settings\Events\DeclarativeSettingsSetValueEvent;
5550
use OCP\Share\IManager as IShareManager;
5651
use OCP\Util;
5752
use Psr\Container\ContainerInterface;
@@ -111,6 +106,8 @@ public function register(IRegistrationContext $context): void {
111106
$context->registerCapability(Capabilities::class);
112107
$context->registerCapability(DirectEditingCapabilities::class);
113108

109+
$context->registerDeclarativeSettings(DeclarativeAdminSettings::class);
110+
114111
$context->registerEventListener(LoadSidebar::class, LoadSidebarListener::class);
115112
$context->registerEventListener(RenderReferenceEvent::class, RenderReferenceEventListener::class);
116113
$context->registerEventListener(BeforeNodeRenamedEvent::class, SyncLivePhotosListener::class);
@@ -119,9 +116,6 @@ public function register(IRegistrationContext $context): void {
119116
$context->registerEventListener(BeforeNodeCopiedEvent::class, SyncLivePhotosListener::class);
120117
$context->registerEventListener(NodeCopiedEvent::class, SyncLivePhotosListener::class);
121118
$context->registerEventListener(LoadSearchPlugins::class, LoadSearchPluginsListener::class);
122-
$context->registerEventListener(DeclarativeSettingsRegisterFormEvent::class, DeclarativeSettingsRegisterFormEventListener::class);
123-
$context->registerEventListener(DeclarativeSettingsGetValueEvent::class, DeclarativeSettingsGetValueEventListener::class);
124-
$context->registerEventListener(DeclarativeSettingsSetValueEvent::class, DeclarativeSettingsSetValueEventListener::class);
125119

126120
$context->registerSearchProvider(FilesSearchProvider::class);
127121

apps/files/lib/Listener/DeclarativeSettingsGetValueEventListener.php

Lines changed: 0 additions & 39 deletions
This file was deleted.

apps/files/lib/Listener/DeclarativeSettingsSetValueEventListener.php

Lines changed: 0 additions & 40 deletions
This file was deleted.

apps/files/lib/Listener/DeclarativeSettingsRegisterFormEventListener.php renamed to apps/files/lib/Settings/DeclarativeAdminSettings.php

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,42 @@
22

33
declare(strict_types=1);
44
/**
5-
* SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
5+
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
66
* SPDX-License-Identifier: AGPL-3.0-or-later
77
*/
8-
namespace OCA\Files\Listener;
8+
namespace OCA\Files\Settings;
99

10-
use OCA\Files\AppInfo\Application;
11-
use OCP\EventDispatcher\Event;
12-
use OCP\EventDispatcher\IEventListener;
10+
use OCA\Files\Service\SettingsService;
1311
use OCP\IL10N;
12+
use OCP\IUser;
1413
use OCP\Settings\DeclarativeSettingsTypes;
15-
use OCP\Settings\Events\DeclarativeSettingsRegisterFormEvent;
14+
use OCP\Settings\IDeclarativeSettingsFormWithHandlers;
1615

17-
/** @template-implements IEventListener<DeclarativeSettingsRegisterFormEvent> */
18-
class DeclarativeSettingsRegisterFormEventListener implements IEventListener {
16+
class DeclarativeAdminSettings implements IDeclarativeSettingsFormWithHandlers {
1917

2018
public function __construct(
2119
private IL10N $l,
20+
private SettingsService $service,
2221
) {
2322
}
2423

25-
public function handle(Event $event): void {
26-
if (!($event instanceof DeclarativeSettingsRegisterFormEvent)) {
27-
return;
24+
public function getValue(string $fieldId, IUser $user): mixed {
25+
return match($fieldId) {
26+
'windows_support' => $this->service->hasFilesWindowsSupport(),
27+
default => throw new \InvalidArgumentException('Unexpected field id ' . $fieldId),
28+
};
29+
}
30+
31+
public function setValue(string $fieldId, mixed $value, IUser $user): void {
32+
switch ($fieldId) {
33+
case 'windows_support':
34+
$this->service->setFilesWindowsSupport((bool)$value);
35+
break;
2836
}
37+
}
2938

30-
$event->registerSchema(Application::APP_ID, [
39+
public function getSchema(): array {
40+
return [
3141
'id' => 'files-filename-support',
3242
'priority' => 10,
3343
'section_type' => DeclarativeSettingsTypes::SECTION_TYPE_ADMIN,
@@ -45,6 +55,6 @@ public function handle(Event $event): void {
4555
'default' => false,
4656
],
4757
],
48-
]);
58+
];
4959
}
5060
}

lib/composer/composer/autoload_classmap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,7 @@
698698
'OCP\\Settings\\Events\\DeclarativeSettingsSetValueEvent' => $baseDir . '/lib/public/Settings/Events/DeclarativeSettingsSetValueEvent.php',
699699
'OCP\\Settings\\IDeclarativeManager' => $baseDir . '/lib/public/Settings/IDeclarativeManager.php',
700700
'OCP\\Settings\\IDeclarativeSettingsForm' => $baseDir . '/lib/public/Settings/IDeclarativeSettingsForm.php',
701+
'OCP\\Settings\\IDeclarativeSettingsFormWithHandlers' => $baseDir . '/lib/public/Settings/IDeclarativeSettingsFormWithHandlers.php',
701702
'OCP\\Settings\\IDelegatedSettings' => $baseDir . '/lib/public/Settings/IDelegatedSettings.php',
702703
'OCP\\Settings\\IIconSection' => $baseDir . '/lib/public/Settings/IIconSection.php',
703704
'OCP\\Settings\\IManager' => $baseDir . '/lib/public/Settings/IManager.php',

lib/composer/composer/autoload_static.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
731731
'OCP\\Settings\\Events\\DeclarativeSettingsSetValueEvent' => __DIR__ . '/../../..' . '/lib/public/Settings/Events/DeclarativeSettingsSetValueEvent.php',
732732
'OCP\\Settings\\IDeclarativeManager' => __DIR__ . '/../../..' . '/lib/public/Settings/IDeclarativeManager.php',
733733
'OCP\\Settings\\IDeclarativeSettingsForm' => __DIR__ . '/../../..' . '/lib/public/Settings/IDeclarativeSettingsForm.php',
734+
'OCP\\Settings\\IDeclarativeSettingsFormWithHandlers' => __DIR__ . '/../../..' . '/lib/public/Settings/IDeclarativeSettingsFormWithHandlers.php',
734735
'OCP\\Settings\\IDelegatedSettings' => __DIR__ . '/../../..' . '/lib/public/Settings/IDelegatedSettings.php',
735736
'OCP\\Settings\\IIconSection' => __DIR__ . '/../../..' . '/lib/public/Settings/IIconSection.php',
736737
'OCP\\Settings\\IManager' => __DIR__ . '/../../..' . '/lib/public/Settings/IManager.php',

lib/private/Settings/DeclarativeManager.php

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use OCP\Settings\Events\DeclarativeSettingsSetValueEvent;
2323
use OCP\Settings\IDeclarativeManager;
2424
use OCP\Settings\IDeclarativeSettingsForm;
25+
use OCP\Settings\IDeclarativeSettingsFormWithHandlers;
2526
use Psr\Log\LoggerInterface;
2627

2728
/**
@@ -32,6 +33,15 @@
3233
* @psalm-import-type DeclarativeSettingsFormSchemaWithoutValues from IDeclarativeSettingsForm
3334
*/
3435
class DeclarativeManager implements IDeclarativeManager {
36+
37+
/** @var array<string, list<IDeclarativeSettingsForm>> */
38+
private array $declarativeForms = [];
39+
40+
/**
41+
* @var array<string, list<DeclarativeSettingsFormSchemaWithoutValues>>
42+
*/
43+
private array $appSchemas = [];
44+
3545
public function __construct(
3646
private IEventDispatcher $eventDispatcher,
3747
private IGroupManager $groupManager,
@@ -42,11 +52,6 @@ public function __construct(
4252
) {
4353
}
4454

45-
/**
46-
* @var array<string, list<DeclarativeSettingsFormSchemaWithoutValues>>
47-
*/
48-
private array $appSchemas = [];
49-
5055
/**
5156
* @inheritdoc
5257
*/
@@ -77,11 +82,15 @@ public function registerSchema(string $app, array $schema): void {
7782
* @inheritdoc
7883
*/
7984
public function loadSchemas(): void {
80-
$declarativeSettings = $this->coordinator->getRegistrationContext()->getDeclarativeSettings();
81-
foreach ($declarativeSettings as $declarativeSetting) {
82-
/** @var IDeclarativeSettingsForm $declarativeSettingObject */
83-
$declarativeSettingObject = Server::get($declarativeSetting->getService());
84-
$this->registerSchema($declarativeSetting->getAppId(), $declarativeSettingObject->getSchema());
85+
if (empty($this->declarativeForms)) {
86+
$declarativeSettings = $this->coordinator->getRegistrationContext()->getDeclarativeSettings();
87+
foreach ($declarativeSettings as $declarativeSetting) {
88+
$app = $declarativeSetting->getAppId();
89+
/** @var IDeclarativeSettingsForm $declarativeForm */
90+
$declarativeForm = Server::get($declarativeSetting->getService());
91+
$this->registerSchema($app, $declarativeForm->getSchema());
92+
$this->declarativeForms[$app][] = $declarativeForm;
93+
}
8594
}
8695

8796
$this->eventDispatcher->dispatchTyped(new DeclarativeSettingsRegisterFormEvent($this));
@@ -224,6 +233,10 @@ private function getValue(IUser $user, string $app, string $formId, string $fiel
224233
$storageType = $this->getStorageType($app, $fieldId);
225234
switch ($storageType) {
226235
case DeclarativeSettingsTypes::STORAGE_TYPE_EXTERNAL:
236+
$form = $this->getForm($app, $formId);
237+
if ($form !== null && $form instanceof IDeclarativeSettingsFormWithHandlers) {
238+
return $form->getValue($fieldId, $user);
239+
}
227240
$event = new DeclarativeSettingsGetValueEvent($user, $app, $formId, $fieldId);
228241
$this->eventDispatcher->dispatchTyped($event);
229242
return $event->getValue();
@@ -244,6 +257,12 @@ public function setValue(IUser $user, string $app, string $formId, string $field
244257
$storageType = $this->getStorageType($app, $fieldId);
245258
switch ($storageType) {
246259
case DeclarativeSettingsTypes::STORAGE_TYPE_EXTERNAL:
260+
$form = $this->getForm($app, $formId);
261+
if ($form !== null && $form instanceof IDeclarativeSettingsFormWithHandlers) {
262+
$form->setValue($fieldId, $value, $user);
263+
break;
264+
}
265+
// fall back to event handling
247266
$this->eventDispatcher->dispatchTyped(new DeclarativeSettingsSetValueEvent($user, $app, $formId, $fieldId, $value));
248267
break;
249268
case DeclarativeSettingsTypes::STORAGE_TYPE_INTERNAL:
@@ -254,6 +273,20 @@ public function setValue(IUser $user, string $app, string $formId, string $field
254273
}
255274
}
256275

276+
/**
277+
* If a declarative setting was registered as a form and not just a schema
278+
* then this will yield the registering form.
279+
*/
280+
private function getForm(string $app, string $formId): ?IDeclarativeSettingsForm {
281+
$allForms = $this->declarativeForms[$app] ?? [];
282+
foreach ($allForms as $form) {
283+
if ($form->getSchema()['id'] === $formId) {
284+
return $form;
285+
}
286+
}
287+
return null;
288+
}
289+
257290
private function getInternalValue(IUser $user, string $app, string $formId, string $fieldId): mixed {
258291
$sectionType = $this->getSectionType($app, $fieldId);
259292
$defaultValue = $this->getDefaultValue($app, $formId, $fieldId);

lib/public/Settings/DeclarativeSettingsTypes.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ final class DeclarativeSettingsTypes {
3232
/**
3333
* IDeclarativeSettingsForm storage_type which is determines where and how the config value is stored
3434
*
35-
*
36-
* For `external` storage_type the app implementing \OCP\Settings\SetDeclarativeSettingsValueEvent and \OCP\Settings\GetDeclarativeSettingsValueEvent events is responsible for storing and retrieving the config value.
35+
* For `external` storage_type the app needs to either implement event listeners for \OCP\Settings\SetDeclarativeSettingsValueEvent
36+
* and \OCP\Settings\GetDeclarativeSettingsValueEvent or the IDeclarativeSettingsForm also needs to implement
37+
* IDeclarativeSettingsFormWithHandlers for storing and retrieving the config value.
3738
*
3839
* @since 29.0.0
3940
*/
@@ -43,7 +44,6 @@ final class DeclarativeSettingsTypes {
4344
* IDeclarativeSettingsForm storage_type which is determines where and how the config value is stored
4445
*
4546
* For `internal` storage_type the config value is stored in default `appconfig` and `preferences` tables.
46-
* For `external` storage_type the app implementing \OCP\Settings\SetDeclarativeSettingsValueEvent and \OCP\Settings\GetDeclarativeSettingsValueEvent events is responsible for storing and retrieving the config value.
4747
*
4848
* @since 29.0.0
4949
*/

0 commit comments

Comments
 (0)