|
| 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. This lexicon list config keys, their type and additional details. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +Registering your Lexicon |
| 18 | +^^^^^^^^^^^^^^^^^^^^^^^^ |
| 19 | + |
| 20 | +From your ``Application.php``: |
| 21 | + |
| 22 | +.. code-block:: php |
| 23 | +
|
| 24 | + public function register(IRegistrationContext $context): void { |
| 25 | + $context->registerConfigLexicon(\OCA\MyApp\ConfigLexicon::class); |
| 26 | + } |
| 27 | +
|
| 28 | +Then, the ``ConfigLexicon.php``: |
| 29 | + |
| 30 | +.. code-block:: php |
| 31 | +
|
| 32 | + class Lexicon implements IConfigLexicon { |
| 33 | + public function getStrictness(): ConfigLexiconStrictness { |
| 34 | + return ConfigLexiconStrictness::WARNING; |
| 35 | + } |
| 36 | +
|
| 37 | + public function getAppConfigs(): array { |
| 38 | + return [ |
| 39 | + new ConfigLexiconEntry('key1', ValueType::STRING, 'abcde', 'test key', true, IAppConfig::FLAG_SENSITIVE), |
| 40 | + new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false) |
| 41 | + ]; |
| 42 | + } |
| 43 | +
|
| 44 | + public function getUserConfigs(): array { |
| 45 | + return [ |
| 46 | + new ConfigLexiconEntry('key1', ValueType::STRING, 'abcde', 'test key', true, IUserConfig::FLAG_SENSITIVE), |
| 47 | + new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false) |
| 48 | + ]; |
| 49 | + } |
| 50 | + } |
| 51 | +
|
| 52 | +
|
| 53 | +Each method ``getUserConfigs()`` and ``getAppConfigs()`` returns a list of ``ConfigLexiconEntry``. |
| 54 | + |
| 55 | +``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. |
| 56 | +Must returns a value from enum ``\OCP\Config\Lexicon\ConfigLexiconStrictness``. |
| 57 | + |
| 58 | +Available values: |
| 59 | + * ``::IGNORE`` - does not limit the set/get on an unknown config key. |
| 60 | + * ``::NOTICE`` - does not limit the set/get on an unknown config key, but generate a notice in the logs. |
| 61 | + * ``::WARNING`` - unknown config key will not be set, and get will returns the default value. A warning will be issued in the logs. |
| 62 | + * ``::EXCEPTION`` - set/get on an unknown config key will generate an exception. |
| 63 | + |
| 64 | + |
| 65 | +ConfigLexiconEntry |
| 66 | +^^^^^^^^^^^^^^^^^^ |
| 67 | + |
| 68 | +.. code-block:: php |
| 69 | +
|
| 70 | + new ``ConfigLexiconEntry``( |
| 71 | + 'my_config_key', |
| 72 | + \OCP\Config\ValueType::STRING, |
| 73 | + 'default value', |
| 74 | + 'this is a description of the use for this config key', |
| 75 | + lazy: true, |
| 76 | + flags: FLAG_SENSITIVE |
| 77 | + ); |
| 78 | +
|
| 79 | +Each config key is defined in a object through those arguments: |
| 80 | + |
| 81 | + * ``key``: config key |
| 82 | + * ``type``: expected value type when the code set/get the config key's value |
| 83 | + * ``defaultRaw``: value to returns if a config value is not available in the database |
| 84 | + * ``description``: textual description of the use made from the config value, |
| 85 | + * ``lazy``: config value is stored as Lazy |
| 86 | + * ``flags``: value is sensitive and/or indexable, using ``IAppConfig::FLAG_SENSITIVE``, ``IUserConfig::FLAG_SENSITIVE``, ``IUserConfig::FLAG_INDEXED``, |
| 87 | + * ``deprecated``: if set to ``true`` will generate a notice entry in the nextcloud logs when called |
| 88 | + |
| 89 | + |
| 90 | +.. 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');`` |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
0 commit comments