Skip to content

Commit eee62ee

Browse files
committed
feat(config): frame config keys/values
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent 4121b84 commit eee62ee

File tree

9 files changed

+316
-0
lines changed

9 files changed

+316
-0
lines changed

lib/composer/composer/autoload_classmap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
'OCP\\AppFramework\\Bootstrap\\IBootContext' => $baseDir . '/lib/public/AppFramework/Bootstrap/IBootContext.php',
2929
'OCP\\AppFramework\\Bootstrap\\IBootstrap' => $baseDir . '/lib/public/AppFramework/Bootstrap/IBootstrap.php',
3030
'OCP\\AppFramework\\Bootstrap\\IRegistrationContext' => $baseDir . '/lib/public/AppFramework/Bootstrap/IRegistrationContext.php',
31+
'OCP\\AppFramework\\ConfigValues\\IConfigValue' => $baseDir . '/lib/public/AppFramework/ConfigValues/IConfigValue.php',
3132
'OCP\\AppFramework\\Controller' => $baseDir . '/lib/public/AppFramework/Controller.php',
3233
'OCP\\AppFramework\\Db\\DoesNotExistException' => $baseDir . '/lib/public/AppFramework/Db/DoesNotExistException.php',
3334
'OCP\\AppFramework\\Db\\Entity' => $baseDir . '/lib/public/AppFramework/Db/Entity.php',
@@ -830,6 +831,9 @@
830831
'OC\\AppFramework\\Bootstrap\\ServiceAliasRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/ServiceAliasRegistration.php',
831832
'OC\\AppFramework\\Bootstrap\\ServiceFactoryRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/ServiceFactoryRegistration.php',
832833
'OC\\AppFramework\\Bootstrap\\ServiceRegistration' => $baseDir . '/lib/private/AppFramework/Bootstrap/ServiceRegistration.php',
834+
'OC\\AppFramework\\ConfigValues\\AConfigValue' => $baseDir . '/lib/private/AppFramework/ConfigValues/AConfigValue.php',
835+
'OC\\AppFramework\\ConfigValues\\AppConfigValue' => $baseDir . '/lib/private/AppFramework/ConfigValues/AppConfigValue.php',
836+
'OC\\AppFramework\\ConfigValues\\UserPreferenceValue' => $baseDir . '/lib/private/AppFramework/ConfigValues/UserPreferenceValue.php',
833837
'OC\\AppFramework\\DependencyInjection\\DIContainer' => $baseDir . '/lib/private/AppFramework/DependencyInjection/DIContainer.php',
834838
'OC\\AppFramework\\Http' => $baseDir . '/lib/private/AppFramework/Http.php',
835839
'OC\\AppFramework\\Http\\Dispatcher' => $baseDir . '/lib/private/AppFramework/Http/Dispatcher.php',

lib/composer/composer/autoload_static.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
6161
'OCP\\AppFramework\\Bootstrap\\IBootContext' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Bootstrap/IBootContext.php',
6262
'OCP\\AppFramework\\Bootstrap\\IBootstrap' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Bootstrap/IBootstrap.php',
6363
'OCP\\AppFramework\\Bootstrap\\IRegistrationContext' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Bootstrap/IRegistrationContext.php',
64+
'OCP\\AppFramework\\ConfigValues\\IConfigValue' => __DIR__ . '/../../..' . '/lib/public/AppFramework/ConfigValues/IConfigValue.php',
6465
'OCP\\AppFramework\\Controller' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Controller.php',
6566
'OCP\\AppFramework\\Db\\DoesNotExistException' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/DoesNotExistException.php',
6667
'OCP\\AppFramework\\Db\\Entity' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Db/Entity.php',
@@ -863,6 +864,9 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
863864
'OC\\AppFramework\\Bootstrap\\ServiceAliasRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/ServiceAliasRegistration.php',
864865
'OC\\AppFramework\\Bootstrap\\ServiceFactoryRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/ServiceFactoryRegistration.php',
865866
'OC\\AppFramework\\Bootstrap\\ServiceRegistration' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Bootstrap/ServiceRegistration.php',
867+
'OC\\AppFramework\\ConfigValues\\AConfigValue' => __DIR__ . '/../../..' . '/lib/private/AppFramework/ConfigValues/AConfigValue.php',
868+
'OC\\AppFramework\\ConfigValues\\AppConfigValue' => __DIR__ . '/../../..' . '/lib/private/AppFramework/ConfigValues/AppConfigValue.php',
869+
'OC\\AppFramework\\ConfigValues\\UserPreferenceValue' => __DIR__ . '/../../..' . '/lib/private/AppFramework/ConfigValues/UserPreferenceValue.php',
866870
'OC\\AppFramework\\DependencyInjection\\DIContainer' => __DIR__ . '/../../..' . '/lib/private/AppFramework/DependencyInjection/DIContainer.php',
867871
'OC\\AppFramework\\Http' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Http.php',
868872
'OC\\AppFramework\\Http\\Dispatcher' => __DIR__ . '/../../..' . '/lib/private/AppFramework/Http/Dispatcher.php',

