Skip to content

Commit

Permalink
feat(lexicon): configurable default value
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <[email protected]>
  • Loading branch information
ArtificialOwl committed Dec 13, 2024
1 parent 8159917 commit be84419
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 3 deletions.
4 changes: 3 additions & 1 deletion core/Command/Config/App/GetConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if (!$input->hasParameterOption('--default-value')) {
return 1;
}
$configValue = $defaultValue;

// get the default value defined from lexicon
$configValue = $this->appConfig->getValueMixed($appName, $configName, $defaultValue ?? '');

Check failure on line 84 in core/Command/Config/App/GetConfig.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedInterfaceMethod

core/Command/Config/App/GetConfig.php:84:37: UndefinedInterfaceMethod: Method OCP\IAppConfig::getValueMixed does not exist (see https://psalm.dev/181)
}

$this->writeMixedInOutputFormat($input, $output, $configValue);
Expand Down
3 changes: 2 additions & 1 deletion lib/private/AppFramework/Bootstrap/RegistrationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

use Closure;
use NCU\Config\Lexicon\IConfigLexicon;
use OC\Config\Lexicon\CoreConfigLexicon;
use OC\Support\CrashReport\Registry;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
Expand Down Expand Up @@ -143,7 +144,7 @@ class RegistrationContext {
private array $declarativeSettings = [];

/** @var array<array-key, string> */
private array $configLexiconClasses = [];
private array $configLexiconClasses = ['core' => CoreConfigLexicon::class];

/** @var ServiceRegistration<ITeamResourceProvider>[] */
private array $teamResourceProviders = [];
Expand Down
52 changes: 52 additions & 0 deletions lib/private/Config/Lexicon/CoreConfigLexicon.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/

namespace OC\Config\Lexicon;

use NCU\Config\Lexicon\ConfigLexiconEntry;
use NCU\Config\Lexicon\ConfigLexiconStrictness;
use NCU\Config\Lexicon\IConfigLexicon;
use NCU\Config\ValueType;

/**
* ConfigLexicon for 'core' app/user configs
*
* @since 31.0.0
*/
class CoreConfigLexicon implements IConfigLexicon {
/**
* @inheritDoc
* @return ConfigLexiconStrictness
* @since 31.0.0
*/
public function getStrictness(): ConfigLexiconStrictness {
return ConfigLexiconStrictness::IGNORE;
}

/**
* @inheritDoc
* @return ConfigLexiconEntry[]
* @since 31.0.0
*/
public function getAppConfigs(): array {
return [
new ConfigLexiconEntry('lastcron', ValueType::INT, 0, 'timestamp of last cron execution'),
];
}

/**
* @inheritDoc
* @return ConfigLexiconEntry[]
* @since 31.0.0
*/
public function getUserConfigs(): array {
return [
new ConfigLexiconEntry('lang', ValueType::STRING, null, 'language'),
];
}
}
5 changes: 4 additions & 1 deletion lib/private/Config/UserConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class UserConfig implements IUserConfig {

public function __construct(
protected IDBConnection $connection,
protected IConfig $config,
protected LoggerInterface $logger,
protected ICrypto $crypto,
) {
Expand Down Expand Up @@ -1843,7 +1844,9 @@ private function matchAndApplyLexiconDefinition(
}

$lazy = $configValue->isLazy();
$default = $configValue->getDefault() ?? $default; // default from Lexicon got priority
// default from Lexicon got priority but it can still be overwritten by admin
// with 'lexicon.default.userconfig' => [<app>.<key> => 'my value'], in config.php
$default = $this->config->getSystemValue('lexicon.default.userconfig', [])[$app . '.' . $key] ?? $configValue->getDefault() ?? $default;
$flags = $configValue->getFlags();

if ($configValue->isDeprecated()) {
Expand Down
4 changes: 4 additions & 0 deletions tests/lib/Config/UserConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use NCU\Config\IUserConfig;
use NCU\Config\ValueType;
use OC\Config\UserConfig;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
use Psr\Log\LoggerInterface;
Expand All @@ -26,6 +27,7 @@
*/
class UserConfigTest extends TestCase {
protected IDBConnection $connection;
private IConfig $config;
private LoggerInterface $logger;
private ICrypto $crypto;
private array $originalPreferences;
Expand Down Expand Up @@ -169,6 +171,7 @@ protected function setUp(): void {
parent::setUp();

$this->connection = \OCP\Server::get(IDBConnection::class);
$this->config = \OCP\Server::get(IConfig::class);
$this->logger = \OCP\Server::get(LoggerInterface::class);
$this->crypto = \OCP\Server::get(ICrypto::class);

Expand Down Expand Up @@ -277,6 +280,7 @@ protected function tearDown(): void {
private function generateUserConfig(array $preLoading = []): IUserConfig {
$userConfig = new \OC\Config\UserConfig(
$this->connection,
$this->config,
$this->logger,
$this->crypto,
);
Expand Down

0 comments on commit be84419

Please sign in to comment.