nextcloud / server

☁️ Nextcloud server, a safe home for all your data
https://nextcloud.com
GNU Affero General Public License v3.0
27.49k stars 4.08k forks source link

feat(config): implementation of lexicon #49399

Open ArtificialOwl opened 2 days ago

ArtificialOwl commented 2 days ago

A Config Lexicon is a list of config keys used by current app. Each entry also define the expected type for the config value, its lazyness/sensitivity. The loading is done by registering a IConfigLexicon.

Application.php

    public function register(IRegistrationContext $context): void {
        $context->registerConfigLexicon(ConfigLexicon::class);
    }

ConfigLexicon.php

class Lexicon implements IConfigLexicon {
    public function isStrict(): bool {
        return false;
    }

    public function getAppConfigs(): array {
        return [
            new ConfigLexiconEntry('key1', IConfigLexiconEntry::TYPE_STRING, defintion: 'this is a test config key', lazy: true),
            new ConfigLexiconEntry('key2', IConfigLexiconEntry::TYPE_STRING, sensitive: true),
            new ConfigLexiconEntry('key3', IConfigLexiconEntry::TYPE_INT, 42),
            new ConfigLexiconEntry('key4', IConfigLexiconEntry::TYPE_STRING),
        ];
    }

    public function getUserPreferences(): array {
        return [];
    }
}

Note: A description of the config key can be added as 3rd parameter of the ConfigLexiconEntry. This information is not stored when running from a web process.

store and retrieve config value

Once this is done:

any code trying to wrongly type config values will get an exception

$ ./occ config:app:set myapp key1 --value 1 --type integer

In AppConfig.php line 1544:                                                                                     
  The key is typed incorrectly in relation to the app config lexicon

set/get on config values set as lazy will work even if not specified in the process

    $this->appConfig->setValueInt('myapp', 'key1', 'abc');
$ ./occ config:app:get myapp key1 --details
  - app: myapp
  - key: key1
  - value: abc
  - type: string
  - lazy: true
  - sensitive: false

default value can be overwritten

    $this->appConfig->getValueInt('myapp', 'key3', 0);

will returns 42

If configured as strict, setting an unlisted config values returns an exception

$ ./occ config:app:set myapp key5 --value 1

In AppConfig.php line 1530:                                                                                
  The key is not defined in the app config lexicon  

Also, setting a config key as deprecated will generate a level 1 log entry when the config key is used