lib/private/AppConfig.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838

3939
use InvalidArgumentException;
4040
use JsonException;
41+
use OC\AppFramework\Bootstrap\Coordinator;
42+
use OC\AppFramework\ConfigValues\AppConfigValue;
43+
use OCP\AppFramework\ConfigValues\IConfigValue;
4144
use OCP\DB\Exception as DBException;
4245
use OCP\DB\QueryBuilder\IQueryBuilder;
4346
use OCP\Exceptions\AppConfigIncorrectTypeException;
@@ -97,6 +100,7 @@ public function __construct(
97100
protected IDBConnection $connection,
98101
protected LoggerInterface $logger,
99102
protected ICrypto $crypto,
103+
private Coordinator $coordinator,
100104
) {
101105
}
102106

@@ -455,6 +459,7 @@ private function getTypedValue(
455459
int $type
456460
): string {
457461
$this->assertParams($app, $key, valueType: $type);
462+
$this->compareRegisteredConfigValues($app, $key, $lazy, $type, $default);
458463
$this->loadConfig($lazy);
459464

460465
/**
@@ -746,6 +751,7 @@ private function setTypedValue(
746751
int $type
747752
): bool {
748753
$this->assertParams($app, $key);
754+
$this->compareRegisteredConfigValues($app, $key, $lazy, $type);
749755
$this->loadConfig($lazy);
750756

751757
$sensitive = $this->isTyped(self::VALUE_SENSITIVE, $type);
@@ -1506,4 +1512,45 @@ private function getSensitiveKeys(string $app): array {
15061512
public function clearCachedConfig(): void {
15071513
$this->clearCache();
15081514
}
1515+
1516+
1517+
1518+
private function compareRegisteredConfigValues(
1519+
string $app,
1520+
string $key,
1521+
bool &$lazy,
1522+
int &$type,
1523+
string &$default = '',
1524+
): void {
1525+
$context = $this->coordinator->getRegistrationContext();
1526+
$configValues = $context->getConfigValues($app, AppConfigValue::TYPE);
1527+
1528+
if (!array_key_exists($key, $configValues)) {
1529+
if ($context->strictConfigValues($app)) {
1530+
throw new AppConfigUnknownKeyException('This key is not defined in the list of available AppConfig keys for this app');
1531+
}
1532+
return;
1533+
}
1534+
1535+
$configValue = $configValues[$key];
1536+
1537+
if ($configValue->getValueType() !== match($type) {
1538+
self::VALUE_STRING => IConfigValue::TYPE_STRING,
1539+
self::VALUE_INT => IConfigValue::TYPE_INT,
1540+
self::VALUE_FLOAT => IConfigValue::TYPE_FLOAT,
1541+
self::VALUE_BOOL => IConfigValue::TYPE_BOOL,
1542+
self::VALUE_ARRAY => IConfigValue::TYPE_ARRAY,
1543+
}) {
1544+
throw new AppConfigTypeConflictException('This key is mistyped compare to the list of available AppConfig keys for this app');
1545+
}
1546+
1547+
$lazy = $configValue->isLazy();
1548+
$default = $configValue->getDefault() ?? $default;
1549+
if ($configValue->isSensitive()) {
1550+
$type |= self::VALUE_SENSITIVE;
1551+
}
1552+
if ($configValue->isDeprecated()) {
1553+
$this->logger->notice('config value ' . $key . ' from ' . $app . ' is set as deprecated');
1554+
}
1555+
}
15091556
}

lib/private/AppFramework/Bootstrap/RegistrationContext.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use OC\Support\CrashReport\Registry;
3434
use OCP\AppFramework\App;
3535
use OCP\AppFramework\Bootstrap\IRegistrationContext;
36+
use OCP\AppFramework\ConfigValues\IConfigValue;
3637
use OCP\AppFramework\Middleware;
3738
use OCP\AppFramework\Services\InitialStateProvider;
3839
use OCP\Authentication\IAlternativeLogin;
@@ -160,6 +161,7 @@ class RegistrationContext {
160161
/** @var ServiceRegistration<IDeclarativeSettingsForm>[] */
161162
private array $declarativeSettings = [];
162163

164+
private array $configValues = [];
163165
/** @var ServiceRegistration<ITeamResourceProvider>[] */
164166
private array $teamResourceProviders = [];
165167

@@ -411,6 +413,14 @@ public function registerDeclarativeSettings(string $declarativeSettingsClass): v
411413
$declarativeSettingsClass
412414
);
413415
}
416+
417+
public function registerConfigValues(bool $strict, IConfigValue ...$configValues): void {
418+
$this->context->registerConfigValues(
419+
$this->appId,
420+
$strict,
421+
...$configValues
422+
);
423+
}
414424
};
415425
}
416426

