|
| 1 | +======= |
| 2 | +Lexicon |
| 3 | +======= |
| 4 | + |
| 5 | +.. versionadded:: 31 |
| 6 | + |
| 7 | +Since v31, Nextcloud provides a way to centralize the definition of your app's configuration keys and values in a single place. |
| 8 | + |
| 9 | + |
| 10 | +Concept overview |
| 11 | +---------------- |
| 12 | + |
| 13 | +Nextcloud includes the ``IConfigLexicon`` API to create a **Lexicon** of your config keys. |
| 14 | +This lexicon allow to define all the config keys used by your app, their value type and additional details. |
| 15 | + |
| 16 | +.. _lexicon_concepts: |
| 17 | + |
| 18 | +Enforcing a unique configuration for each of your config keys in a single location help avoid their misuse. |
| 19 | + |
| 20 | +Details about each config key are: |
| 21 | + - config value expected type, |
| 22 | + - a default value, |
| 23 | + - a description of its use, |
| 24 | + - lazy loading setting, |
| 25 | + - flags the config key as sensitive or indexable |
| 26 | + |
| 27 | + |
| 28 | +Registering your Lexicon |
| 29 | +^^^^^^^^^^^^^^^^^^^^^^^^ |
| 30 | + |
| 31 | +The Lexicon is set in a local class that implements `IconfigLexicon` and registered from your ``Application.php``: |
| 32 | + |
| 33 | +.. code-block:: php |
| 34 | +
|
| 35 | + public function register(IRegistrationContext $context): void { |
| 36 | + $context->registerConfigLexicon(\OCA\MyApp\ConfigLexicon::class); |
| 37 | + } |
| 38 | +
|
| 39 | +Example of the registered ``ConfigLexicon.php``: |
| 40 | + |
| 41 | +.. code-block:: php |
| 42 | +
|
| 43 | + class Lexicon implements IConfigLexicon { |
| 44 | + public function getStrictness(): ConfigLexiconStrictness { |
| 45 | + return ConfigLexiconStrictness::WARNING; |
| 46 | + } |
| 47 | +
|
| 48 | + public function getAppConfigs(): array { |
| 49 | + return [ |
| 50 | + new ConfigLexiconEntry('key1', ValueType::STRING, 'abcde', 'test key', true, IAppConfig::FLAG_SENSITIVE), |
| 51 | + new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false) |
| 52 | + ]; |
| 53 | + } |
| 54 | +
|
| 55 | + public function getUserConfigs(): array { |
| 56 | + return [ |
| 57 | + new ConfigLexiconEntry('key1', ValueType::STRING, 'abcde', 'test key', true, IUserConfig::FLAG_SENSITIVE), |
| 58 | + new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false) |
| 59 | + ]; |
| 60 | + } |
| 61 | + } |
| 62 | +
|
| 63 | +
|
| 64 | +Each method ``getUserConfigs()`` and ``getAppConfigs()`` returns a list of ``ConfigLexiconEntry``. |
| 65 | + |
| 66 | +``getStrictness()`` is used to define the expected behavior of the process when reaching a config keys that is not listed in your app's Config Lexicon. |
| 67 | +Must returns a value from enum ``\OCP\Config\Lexicon\ConfigLexiconStrictness``. |
| 68 | + |
| 69 | +Available values: |
| 70 | + * ``::IGNORE`` - does not limit the set/get on an unknown config key. |
| 71 | + * ``::NOTICE`` - does not limit the set/get on an unknown config key, but generate a notice in the logs. |
| 72 | + * ``::WARNING`` - unknown config key will not be set, and get will returns the default value. A warning will be issued in the logs. |
| 73 | + * ``::EXCEPTION`` - set/get on an unknown config key will generate an exception. |
| 74 | + |
| 75 | + |
| 76 | +ConfigLexiconEntry |
| 77 | +^^^^^^^^^^^^^^^^^^ |
| 78 | + |
| 79 | +.. code-block:: php |
| 80 | +
|
| 81 | + new ``ConfigLexiconEntry``( |
| 82 | + 'my_config_key', |
| 83 | + \OCP\Config\ValueType::STRING, |
| 84 | + 'default value', |
| 85 | + 'this is a description of the use for this config key', |
| 86 | + lazy: true, |
| 87 | + flags: FLAG_SENSITIVE |
| 88 | + ); |
| 89 | +
|
| 90 | +Each config key is defined in a object through those arguments: |
| 91 | + |
| 92 | + * ``key``: config key |
| 93 | + * ``type``: expected value type when the code set/get the config key's value |
| 94 | + * ``defaultRaw``: value to returns if a config value is not available in the database |
| 95 | + * ``description``: textual description of the use made from the config value, |
| 96 | + * ``lazy``: config value is stored as Lazy |
| 97 | + * ``flags``: value is sensitive and/or indexable, using ``IAppConfig::FLAG_SENSITIVE``, ``IUserConfig::FLAG_SENSITIVE``, ``IUserConfig::FLAG_INDEXED``, |
| 98 | + * ``deprecated``: if set to ``true`` will generate a notice entry in the nextcloud logs when called |
| 99 | + |
| 100 | + |
| 101 | +.. note:: Unless if set to ``null``, the default value set in the config lexicon overwrite the default value used as argument when calling ``getValueString('my_config_key', 'another default value');`` |
| 102 | + |
| 103 | + |
| 104 | + |
| 105 | +./occ config:app:get --details |
| 106 | +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 107 | + |
| 108 | +Details from the Lexicon can be extracted using the ``occ`` command |
| 109 | + |
| 110 | +:: |
| 111 | + |
| 112 | + $ ./occ config:app:get myapp my_config_key --details |
| 113 | + - app: myapp |
| 114 | + - key: my_config_key |
| 115 | + - value: 'a_value' |
| 116 | + - type: string |
| 117 | + - lazy: true |
| 118 | + - description: |
| 119 | + - sensitive: false |
| 120 | + |
0 commit comments