@@ -721,7 +721,7 @@ private function getTypedValue(
721721 ): string {
722722 $ this ->assertParams ($ userId , $ app , $ key );
723723 $ origKey = $ key ;
724- if (!$ this ->matchAndApplyLexiconDefinition ($ userId , $ app , $ key , $ lazy , $ type , default: $ default )) {
724+ if (!$ this ->matchAndApplyLexiconDefinition ($ userId , $ app , $ key , $ lazy , $ type , default: $ default, lexiconEntry: $ lexiconEntry )) {
725725 // returns default if strictness of lexicon is set to WARNING (block and report)
726726 return $ default ;
727727 }
@@ -753,6 +753,13 @@ private function getTypedValue(
753753 } elseif (isset ($ this ->fastCache [$ userId ][$ app ][$ key ])) {
754754 $ value = $ this ->fastCache [$ userId ][$ app ][$ key ];
755755 } else {
756+ // unknown value, might want to execute something instead of just returning default.
757+ // default value will be stored in database, unless false is returned
758+ if (($ lexiconEntry ?->executeOnInit() !== null )
759+ && $ lexiconEntry ->executeOnInit ()($ default ) !== false ) {
760+ $ this ->setTypedValue ($ userId , $ app , $ key , $ default , $ lazy , $ type );
761+ }
762+
756763 return $ default ;
757764 }
758765
@@ -1068,12 +1075,19 @@ private function setTypedValue(
10681075 ValueType $ type ,
10691076 ): bool {
10701077 $ this ->assertParams ($ userId , $ app , $ key );
1071- if (!$ this ->matchAndApplyLexiconDefinition ($ userId , $ app , $ key , $ lazy , $ type , $ flags )) {
1078+ /** @var ConfigLexiconEntry $lexiconEntry */
1079+ if (!$ this ->matchAndApplyLexiconDefinition ($ userId , $ app , $ key , $ lazy , $ type , $ flags , lexiconEntry: $ lexiconEntry )) {
10721080 // returns false as database is not updated
10731081 return false ;
10741082 }
10751083 $ this ->loadConfig ($ userId , $ lazy );
10761084
1085+ // might want to execute something before storing new value
1086+ // will cancel storing of new value in database if false is returned
1087+ if ($ lexiconEntry ?->executeOnSet() !== null && $ lexiconEntry ?->executeOnSet()($ value ) === false ) {
1088+ return false ;
1089+ }
1090+
10771091 $ inserted = $ refreshCache = false ;
10781092 $ origValue = $ value ;
10791093 $ sensitive = $ this ->isFlagged (self ::FLAG_SENSITIVE , $ flags );
@@ -1882,6 +1896,7 @@ private function matchAndApplyLexiconDefinition(
18821896 ValueType &$ type = ValueType::MIXED ,
18831897 int &$ flags = 0 ,
18841898 string &$ default = '' ,
1899+ ?ConfigLexiconEntry &$ lexiconEntry = null ,
18851900 ): bool {
18861901 $ configDetails = $ this ->getConfigDetailsFromLexicon ($ app );
18871902 if (array_key_exists ($ key , $ configDetails ['aliases ' ]) && !$ this ->ignoreLexiconAliases ) {
@@ -1898,18 +1913,18 @@ private function matchAndApplyLexiconDefinition(
18981913 return true ;
18991914 }
19001915
1901- /** @var ConfigLexiconEntry $configValue */
1902- $ configValue = $ configDetails ['entries ' ][$ key ];
1916+ /** @var ConfigLexiconEntry $lexiconEntry */
1917+ $ lexiconEntry = $ configDetails ['entries ' ][$ key ];
19031918 if ($ type === ValueType::MIXED ) {
19041919 // we overwrite if value was requested as mixed
1905- $ type = $ configValue ->getValueType ();
1906- } elseif ($ configValue ->getValueType () !== $ type ) {
1920+ $ type = $ lexiconEntry ->getValueType ();
1921+ } elseif ($ lexiconEntry ->getValueType () !== $ type ) {
19071922 throw new TypeConflictException ('The user config key ' . $ app . '/ ' . $ key . ' is typed incorrectly in relation to the config lexicon ' );
19081923 }
19091924
1910- $ lazy = $ configValue ->isLazy ();
1911- $ flags = $ configValue ->getFlags ();
1912- if ($ configValue ->isDeprecated ()) {
1925+ $ lazy = $ lexiconEntry ->isLazy ();
1926+ $ flags = $ lexiconEntry ->getFlags ();
1927+ if ($ lexiconEntry ->isDeprecated ()) {
19131928 $ this ->logger ->notice ('User config key ' . $ app . '/ ' . $ key . ' is set as deprecated. ' );
19141929 }
19151930
@@ -1920,7 +1935,7 @@ private function matchAndApplyLexiconDefinition(
19201935 }
19211936
19221937 // default from Lexicon got priority but it can still be overwritten by admin
1923- $ default = $ this ->getSystemDefault ($ app , $ configValue ) ?? $ configValue ->getDefault () ?? $ default ;
1938+ $ default = $ this ->getSystemDefault ($ app , $ lexiconEntry ) ?? $ lexiconEntry ->getDefault () ?? $ default ;
19241939
19251940 // returning false will make get() returning $default and set() not changing value in database
19261941 return !$ enforcedValue ;
0 commit comments