@@ -590,6 +600,16 @@ public function registerDeclarativeSettings(string $appId, string $declarativeSe
590600
$this->declarativeSettings[] = new ServiceRegistration($appId, $declarativeSettingsClass);
591601
}
592602

603+
public function registerConfigValues(string $appId, bool $strict, IConfigValue ...$configValues): void {
604+
$values = ['_strict' => $strict];
605+
foreach ($configValues as $configValue) {
606+
$values[$configValue->getConfigType()][$configValue->getKey()] = $configValue;
607+
}
608+
609+
$this->configValues[$appId] = $values;
610+
}
611+
612+
593613
/**
594614
* @param App[] $apps
595615
*/
@@ -920,4 +940,18 @@ public function getTeamResourceProviders(): array {
920940
public function getDeclarativeSettings(): array {
921941
return $this->declarativeSettings;
922942
}
943+
944+
/**
945+
* @param string $app
946+
* @param string $configType
947+
*
948+
* @return array<string, IConfigValue>
949+
*/
950+
public function getConfigValues(string $app, string $configType): array {
951+
return $this->configValues[$app][$configType] ?? [];
952+
}
953+
954+
public function strictConfigValues(string $app): bool {
955+
return $this->configValues[$app]['_strict'] ?? false;
956+
}
923957
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2024 Maxence Lange <maxence@artificial-owl.com>
6+
*
7+
* @author Maxence Lange <maxence@artificial-owl.com>
8+
*
9+
* @license AGPL-3.0 or later
10+
*
11+
* This code is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License, version 3,
13+
* as published by the Free Software Foundation.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License, version 3,
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>
22+
*
23+
*/
24+
25+
namespace OC\AppFramework\ConfigValues;
26+
27+
use OCP\AppFramework\ConfigValues\IConfigValue;
28+
29+
abstract class AConfigValue implements IConfigValue {
30+
private ?string $default = null;
31+
private bool $lazy = false;
32+
private bool $sensitive = false;
33+
private bool $deprecated = false;
34+
35+
public function __construct(
36+
private string $key,
37+
private int $valueType
38+
) {
39+
}
40+
41+
abstract public function getConfigType(): string;
42+
43+
public function getKey(): string {
44+
return $this->key;
45+
}
46+
47+
public function getValueType(): int {
48+
return $this->valueType;
49+
}
50+
51+
public function withDefaultString(string $default): self {
52+
$this->default = $default;
53+
return $this;
54+
}
55+
56+
public function withDefaultInt(int $default): self {
57+
$this->default = (string) $default;
58+
return $this;
59+
}
60+
61+
public function withDefaultFloat(float $default): self {
62+
$this->default = (string) $default;
63+
return $this;
64+
}
65+
66+
public function withDefaultBool(bool $default): self {
67+
$this->default = ($default) ? '1' : '0';
68+
return $this;
69+
}
70+
71+
public function withDefaultArray(array $default): self {
72+
$this->default = json_encode($default);
73+
return $this;
74+
}
75+
76+
public function getDefault(): ?string {
77+
return $this->default;
78+
}
79+
80+
public function asLazy(bool $lazy = true): self {
81+
$this->lazy = $lazy;
82+
return $this;
83+
}
84+
85+
public function isLazy(): bool {
86+
return $this->lazy;
87+
}
88+
89+
public function asSensitive(bool $sensitive = true): self {
90+
$this->sensitive = $sensitive;
91+
return $this;
92+
}
93+
94+
public function isSensitive(): bool {
95+
return $this->sensitive;
96+
}
97+
98+
public function asDeprecated(bool $deprecated = true): self {
99+
$this->deprecated = $deprecated;
100+
return $this;
101+
}
102+
103+
public function isDeprecated(): bool {
104+
return $this->deprecated;
105+
}
106+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2024 Maxence Lange <maxence@artificial-owl.com>
6+
*
7+
* @author Maxence Lange <maxence@artificial-owl.com>
8+
*
9+
* @license AGPL-3.0 or later
10+
*
11+
* This code is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License, version 3,
13+
* as published by the Free Software Foundation.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License, version 3,
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>
22+
*
23+
*/
24+
25+
namespace OC\AppFramework\ConfigValues;
26+
27+
class AppConfigValue extends AConfigValue {
28+
public const TYPE = 'AppConfig';
29+
30+
public function getConfigType(): string {
31+
return self::TYPE;
32+
}
33+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* @copyright Copyright (c) 2024 Maxence Lange <maxence@artificial-owl.com>
6+
*
7+
* @author Maxence Lange <maxence@artificial-owl.com>
8+
*
9+
* @license AGPL-3.0 or later
10+
*
11+
* This code is free software: you can redistribute it and/or modify
12+
* it under the terms of the GNU Affero General Public License, version 3,
13+
* as published by the Free Software Foundation.
14+
*
15+
* This program is distributed in the hope that it will be useful,
16+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
* GNU Affero General Public License for more details.
19+
*
20+
* You should have received a copy of the GNU Affero General Public License, version 3,
21+
* along with this program. If not, see <http://www.gnu.org/licenses/>
22+
*
23+
*/
24+
25+
namespace OC\AppFramework\ConfigValues;
26+
27+
class UserPreferenceValue extends AConfigValue {
28+
public const TYPE = 'UserPreference';
29+
30+
public function getConfigType(): string {
31+
return self::TYPE;
32+
}
33+
}

lib/public/AppFramework/Bootstrap/IRegistrationContext.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
namespace OCP\AppFramework\Bootstrap;
3131

32+
use OCP\AppFramework\ConfigValues\IConfigValue;
3233
use OCP\AppFramework\IAppContainer;
3334
use OCP\Authentication\TwoFactorAuth\IProvider;
3435
use OCP\Calendar\ICalendarProvider;
@@ -410,4 +411,6 @@ public function registerSetupCheck(string $setupCheckClass): void;
410411
* @since 29.0.0
411412
*/
412413
public function registerDeclarativeSettings(string $declarativeSettingsClass): void;
414+
415+
public function registerConfigValues(bool $strict, IConfigValue ...$configValues): void;
413416
}

0 commit comments

Comments
 (0)