@@ -25,22 +25,22 @@ |
||
| 25 | 25 | * @experimental 32.0.0 |
| 26 | 26 | */ |
| 27 | 27 | enum Preset: int { |
| 28 | - /** @experimental 32.0.0 */ |
|
| 29 | - case LARGE = 8; |
|
| 30 | - /** @experimental 32.0.0 */ |
|
| 31 | - case MEDIUM = 7; |
|
| 32 | - /** @experimental 32.0.0 */ |
|
| 33 | - case SMALL = 6; |
|
| 34 | - /** @experimental 32.0.0 */ |
|
| 35 | - case SHARED = 5; |
|
| 36 | - /** @experimental 32.0.0 */ |
|
| 37 | - case EDUCATION = 4; |
|
| 38 | - /** @experimental 32.0.0 */ |
|
| 39 | - case CLUB = 3; |
|
| 40 | - /** @experimental 32.0.0 */ |
|
| 41 | - case FAMILY = 2; |
|
| 42 | - /** @experimental 32.0.0 */ |
|
| 43 | - case PRIVATE = 1; |
|
| 44 | - /** @experimental 32.0.0 */ |
|
| 45 | - case NONE = 0; |
|
| 28 | + /** @experimental 32.0.0 */ |
|
| 29 | + case LARGE = 8; |
|
| 30 | + /** @experimental 32.0.0 */ |
|
| 31 | + case MEDIUM = 7; |
|
| 32 | + /** @experimental 32.0.0 */ |
|
| 33 | + case SMALL = 6; |
|
| 34 | + /** @experimental 32.0.0 */ |
|
| 35 | + case SHARED = 5; |
|
| 36 | + /** @experimental 32.0.0 */ |
|
| 37 | + case EDUCATION = 4; |
|
| 38 | + /** @experimental 32.0.0 */ |
|
| 39 | + case CLUB = 3; |
|
| 40 | + /** @experimental 32.0.0 */ |
|
| 41 | + case FAMILY = 2; |
|
| 42 | + /** @experimental 32.0.0 */ |
|
| 43 | + case PRIVATE = 1; |
|
| 44 | + /** @experimental 32.0.0 */ |
|
| 45 | + case NONE = 0; |
|
| 46 | 46 | } |
@@ -18,232 +18,232 @@ |
||
| 18 | 18 | * @experimental 31.0.0 |
| 19 | 19 | */ |
| 20 | 20 | class ConfigLexiconEntry { |
| 21 | - /** @experimental 32.0.0 */ |
|
| 22 | - public const RENAME_INVERT_BOOLEAN = 1; |
|
| 23 | - |
|
| 24 | - private string $definition = ''; |
|
| 25 | - private ?string $default = null; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * @param string $key config key, can only contain alphanumerical chars and -._ |
|
| 29 | - * @param ValueType $type type of config value |
|
| 30 | - * @param string $definition optional description of config key available when using occ command |
|
| 31 | - * @param bool $lazy set config value as lazy |
|
| 32 | - * @param int $flags set flags |
|
| 33 | - * @param string|null $rename previous config key to migrate config value from |
|
| 34 | - * @param bool $deprecated set config key as deprecated |
|
| 35 | - * |
|
| 36 | - * @experimental 31.0.0 |
|
| 37 | - * @psalm-suppress PossiblyInvalidCast |
|
| 38 | - * @psalm-suppress RiskyCast |
|
| 39 | - */ |
|
| 40 | - public function __construct( |
|
| 41 | - private readonly string $key, |
|
| 42 | - private readonly ValueType $type, |
|
| 43 | - private null|string|int|float|bool|array|Closure $defaultRaw = null, |
|
| 44 | - string $definition = '', |
|
| 45 | - private readonly bool $lazy = false, |
|
| 46 | - private readonly int $flags = 0, |
|
| 47 | - private readonly bool $deprecated = false, |
|
| 48 | - private readonly ?string $rename = null, |
|
| 49 | - private readonly int $options = 0, |
|
| 50 | - ) { |
|
| 51 | - // key can only contain alphanumeric chars and underscore "_" |
|
| 52 | - if (preg_match('/[^[:alnum:]_]/', $key)) { |
|
| 53 | - throw new \Exception('invalid config key'); |
|
| 54 | - } |
|
| 55 | - |
|
| 56 | - /** @psalm-suppress UndefinedClass */ |
|
| 57 | - if (\OC::$CLI) { // only store definition if ran from CLI |
|
| 58 | - $this->definition = $definition; |
|
| 59 | - } |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * returns the config key |
|
| 64 | - * |
|
| 65 | - * @return string config key |
|
| 66 | - * @experimental 31.0.0 |
|
| 67 | - */ |
|
| 68 | - public function getKey(): string { |
|
| 69 | - return $this->key; |
|
| 70 | - } |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * get expected type for config value |
|
| 74 | - * |
|
| 75 | - * @return ValueType |
|
| 76 | - * @experimental 31.0.0 |
|
| 77 | - */ |
|
| 78 | - public function getValueType(): ValueType { |
|
| 79 | - return $this->type; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * @param string $default |
|
| 84 | - * @return string |
|
| 85 | - * @experimental 31.0.0 |
|
| 86 | - */ |
|
| 87 | - private function convertFromString(string $default): string { |
|
| 88 | - return $default; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - /** |
|
| 92 | - * @param int $default |
|
| 93 | - * @return string |
|
| 94 | - * @experimental 31.0.0 |
|
| 95 | - */ |
|
| 96 | - private function convertFromInt(int $default): string { |
|
| 97 | - return (string)$default; |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * @param float $default |
|
| 102 | - * @return string |
|
| 103 | - * @experimental 31.0.0 |
|
| 104 | - */ |
|
| 105 | - private function convertFromFloat(float $default): string { |
|
| 106 | - return (string)$default; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @param bool $default |
|
| 111 | - * @return string |
|
| 112 | - * @experimental 31.0.0 |
|
| 113 | - */ |
|
| 114 | - private function convertFromBool(bool $default): string { |
|
| 115 | - return ($default) ? '1' : '0'; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * @param array $default |
|
| 120 | - * @return string |
|
| 121 | - * @experimental 31.0.0 |
|
| 122 | - */ |
|
| 123 | - private function convertFromArray(array $default): string { |
|
| 124 | - return json_encode($default); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * returns default value |
|
| 129 | - * |
|
| 130 | - * @return string|null NULL if no default is set |
|
| 131 | - * @experimental 31.0.0 |
|
| 132 | - */ |
|
| 133 | - public function getDefault(Preset $preset): ?string { |
|
| 134 | - if ($this->default !== null) { |
|
| 135 | - return $this->default; |
|
| 136 | - } |
|
| 137 | - |
|
| 138 | - if ($this->defaultRaw === null) { |
|
| 139 | - return null; |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - if ($this->defaultRaw instanceof Closure) { |
|
| 143 | - /** @psalm-suppress MixedAssignment we expect closure to returns string|int|float|bool|array */ |
|
| 144 | - $this->defaultRaw = ($this->defaultRaw)($preset); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - /** @psalm-suppress MixedArgument closure should be managed previously */ |
|
| 148 | - $this->default = $this->convertToString($this->defaultRaw); |
|
| 149 | - |
|
| 150 | - return $this->default; |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - /** |
|
| 154 | - * convert $entry into string, based on the expected type for config value |
|
| 155 | - * |
|
| 156 | - * @param string|int|float|bool|array $entry |
|
| 157 | - * |
|
| 158 | - * @return string |
|
| 159 | - * @experimental 31.0.0 |
|
| 160 | - * @psalm-suppress PossiblyInvalidCast arrays are managed pre-cast |
|
| 161 | - * @psalm-suppress RiskyCast |
|
| 162 | - */ |
|
| 163 | - public function convertToString(string|int|float|bool|array $entry): string { |
|
| 164 | - // in case $default is array but is not expected to be an array... |
|
| 165 | - if ($this->getValueType() !== ValueType::ARRAY && is_array($entry)) { |
|
| 166 | - $entry = json_encode($entry, JSON_THROW_ON_ERROR); |
|
| 167 | - } |
|
| 168 | - |
|
| 169 | - return match ($this->getValueType()) { |
|
| 170 | - ValueType::MIXED => (string)$entry, |
|
| 171 | - ValueType::STRING => $this->convertFromString((string)$entry), |
|
| 172 | - ValueType::INT => $this->convertFromInt((int)$entry), |
|
| 173 | - ValueType::FLOAT => $this->convertFromFloat((float)$entry), |
|
| 174 | - ValueType::BOOL => $this->convertFromBool((bool)$entry), |
|
| 175 | - ValueType::ARRAY => $this->convertFromArray((array)$entry) |
|
| 176 | - }; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * returns definition |
|
| 181 | - * |
|
| 182 | - * @return string |
|
| 183 | - * @experimental 31.0.0 |
|
| 184 | - */ |
|
| 185 | - public function getDefinition(): string { |
|
| 186 | - return $this->definition; |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * returns if config key is set as lazy |
|
| 191 | - * |
|
| 192 | - * @see IAppConfig for details on lazy config values |
|
| 193 | - * @return bool TRUE if config value is lazy |
|
| 194 | - * @experimental 31.0.0 |
|
| 195 | - */ |
|
| 196 | - public function isLazy(): bool { |
|
| 197 | - return $this->lazy; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * returns flags |
|
| 202 | - * |
|
| 203 | - * @see IAppConfig for details on sensitive config values |
|
| 204 | - * @return int bitflag about the config value |
|
| 205 | - * @experimental 31.0.0 |
|
| 206 | - */ |
|
| 207 | - public function getFlags(): int { |
|
| 208 | - return $this->flags; |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - /** |
|
| 212 | - * @param int $flag |
|
| 213 | - * |
|
| 214 | - * @return bool TRUE is config value bitflag contains $flag |
|
| 215 | - * @experimental 31.0.0 |
|
| 216 | - */ |
|
| 217 | - public function isFlagged(int $flag): bool { |
|
| 218 | - return (($flag & $this->getFlags()) === $flag); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - /** |
|
| 222 | - * should be called/used only during migration/upgrade. |
|
| 223 | - * link to an old config key. |
|
| 224 | - * |
|
| 225 | - * @return string|null not NULL if value can be imported from a previous key |
|
| 226 | - * @experimental 32.0.0 |
|
| 227 | - */ |
|
| 228 | - public function getRename(): ?string { |
|
| 229 | - return $this->rename; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - /** |
|
| 233 | - * @experimental 32.0.0 |
|
| 234 | - * @return bool TRUE if $option was set during the creation of the entry. |
|
| 235 | - */ |
|
| 236 | - public function hasOption(int $option): bool { |
|
| 237 | - return (($option & $this->options) !== 0); |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - /** |
|
| 241 | - * returns if config key is set as deprecated |
|
| 242 | - * |
|
| 243 | - * @return bool TRUE if config si deprecated |
|
| 244 | - * @experimental 31.0.0 |
|
| 245 | - */ |
|
| 246 | - public function isDeprecated(): bool { |
|
| 247 | - return $this->deprecated; |
|
| 248 | - } |
|
| 21 | + /** @experimental 32.0.0 */ |
|
| 22 | + public const RENAME_INVERT_BOOLEAN = 1; |
|
| 23 | + |
|
| 24 | + private string $definition = ''; |
|
| 25 | + private ?string $default = null; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * @param string $key config key, can only contain alphanumerical chars and -._ |
|
| 29 | + * @param ValueType $type type of config value |
|
| 30 | + * @param string $definition optional description of config key available when using occ command |
|
| 31 | + * @param bool $lazy set config value as lazy |
|
| 32 | + * @param int $flags set flags |
|
| 33 | + * @param string|null $rename previous config key to migrate config value from |
|
| 34 | + * @param bool $deprecated set config key as deprecated |
|
| 35 | + * |
|
| 36 | + * @experimental 31.0.0 |
|
| 37 | + * @psalm-suppress PossiblyInvalidCast |
|
| 38 | + * @psalm-suppress RiskyCast |
|
| 39 | + */ |
|
| 40 | + public function __construct( |
|
| 41 | + private readonly string $key, |
|
| 42 | + private readonly ValueType $type, |
|
| 43 | + private null|string|int|float|bool|array|Closure $defaultRaw = null, |
|
| 44 | + string $definition = '', |
|
| 45 | + private readonly bool $lazy = false, |
|
| 46 | + private readonly int $flags = 0, |
|
| 47 | + private readonly bool $deprecated = false, |
|
| 48 | + private readonly ?string $rename = null, |
|
| 49 | + private readonly int $options = 0, |
|
| 50 | + ) { |
|
| 51 | + // key can only contain alphanumeric chars and underscore "_" |
|
| 52 | + if (preg_match('/[^[:alnum:]_]/', $key)) { |
|
| 53 | + throw new \Exception('invalid config key'); |
|
| 54 | + } |
|
| 55 | + |
|
| 56 | + /** @psalm-suppress UndefinedClass */ |
|
| 57 | + if (\OC::$CLI) { // only store definition if ran from CLI |
|
| 58 | + $this->definition = $definition; |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * returns the config key |
|
| 64 | + * |
|
| 65 | + * @return string config key |
|
| 66 | + * @experimental 31.0.0 |
|
| 67 | + */ |
|
| 68 | + public function getKey(): string { |
|
| 69 | + return $this->key; |
|
| 70 | + } |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * get expected type for config value |
|
| 74 | + * |
|
| 75 | + * @return ValueType |
|
| 76 | + * @experimental 31.0.0 |
|
| 77 | + */ |
|
| 78 | + public function getValueType(): ValueType { |
|
| 79 | + return $this->type; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * @param string $default |
|
| 84 | + * @return string |
|
| 85 | + * @experimental 31.0.0 |
|
| 86 | + */ |
|
| 87 | + private function convertFromString(string $default): string { |
|
| 88 | + return $default; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + /** |
|
| 92 | + * @param int $default |
|
| 93 | + * @return string |
|
| 94 | + * @experimental 31.0.0 |
|
| 95 | + */ |
|
| 96 | + private function convertFromInt(int $default): string { |
|
| 97 | + return (string)$default; |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * @param float $default |
|
| 102 | + * @return string |
|
| 103 | + * @experimental 31.0.0 |
|
| 104 | + */ |
|
| 105 | + private function convertFromFloat(float $default): string { |
|
| 106 | + return (string)$default; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @param bool $default |
|
| 111 | + * @return string |
|
| 112 | + * @experimental 31.0.0 |
|
| 113 | + */ |
|
| 114 | + private function convertFromBool(bool $default): string { |
|
| 115 | + return ($default) ? '1' : '0'; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * @param array $default |
|
| 120 | + * @return string |
|
| 121 | + * @experimental 31.0.0 |
|
| 122 | + */ |
|
| 123 | + private function convertFromArray(array $default): string { |
|
| 124 | + return json_encode($default); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * returns default value |
|
| 129 | + * |
|
| 130 | + * @return string|null NULL if no default is set |
|
| 131 | + * @experimental 31.0.0 |
|
| 132 | + */ |
|
| 133 | + public function getDefault(Preset $preset): ?string { |
|
| 134 | + if ($this->default !== null) { |
|
| 135 | + return $this->default; |
|
| 136 | + } |
|
| 137 | + |
|
| 138 | + if ($this->defaultRaw === null) { |
|
| 139 | + return null; |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + if ($this->defaultRaw instanceof Closure) { |
|
| 143 | + /** @psalm-suppress MixedAssignment we expect closure to returns string|int|float|bool|array */ |
|
| 144 | + $this->defaultRaw = ($this->defaultRaw)($preset); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + /** @psalm-suppress MixedArgument closure should be managed previously */ |
|
| 148 | + $this->default = $this->convertToString($this->defaultRaw); |
|
| 149 | + |
|
| 150 | + return $this->default; |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + /** |
|
| 154 | + * convert $entry into string, based on the expected type for config value |
|
| 155 | + * |
|
| 156 | + * @param string|int|float|bool|array $entry |
|
| 157 | + * |
|
| 158 | + * @return string |
|
| 159 | + * @experimental 31.0.0 |
|
| 160 | + * @psalm-suppress PossiblyInvalidCast arrays are managed pre-cast |
|
| 161 | + * @psalm-suppress RiskyCast |
|
| 162 | + */ |
|
| 163 | + public function convertToString(string|int|float|bool|array $entry): string { |
|
| 164 | + // in case $default is array but is not expected to be an array... |
|
| 165 | + if ($this->getValueType() !== ValueType::ARRAY && is_array($entry)) { |
|
| 166 | + $entry = json_encode($entry, JSON_THROW_ON_ERROR); |
|
| 167 | + } |
|
| 168 | + |
|
| 169 | + return match ($this->getValueType()) { |
|
| 170 | + ValueType::MIXED => (string)$entry, |
|
| 171 | + ValueType::STRING => $this->convertFromString((string)$entry), |
|
| 172 | + ValueType::INT => $this->convertFromInt((int)$entry), |
|
| 173 | + ValueType::FLOAT => $this->convertFromFloat((float)$entry), |
|
| 174 | + ValueType::BOOL => $this->convertFromBool((bool)$entry), |
|
| 175 | + ValueType::ARRAY => $this->convertFromArray((array)$entry) |
|
| 176 | + }; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * returns definition |
|
| 181 | + * |
|
| 182 | + * @return string |
|
| 183 | + * @experimental 31.0.0 |
|
| 184 | + */ |
|
| 185 | + public function getDefinition(): string { |
|
| 186 | + return $this->definition; |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * returns if config key is set as lazy |
|
| 191 | + * |
|
| 192 | + * @see IAppConfig for details on lazy config values |
|
| 193 | + * @return bool TRUE if config value is lazy |
|
| 194 | + * @experimental 31.0.0 |
|
| 195 | + */ |
|
| 196 | + public function isLazy(): bool { |
|
| 197 | + return $this->lazy; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * returns flags |
|
| 202 | + * |
|
| 203 | + * @see IAppConfig for details on sensitive config values |
|
| 204 | + * @return int bitflag about the config value |
|
| 205 | + * @experimental 31.0.0 |
|
| 206 | + */ |
|
| 207 | + public function getFlags(): int { |
|
| 208 | + return $this->flags; |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + /** |
|
| 212 | + * @param int $flag |
|
| 213 | + * |
|
| 214 | + * @return bool TRUE is config value bitflag contains $flag |
|
| 215 | + * @experimental 31.0.0 |
|
| 216 | + */ |
|
| 217 | + public function isFlagged(int $flag): bool { |
|
| 218 | + return (($flag & $this->getFlags()) === $flag); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + /** |
|
| 222 | + * should be called/used only during migration/upgrade. |
|
| 223 | + * link to an old config key. |
|
| 224 | + * |
|
| 225 | + * @return string|null not NULL if value can be imported from a previous key |
|
| 226 | + * @experimental 32.0.0 |
|
| 227 | + */ |
|
| 228 | + public function getRename(): ?string { |
|
| 229 | + return $this->rename; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + /** |
|
| 233 | + * @experimental 32.0.0 |
|
| 234 | + * @return bool TRUE if $option was set during the creation of the entry. |
|
| 235 | + */ |
|
| 236 | + public function hasOption(int $option): bool { |
|
| 237 | + return (($option & $this->options) !== 0); |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + /** |
|
| 241 | + * returns if config key is set as deprecated |
|
| 242 | + * |
|
| 243 | + * @return bool TRUE if config si deprecated |
|
| 244 | + * @experimental 31.0.0 |
|
| 245 | + */ |
|
| 246 | + public function isDeprecated(): bool { |
|
| 247 | + return $this->deprecated; |
|
| 248 | + } |
|
| 249 | 249 | } |
@@ -49,1712 +49,1712 @@ |
||
| 49 | 49 | * @since 29.0.0 - Supporting types and lazy loading |
| 50 | 50 | */ |
| 51 | 51 | class AppConfig implements IAppConfig { |
| 52 | - private const APP_MAX_LENGTH = 32; |
|
| 53 | - private const KEY_MAX_LENGTH = 64; |
|
| 54 | - private const ENCRYPTION_PREFIX = '$AppConfigEncryption$'; |
|
| 55 | - private const ENCRYPTION_PREFIX_LENGTH = 21; // strlen(self::ENCRYPTION_PREFIX) |
|
| 56 | - |
|
| 57 | - /** @var array<string, array<string, mixed>> ['app_id' => ['config_key' => 'config_value']] */ |
|
| 58 | - private array $fastCache = []; // cache for normal config keys |
|
| 59 | - /** @var array<string, array<string, mixed>> ['app_id' => ['config_key' => 'config_value']] */ |
|
| 60 | - private array $lazyCache = []; // cache for lazy config keys |
|
| 61 | - /** @var array<string, array<string, int>> ['app_id' => ['config_key' => bitflag]] */ |
|
| 62 | - private array $valueTypes = []; // type for all config values |
|
| 63 | - private bool $fastLoaded = false; |
|
| 64 | - private bool $lazyLoaded = false; |
|
| 65 | - /** @var array<string, array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ |
|
| 66 | - private array $configLexiconDetails = []; |
|
| 67 | - private bool $ignoreLexiconAliases = false; |
|
| 68 | - private ?Preset $configLexiconPreset = null; |
|
| 69 | - /** @var ?array<string, string> */ |
|
| 70 | - private ?array $appVersionsCache = null; |
|
| 71 | - |
|
| 72 | - public function __construct( |
|
| 73 | - protected IDBConnection $connection, |
|
| 74 | - protected IConfig $config, |
|
| 75 | - protected LoggerInterface $logger, |
|
| 76 | - protected ICrypto $crypto, |
|
| 77 | - ) { |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @inheritDoc |
|
| 82 | - * |
|
| 83 | - * @return list<string> list of app ids |
|
| 84 | - * @since 7.0.0 |
|
| 85 | - */ |
|
| 86 | - public function getApps(): array { |
|
| 87 | - $this->loadConfigAll(); |
|
| 88 | - $apps = array_merge(array_keys($this->fastCache), array_keys($this->lazyCache)); |
|
| 89 | - sort($apps); |
|
| 90 | - |
|
| 91 | - return array_values(array_unique($apps)); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * @inheritDoc |
|
| 96 | - * |
|
| 97 | - * @param string $app id of the app |
|
| 98 | - * |
|
| 99 | - * @return list<string> list of stored config keys |
|
| 100 | - * @since 29.0.0 |
|
| 101 | - */ |
|
| 102 | - public function getKeys(string $app): array { |
|
| 103 | - $this->assertParams($app); |
|
| 104 | - $this->loadConfigAll($app); |
|
| 105 | - $keys = array_merge(array_keys($this->fastCache[$app] ?? []), array_keys($this->lazyCache[$app] ?? [])); |
|
| 106 | - sort($keys); |
|
| 107 | - |
|
| 108 | - return array_values(array_unique($keys)); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - /** |
|
| 112 | - * @inheritDoc |
|
| 113 | - * |
|
| 114 | - * @param string $app id of the app |
|
| 115 | - * @param string $key config key |
|
| 116 | - * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 117 | - * |
|
| 118 | - * @return bool TRUE if key exists |
|
| 119 | - * @since 7.0.0 |
|
| 120 | - * @since 29.0.0 Added the $lazy argument |
|
| 121 | - */ |
|
| 122 | - public function hasKey(string $app, string $key, ?bool $lazy = false): bool { |
|
| 123 | - $this->assertParams($app, $key); |
|
| 124 | - $this->loadConfig($app, $lazy); |
|
| 125 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 126 | - |
|
| 127 | - if ($lazy === null) { |
|
| 128 | - $appCache = $this->getAllValues($app); |
|
| 129 | - return isset($appCache[$key]); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - if ($lazy) { |
|
| 133 | - return isset($this->lazyCache[$app][$key]); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return isset($this->fastCache[$app][$key]); |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - /** |
|
| 140 | - * @param string $app id of the app |
|
| 141 | - * @param string $key config key |
|
| 142 | - * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 143 | - * |
|
| 144 | - * @return bool |
|
| 145 | - * @throws AppConfigUnknownKeyException if config key is not known |
|
| 146 | - * @since 29.0.0 |
|
| 147 | - */ |
|
| 148 | - public function isSensitive(string $app, string $key, ?bool $lazy = false): bool { |
|
| 149 | - $this->assertParams($app, $key); |
|
| 150 | - $this->loadConfig(null, $lazy); |
|
| 151 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 152 | - |
|
| 153 | - if (!isset($this->valueTypes[$app][$key])) { |
|
| 154 | - throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return $this->isTyped(self::VALUE_SENSITIVE, $this->valueTypes[$app][$key]); |
|
| 158 | - } |
|
| 159 | - |
|
| 160 | - /** |
|
| 161 | - * @inheritDoc |
|
| 162 | - * |
|
| 163 | - * @param string $app if of the app |
|
| 164 | - * @param string $key config key |
|
| 165 | - * |
|
| 166 | - * @return bool TRUE if config is lazy loaded |
|
| 167 | - * @throws AppConfigUnknownKeyException if config key is not known |
|
| 168 | - * @see IAppConfig for details about lazy loading |
|
| 169 | - * @since 29.0.0 |
|
| 170 | - */ |
|
| 171 | - public function isLazy(string $app, string $key): bool { |
|
| 172 | - $this->assertParams($app, $key); |
|
| 173 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 174 | - |
|
| 175 | - // there is a huge probability the non-lazy config are already loaded |
|
| 176 | - if ($this->hasKey($app, $key, false)) { |
|
| 177 | - return false; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - // key not found, we search in the lazy config |
|
| 181 | - if ($this->hasKey($app, $key, true)) { |
|
| 182 | - return true; |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 186 | - } |
|
| 187 | - |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * @inheritDoc |
|
| 191 | - * |
|
| 192 | - * @param string $app id of the app |
|
| 193 | - * @param string $prefix config keys prefix to search |
|
| 194 | - * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 195 | - * |
|
| 196 | - * @return array<string, string|int|float|bool|array> [configKey => configValue] |
|
| 197 | - * @since 29.0.0 |
|
| 198 | - */ |
|
| 199 | - public function getAllValues(string $app, string $prefix = '', bool $filtered = false): array { |
|
| 200 | - $this->assertParams($app, $prefix); |
|
| 201 | - // if we want to filter values, we need to get sensitivity |
|
| 202 | - $this->loadConfigAll($app); |
|
| 203 | - // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 204 | - $values = $this->formatAppValues($app, ($this->fastCache[$app] ?? []) + ($this->lazyCache[$app] ?? [])); |
|
| 205 | - $values = array_filter( |
|
| 206 | - $values, |
|
| 207 | - function (string $key) use ($prefix): bool { |
|
| 208 | - return str_starts_with($key, $prefix); // filter values based on $prefix |
|
| 209 | - }, ARRAY_FILTER_USE_KEY |
|
| 210 | - ); |
|
| 211 | - |
|
| 212 | - if (!$filtered) { |
|
| 213 | - return $values; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * Using the old (deprecated) list of sensitive values. |
|
| 218 | - */ |
|
| 219 | - foreach ($this->getSensitiveKeys($app) as $sensitiveKeyExp) { |
|
| 220 | - $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values)); |
|
| 221 | - foreach ($sensitiveKeys as $sensitiveKey) { |
|
| 222 | - $this->valueTypes[$app][$sensitiveKey] = ($this->valueTypes[$app][$sensitiveKey] ?? 0) | self::VALUE_SENSITIVE; |
|
| 223 | - } |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - $result = []; |
|
| 227 | - foreach ($values as $key => $value) { |
|
| 228 | - $result[$key] = $this->isTyped(self::VALUE_SENSITIVE, $this->valueTypes[$app][$key] ?? 0) ? IConfig::SENSITIVE_VALUE : $value; |
|
| 229 | - } |
|
| 230 | - |
|
| 231 | - return $result; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * @inheritDoc |
|
| 236 | - * |
|
| 237 | - * @param string $key config key |
|
| 238 | - * @param bool $lazy search within lazy loaded config |
|
| 239 | - * @param int|null $typedAs enforce type for the returned values ({@see self::VALUE_STRING} and others) |
|
| 240 | - * |
|
| 241 | - * @return array<string, string|int|float|bool|array> [appId => configValue] |
|
| 242 | - * @since 29.0.0 |
|
| 243 | - */ |
|
| 244 | - public function searchValues(string $key, bool $lazy = false, ?int $typedAs = null): array { |
|
| 245 | - $this->assertParams('', $key, true); |
|
| 246 | - $this->loadConfig(null, $lazy); |
|
| 247 | - |
|
| 248 | - /** @var array<array-key, array<array-key, mixed>> $cache */ |
|
| 249 | - if ($lazy) { |
|
| 250 | - $cache = $this->lazyCache; |
|
| 251 | - } else { |
|
| 252 | - $cache = $this->fastCache; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - $values = []; |
|
| 256 | - foreach (array_keys($cache) as $app) { |
|
| 257 | - if (isset($cache[$app][$key])) { |
|
| 258 | - $values[$app] = $this->convertTypedValue($cache[$app][$key], $typedAs ?? $this->getValueType((string)$app, $key, $lazy)); |
|
| 259 | - } |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - return $values; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - |
|
| 266 | - /** |
|
| 267 | - * Get the config value as string. |
|
| 268 | - * If the value does not exist the given default will be returned. |
|
| 269 | - * |
|
| 270 | - * Set lazy to `null` to ignore it and get the value from either source. |
|
| 271 | - * |
|
| 272 | - * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type. |
|
| 273 | - * |
|
| 274 | - * @param string $app id of the app |
|
| 275 | - * @param string $key config key |
|
| 276 | - * @param string $default config value |
|
| 277 | - * @param null|bool $lazy get config as lazy loaded or not. can be NULL |
|
| 278 | - * |
|
| 279 | - * @return string the value or $default |
|
| 280 | - * @internal |
|
| 281 | - * @since 29.0.0 |
|
| 282 | - * @see IAppConfig for explanation about lazy loading |
|
| 283 | - * @see getValueString() |
|
| 284 | - * @see getValueInt() |
|
| 285 | - * @see getValueFloat() |
|
| 286 | - * @see getValueBool() |
|
| 287 | - * @see getValueArray() |
|
| 288 | - */ |
|
| 289 | - public function getValueMixed( |
|
| 290 | - string $app, |
|
| 291 | - string $key, |
|
| 292 | - string $default = '', |
|
| 293 | - ?bool $lazy = false, |
|
| 294 | - ): string { |
|
| 295 | - try { |
|
| 296 | - $lazy = ($lazy === null) ? $this->isLazy($app, $key) : $lazy; |
|
| 297 | - } catch (AppConfigUnknownKeyException) { |
|
| 298 | - return $default; |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - return $this->getTypedValue( |
|
| 302 | - $app, |
|
| 303 | - $key, |
|
| 304 | - $default, |
|
| 305 | - $lazy, |
|
| 306 | - self::VALUE_MIXED |
|
| 307 | - ); |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - /** |
|
| 311 | - * @inheritDoc |
|
| 312 | - * |
|
| 313 | - * @param string $app id of the app |
|
| 314 | - * @param string $key config key |
|
| 315 | - * @param string $default default value |
|
| 316 | - * @param bool $lazy search within lazy loaded config |
|
| 317 | - * |
|
| 318 | - * @return string stored config value or $default if not set in database |
|
| 319 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 320 | - * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 321 | - * @since 29.0.0 |
|
| 322 | - * @see IAppConfig for explanation about lazy loading |
|
| 323 | - */ |
|
| 324 | - public function getValueString( |
|
| 325 | - string $app, |
|
| 326 | - string $key, |
|
| 327 | - string $default = '', |
|
| 328 | - bool $lazy = false, |
|
| 329 | - ): string { |
|
| 330 | - return $this->getTypedValue($app, $key, $default, $lazy, self::VALUE_STRING); |
|
| 331 | - } |
|
| 332 | - |
|
| 333 | - /** |
|
| 334 | - * @inheritDoc |
|
| 335 | - * |
|
| 336 | - * @param string $app id of the app |
|
| 337 | - * @param string $key config key |
|
| 338 | - * @param int $default default value |
|
| 339 | - * @param bool $lazy search within lazy loaded config |
|
| 340 | - * |
|
| 341 | - * @return int stored config value or $default if not set in database |
|
| 342 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 343 | - * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 344 | - * @since 29.0.0 |
|
| 345 | - * @see IAppConfig for explanation about lazy loading |
|
| 346 | - */ |
|
| 347 | - public function getValueInt( |
|
| 348 | - string $app, |
|
| 349 | - string $key, |
|
| 350 | - int $default = 0, |
|
| 351 | - bool $lazy = false, |
|
| 352 | - ): int { |
|
| 353 | - return (int)$this->getTypedValue($app, $key, (string)$default, $lazy, self::VALUE_INT); |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - /** |
|
| 357 | - * @inheritDoc |
|
| 358 | - * |
|
| 359 | - * @param string $app id of the app |
|
| 360 | - * @param string $key config key |
|
| 361 | - * @param float $default default value |
|
| 362 | - * @param bool $lazy search within lazy loaded config |
|
| 363 | - * |
|
| 364 | - * @return float stored config value or $default if not set in database |
|
| 365 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 366 | - * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 367 | - * @since 29.0.0 |
|
| 368 | - * @see IAppConfig for explanation about lazy loading |
|
| 369 | - */ |
|
| 370 | - public function getValueFloat(string $app, string $key, float $default = 0, bool $lazy = false): float { |
|
| 371 | - return (float)$this->getTypedValue($app, $key, (string)$default, $lazy, self::VALUE_FLOAT); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - /** |
|
| 375 | - * @inheritDoc |
|
| 376 | - * |
|
| 377 | - * @param string $app id of the app |
|
| 378 | - * @param string $key config key |
|
| 379 | - * @param bool $default default value |
|
| 380 | - * @param bool $lazy search within lazy loaded config |
|
| 381 | - * |
|
| 382 | - * @return bool stored config value or $default if not set in database |
|
| 383 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 384 | - * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 385 | - * @since 29.0.0 |
|
| 386 | - * @see IAppConfig for explanation about lazy loading |
|
| 387 | - */ |
|
| 388 | - public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool { |
|
| 389 | - $b = strtolower($this->getTypedValue($app, $key, $default ? 'true' : 'false', $lazy, self::VALUE_BOOL)); |
|
| 390 | - return in_array($b, ['1', 'true', 'yes', 'on']); |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * @inheritDoc |
|
| 395 | - * |
|
| 396 | - * @param string $app id of the app |
|
| 397 | - * @param string $key config key |
|
| 398 | - * @param array $default default value |
|
| 399 | - * @param bool $lazy search within lazy loaded config |
|
| 400 | - * |
|
| 401 | - * @return array stored config value or $default if not set in database |
|
| 402 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 403 | - * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 404 | - * @since 29.0.0 |
|
| 405 | - * @see IAppConfig for explanation about lazy loading |
|
| 406 | - */ |
|
| 407 | - public function getValueArray( |
|
| 408 | - string $app, |
|
| 409 | - string $key, |
|
| 410 | - array $default = [], |
|
| 411 | - bool $lazy = false, |
|
| 412 | - ): array { |
|
| 413 | - try { |
|
| 414 | - $defaultJson = json_encode($default, JSON_THROW_ON_ERROR); |
|
| 415 | - $value = json_decode($this->getTypedValue($app, $key, $defaultJson, $lazy, self::VALUE_ARRAY), true, flags: JSON_THROW_ON_ERROR); |
|
| 416 | - |
|
| 417 | - return is_array($value) ? $value : []; |
|
| 418 | - } catch (JsonException) { |
|
| 419 | - return []; |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * @param string $app id of the app |
|
| 425 | - * @param string $key config key |
|
| 426 | - * @param string $default default value |
|
| 427 | - * @param bool $lazy search within lazy loaded config |
|
| 428 | - * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT}{@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 429 | - * |
|
| 430 | - * @return string |
|
| 431 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 432 | - * @throws InvalidArgumentException |
|
| 433 | - */ |
|
| 434 | - private function getTypedValue( |
|
| 435 | - string $app, |
|
| 436 | - string $key, |
|
| 437 | - string $default, |
|
| 438 | - bool $lazy, |
|
| 439 | - int $type, |
|
| 440 | - ): string { |
|
| 441 | - $this->assertParams($app, $key, valueType: $type); |
|
| 442 | - $origKey = $key; |
|
| 443 | - $matched = $this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type, $default); |
|
| 444 | - if ($default === null) { |
|
| 445 | - // there is no logical reason for it to be null |
|
| 446 | - throw new \Exception('default cannot be null'); |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - // returns default if strictness of lexicon is set to WARNING (block and report) |
|
| 450 | - if (!$matched) { |
|
| 451 | - return $default; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - $this->loadConfig($app, $lazy); |
|
| 455 | - |
|
| 456 | - /** |
|
| 457 | - * We ignore check if mixed type is requested. |
|
| 458 | - * If type of stored value is set as mixed, we don't filter. |
|
| 459 | - * If type of stored value is defined, we compare with the one requested. |
|
| 460 | - */ |
|
| 461 | - $knownType = $this->valueTypes[$app][$key] ?? 0; |
|
| 462 | - if (!$this->isTyped(self::VALUE_MIXED, $type) |
|
| 463 | - && $knownType > 0 |
|
| 464 | - && !$this->isTyped(self::VALUE_MIXED, $knownType) |
|
| 465 | - && !$this->isTyped($type, $knownType)) { |
|
| 466 | - $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]); |
|
| 467 | - throw new AppConfigTypeConflictException('conflict with value type from database'); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * - the pair $app/$key cannot exist in both array, |
|
| 472 | - * - we should still return an existing non-lazy value even if current method |
|
| 473 | - * is called with $lazy is true |
|
| 474 | - * |
|
| 475 | - * This way, lazyCache will be empty until the load for lazy config value is requested. |
|
| 476 | - */ |
|
| 477 | - if (isset($this->lazyCache[$app][$key])) { |
|
| 478 | - $value = $this->lazyCache[$app][$key]; |
|
| 479 | - } elseif (isset($this->fastCache[$app][$key])) { |
|
| 480 | - $value = $this->fastCache[$app][$key]; |
|
| 481 | - } else { |
|
| 482 | - return $default; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - $sensitive = $this->isTyped(self::VALUE_SENSITIVE, $knownType); |
|
| 486 | - if ($sensitive && str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 487 | - // Only decrypt values that are stored encrypted |
|
| 488 | - $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 489 | - } |
|
| 490 | - |
|
| 491 | - // in case the key was modified while running matchAndApplyLexiconDefinition() we are |
|
| 492 | - // interested to check options in case a modification of the value is needed |
|
| 493 | - // ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN |
|
| 494 | - if ($origKey !== $key && $type === self::VALUE_BOOL) { |
|
| 495 | - $configManager = Server::get(ConfigManager::class); |
|
| 496 | - $value = ($configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0'; |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - return $value; |
|
| 500 | - } |
|
| 501 | - |
|
| 502 | - /** |
|
| 503 | - * @inheritDoc |
|
| 504 | - * |
|
| 505 | - * @param string $app id of the app |
|
| 506 | - * @param string $key config key |
|
| 507 | - * |
|
| 508 | - * @return int type of the value |
|
| 509 | - * @throws AppConfigUnknownKeyException if config key is not known |
|
| 510 | - * @since 29.0.0 |
|
| 511 | - * @see VALUE_STRING |
|
| 512 | - * @see VALUE_INT |
|
| 513 | - * @see VALUE_FLOAT |
|
| 514 | - * @see VALUE_BOOL |
|
| 515 | - * @see VALUE_ARRAY |
|
| 516 | - */ |
|
| 517 | - public function getValueType(string $app, string $key, ?bool $lazy = null): int { |
|
| 518 | - $type = self::VALUE_MIXED; |
|
| 519 | - $ignorable = $lazy ?? false; |
|
| 520 | - $this->matchAndApplyLexiconDefinition($app, $key, $ignorable, $type); |
|
| 521 | - if ($type !== self::VALUE_MIXED) { |
|
| 522 | - // a modified $type means config key is set in Lexicon |
|
| 523 | - return $type; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - $this->assertParams($app, $key); |
|
| 527 | - $this->loadConfig($app, $lazy); |
|
| 528 | - |
|
| 529 | - if (!isset($this->valueTypes[$app][$key])) { |
|
| 530 | - throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 531 | - } |
|
| 532 | - |
|
| 533 | - $type = $this->valueTypes[$app][$key]; |
|
| 534 | - $type &= ~self::VALUE_SENSITIVE; |
|
| 535 | - return $type; |
|
| 536 | - } |
|
| 537 | - |
|
| 538 | - |
|
| 539 | - /** |
|
| 540 | - * Store a config key and its value in database as VALUE_MIXED |
|
| 541 | - * |
|
| 542 | - * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type |
|
| 543 | - * |
|
| 544 | - * @param string $app id of the app |
|
| 545 | - * @param string $key config key |
|
| 546 | - * @param string $value config value |
|
| 547 | - * @param bool $lazy set config as lazy loaded |
|
| 548 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 549 | - * |
|
| 550 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 551 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED |
|
| 552 | - * @internal |
|
| 553 | - * @since 29.0.0 |
|
| 554 | - * @see IAppConfig for explanation about lazy loading |
|
| 555 | - * @see setValueString() |
|
| 556 | - * @see setValueInt() |
|
| 557 | - * @see setValueFloat() |
|
| 558 | - * @see setValueBool() |
|
| 559 | - * @see setValueArray() |
|
| 560 | - */ |
|
| 561 | - public function setValueMixed( |
|
| 562 | - string $app, |
|
| 563 | - string $key, |
|
| 564 | - string $value, |
|
| 565 | - bool $lazy = false, |
|
| 566 | - bool $sensitive = false, |
|
| 567 | - ): bool { |
|
| 568 | - return $this->setTypedValue( |
|
| 569 | - $app, |
|
| 570 | - $key, |
|
| 571 | - $value, |
|
| 572 | - $lazy, |
|
| 573 | - self::VALUE_MIXED | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 574 | - ); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - |
|
| 578 | - /** |
|
| 579 | - * @inheritDoc |
|
| 580 | - * |
|
| 581 | - * @param string $app id of the app |
|
| 582 | - * @param string $key config key |
|
| 583 | - * @param string $value config value |
|
| 584 | - * @param bool $lazy set config as lazy loaded |
|
| 585 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 586 | - * |
|
| 587 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 588 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 589 | - * @since 29.0.0 |
|
| 590 | - * @see IAppConfig for explanation about lazy loading |
|
| 591 | - */ |
|
| 592 | - public function setValueString( |
|
| 593 | - string $app, |
|
| 594 | - string $key, |
|
| 595 | - string $value, |
|
| 596 | - bool $lazy = false, |
|
| 597 | - bool $sensitive = false, |
|
| 598 | - ): bool { |
|
| 599 | - return $this->setTypedValue( |
|
| 600 | - $app, |
|
| 601 | - $key, |
|
| 602 | - $value, |
|
| 603 | - $lazy, |
|
| 604 | - self::VALUE_STRING | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 605 | - ); |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - /** |
|
| 609 | - * @inheritDoc |
|
| 610 | - * |
|
| 611 | - * @param string $app id of the app |
|
| 612 | - * @param string $key config key |
|
| 613 | - * @param int $value config value |
|
| 614 | - * @param bool $lazy set config as lazy loaded |
|
| 615 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 616 | - * |
|
| 617 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 618 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 619 | - * @since 29.0.0 |
|
| 620 | - * @see IAppConfig for explanation about lazy loading |
|
| 621 | - */ |
|
| 622 | - public function setValueInt( |
|
| 623 | - string $app, |
|
| 624 | - string $key, |
|
| 625 | - int $value, |
|
| 626 | - bool $lazy = false, |
|
| 627 | - bool $sensitive = false, |
|
| 628 | - ): bool { |
|
| 629 | - if ($value > 2000000000) { |
|
| 630 | - $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.'); |
|
| 631 | - } |
|
| 632 | - |
|
| 633 | - return $this->setTypedValue( |
|
| 634 | - $app, |
|
| 635 | - $key, |
|
| 636 | - (string)$value, |
|
| 637 | - $lazy, |
|
| 638 | - self::VALUE_INT | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 639 | - ); |
|
| 640 | - } |
|
| 641 | - |
|
| 642 | - /** |
|
| 643 | - * @inheritDoc |
|
| 644 | - * |
|
| 645 | - * @param string $app id of the app |
|
| 646 | - * @param string $key config key |
|
| 647 | - * @param float $value config value |
|
| 648 | - * @param bool $lazy set config as lazy loaded |
|
| 649 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 650 | - * |
|
| 651 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 652 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 653 | - * @since 29.0.0 |
|
| 654 | - * @see IAppConfig for explanation about lazy loading |
|
| 655 | - */ |
|
| 656 | - public function setValueFloat( |
|
| 657 | - string $app, |
|
| 658 | - string $key, |
|
| 659 | - float $value, |
|
| 660 | - bool $lazy = false, |
|
| 661 | - bool $sensitive = false, |
|
| 662 | - ): bool { |
|
| 663 | - return $this->setTypedValue( |
|
| 664 | - $app, |
|
| 665 | - $key, |
|
| 666 | - (string)$value, |
|
| 667 | - $lazy, |
|
| 668 | - self::VALUE_FLOAT | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 669 | - ); |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - /** |
|
| 673 | - * @inheritDoc |
|
| 674 | - * |
|
| 675 | - * @param string $app id of the app |
|
| 676 | - * @param string $key config key |
|
| 677 | - * @param bool $value config value |
|
| 678 | - * @param bool $lazy set config as lazy loaded |
|
| 679 | - * |
|
| 680 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 681 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 682 | - * @since 29.0.0 |
|
| 683 | - * @see IAppConfig for explanation about lazy loading |
|
| 684 | - */ |
|
| 685 | - public function setValueBool( |
|
| 686 | - string $app, |
|
| 687 | - string $key, |
|
| 688 | - bool $value, |
|
| 689 | - bool $lazy = false, |
|
| 690 | - ): bool { |
|
| 691 | - return $this->setTypedValue( |
|
| 692 | - $app, |
|
| 693 | - $key, |
|
| 694 | - ($value) ? '1' : '0', |
|
| 695 | - $lazy, |
|
| 696 | - self::VALUE_BOOL |
|
| 697 | - ); |
|
| 698 | - } |
|
| 699 | - |
|
| 700 | - /** |
|
| 701 | - * @inheritDoc |
|
| 702 | - * |
|
| 703 | - * @param string $app id of the app |
|
| 704 | - * @param string $key config key |
|
| 705 | - * @param array $value config value |
|
| 706 | - * @param bool $lazy set config as lazy loaded |
|
| 707 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 708 | - * |
|
| 709 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 710 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 711 | - * @throws JsonException |
|
| 712 | - * @since 29.0.0 |
|
| 713 | - * @see IAppConfig for explanation about lazy loading |
|
| 714 | - */ |
|
| 715 | - public function setValueArray( |
|
| 716 | - string $app, |
|
| 717 | - string $key, |
|
| 718 | - array $value, |
|
| 719 | - bool $lazy = false, |
|
| 720 | - bool $sensitive = false, |
|
| 721 | - ): bool { |
|
| 722 | - try { |
|
| 723 | - return $this->setTypedValue( |
|
| 724 | - $app, |
|
| 725 | - $key, |
|
| 726 | - json_encode($value, JSON_THROW_ON_ERROR), |
|
| 727 | - $lazy, |
|
| 728 | - self::VALUE_ARRAY | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 729 | - ); |
|
| 730 | - } catch (JsonException $e) { |
|
| 731 | - $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]); |
|
| 732 | - throw $e; |
|
| 733 | - } |
|
| 734 | - } |
|
| 735 | - |
|
| 736 | - /** |
|
| 737 | - * Store a config key and its value in database |
|
| 738 | - * |
|
| 739 | - * If config key is already known with the exact same config value and same sensitive/lazy status, the |
|
| 740 | - * database is not updated. If config value was previously stored as sensitive, status will not be |
|
| 741 | - * altered. |
|
| 742 | - * |
|
| 743 | - * @param string $app id of the app |
|
| 744 | - * @param string $key config key |
|
| 745 | - * @param string $value config value |
|
| 746 | - * @param bool $lazy config set as lazy loaded |
|
| 747 | - * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT} {@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 748 | - * |
|
| 749 | - * @return bool TRUE if value was updated in database |
|
| 750 | - * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 751 | - * @see IAppConfig for explanation about lazy loading |
|
| 752 | - */ |
|
| 753 | - private function setTypedValue( |
|
| 754 | - string $app, |
|
| 755 | - string $key, |
|
| 756 | - string $value, |
|
| 757 | - bool $lazy, |
|
| 758 | - int $type, |
|
| 759 | - ): bool { |
|
| 760 | - $this->assertParams($app, $key); |
|
| 761 | - if (!$this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type)) { |
|
| 762 | - return false; // returns false as database is not updated |
|
| 763 | - } |
|
| 764 | - $this->loadConfig(null, $lazy); |
|
| 765 | - |
|
| 766 | - $sensitive = $this->isTyped(self::VALUE_SENSITIVE, $type); |
|
| 767 | - $inserted = $refreshCache = false; |
|
| 768 | - |
|
| 769 | - $origValue = $value; |
|
| 770 | - if ($sensitive || ($this->hasKey($app, $key, $lazy) && $this->isSensitive($app, $key, $lazy))) { |
|
| 771 | - $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 772 | - } |
|
| 773 | - |
|
| 774 | - if ($this->hasKey($app, $key, $lazy)) { |
|
| 775 | - /** |
|
| 776 | - * no update if key is already known with set lazy status and value is |
|
| 777 | - * not different, unless sensitivity is switched from false to true. |
|
| 778 | - */ |
|
| 779 | - if ($origValue === $this->getTypedValue($app, $key, $value, $lazy, $type) |
|
| 780 | - && (!$sensitive || $this->isSensitive($app, $key, $lazy))) { |
|
| 781 | - return false; |
|
| 782 | - } |
|
| 783 | - } else { |
|
| 784 | - /** |
|
| 785 | - * if key is not known yet, we try to insert. |
|
| 786 | - * It might fail if the key exists with a different lazy flag. |
|
| 787 | - */ |
|
| 788 | - try { |
|
| 789 | - $insert = $this->connection->getQueryBuilder(); |
|
| 790 | - $insert->insert('appconfig') |
|
| 791 | - ->setValue('appid', $insert->createNamedParameter($app)) |
|
| 792 | - ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 793 | - ->setValue('type', $insert->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 794 | - ->setValue('configkey', $insert->createNamedParameter($key)) |
|
| 795 | - ->setValue('configvalue', $insert->createNamedParameter($value)); |
|
| 796 | - $insert->executeStatement(); |
|
| 797 | - $inserted = true; |
|
| 798 | - } catch (DBException $e) { |
|
| 799 | - if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { |
|
| 800 | - throw $e; // TODO: throw exception or just log and returns false !? |
|
| 801 | - } |
|
| 802 | - } |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - /** |
|
| 806 | - * We cannot insert a new row, meaning we need to update an already existing one |
|
| 807 | - */ |
|
| 808 | - if (!$inserted) { |
|
| 809 | - $currType = $this->valueTypes[$app][$key] ?? 0; |
|
| 810 | - if ($currType === 0) { // this might happen when switching lazy loading status |
|
| 811 | - $this->loadConfigAll(); |
|
| 812 | - $currType = $this->valueTypes[$app][$key] ?? 0; |
|
| 813 | - } |
|
| 814 | - |
|
| 815 | - /** |
|
| 816 | - * This should only happen during the upgrade process from 28 to 29. |
|
| 817 | - * We only log a warning and set it to VALUE_MIXED. |
|
| 818 | - */ |
|
| 819 | - if ($currType === 0) { |
|
| 820 | - $this->logger->warning('Value type is set to zero (0) in database. This is fine only during the upgrade process from 28 to 29.', ['app' => $app, 'key' => $key]); |
|
| 821 | - $currType = self::VALUE_MIXED; |
|
| 822 | - } |
|
| 823 | - |
|
| 824 | - /** |
|
| 825 | - * we only accept a different type from the one stored in database |
|
| 826 | - * if the one stored in database is not-defined (VALUE_MIXED) |
|
| 827 | - */ |
|
| 828 | - if (!$this->isTyped(self::VALUE_MIXED, $currType) |
|
| 829 | - && ($type | self::VALUE_SENSITIVE) !== ($currType | self::VALUE_SENSITIVE)) { |
|
| 830 | - try { |
|
| 831 | - $currType = $this->convertTypeToString($currType); |
|
| 832 | - $type = $this->convertTypeToString($type); |
|
| 833 | - } catch (AppConfigIncorrectTypeException) { |
|
| 834 | - // can be ignored, this was just needed for a better exception message. |
|
| 835 | - } |
|
| 836 | - throw new AppConfigTypeConflictException('conflict between new type (' . $type . ') and old type (' . $currType . ')'); |
|
| 837 | - } |
|
| 838 | - |
|
| 839 | - // we fix $type if the stored value, or the new value as it might be changed, is set as sensitive |
|
| 840 | - if ($sensitive || $this->isTyped(self::VALUE_SENSITIVE, $currType)) { |
|
| 841 | - $type |= self::VALUE_SENSITIVE; |
|
| 842 | - } |
|
| 843 | - |
|
| 844 | - if ($lazy !== $this->isLazy($app, $key)) { |
|
| 845 | - $refreshCache = true; |
|
| 846 | - } |
|
| 847 | - |
|
| 848 | - $update = $this->connection->getQueryBuilder(); |
|
| 849 | - $update->update('appconfig') |
|
| 850 | - ->set('configvalue', $update->createNamedParameter($value)) |
|
| 851 | - ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 852 | - ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 853 | - ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 854 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 855 | - |
|
| 856 | - $update->executeStatement(); |
|
| 857 | - } |
|
| 858 | - |
|
| 859 | - if ($refreshCache) { |
|
| 860 | - $this->clearCache(); |
|
| 861 | - return true; |
|
| 862 | - } |
|
| 863 | - |
|
| 864 | - // update local cache |
|
| 865 | - if ($lazy) { |
|
| 866 | - $this->lazyCache[$app][$key] = $value; |
|
| 867 | - } else { |
|
| 868 | - $this->fastCache[$app][$key] = $value; |
|
| 869 | - } |
|
| 870 | - $this->valueTypes[$app][$key] = $type; |
|
| 871 | - |
|
| 872 | - return true; |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - /** |
|
| 876 | - * Change the type of config value. |
|
| 877 | - * |
|
| 878 | - * **WARNING:** Method is internal and **MUST** not be used as it may break things. |
|
| 879 | - * |
|
| 880 | - * @param string $app id of the app |
|
| 881 | - * @param string $key config key |
|
| 882 | - * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT} {@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 883 | - * |
|
| 884 | - * @return bool TRUE if database update were necessary |
|
| 885 | - * @throws AppConfigUnknownKeyException if $key is now known in database |
|
| 886 | - * @throws AppConfigIncorrectTypeException if $type is not valid |
|
| 887 | - * @internal |
|
| 888 | - * @since 29.0.0 |
|
| 889 | - */ |
|
| 890 | - public function updateType(string $app, string $key, int $type = self::VALUE_MIXED): bool { |
|
| 891 | - $this->assertParams($app, $key); |
|
| 892 | - $this->loadConfigAll(); |
|
| 893 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 894 | - $this->isLazy($app, $key); // confirm key exists |
|
| 895 | - |
|
| 896 | - // type can only be one type |
|
| 897 | - if (!in_array($type, [self::VALUE_MIXED, self::VALUE_STRING, self::VALUE_INT, self::VALUE_FLOAT, self::VALUE_BOOL, self::VALUE_ARRAY])) { |
|
| 898 | - throw new AppConfigIncorrectTypeException('Unknown value type'); |
|
| 899 | - } |
|
| 900 | - |
|
| 901 | - $currType = $this->valueTypes[$app][$key]; |
|
| 902 | - if (($type | self::VALUE_SENSITIVE) === ($currType | self::VALUE_SENSITIVE)) { |
|
| 903 | - return false; |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - // we complete with sensitive flag if the stored value is set as sensitive |
|
| 907 | - if ($this->isTyped(self::VALUE_SENSITIVE, $currType)) { |
|
| 908 | - $type = $type | self::VALUE_SENSITIVE; |
|
| 909 | - } |
|
| 910 | - |
|
| 911 | - $update = $this->connection->getQueryBuilder(); |
|
| 912 | - $update->update('appconfig') |
|
| 913 | - ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 914 | - ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 915 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 916 | - $update->executeStatement(); |
|
| 917 | - $this->valueTypes[$app][$key] = $type; |
|
| 918 | - |
|
| 919 | - return true; |
|
| 920 | - } |
|
| 921 | - |
|
| 922 | - |
|
| 923 | - /** |
|
| 924 | - * @inheritDoc |
|
| 925 | - * |
|
| 926 | - * @param string $app id of the app |
|
| 927 | - * @param string $key config key |
|
| 928 | - * @param bool $sensitive TRUE to set as sensitive, FALSE to unset |
|
| 929 | - * |
|
| 930 | - * @return bool TRUE if entry was found in database and an update was necessary |
|
| 931 | - * @since 29.0.0 |
|
| 932 | - */ |
|
| 933 | - public function updateSensitive(string $app, string $key, bool $sensitive): bool { |
|
| 934 | - $this->assertParams($app, $key); |
|
| 935 | - $this->loadConfigAll(); |
|
| 936 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 937 | - |
|
| 938 | - try { |
|
| 939 | - if ($sensitive === $this->isSensitive($app, $key, null)) { |
|
| 940 | - return false; |
|
| 941 | - } |
|
| 942 | - } catch (AppConfigUnknownKeyException $e) { |
|
| 943 | - return false; |
|
| 944 | - } |
|
| 945 | - |
|
| 946 | - $lazy = $this->isLazy($app, $key); |
|
| 947 | - if ($lazy) { |
|
| 948 | - $cache = $this->lazyCache; |
|
| 949 | - } else { |
|
| 950 | - $cache = $this->fastCache; |
|
| 951 | - } |
|
| 952 | - |
|
| 953 | - if (!isset($cache[$app][$key])) { |
|
| 954 | - throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 955 | - } |
|
| 956 | - |
|
| 957 | - /** |
|
| 958 | - * type returned by getValueType() is already cleaned from sensitive flag |
|
| 959 | - * we just need to update it based on $sensitive and store it in database |
|
| 960 | - */ |
|
| 961 | - $type = $this->getValueType($app, $key); |
|
| 962 | - $value = $cache[$app][$key]; |
|
| 963 | - if ($sensitive) { |
|
| 964 | - $type |= self::VALUE_SENSITIVE; |
|
| 965 | - $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 966 | - } else { |
|
| 967 | - $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 968 | - } |
|
| 969 | - |
|
| 970 | - $update = $this->connection->getQueryBuilder(); |
|
| 971 | - $update->update('appconfig') |
|
| 972 | - ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 973 | - ->set('configvalue', $update->createNamedParameter($value)) |
|
| 974 | - ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 975 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 976 | - $update->executeStatement(); |
|
| 977 | - |
|
| 978 | - $this->valueTypes[$app][$key] = $type; |
|
| 979 | - |
|
| 980 | - return true; |
|
| 981 | - } |
|
| 982 | - |
|
| 983 | - /** |
|
| 984 | - * @inheritDoc |
|
| 985 | - * |
|
| 986 | - * @param string $app id of the app |
|
| 987 | - * @param string $key config key |
|
| 988 | - * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 989 | - * |
|
| 990 | - * @return bool TRUE if entry was found in database and an update was necessary |
|
| 991 | - * @since 29.0.0 |
|
| 992 | - */ |
|
| 993 | - public function updateLazy(string $app, string $key, bool $lazy): bool { |
|
| 994 | - $this->assertParams($app, $key); |
|
| 995 | - $this->loadConfigAll(); |
|
| 996 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 997 | - |
|
| 998 | - try { |
|
| 999 | - if ($lazy === $this->isLazy($app, $key)) { |
|
| 1000 | - return false; |
|
| 1001 | - } |
|
| 1002 | - } catch (AppConfigUnknownKeyException $e) { |
|
| 1003 | - return false; |
|
| 1004 | - } |
|
| 1005 | - |
|
| 1006 | - $update = $this->connection->getQueryBuilder(); |
|
| 1007 | - $update->update('appconfig') |
|
| 1008 | - ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1009 | - ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1010 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1011 | - $update->executeStatement(); |
|
| 1012 | - |
|
| 1013 | - // At this point, it is a lot safer to clean cache |
|
| 1014 | - $this->clearCache(); |
|
| 1015 | - |
|
| 1016 | - return true; |
|
| 1017 | - } |
|
| 1018 | - |
|
| 1019 | - /** |
|
| 1020 | - * @inheritDoc |
|
| 1021 | - * |
|
| 1022 | - * @param string $app id of the app |
|
| 1023 | - * @param string $key config key |
|
| 1024 | - * |
|
| 1025 | - * @return array |
|
| 1026 | - * @throws AppConfigUnknownKeyException if config key is not known in database |
|
| 1027 | - * @since 29.0.0 |
|
| 1028 | - */ |
|
| 1029 | - public function getDetails(string $app, string $key): array { |
|
| 1030 | - $this->assertParams($app, $key); |
|
| 1031 | - $this->loadConfigAll(); |
|
| 1032 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1033 | - $lazy = $this->isLazy($app, $key); |
|
| 1034 | - |
|
| 1035 | - if ($lazy) { |
|
| 1036 | - $cache = $this->lazyCache; |
|
| 1037 | - } else { |
|
| 1038 | - $cache = $this->fastCache; |
|
| 1039 | - } |
|
| 1040 | - |
|
| 1041 | - $type = $this->getValueType($app, $key); |
|
| 1042 | - try { |
|
| 1043 | - $typeString = $this->convertTypeToString($type); |
|
| 1044 | - } catch (AppConfigIncorrectTypeException $e) { |
|
| 1045 | - $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]); |
|
| 1046 | - $typeString = (string)$type; |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - if (!isset($cache[$app][$key])) { |
|
| 1050 | - throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 1051 | - } |
|
| 1052 | - |
|
| 1053 | - $value = $cache[$app][$key]; |
|
| 1054 | - $sensitive = $this->isSensitive($app, $key, null); |
|
| 1055 | - if ($sensitive && str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 1056 | - $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 1057 | - } |
|
| 1058 | - |
|
| 1059 | - return [ |
|
| 1060 | - 'app' => $app, |
|
| 1061 | - 'key' => $key, |
|
| 1062 | - 'value' => $value, |
|
| 1063 | - 'type' => $type, |
|
| 1064 | - 'lazy' => $lazy, |
|
| 1065 | - 'typeString' => $typeString, |
|
| 1066 | - 'sensitive' => $sensitive |
|
| 1067 | - ]; |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - /** |
|
| 1071 | - * @param string $type |
|
| 1072 | - * |
|
| 1073 | - * @return int |
|
| 1074 | - * @throws AppConfigIncorrectTypeException |
|
| 1075 | - * @since 29.0.0 |
|
| 1076 | - */ |
|
| 1077 | - public function convertTypeToInt(string $type): int { |
|
| 1078 | - return match (strtolower($type)) { |
|
| 1079 | - 'mixed' => IAppConfig::VALUE_MIXED, |
|
| 1080 | - 'string' => IAppConfig::VALUE_STRING, |
|
| 1081 | - 'integer' => IAppConfig::VALUE_INT, |
|
| 1082 | - 'float' => IAppConfig::VALUE_FLOAT, |
|
| 1083 | - 'boolean' => IAppConfig::VALUE_BOOL, |
|
| 1084 | - 'array' => IAppConfig::VALUE_ARRAY, |
|
| 1085 | - default => throw new AppConfigIncorrectTypeException('Unknown type ' . $type) |
|
| 1086 | - }; |
|
| 1087 | - } |
|
| 1088 | - |
|
| 1089 | - /** |
|
| 1090 | - * @param int $type |
|
| 1091 | - * |
|
| 1092 | - * @return string |
|
| 1093 | - * @throws AppConfigIncorrectTypeException |
|
| 1094 | - * @since 29.0.0 |
|
| 1095 | - */ |
|
| 1096 | - public function convertTypeToString(int $type): string { |
|
| 1097 | - $type &= ~self::VALUE_SENSITIVE; |
|
| 1098 | - |
|
| 1099 | - return match ($type) { |
|
| 1100 | - IAppConfig::VALUE_MIXED => 'mixed', |
|
| 1101 | - IAppConfig::VALUE_STRING => 'string', |
|
| 1102 | - IAppConfig::VALUE_INT => 'integer', |
|
| 1103 | - IAppConfig::VALUE_FLOAT => 'float', |
|
| 1104 | - IAppConfig::VALUE_BOOL => 'boolean', |
|
| 1105 | - IAppConfig::VALUE_ARRAY => 'array', |
|
| 1106 | - default => throw new AppConfigIncorrectTypeException('Unknown numeric type ' . $type) |
|
| 1107 | - }; |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - /** |
|
| 1111 | - * @inheritDoc |
|
| 1112 | - * |
|
| 1113 | - * @param string $app id of the app |
|
| 1114 | - * @param string $key config key |
|
| 1115 | - * |
|
| 1116 | - * @since 29.0.0 |
|
| 1117 | - */ |
|
| 1118 | - public function deleteKey(string $app, string $key): void { |
|
| 1119 | - $this->assertParams($app, $key); |
|
| 1120 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1121 | - |
|
| 1122 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1123 | - $qb->delete('appconfig') |
|
| 1124 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1125 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1126 | - $qb->executeStatement(); |
|
| 1127 | - |
|
| 1128 | - unset($this->lazyCache[$app][$key]); |
|
| 1129 | - unset($this->fastCache[$app][$key]); |
|
| 1130 | - unset($this->valueTypes[$app][$key]); |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - /** |
|
| 1134 | - * @inheritDoc |
|
| 1135 | - * |
|
| 1136 | - * @param string $app id of the app |
|
| 1137 | - * |
|
| 1138 | - * @since 29.0.0 |
|
| 1139 | - */ |
|
| 1140 | - public function deleteApp(string $app): void { |
|
| 1141 | - $this->assertParams($app); |
|
| 1142 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1143 | - $qb->delete('appconfig') |
|
| 1144 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 1145 | - $qb->executeStatement(); |
|
| 1146 | - |
|
| 1147 | - $this->clearCache(); |
|
| 1148 | - } |
|
| 1149 | - |
|
| 1150 | - /** |
|
| 1151 | - * @inheritDoc |
|
| 1152 | - * |
|
| 1153 | - * @param bool $reload set to TRUE to refill cache instantly after clearing it |
|
| 1154 | - * |
|
| 1155 | - * @since 29.0.0 |
|
| 1156 | - */ |
|
| 1157 | - public function clearCache(bool $reload = false): void { |
|
| 1158 | - $this->lazyLoaded = $this->fastLoaded = false; |
|
| 1159 | - $this->lazyCache = $this->fastCache = $this->valueTypes = $this->configLexiconDetails = []; |
|
| 1160 | - $this->configLexiconPreset = null; |
|
| 1161 | - |
|
| 1162 | - if (!$reload) { |
|
| 1163 | - return; |
|
| 1164 | - } |
|
| 1165 | - |
|
| 1166 | - $this->loadConfigAll(); |
|
| 1167 | - } |
|
| 1168 | - |
|
| 1169 | - |
|
| 1170 | - /** |
|
| 1171 | - * For debug purpose. |
|
| 1172 | - * Returns the cached data. |
|
| 1173 | - * |
|
| 1174 | - * @return array |
|
| 1175 | - * @since 29.0.0 |
|
| 1176 | - * @internal |
|
| 1177 | - */ |
|
| 1178 | - public function statusCache(): array { |
|
| 1179 | - return [ |
|
| 1180 | - 'fastLoaded' => $this->fastLoaded, |
|
| 1181 | - 'fastCache' => $this->fastCache, |
|
| 1182 | - 'lazyLoaded' => $this->lazyLoaded, |
|
| 1183 | - 'lazyCache' => $this->lazyCache, |
|
| 1184 | - ]; |
|
| 1185 | - } |
|
| 1186 | - |
|
| 1187 | - /** |
|
| 1188 | - * @param int $needle bitflag to search |
|
| 1189 | - * @param int $type known value |
|
| 1190 | - * |
|
| 1191 | - * @return bool TRUE if bitflag $needle is set in $type |
|
| 1192 | - */ |
|
| 1193 | - private function isTyped(int $needle, int $type): bool { |
|
| 1194 | - return (($needle & $type) !== 0); |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - /** |
|
| 1198 | - * Confirm the string set for app and key fit the database description |
|
| 1199 | - * |
|
| 1200 | - * @param string $app assert $app fit in database |
|
| 1201 | - * @param string $configKey assert config key fit in database |
|
| 1202 | - * @param bool $allowEmptyApp $app can be empty string |
|
| 1203 | - * @param int $valueType assert value type is only one type |
|
| 1204 | - * |
|
| 1205 | - * @throws InvalidArgumentException |
|
| 1206 | - */ |
|
| 1207 | - private function assertParams(string $app = '', string $configKey = '', bool $allowEmptyApp = false, int $valueType = -1): void { |
|
| 1208 | - if (!$allowEmptyApp && $app === '') { |
|
| 1209 | - throw new InvalidArgumentException('app cannot be an empty string'); |
|
| 1210 | - } |
|
| 1211 | - if (strlen($app) > self::APP_MAX_LENGTH) { |
|
| 1212 | - throw new InvalidArgumentException( |
|
| 1213 | - 'Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')' |
|
| 1214 | - ); |
|
| 1215 | - } |
|
| 1216 | - if (strlen($configKey) > self::KEY_MAX_LENGTH) { |
|
| 1217 | - throw new InvalidArgumentException('Value (' . $configKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); |
|
| 1218 | - } |
|
| 1219 | - if ($valueType > -1) { |
|
| 1220 | - $valueType &= ~self::VALUE_SENSITIVE; |
|
| 1221 | - if (!in_array($valueType, [self::VALUE_MIXED, self::VALUE_STRING, self::VALUE_INT, self::VALUE_FLOAT, self::VALUE_BOOL, self::VALUE_ARRAY])) { |
|
| 1222 | - throw new InvalidArgumentException('Unknown value type'); |
|
| 1223 | - } |
|
| 1224 | - } |
|
| 1225 | - } |
|
| 1226 | - |
|
| 1227 | - private function loadConfigAll(?string $app = null): void { |
|
| 1228 | - $this->loadConfig($app, null); |
|
| 1229 | - } |
|
| 1230 | - |
|
| 1231 | - /** |
|
| 1232 | - * Load normal config or config set as lazy loaded |
|
| 1233 | - * |
|
| 1234 | - * @param bool|null $lazy set to TRUE to load config set as lazy loaded, set to NULL to load all config |
|
| 1235 | - */ |
|
| 1236 | - private function loadConfig(?string $app = null, ?bool $lazy = false): void { |
|
| 1237 | - if ($this->isLoaded($lazy)) { |
|
| 1238 | - return; |
|
| 1239 | - } |
|
| 1240 | - |
|
| 1241 | - // if lazy is null or true, we debug log |
|
| 1242 | - if (($lazy ?? true) !== false && $app !== null) { |
|
| 1243 | - $exception = new \RuntimeException('The loading of lazy AppConfig values have been triggered by app "' . $app . '"'); |
|
| 1244 | - $this->logger->debug($exception->getMessage(), ['exception' => $exception, 'app' => $app]); |
|
| 1245 | - } |
|
| 1246 | - |
|
| 1247 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1248 | - $qb->from('appconfig'); |
|
| 1249 | - |
|
| 1250 | - // we only need value from lazy when loadConfig does not specify it |
|
| 1251 | - $qb->select('appid', 'configkey', 'configvalue', 'type'); |
|
| 1252 | - |
|
| 1253 | - if ($lazy !== null) { |
|
| 1254 | - $qb->where($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))); |
|
| 1255 | - } else { |
|
| 1256 | - $qb->addSelect('lazy'); |
|
| 1257 | - } |
|
| 1258 | - |
|
| 1259 | - $result = $qb->executeQuery(); |
|
| 1260 | - $rows = $result->fetchAll(); |
|
| 1261 | - foreach ($rows as $row) { |
|
| 1262 | - // most of the time, 'lazy' is not in the select because its value is already known |
|
| 1263 | - if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) { |
|
| 1264 | - $this->lazyCache[$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1265 | - } else { |
|
| 1266 | - $this->fastCache[$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1267 | - } |
|
| 1268 | - $this->valueTypes[$row['appid']][$row['configkey']] = (int)($row['type'] ?? 0); |
|
| 1269 | - } |
|
| 1270 | - $result->closeCursor(); |
|
| 1271 | - $this->setAsLoaded($lazy); |
|
| 1272 | - } |
|
| 1273 | - |
|
| 1274 | - /** |
|
| 1275 | - * if $lazy is: |
|
| 1276 | - * - false: will returns true if fast config is loaded |
|
| 1277 | - * - true : will returns true if lazy config is loaded |
|
| 1278 | - * - null : will returns true if both config are loaded |
|
| 1279 | - * |
|
| 1280 | - * @param bool $lazy |
|
| 1281 | - * |
|
| 1282 | - * @return bool |
|
| 1283 | - */ |
|
| 1284 | - private function isLoaded(?bool $lazy): bool { |
|
| 1285 | - if ($lazy === null) { |
|
| 1286 | - return $this->lazyLoaded && $this->fastLoaded; |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - return $lazy ? $this->lazyLoaded : $this->fastLoaded; |
|
| 1290 | - } |
|
| 1291 | - |
|
| 1292 | - /** |
|
| 1293 | - * if $lazy is: |
|
| 1294 | - * - false: set fast config as loaded |
|
| 1295 | - * - true : set lazy config as loaded |
|
| 1296 | - * - null : set both config as loaded |
|
| 1297 | - * |
|
| 1298 | - * @param bool $lazy |
|
| 1299 | - */ |
|
| 1300 | - private function setAsLoaded(?bool $lazy): void { |
|
| 1301 | - if ($lazy === null) { |
|
| 1302 | - $this->fastLoaded = true; |
|
| 1303 | - $this->lazyLoaded = true; |
|
| 1304 | - |
|
| 1305 | - return; |
|
| 1306 | - } |
|
| 1307 | - |
|
| 1308 | - if ($lazy) { |
|
| 1309 | - $this->lazyLoaded = true; |
|
| 1310 | - } else { |
|
| 1311 | - $this->fastLoaded = true; |
|
| 1312 | - } |
|
| 1313 | - } |
|
| 1314 | - |
|
| 1315 | - /** |
|
| 1316 | - * Gets the config value |
|
| 1317 | - * |
|
| 1318 | - * @param string $app app |
|
| 1319 | - * @param string $key key |
|
| 1320 | - * @param string $default = null, default value if the key does not exist |
|
| 1321 | - * |
|
| 1322 | - * @return string the value or $default |
|
| 1323 | - * @deprecated 29.0.0 use getValue*() |
|
| 1324 | - * |
|
| 1325 | - * This function gets a value from the appconfig table. If the key does |
|
| 1326 | - * not exist the default value will be returned |
|
| 1327 | - */ |
|
| 1328 | - public function getValue($app, $key, $default = null) { |
|
| 1329 | - $this->loadConfig($app); |
|
| 1330 | - $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1331 | - |
|
| 1332 | - return $this->fastCache[$app][$key] ?? $default; |
|
| 1333 | - } |
|
| 1334 | - |
|
| 1335 | - /** |
|
| 1336 | - * Sets a value. If the key did not exist before it will be created. |
|
| 1337 | - * |
|
| 1338 | - * @param string $app app |
|
| 1339 | - * @param string $key key |
|
| 1340 | - * @param string|float|int $value value |
|
| 1341 | - * |
|
| 1342 | - * @return bool True if the value was inserted or updated, false if the value was the same |
|
| 1343 | - * @throws AppConfigTypeConflictException |
|
| 1344 | - * @throws AppConfigUnknownKeyException |
|
| 1345 | - * @deprecated 29.0.0 |
|
| 1346 | - */ |
|
| 1347 | - public function setValue($app, $key, $value) { |
|
| 1348 | - /** |
|
| 1349 | - * TODO: would it be overkill, or decently improve performance, to catch |
|
| 1350 | - * call to this method with $key='enabled' and 'hide' config value related |
|
| 1351 | - * to $app when the app is disabled (by modifying entry in database: lazy=lazy+2) |
|
| 1352 | - * or enabled (lazy=lazy-2) |
|
| 1353 | - * |
|
| 1354 | - * this solution would remove the loading of config values from disabled app |
|
| 1355 | - * unless calling the method {@see loadConfigAll()} |
|
| 1356 | - */ |
|
| 1357 | - return $this->setTypedValue($app, $key, (string)$value, false, self::VALUE_MIXED); |
|
| 1358 | - } |
|
| 1359 | - |
|
| 1360 | - |
|
| 1361 | - /** |
|
| 1362 | - * get multiple values, either the app or key can be used as wildcard by setting it to false |
|
| 1363 | - * |
|
| 1364 | - * @param string|false $app |
|
| 1365 | - * @param string|false $key |
|
| 1366 | - * |
|
| 1367 | - * @return array|false |
|
| 1368 | - * @deprecated 29.0.0 use {@see getAllValues()} |
|
| 1369 | - */ |
|
| 1370 | - public function getValues($app, $key) { |
|
| 1371 | - if (($app !== false) === ($key !== false)) { |
|
| 1372 | - return false; |
|
| 1373 | - } |
|
| 1374 | - |
|
| 1375 | - $key = ($key === false) ? '' : $key; |
|
| 1376 | - if (!$app) { |
|
| 1377 | - return $this->searchValues($key, false, self::VALUE_MIXED); |
|
| 1378 | - } else { |
|
| 1379 | - return $this->getAllValues($app, $key); |
|
| 1380 | - } |
|
| 1381 | - } |
|
| 1382 | - |
|
| 1383 | - /** |
|
| 1384 | - * get all values of the app or and filters out sensitive data |
|
| 1385 | - * |
|
| 1386 | - * @param string $app |
|
| 1387 | - * |
|
| 1388 | - * @return array |
|
| 1389 | - * @deprecated 29.0.0 use {@see getAllValues()} |
|
| 1390 | - */ |
|
| 1391 | - public function getFilteredValues($app) { |
|
| 1392 | - return $this->getAllValues($app, filtered: true); |
|
| 1393 | - } |
|
| 1394 | - |
|
| 1395 | - |
|
| 1396 | - /** |
|
| 1397 | - * **Warning:** avoid default NULL value for $lazy as this will |
|
| 1398 | - * load all lazy values from the database |
|
| 1399 | - * |
|
| 1400 | - * @param string $app |
|
| 1401 | - * @param array<string, string> $values ['key' => 'value'] |
|
| 1402 | - * @param bool|null $lazy |
|
| 1403 | - * |
|
| 1404 | - * @return array<string, string|int|float|bool|array> |
|
| 1405 | - */ |
|
| 1406 | - private function formatAppValues(string $app, array $values, ?bool $lazy = null): array { |
|
| 1407 | - foreach ($values as $key => $value) { |
|
| 1408 | - try { |
|
| 1409 | - $type = $this->getValueType($app, $key, $lazy); |
|
| 1410 | - } catch (AppConfigUnknownKeyException) { |
|
| 1411 | - continue; |
|
| 1412 | - } |
|
| 1413 | - |
|
| 1414 | - $values[$key] = $this->convertTypedValue($value, $type); |
|
| 1415 | - } |
|
| 1416 | - |
|
| 1417 | - return $values; |
|
| 1418 | - } |
|
| 1419 | - |
|
| 1420 | - /** |
|
| 1421 | - * convert string value to the expected type |
|
| 1422 | - * |
|
| 1423 | - * @param string $value |
|
| 1424 | - * @param int $type |
|
| 1425 | - * |
|
| 1426 | - * @return string|int|float|bool|array |
|
| 1427 | - */ |
|
| 1428 | - private function convertTypedValue(string $value, int $type): string|int|float|bool|array { |
|
| 1429 | - switch ($type) { |
|
| 1430 | - case self::VALUE_INT: |
|
| 1431 | - return (int)$value; |
|
| 1432 | - case self::VALUE_FLOAT: |
|
| 1433 | - return (float)$value; |
|
| 1434 | - case self::VALUE_BOOL: |
|
| 1435 | - return in_array(strtolower($value), ['1', 'true', 'yes', 'on']); |
|
| 1436 | - case self::VALUE_ARRAY: |
|
| 1437 | - try { |
|
| 1438 | - return json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 1439 | - } catch (JsonException $e) { |
|
| 1440 | - // ignoreable |
|
| 1441 | - } |
|
| 1442 | - break; |
|
| 1443 | - } |
|
| 1444 | - return $value; |
|
| 1445 | - } |
|
| 1446 | - |
|
| 1447 | - /** |
|
| 1448 | - * @param string $app |
|
| 1449 | - * |
|
| 1450 | - * @return string[] |
|
| 1451 | - * @deprecated 29.0.0 data sensitivity should be set when calling setValue*() |
|
| 1452 | - */ |
|
| 1453 | - private function getSensitiveKeys(string $app): array { |
|
| 1454 | - $sensitiveValues = [ |
|
| 1455 | - 'circles' => [ |
|
| 1456 | - '/^key_pairs$/', |
|
| 1457 | - '/^local_gskey$/', |
|
| 1458 | - ], |
|
| 1459 | - 'call_summary_bot' => [ |
|
| 1460 | - '/^secret_(.*)$/', |
|
| 1461 | - ], |
|
| 1462 | - 'external' => [ |
|
| 1463 | - '/^sites$/', |
|
| 1464 | - '/^jwt_token_privkey_(.*)$/', |
|
| 1465 | - ], |
|
| 1466 | - 'globalsiteselector' => [ |
|
| 1467 | - '/^gss\.jwt\.key$/', |
|
| 1468 | - ], |
|
| 1469 | - 'gpgmailer' => [ |
|
| 1470 | - '/^GpgServerKey$/', |
|
| 1471 | - ], |
|
| 1472 | - 'integration_discourse' => [ |
|
| 1473 | - '/^private_key$/', |
|
| 1474 | - '/^public_key$/', |
|
| 1475 | - ], |
|
| 1476 | - 'integration_dropbox' => [ |
|
| 1477 | - '/^client_id$/', |
|
| 1478 | - '/^client_secret$/', |
|
| 1479 | - ], |
|
| 1480 | - 'integration_github' => [ |
|
| 1481 | - '/^client_id$/', |
|
| 1482 | - '/^client_secret$/', |
|
| 1483 | - ], |
|
| 1484 | - 'integration_gitlab' => [ |
|
| 1485 | - '/^client_id$/', |
|
| 1486 | - '/^client_secret$/', |
|
| 1487 | - '/^oauth_instance_url$/', |
|
| 1488 | - ], |
|
| 1489 | - 'integration_google' => [ |
|
| 1490 | - '/^client_id$/', |
|
| 1491 | - '/^client_secret$/', |
|
| 1492 | - ], |
|
| 1493 | - 'integration_jira' => [ |
|
| 1494 | - '/^client_id$/', |
|
| 1495 | - '/^client_secret$/', |
|
| 1496 | - '/^forced_instance_url$/', |
|
| 1497 | - ], |
|
| 1498 | - 'integration_onedrive' => [ |
|
| 1499 | - '/^client_id$/', |
|
| 1500 | - '/^client_secret$/', |
|
| 1501 | - ], |
|
| 1502 | - 'integration_openproject' => [ |
|
| 1503 | - '/^client_id$/', |
|
| 1504 | - '/^client_secret$/', |
|
| 1505 | - '/^oauth_instance_url$/', |
|
| 1506 | - ], |
|
| 1507 | - 'integration_reddit' => [ |
|
| 1508 | - '/^client_id$/', |
|
| 1509 | - '/^client_secret$/', |
|
| 1510 | - ], |
|
| 1511 | - 'integration_suitecrm' => [ |
|
| 1512 | - '/^client_id$/', |
|
| 1513 | - '/^client_secret$/', |
|
| 1514 | - '/^oauth_instance_url$/', |
|
| 1515 | - ], |
|
| 1516 | - 'integration_twitter' => [ |
|
| 1517 | - '/^consumer_key$/', |
|
| 1518 | - '/^consumer_secret$/', |
|
| 1519 | - '/^followed_user$/', |
|
| 1520 | - ], |
|
| 1521 | - 'integration_zammad' => [ |
|
| 1522 | - '/^client_id$/', |
|
| 1523 | - '/^client_secret$/', |
|
| 1524 | - '/^oauth_instance_url$/', |
|
| 1525 | - ], |
|
| 1526 | - 'maps' => [ |
|
| 1527 | - '/^mapboxAPIKEY$/', |
|
| 1528 | - ], |
|
| 1529 | - 'notify_push' => [ |
|
| 1530 | - '/^cookie$/', |
|
| 1531 | - ], |
|
| 1532 | - 'onlyoffice' => [ |
|
| 1533 | - '/^jwt_secret$/', |
|
| 1534 | - ], |
|
| 1535 | - 'passwords' => [ |
|
| 1536 | - '/^SSEv1ServerKey$/', |
|
| 1537 | - ], |
|
| 1538 | - 'serverinfo' => [ |
|
| 1539 | - '/^token$/', |
|
| 1540 | - ], |
|
| 1541 | - 'spreed' => [ |
|
| 1542 | - '/^bridge_bot_password$/', |
|
| 1543 | - '/^hosted-signaling-server-(.*)$/', |
|
| 1544 | - '/^recording_servers$/', |
|
| 1545 | - '/^signaling_servers$/', |
|
| 1546 | - '/^signaling_ticket_secret$/', |
|
| 1547 | - '/^signaling_token_privkey_(.*)$/', |
|
| 1548 | - '/^signaling_token_pubkey_(.*)$/', |
|
| 1549 | - '/^sip_bridge_dialin_info$/', |
|
| 1550 | - '/^sip_bridge_shared_secret$/', |
|
| 1551 | - '/^stun_servers$/', |
|
| 1552 | - '/^turn_servers$/', |
|
| 1553 | - '/^turn_server_secret$/', |
|
| 1554 | - ], |
|
| 1555 | - 'support' => [ |
|
| 1556 | - '/^last_response$/', |
|
| 1557 | - '/^potential_subscription_key$/', |
|
| 1558 | - '/^subscription_key$/', |
|
| 1559 | - ], |
|
| 1560 | - 'theming' => [ |
|
| 1561 | - '/^imprintUrl$/', |
|
| 1562 | - '/^privacyUrl$/', |
|
| 1563 | - '/^slogan$/', |
|
| 1564 | - '/^url$/', |
|
| 1565 | - ], |
|
| 1566 | - 'twofactor_gateway' => [ |
|
| 1567 | - '/^.*token$/', |
|
| 1568 | - ], |
|
| 1569 | - 'user_ldap' => [ |
|
| 1570 | - '/^(s..)?ldap_agent_password$/', |
|
| 1571 | - ], |
|
| 1572 | - 'user_saml' => [ |
|
| 1573 | - '/^idp-x509cert$/', |
|
| 1574 | - ], |
|
| 1575 | - 'whiteboard' => [ |
|
| 1576 | - '/^jwt_secret_key$/', |
|
| 1577 | - ], |
|
| 1578 | - ]; |
|
| 1579 | - |
|
| 1580 | - return $sensitiveValues[$app] ?? []; |
|
| 1581 | - } |
|
| 1582 | - |
|
| 1583 | - /** |
|
| 1584 | - * Clear all the cached app config values |
|
| 1585 | - * New cache will be generated next time a config value is retrieved |
|
| 1586 | - * |
|
| 1587 | - * @deprecated 29.0.0 use {@see clearCache()} |
|
| 1588 | - */ |
|
| 1589 | - public function clearCachedConfig(): void { |
|
| 1590 | - $this->clearCache(); |
|
| 1591 | - } |
|
| 1592 | - |
|
| 1593 | - /** |
|
| 1594 | - * Match and apply current use of config values with defined lexicon. |
|
| 1595 | - * Set $lazy to NULL only if only interested into checking that $key is alias. |
|
| 1596 | - * |
|
| 1597 | - * @throws AppConfigUnknownKeyException |
|
| 1598 | - * @throws AppConfigTypeConflictException |
|
| 1599 | - * @return bool TRUE if everything is fine compared to lexicon or lexicon does not exist |
|
| 1600 | - */ |
|
| 1601 | - private function matchAndApplyLexiconDefinition( |
|
| 1602 | - string $app, |
|
| 1603 | - string &$key, |
|
| 1604 | - ?bool &$lazy = null, |
|
| 1605 | - int &$type = self::VALUE_MIXED, |
|
| 1606 | - ?string &$default = null, |
|
| 1607 | - ): bool { |
|
| 1608 | - if (in_array($key, |
|
| 1609 | - [ |
|
| 1610 | - 'enabled', |
|
| 1611 | - 'installed_version', |
|
| 1612 | - 'types', |
|
| 1613 | - ])) { |
|
| 1614 | - return true; // we don't break stuff for this list of config keys. |
|
| 1615 | - } |
|
| 1616 | - $configDetails = $this->getConfigDetailsFromLexicon($app); |
|
| 1617 | - if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) { |
|
| 1618 | - // in case '$rename' is set in ConfigLexiconEntry, we use the new config key |
|
| 1619 | - $key = $configDetails['aliases'][$key]; |
|
| 1620 | - } |
|
| 1621 | - |
|
| 1622 | - if (!array_key_exists($key, $configDetails['entries'])) { |
|
| 1623 | - return $this->applyLexiconStrictness($configDetails['strictness'], 'The app config key ' . $app . '/' . $key . ' is not defined in the config lexicon'); |
|
| 1624 | - } |
|
| 1625 | - |
|
| 1626 | - // if lazy is NULL, we ignore all check on the type/lazyness/default from Lexicon |
|
| 1627 | - if ($lazy === null) { |
|
| 1628 | - return true; |
|
| 1629 | - } |
|
| 1630 | - |
|
| 1631 | - /** @var ConfigLexiconEntry $configValue */ |
|
| 1632 | - $configValue = $configDetails['entries'][$key]; |
|
| 1633 | - $type &= ~self::VALUE_SENSITIVE; |
|
| 1634 | - |
|
| 1635 | - $appConfigValueType = $configValue->getValueType()->toAppConfigFlag(); |
|
| 1636 | - if ($type === self::VALUE_MIXED) { |
|
| 1637 | - $type = $appConfigValueType; // we overwrite if value was requested as mixed |
|
| 1638 | - } elseif ($appConfigValueType !== $type) { |
|
| 1639 | - throw new AppConfigTypeConflictException('The app config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon'); |
|
| 1640 | - } |
|
| 1641 | - |
|
| 1642 | - $lazy = $configValue->isLazy(); |
|
| 1643 | - // only look for default if needed, default from Lexicon got priority |
|
| 1644 | - if ($default !== null) { |
|
| 1645 | - $default = $configValue->getDefault($this->getLexiconPreset()) ?? $default; |
|
| 1646 | - } |
|
| 1647 | - |
|
| 1648 | - if ($configValue->isFlagged(self::FLAG_SENSITIVE)) { |
|
| 1649 | - $type |= self::VALUE_SENSITIVE; |
|
| 1650 | - } |
|
| 1651 | - if ($configValue->isDeprecated()) { |
|
| 1652 | - $this->logger->notice('App config key ' . $app . '/' . $key . ' is set as deprecated.'); |
|
| 1653 | - } |
|
| 1654 | - |
|
| 1655 | - return true; |
|
| 1656 | - } |
|
| 1657 | - |
|
| 1658 | - /** |
|
| 1659 | - * manage ConfigLexicon behavior based on strictness set in IConfigLexicon |
|
| 1660 | - * |
|
| 1661 | - * @param ConfigLexiconStrictness|null $strictness |
|
| 1662 | - * @param string $line |
|
| 1663 | - * |
|
| 1664 | - * @return bool TRUE if conflict can be fully ignored, FALSE if action should be not performed |
|
| 1665 | - * @throws AppConfigUnknownKeyException if strictness implies exception |
|
| 1666 | - * @see IConfigLexicon::getStrictness() |
|
| 1667 | - */ |
|
| 1668 | - private function applyLexiconStrictness( |
|
| 1669 | - ?ConfigLexiconStrictness $strictness, |
|
| 1670 | - string $line = '', |
|
| 1671 | - ): bool { |
|
| 1672 | - if ($strictness === null) { |
|
| 1673 | - return true; |
|
| 1674 | - } |
|
| 1675 | - |
|
| 1676 | - switch ($strictness) { |
|
| 1677 | - case ConfigLexiconStrictness::IGNORE: |
|
| 1678 | - return true; |
|
| 1679 | - case ConfigLexiconStrictness::NOTICE: |
|
| 1680 | - $this->logger->notice($line); |
|
| 1681 | - return true; |
|
| 1682 | - case ConfigLexiconStrictness::WARNING: |
|
| 1683 | - $this->logger->warning($line); |
|
| 1684 | - return false; |
|
| 1685 | - } |
|
| 1686 | - |
|
| 1687 | - throw new AppConfigUnknownKeyException($line); |
|
| 1688 | - } |
|
| 1689 | - |
|
| 1690 | - /** |
|
| 1691 | - * extract details from registered $appId's config lexicon |
|
| 1692 | - * |
|
| 1693 | - * @param string $appId |
|
| 1694 | - * @internal |
|
| 1695 | - * |
|
| 1696 | - * @return array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness} |
|
| 1697 | - */ |
|
| 1698 | - public function getConfigDetailsFromLexicon(string $appId): array { |
|
| 1699 | - if (!array_key_exists($appId, $this->configLexiconDetails)) { |
|
| 1700 | - $entries = $aliases = []; |
|
| 1701 | - $bootstrapCoordinator = \OCP\Server::get(Coordinator::class); |
|
| 1702 | - $configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId); |
|
| 1703 | - foreach ($configLexicon?->getAppConfigs() ?? [] as $configEntry) { |
|
| 1704 | - $entries[$configEntry->getKey()] = $configEntry; |
|
| 1705 | - if ($configEntry->getRename() !== null) { |
|
| 1706 | - $aliases[$configEntry->getRename()] = $configEntry->getKey(); |
|
| 1707 | - } |
|
| 1708 | - } |
|
| 1709 | - |
|
| 1710 | - $this->configLexiconDetails[$appId] = [ |
|
| 1711 | - 'entries' => $entries, |
|
| 1712 | - 'aliases' => $aliases, |
|
| 1713 | - 'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE |
|
| 1714 | - ]; |
|
| 1715 | - } |
|
| 1716 | - |
|
| 1717 | - return $this->configLexiconDetails[$appId]; |
|
| 1718 | - } |
|
| 1719 | - |
|
| 1720 | - private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry { |
|
| 1721 | - return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null; |
|
| 1722 | - } |
|
| 1723 | - |
|
| 1724 | - /** |
|
| 1725 | - * if set to TRUE, ignore aliases defined in Config Lexicon during the use of the methods of this class |
|
| 1726 | - * |
|
| 1727 | - * @internal |
|
| 1728 | - */ |
|
| 1729 | - public function ignoreLexiconAliases(bool $ignore): void { |
|
| 1730 | - $this->ignoreLexiconAliases = $ignore; |
|
| 1731 | - } |
|
| 1732 | - |
|
| 1733 | - private function getLexiconPreset(): Preset { |
|
| 1734 | - if ($this->configLexiconPreset === null) { |
|
| 1735 | - $this->configLexiconPreset = Preset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? Preset::NONE; |
|
| 1736 | - } |
|
| 1737 | - |
|
| 1738 | - return $this->configLexiconPreset; |
|
| 1739 | - } |
|
| 1740 | - |
|
| 1741 | - /** |
|
| 1742 | - * Returns the installed versions of all apps |
|
| 1743 | - * |
|
| 1744 | - * @return array<string, string> |
|
| 1745 | - */ |
|
| 1746 | - public function getAppInstalledVersions(bool $onlyEnabled = false): array { |
|
| 1747 | - if ($this->appVersionsCache === null) { |
|
| 1748 | - /** @var array<string, string> */ |
|
| 1749 | - $this->appVersionsCache = $this->searchValues('installed_version', false, IAppConfig::VALUE_STRING); |
|
| 1750 | - } |
|
| 1751 | - if ($onlyEnabled) { |
|
| 1752 | - return array_filter( |
|
| 1753 | - $this->appVersionsCache, |
|
| 1754 | - fn (string $app): bool => $this->getValueString($app, 'enabled', 'no') !== 'no', |
|
| 1755 | - ARRAY_FILTER_USE_KEY |
|
| 1756 | - ); |
|
| 1757 | - } |
|
| 1758 | - return $this->appVersionsCache; |
|
| 1759 | - } |
|
| 52 | + private const APP_MAX_LENGTH = 32; |
|
| 53 | + private const KEY_MAX_LENGTH = 64; |
|
| 54 | + private const ENCRYPTION_PREFIX = '$AppConfigEncryption$'; |
|
| 55 | + private const ENCRYPTION_PREFIX_LENGTH = 21; // strlen(self::ENCRYPTION_PREFIX) |
|
| 56 | + |
|
| 57 | + /** @var array<string, array<string, mixed>> ['app_id' => ['config_key' => 'config_value']] */ |
|
| 58 | + private array $fastCache = []; // cache for normal config keys |
|
| 59 | + /** @var array<string, array<string, mixed>> ['app_id' => ['config_key' => 'config_value']] */ |
|
| 60 | + private array $lazyCache = []; // cache for lazy config keys |
|
| 61 | + /** @var array<string, array<string, int>> ['app_id' => ['config_key' => bitflag]] */ |
|
| 62 | + private array $valueTypes = []; // type for all config values |
|
| 63 | + private bool $fastLoaded = false; |
|
| 64 | + private bool $lazyLoaded = false; |
|
| 65 | + /** @var array<string, array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ |
|
| 66 | + private array $configLexiconDetails = []; |
|
| 67 | + private bool $ignoreLexiconAliases = false; |
|
| 68 | + private ?Preset $configLexiconPreset = null; |
|
| 69 | + /** @var ?array<string, string> */ |
|
| 70 | + private ?array $appVersionsCache = null; |
|
| 71 | + |
|
| 72 | + public function __construct( |
|
| 73 | + protected IDBConnection $connection, |
|
| 74 | + protected IConfig $config, |
|
| 75 | + protected LoggerInterface $logger, |
|
| 76 | + protected ICrypto $crypto, |
|
| 77 | + ) { |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @inheritDoc |
|
| 82 | + * |
|
| 83 | + * @return list<string> list of app ids |
|
| 84 | + * @since 7.0.0 |
|
| 85 | + */ |
|
| 86 | + public function getApps(): array { |
|
| 87 | + $this->loadConfigAll(); |
|
| 88 | + $apps = array_merge(array_keys($this->fastCache), array_keys($this->lazyCache)); |
|
| 89 | + sort($apps); |
|
| 90 | + |
|
| 91 | + return array_values(array_unique($apps)); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * @inheritDoc |
|
| 96 | + * |
|
| 97 | + * @param string $app id of the app |
|
| 98 | + * |
|
| 99 | + * @return list<string> list of stored config keys |
|
| 100 | + * @since 29.0.0 |
|
| 101 | + */ |
|
| 102 | + public function getKeys(string $app): array { |
|
| 103 | + $this->assertParams($app); |
|
| 104 | + $this->loadConfigAll($app); |
|
| 105 | + $keys = array_merge(array_keys($this->fastCache[$app] ?? []), array_keys($this->lazyCache[$app] ?? [])); |
|
| 106 | + sort($keys); |
|
| 107 | + |
|
| 108 | + return array_values(array_unique($keys)); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + /** |
|
| 112 | + * @inheritDoc |
|
| 113 | + * |
|
| 114 | + * @param string $app id of the app |
|
| 115 | + * @param string $key config key |
|
| 116 | + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 117 | + * |
|
| 118 | + * @return bool TRUE if key exists |
|
| 119 | + * @since 7.0.0 |
|
| 120 | + * @since 29.0.0 Added the $lazy argument |
|
| 121 | + */ |
|
| 122 | + public function hasKey(string $app, string $key, ?bool $lazy = false): bool { |
|
| 123 | + $this->assertParams($app, $key); |
|
| 124 | + $this->loadConfig($app, $lazy); |
|
| 125 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 126 | + |
|
| 127 | + if ($lazy === null) { |
|
| 128 | + $appCache = $this->getAllValues($app); |
|
| 129 | + return isset($appCache[$key]); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + if ($lazy) { |
|
| 133 | + return isset($this->lazyCache[$app][$key]); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return isset($this->fastCache[$app][$key]); |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + /** |
|
| 140 | + * @param string $app id of the app |
|
| 141 | + * @param string $key config key |
|
| 142 | + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 143 | + * |
|
| 144 | + * @return bool |
|
| 145 | + * @throws AppConfigUnknownKeyException if config key is not known |
|
| 146 | + * @since 29.0.0 |
|
| 147 | + */ |
|
| 148 | + public function isSensitive(string $app, string $key, ?bool $lazy = false): bool { |
|
| 149 | + $this->assertParams($app, $key); |
|
| 150 | + $this->loadConfig(null, $lazy); |
|
| 151 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 152 | + |
|
| 153 | + if (!isset($this->valueTypes[$app][$key])) { |
|
| 154 | + throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return $this->isTyped(self::VALUE_SENSITIVE, $this->valueTypes[$app][$key]); |
|
| 158 | + } |
|
| 159 | + |
|
| 160 | + /** |
|
| 161 | + * @inheritDoc |
|
| 162 | + * |
|
| 163 | + * @param string $app if of the app |
|
| 164 | + * @param string $key config key |
|
| 165 | + * |
|
| 166 | + * @return bool TRUE if config is lazy loaded |
|
| 167 | + * @throws AppConfigUnknownKeyException if config key is not known |
|
| 168 | + * @see IAppConfig for details about lazy loading |
|
| 169 | + * @since 29.0.0 |
|
| 170 | + */ |
|
| 171 | + public function isLazy(string $app, string $key): bool { |
|
| 172 | + $this->assertParams($app, $key); |
|
| 173 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 174 | + |
|
| 175 | + // there is a huge probability the non-lazy config are already loaded |
|
| 176 | + if ($this->hasKey($app, $key, false)) { |
|
| 177 | + return false; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + // key not found, we search in the lazy config |
|
| 181 | + if ($this->hasKey($app, $key, true)) { |
|
| 182 | + return true; |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 186 | + } |
|
| 187 | + |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * @inheritDoc |
|
| 191 | + * |
|
| 192 | + * @param string $app id of the app |
|
| 193 | + * @param string $prefix config keys prefix to search |
|
| 194 | + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 195 | + * |
|
| 196 | + * @return array<string, string|int|float|bool|array> [configKey => configValue] |
|
| 197 | + * @since 29.0.0 |
|
| 198 | + */ |
|
| 199 | + public function getAllValues(string $app, string $prefix = '', bool $filtered = false): array { |
|
| 200 | + $this->assertParams($app, $prefix); |
|
| 201 | + // if we want to filter values, we need to get sensitivity |
|
| 202 | + $this->loadConfigAll($app); |
|
| 203 | + // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 204 | + $values = $this->formatAppValues($app, ($this->fastCache[$app] ?? []) + ($this->lazyCache[$app] ?? [])); |
|
| 205 | + $values = array_filter( |
|
| 206 | + $values, |
|
| 207 | + function (string $key) use ($prefix): bool { |
|
| 208 | + return str_starts_with($key, $prefix); // filter values based on $prefix |
|
| 209 | + }, ARRAY_FILTER_USE_KEY |
|
| 210 | + ); |
|
| 211 | + |
|
| 212 | + if (!$filtered) { |
|
| 213 | + return $values; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * Using the old (deprecated) list of sensitive values. |
|
| 218 | + */ |
|
| 219 | + foreach ($this->getSensitiveKeys($app) as $sensitiveKeyExp) { |
|
| 220 | + $sensitiveKeys = preg_grep($sensitiveKeyExp, array_keys($values)); |
|
| 221 | + foreach ($sensitiveKeys as $sensitiveKey) { |
|
| 222 | + $this->valueTypes[$app][$sensitiveKey] = ($this->valueTypes[$app][$sensitiveKey] ?? 0) | self::VALUE_SENSITIVE; |
|
| 223 | + } |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + $result = []; |
|
| 227 | + foreach ($values as $key => $value) { |
|
| 228 | + $result[$key] = $this->isTyped(self::VALUE_SENSITIVE, $this->valueTypes[$app][$key] ?? 0) ? IConfig::SENSITIVE_VALUE : $value; |
|
| 229 | + } |
|
| 230 | + |
|
| 231 | + return $result; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * @inheritDoc |
|
| 236 | + * |
|
| 237 | + * @param string $key config key |
|
| 238 | + * @param bool $lazy search within lazy loaded config |
|
| 239 | + * @param int|null $typedAs enforce type for the returned values ({@see self::VALUE_STRING} and others) |
|
| 240 | + * |
|
| 241 | + * @return array<string, string|int|float|bool|array> [appId => configValue] |
|
| 242 | + * @since 29.0.0 |
|
| 243 | + */ |
|
| 244 | + public function searchValues(string $key, bool $lazy = false, ?int $typedAs = null): array { |
|
| 245 | + $this->assertParams('', $key, true); |
|
| 246 | + $this->loadConfig(null, $lazy); |
|
| 247 | + |
|
| 248 | + /** @var array<array-key, array<array-key, mixed>> $cache */ |
|
| 249 | + if ($lazy) { |
|
| 250 | + $cache = $this->lazyCache; |
|
| 251 | + } else { |
|
| 252 | + $cache = $this->fastCache; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + $values = []; |
|
| 256 | + foreach (array_keys($cache) as $app) { |
|
| 257 | + if (isset($cache[$app][$key])) { |
|
| 258 | + $values[$app] = $this->convertTypedValue($cache[$app][$key], $typedAs ?? $this->getValueType((string)$app, $key, $lazy)); |
|
| 259 | + } |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + return $values; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + |
|
| 266 | + /** |
|
| 267 | + * Get the config value as string. |
|
| 268 | + * If the value does not exist the given default will be returned. |
|
| 269 | + * |
|
| 270 | + * Set lazy to `null` to ignore it and get the value from either source. |
|
| 271 | + * |
|
| 272 | + * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type. |
|
| 273 | + * |
|
| 274 | + * @param string $app id of the app |
|
| 275 | + * @param string $key config key |
|
| 276 | + * @param string $default config value |
|
| 277 | + * @param null|bool $lazy get config as lazy loaded or not. can be NULL |
|
| 278 | + * |
|
| 279 | + * @return string the value or $default |
|
| 280 | + * @internal |
|
| 281 | + * @since 29.0.0 |
|
| 282 | + * @see IAppConfig for explanation about lazy loading |
|
| 283 | + * @see getValueString() |
|
| 284 | + * @see getValueInt() |
|
| 285 | + * @see getValueFloat() |
|
| 286 | + * @see getValueBool() |
|
| 287 | + * @see getValueArray() |
|
| 288 | + */ |
|
| 289 | + public function getValueMixed( |
|
| 290 | + string $app, |
|
| 291 | + string $key, |
|
| 292 | + string $default = '', |
|
| 293 | + ?bool $lazy = false, |
|
| 294 | + ): string { |
|
| 295 | + try { |
|
| 296 | + $lazy = ($lazy === null) ? $this->isLazy($app, $key) : $lazy; |
|
| 297 | + } catch (AppConfigUnknownKeyException) { |
|
| 298 | + return $default; |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + return $this->getTypedValue( |
|
| 302 | + $app, |
|
| 303 | + $key, |
|
| 304 | + $default, |
|
| 305 | + $lazy, |
|
| 306 | + self::VALUE_MIXED |
|
| 307 | + ); |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + /** |
|
| 311 | + * @inheritDoc |
|
| 312 | + * |
|
| 313 | + * @param string $app id of the app |
|
| 314 | + * @param string $key config key |
|
| 315 | + * @param string $default default value |
|
| 316 | + * @param bool $lazy search within lazy loaded config |
|
| 317 | + * |
|
| 318 | + * @return string stored config value or $default if not set in database |
|
| 319 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 320 | + * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 321 | + * @since 29.0.0 |
|
| 322 | + * @see IAppConfig for explanation about lazy loading |
|
| 323 | + */ |
|
| 324 | + public function getValueString( |
|
| 325 | + string $app, |
|
| 326 | + string $key, |
|
| 327 | + string $default = '', |
|
| 328 | + bool $lazy = false, |
|
| 329 | + ): string { |
|
| 330 | + return $this->getTypedValue($app, $key, $default, $lazy, self::VALUE_STRING); |
|
| 331 | + } |
|
| 332 | + |
|
| 333 | + /** |
|
| 334 | + * @inheritDoc |
|
| 335 | + * |
|
| 336 | + * @param string $app id of the app |
|
| 337 | + * @param string $key config key |
|
| 338 | + * @param int $default default value |
|
| 339 | + * @param bool $lazy search within lazy loaded config |
|
| 340 | + * |
|
| 341 | + * @return int stored config value or $default if not set in database |
|
| 342 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 343 | + * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 344 | + * @since 29.0.0 |
|
| 345 | + * @see IAppConfig for explanation about lazy loading |
|
| 346 | + */ |
|
| 347 | + public function getValueInt( |
|
| 348 | + string $app, |
|
| 349 | + string $key, |
|
| 350 | + int $default = 0, |
|
| 351 | + bool $lazy = false, |
|
| 352 | + ): int { |
|
| 353 | + return (int)$this->getTypedValue($app, $key, (string)$default, $lazy, self::VALUE_INT); |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + /** |
|
| 357 | + * @inheritDoc |
|
| 358 | + * |
|
| 359 | + * @param string $app id of the app |
|
| 360 | + * @param string $key config key |
|
| 361 | + * @param float $default default value |
|
| 362 | + * @param bool $lazy search within lazy loaded config |
|
| 363 | + * |
|
| 364 | + * @return float stored config value or $default if not set in database |
|
| 365 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 366 | + * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 367 | + * @since 29.0.0 |
|
| 368 | + * @see IAppConfig for explanation about lazy loading |
|
| 369 | + */ |
|
| 370 | + public function getValueFloat(string $app, string $key, float $default = 0, bool $lazy = false): float { |
|
| 371 | + return (float)$this->getTypedValue($app, $key, (string)$default, $lazy, self::VALUE_FLOAT); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + /** |
|
| 375 | + * @inheritDoc |
|
| 376 | + * |
|
| 377 | + * @param string $app id of the app |
|
| 378 | + * @param string $key config key |
|
| 379 | + * @param bool $default default value |
|
| 380 | + * @param bool $lazy search within lazy loaded config |
|
| 381 | + * |
|
| 382 | + * @return bool stored config value or $default if not set in database |
|
| 383 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 384 | + * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 385 | + * @since 29.0.0 |
|
| 386 | + * @see IAppConfig for explanation about lazy loading |
|
| 387 | + */ |
|
| 388 | + public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool { |
|
| 389 | + $b = strtolower($this->getTypedValue($app, $key, $default ? 'true' : 'false', $lazy, self::VALUE_BOOL)); |
|
| 390 | + return in_array($b, ['1', 'true', 'yes', 'on']); |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * @inheritDoc |
|
| 395 | + * |
|
| 396 | + * @param string $app id of the app |
|
| 397 | + * @param string $key config key |
|
| 398 | + * @param array $default default value |
|
| 399 | + * @param bool $lazy search within lazy loaded config |
|
| 400 | + * |
|
| 401 | + * @return array stored config value or $default if not set in database |
|
| 402 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 403 | + * @throws AppConfigTypeConflictException in case of conflict with the value type set in database |
|
| 404 | + * @since 29.0.0 |
|
| 405 | + * @see IAppConfig for explanation about lazy loading |
|
| 406 | + */ |
|
| 407 | + public function getValueArray( |
|
| 408 | + string $app, |
|
| 409 | + string $key, |
|
| 410 | + array $default = [], |
|
| 411 | + bool $lazy = false, |
|
| 412 | + ): array { |
|
| 413 | + try { |
|
| 414 | + $defaultJson = json_encode($default, JSON_THROW_ON_ERROR); |
|
| 415 | + $value = json_decode($this->getTypedValue($app, $key, $defaultJson, $lazy, self::VALUE_ARRAY), true, flags: JSON_THROW_ON_ERROR); |
|
| 416 | + |
|
| 417 | + return is_array($value) ? $value : []; |
|
| 418 | + } catch (JsonException) { |
|
| 419 | + return []; |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * @param string $app id of the app |
|
| 425 | + * @param string $key config key |
|
| 426 | + * @param string $default default value |
|
| 427 | + * @param bool $lazy search within lazy loaded config |
|
| 428 | + * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT}{@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 429 | + * |
|
| 430 | + * @return string |
|
| 431 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 432 | + * @throws InvalidArgumentException |
|
| 433 | + */ |
|
| 434 | + private function getTypedValue( |
|
| 435 | + string $app, |
|
| 436 | + string $key, |
|
| 437 | + string $default, |
|
| 438 | + bool $lazy, |
|
| 439 | + int $type, |
|
| 440 | + ): string { |
|
| 441 | + $this->assertParams($app, $key, valueType: $type); |
|
| 442 | + $origKey = $key; |
|
| 443 | + $matched = $this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type, $default); |
|
| 444 | + if ($default === null) { |
|
| 445 | + // there is no logical reason for it to be null |
|
| 446 | + throw new \Exception('default cannot be null'); |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + // returns default if strictness of lexicon is set to WARNING (block and report) |
|
| 450 | + if (!$matched) { |
|
| 451 | + return $default; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + $this->loadConfig($app, $lazy); |
|
| 455 | + |
|
| 456 | + /** |
|
| 457 | + * We ignore check if mixed type is requested. |
|
| 458 | + * If type of stored value is set as mixed, we don't filter. |
|
| 459 | + * If type of stored value is defined, we compare with the one requested. |
|
| 460 | + */ |
|
| 461 | + $knownType = $this->valueTypes[$app][$key] ?? 0; |
|
| 462 | + if (!$this->isTyped(self::VALUE_MIXED, $type) |
|
| 463 | + && $knownType > 0 |
|
| 464 | + && !$this->isTyped(self::VALUE_MIXED, $knownType) |
|
| 465 | + && !$this->isTyped($type, $knownType)) { |
|
| 466 | + $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]); |
|
| 467 | + throw new AppConfigTypeConflictException('conflict with value type from database'); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * - the pair $app/$key cannot exist in both array, |
|
| 472 | + * - we should still return an existing non-lazy value even if current method |
|
| 473 | + * is called with $lazy is true |
|
| 474 | + * |
|
| 475 | + * This way, lazyCache will be empty until the load for lazy config value is requested. |
|
| 476 | + */ |
|
| 477 | + if (isset($this->lazyCache[$app][$key])) { |
|
| 478 | + $value = $this->lazyCache[$app][$key]; |
|
| 479 | + } elseif (isset($this->fastCache[$app][$key])) { |
|
| 480 | + $value = $this->fastCache[$app][$key]; |
|
| 481 | + } else { |
|
| 482 | + return $default; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + $sensitive = $this->isTyped(self::VALUE_SENSITIVE, $knownType); |
|
| 486 | + if ($sensitive && str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 487 | + // Only decrypt values that are stored encrypted |
|
| 488 | + $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 489 | + } |
|
| 490 | + |
|
| 491 | + // in case the key was modified while running matchAndApplyLexiconDefinition() we are |
|
| 492 | + // interested to check options in case a modification of the value is needed |
|
| 493 | + // ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN |
|
| 494 | + if ($origKey !== $key && $type === self::VALUE_BOOL) { |
|
| 495 | + $configManager = Server::get(ConfigManager::class); |
|
| 496 | + $value = ($configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0'; |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + return $value; |
|
| 500 | + } |
|
| 501 | + |
|
| 502 | + /** |
|
| 503 | + * @inheritDoc |
|
| 504 | + * |
|
| 505 | + * @param string $app id of the app |
|
| 506 | + * @param string $key config key |
|
| 507 | + * |
|
| 508 | + * @return int type of the value |
|
| 509 | + * @throws AppConfigUnknownKeyException if config key is not known |
|
| 510 | + * @since 29.0.0 |
|
| 511 | + * @see VALUE_STRING |
|
| 512 | + * @see VALUE_INT |
|
| 513 | + * @see VALUE_FLOAT |
|
| 514 | + * @see VALUE_BOOL |
|
| 515 | + * @see VALUE_ARRAY |
|
| 516 | + */ |
|
| 517 | + public function getValueType(string $app, string $key, ?bool $lazy = null): int { |
|
| 518 | + $type = self::VALUE_MIXED; |
|
| 519 | + $ignorable = $lazy ?? false; |
|
| 520 | + $this->matchAndApplyLexiconDefinition($app, $key, $ignorable, $type); |
|
| 521 | + if ($type !== self::VALUE_MIXED) { |
|
| 522 | + // a modified $type means config key is set in Lexicon |
|
| 523 | + return $type; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + $this->assertParams($app, $key); |
|
| 527 | + $this->loadConfig($app, $lazy); |
|
| 528 | + |
|
| 529 | + if (!isset($this->valueTypes[$app][$key])) { |
|
| 530 | + throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 531 | + } |
|
| 532 | + |
|
| 533 | + $type = $this->valueTypes[$app][$key]; |
|
| 534 | + $type &= ~self::VALUE_SENSITIVE; |
|
| 535 | + return $type; |
|
| 536 | + } |
|
| 537 | + |
|
| 538 | + |
|
| 539 | + /** |
|
| 540 | + * Store a config key and its value in database as VALUE_MIXED |
|
| 541 | + * |
|
| 542 | + * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type |
|
| 543 | + * |
|
| 544 | + * @param string $app id of the app |
|
| 545 | + * @param string $key config key |
|
| 546 | + * @param string $value config value |
|
| 547 | + * @param bool $lazy set config as lazy loaded |
|
| 548 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 549 | + * |
|
| 550 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 551 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED |
|
| 552 | + * @internal |
|
| 553 | + * @since 29.0.0 |
|
| 554 | + * @see IAppConfig for explanation about lazy loading |
|
| 555 | + * @see setValueString() |
|
| 556 | + * @see setValueInt() |
|
| 557 | + * @see setValueFloat() |
|
| 558 | + * @see setValueBool() |
|
| 559 | + * @see setValueArray() |
|
| 560 | + */ |
|
| 561 | + public function setValueMixed( |
|
| 562 | + string $app, |
|
| 563 | + string $key, |
|
| 564 | + string $value, |
|
| 565 | + bool $lazy = false, |
|
| 566 | + bool $sensitive = false, |
|
| 567 | + ): bool { |
|
| 568 | + return $this->setTypedValue( |
|
| 569 | + $app, |
|
| 570 | + $key, |
|
| 571 | + $value, |
|
| 572 | + $lazy, |
|
| 573 | + self::VALUE_MIXED | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 574 | + ); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + |
|
| 578 | + /** |
|
| 579 | + * @inheritDoc |
|
| 580 | + * |
|
| 581 | + * @param string $app id of the app |
|
| 582 | + * @param string $key config key |
|
| 583 | + * @param string $value config value |
|
| 584 | + * @param bool $lazy set config as lazy loaded |
|
| 585 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 586 | + * |
|
| 587 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 588 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 589 | + * @since 29.0.0 |
|
| 590 | + * @see IAppConfig for explanation about lazy loading |
|
| 591 | + */ |
|
| 592 | + public function setValueString( |
|
| 593 | + string $app, |
|
| 594 | + string $key, |
|
| 595 | + string $value, |
|
| 596 | + bool $lazy = false, |
|
| 597 | + bool $sensitive = false, |
|
| 598 | + ): bool { |
|
| 599 | + return $this->setTypedValue( |
|
| 600 | + $app, |
|
| 601 | + $key, |
|
| 602 | + $value, |
|
| 603 | + $lazy, |
|
| 604 | + self::VALUE_STRING | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 605 | + ); |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + /** |
|
| 609 | + * @inheritDoc |
|
| 610 | + * |
|
| 611 | + * @param string $app id of the app |
|
| 612 | + * @param string $key config key |
|
| 613 | + * @param int $value config value |
|
| 614 | + * @param bool $lazy set config as lazy loaded |
|
| 615 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 616 | + * |
|
| 617 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 618 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 619 | + * @since 29.0.0 |
|
| 620 | + * @see IAppConfig for explanation about lazy loading |
|
| 621 | + */ |
|
| 622 | + public function setValueInt( |
|
| 623 | + string $app, |
|
| 624 | + string $key, |
|
| 625 | + int $value, |
|
| 626 | + bool $lazy = false, |
|
| 627 | + bool $sensitive = false, |
|
| 628 | + ): bool { |
|
| 629 | + if ($value > 2000000000) { |
|
| 630 | + $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.'); |
|
| 631 | + } |
|
| 632 | + |
|
| 633 | + return $this->setTypedValue( |
|
| 634 | + $app, |
|
| 635 | + $key, |
|
| 636 | + (string)$value, |
|
| 637 | + $lazy, |
|
| 638 | + self::VALUE_INT | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 639 | + ); |
|
| 640 | + } |
|
| 641 | + |
|
| 642 | + /** |
|
| 643 | + * @inheritDoc |
|
| 644 | + * |
|
| 645 | + * @param string $app id of the app |
|
| 646 | + * @param string $key config key |
|
| 647 | + * @param float $value config value |
|
| 648 | + * @param bool $lazy set config as lazy loaded |
|
| 649 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 650 | + * |
|
| 651 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 652 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 653 | + * @since 29.0.0 |
|
| 654 | + * @see IAppConfig for explanation about lazy loading |
|
| 655 | + */ |
|
| 656 | + public function setValueFloat( |
|
| 657 | + string $app, |
|
| 658 | + string $key, |
|
| 659 | + float $value, |
|
| 660 | + bool $lazy = false, |
|
| 661 | + bool $sensitive = false, |
|
| 662 | + ): bool { |
|
| 663 | + return $this->setTypedValue( |
|
| 664 | + $app, |
|
| 665 | + $key, |
|
| 666 | + (string)$value, |
|
| 667 | + $lazy, |
|
| 668 | + self::VALUE_FLOAT | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 669 | + ); |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + /** |
|
| 673 | + * @inheritDoc |
|
| 674 | + * |
|
| 675 | + * @param string $app id of the app |
|
| 676 | + * @param string $key config key |
|
| 677 | + * @param bool $value config value |
|
| 678 | + * @param bool $lazy set config as lazy loaded |
|
| 679 | + * |
|
| 680 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 681 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 682 | + * @since 29.0.0 |
|
| 683 | + * @see IAppConfig for explanation about lazy loading |
|
| 684 | + */ |
|
| 685 | + public function setValueBool( |
|
| 686 | + string $app, |
|
| 687 | + string $key, |
|
| 688 | + bool $value, |
|
| 689 | + bool $lazy = false, |
|
| 690 | + ): bool { |
|
| 691 | + return $this->setTypedValue( |
|
| 692 | + $app, |
|
| 693 | + $key, |
|
| 694 | + ($value) ? '1' : '0', |
|
| 695 | + $lazy, |
|
| 696 | + self::VALUE_BOOL |
|
| 697 | + ); |
|
| 698 | + } |
|
| 699 | + |
|
| 700 | + /** |
|
| 701 | + * @inheritDoc |
|
| 702 | + * |
|
| 703 | + * @param string $app id of the app |
|
| 704 | + * @param string $key config key |
|
| 705 | + * @param array $value config value |
|
| 706 | + * @param bool $lazy set config as lazy loaded |
|
| 707 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 708 | + * |
|
| 709 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 710 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 711 | + * @throws JsonException |
|
| 712 | + * @since 29.0.0 |
|
| 713 | + * @see IAppConfig for explanation about lazy loading |
|
| 714 | + */ |
|
| 715 | + public function setValueArray( |
|
| 716 | + string $app, |
|
| 717 | + string $key, |
|
| 718 | + array $value, |
|
| 719 | + bool $lazy = false, |
|
| 720 | + bool $sensitive = false, |
|
| 721 | + ): bool { |
|
| 722 | + try { |
|
| 723 | + return $this->setTypedValue( |
|
| 724 | + $app, |
|
| 725 | + $key, |
|
| 726 | + json_encode($value, JSON_THROW_ON_ERROR), |
|
| 727 | + $lazy, |
|
| 728 | + self::VALUE_ARRAY | ($sensitive ? self::VALUE_SENSITIVE : 0) |
|
| 729 | + ); |
|
| 730 | + } catch (JsonException $e) { |
|
| 731 | + $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]); |
|
| 732 | + throw $e; |
|
| 733 | + } |
|
| 734 | + } |
|
| 735 | + |
|
| 736 | + /** |
|
| 737 | + * Store a config key and its value in database |
|
| 738 | + * |
|
| 739 | + * If config key is already known with the exact same config value and same sensitive/lazy status, the |
|
| 740 | + * database is not updated. If config value was previously stored as sensitive, status will not be |
|
| 741 | + * altered. |
|
| 742 | + * |
|
| 743 | + * @param string $app id of the app |
|
| 744 | + * @param string $key config key |
|
| 745 | + * @param string $value config value |
|
| 746 | + * @param bool $lazy config set as lazy loaded |
|
| 747 | + * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT} {@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 748 | + * |
|
| 749 | + * @return bool TRUE if value was updated in database |
|
| 750 | + * @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 751 | + * @see IAppConfig for explanation about lazy loading |
|
| 752 | + */ |
|
| 753 | + private function setTypedValue( |
|
| 754 | + string $app, |
|
| 755 | + string $key, |
|
| 756 | + string $value, |
|
| 757 | + bool $lazy, |
|
| 758 | + int $type, |
|
| 759 | + ): bool { |
|
| 760 | + $this->assertParams($app, $key); |
|
| 761 | + if (!$this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type)) { |
|
| 762 | + return false; // returns false as database is not updated |
|
| 763 | + } |
|
| 764 | + $this->loadConfig(null, $lazy); |
|
| 765 | + |
|
| 766 | + $sensitive = $this->isTyped(self::VALUE_SENSITIVE, $type); |
|
| 767 | + $inserted = $refreshCache = false; |
|
| 768 | + |
|
| 769 | + $origValue = $value; |
|
| 770 | + if ($sensitive || ($this->hasKey($app, $key, $lazy) && $this->isSensitive($app, $key, $lazy))) { |
|
| 771 | + $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 772 | + } |
|
| 773 | + |
|
| 774 | + if ($this->hasKey($app, $key, $lazy)) { |
|
| 775 | + /** |
|
| 776 | + * no update if key is already known with set lazy status and value is |
|
| 777 | + * not different, unless sensitivity is switched from false to true. |
|
| 778 | + */ |
|
| 779 | + if ($origValue === $this->getTypedValue($app, $key, $value, $lazy, $type) |
|
| 780 | + && (!$sensitive || $this->isSensitive($app, $key, $lazy))) { |
|
| 781 | + return false; |
|
| 782 | + } |
|
| 783 | + } else { |
|
| 784 | + /** |
|
| 785 | + * if key is not known yet, we try to insert. |
|
| 786 | + * It might fail if the key exists with a different lazy flag. |
|
| 787 | + */ |
|
| 788 | + try { |
|
| 789 | + $insert = $this->connection->getQueryBuilder(); |
|
| 790 | + $insert->insert('appconfig') |
|
| 791 | + ->setValue('appid', $insert->createNamedParameter($app)) |
|
| 792 | + ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 793 | + ->setValue('type', $insert->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 794 | + ->setValue('configkey', $insert->createNamedParameter($key)) |
|
| 795 | + ->setValue('configvalue', $insert->createNamedParameter($value)); |
|
| 796 | + $insert->executeStatement(); |
|
| 797 | + $inserted = true; |
|
| 798 | + } catch (DBException $e) { |
|
| 799 | + if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { |
|
| 800 | + throw $e; // TODO: throw exception or just log and returns false !? |
|
| 801 | + } |
|
| 802 | + } |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + /** |
|
| 806 | + * We cannot insert a new row, meaning we need to update an already existing one |
|
| 807 | + */ |
|
| 808 | + if (!$inserted) { |
|
| 809 | + $currType = $this->valueTypes[$app][$key] ?? 0; |
|
| 810 | + if ($currType === 0) { // this might happen when switching lazy loading status |
|
| 811 | + $this->loadConfigAll(); |
|
| 812 | + $currType = $this->valueTypes[$app][$key] ?? 0; |
|
| 813 | + } |
|
| 814 | + |
|
| 815 | + /** |
|
| 816 | + * This should only happen during the upgrade process from 28 to 29. |
|
| 817 | + * We only log a warning and set it to VALUE_MIXED. |
|
| 818 | + */ |
|
| 819 | + if ($currType === 0) { |
|
| 820 | + $this->logger->warning('Value type is set to zero (0) in database. This is fine only during the upgrade process from 28 to 29.', ['app' => $app, 'key' => $key]); |
|
| 821 | + $currType = self::VALUE_MIXED; |
|
| 822 | + } |
|
| 823 | + |
|
| 824 | + /** |
|
| 825 | + * we only accept a different type from the one stored in database |
|
| 826 | + * if the one stored in database is not-defined (VALUE_MIXED) |
|
| 827 | + */ |
|
| 828 | + if (!$this->isTyped(self::VALUE_MIXED, $currType) |
|
| 829 | + && ($type | self::VALUE_SENSITIVE) !== ($currType | self::VALUE_SENSITIVE)) { |
|
| 830 | + try { |
|
| 831 | + $currType = $this->convertTypeToString($currType); |
|
| 832 | + $type = $this->convertTypeToString($type); |
|
| 833 | + } catch (AppConfigIncorrectTypeException) { |
|
| 834 | + // can be ignored, this was just needed for a better exception message. |
|
| 835 | + } |
|
| 836 | + throw new AppConfigTypeConflictException('conflict between new type (' . $type . ') and old type (' . $currType . ')'); |
|
| 837 | + } |
|
| 838 | + |
|
| 839 | + // we fix $type if the stored value, or the new value as it might be changed, is set as sensitive |
|
| 840 | + if ($sensitive || $this->isTyped(self::VALUE_SENSITIVE, $currType)) { |
|
| 841 | + $type |= self::VALUE_SENSITIVE; |
|
| 842 | + } |
|
| 843 | + |
|
| 844 | + if ($lazy !== $this->isLazy($app, $key)) { |
|
| 845 | + $refreshCache = true; |
|
| 846 | + } |
|
| 847 | + |
|
| 848 | + $update = $this->connection->getQueryBuilder(); |
|
| 849 | + $update->update('appconfig') |
|
| 850 | + ->set('configvalue', $update->createNamedParameter($value)) |
|
| 851 | + ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 852 | + ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 853 | + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 854 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 855 | + |
|
| 856 | + $update->executeStatement(); |
|
| 857 | + } |
|
| 858 | + |
|
| 859 | + if ($refreshCache) { |
|
| 860 | + $this->clearCache(); |
|
| 861 | + return true; |
|
| 862 | + } |
|
| 863 | + |
|
| 864 | + // update local cache |
|
| 865 | + if ($lazy) { |
|
| 866 | + $this->lazyCache[$app][$key] = $value; |
|
| 867 | + } else { |
|
| 868 | + $this->fastCache[$app][$key] = $value; |
|
| 869 | + } |
|
| 870 | + $this->valueTypes[$app][$key] = $type; |
|
| 871 | + |
|
| 872 | + return true; |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + /** |
|
| 876 | + * Change the type of config value. |
|
| 877 | + * |
|
| 878 | + * **WARNING:** Method is internal and **MUST** not be used as it may break things. |
|
| 879 | + * |
|
| 880 | + * @param string $app id of the app |
|
| 881 | + * @param string $key config key |
|
| 882 | + * @param int $type value type {@see VALUE_STRING} {@see VALUE_INT} {@see VALUE_FLOAT} {@see VALUE_BOOL} {@see VALUE_ARRAY} |
|
| 883 | + * |
|
| 884 | + * @return bool TRUE if database update were necessary |
|
| 885 | + * @throws AppConfigUnknownKeyException if $key is now known in database |
|
| 886 | + * @throws AppConfigIncorrectTypeException if $type is not valid |
|
| 887 | + * @internal |
|
| 888 | + * @since 29.0.0 |
|
| 889 | + */ |
|
| 890 | + public function updateType(string $app, string $key, int $type = self::VALUE_MIXED): bool { |
|
| 891 | + $this->assertParams($app, $key); |
|
| 892 | + $this->loadConfigAll(); |
|
| 893 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 894 | + $this->isLazy($app, $key); // confirm key exists |
|
| 895 | + |
|
| 896 | + // type can only be one type |
|
| 897 | + if (!in_array($type, [self::VALUE_MIXED, self::VALUE_STRING, self::VALUE_INT, self::VALUE_FLOAT, self::VALUE_BOOL, self::VALUE_ARRAY])) { |
|
| 898 | + throw new AppConfigIncorrectTypeException('Unknown value type'); |
|
| 899 | + } |
|
| 900 | + |
|
| 901 | + $currType = $this->valueTypes[$app][$key]; |
|
| 902 | + if (($type | self::VALUE_SENSITIVE) === ($currType | self::VALUE_SENSITIVE)) { |
|
| 903 | + return false; |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + // we complete with sensitive flag if the stored value is set as sensitive |
|
| 907 | + if ($this->isTyped(self::VALUE_SENSITIVE, $currType)) { |
|
| 908 | + $type = $type | self::VALUE_SENSITIVE; |
|
| 909 | + } |
|
| 910 | + |
|
| 911 | + $update = $this->connection->getQueryBuilder(); |
|
| 912 | + $update->update('appconfig') |
|
| 913 | + ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 914 | + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 915 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 916 | + $update->executeStatement(); |
|
| 917 | + $this->valueTypes[$app][$key] = $type; |
|
| 918 | + |
|
| 919 | + return true; |
|
| 920 | + } |
|
| 921 | + |
|
| 922 | + |
|
| 923 | + /** |
|
| 924 | + * @inheritDoc |
|
| 925 | + * |
|
| 926 | + * @param string $app id of the app |
|
| 927 | + * @param string $key config key |
|
| 928 | + * @param bool $sensitive TRUE to set as sensitive, FALSE to unset |
|
| 929 | + * |
|
| 930 | + * @return bool TRUE if entry was found in database and an update was necessary |
|
| 931 | + * @since 29.0.0 |
|
| 932 | + */ |
|
| 933 | + public function updateSensitive(string $app, string $key, bool $sensitive): bool { |
|
| 934 | + $this->assertParams($app, $key); |
|
| 935 | + $this->loadConfigAll(); |
|
| 936 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 937 | + |
|
| 938 | + try { |
|
| 939 | + if ($sensitive === $this->isSensitive($app, $key, null)) { |
|
| 940 | + return false; |
|
| 941 | + } |
|
| 942 | + } catch (AppConfigUnknownKeyException $e) { |
|
| 943 | + return false; |
|
| 944 | + } |
|
| 945 | + |
|
| 946 | + $lazy = $this->isLazy($app, $key); |
|
| 947 | + if ($lazy) { |
|
| 948 | + $cache = $this->lazyCache; |
|
| 949 | + } else { |
|
| 950 | + $cache = $this->fastCache; |
|
| 951 | + } |
|
| 952 | + |
|
| 953 | + if (!isset($cache[$app][$key])) { |
|
| 954 | + throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 955 | + } |
|
| 956 | + |
|
| 957 | + /** |
|
| 958 | + * type returned by getValueType() is already cleaned from sensitive flag |
|
| 959 | + * we just need to update it based on $sensitive and store it in database |
|
| 960 | + */ |
|
| 961 | + $type = $this->getValueType($app, $key); |
|
| 962 | + $value = $cache[$app][$key]; |
|
| 963 | + if ($sensitive) { |
|
| 964 | + $type |= self::VALUE_SENSITIVE; |
|
| 965 | + $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 966 | + } else { |
|
| 967 | + $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 968 | + } |
|
| 969 | + |
|
| 970 | + $update = $this->connection->getQueryBuilder(); |
|
| 971 | + $update->update('appconfig') |
|
| 972 | + ->set('type', $update->createNamedParameter($type, IQueryBuilder::PARAM_INT)) |
|
| 973 | + ->set('configvalue', $update->createNamedParameter($value)) |
|
| 974 | + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 975 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 976 | + $update->executeStatement(); |
|
| 977 | + |
|
| 978 | + $this->valueTypes[$app][$key] = $type; |
|
| 979 | + |
|
| 980 | + return true; |
|
| 981 | + } |
|
| 982 | + |
|
| 983 | + /** |
|
| 984 | + * @inheritDoc |
|
| 985 | + * |
|
| 986 | + * @param string $app id of the app |
|
| 987 | + * @param string $key config key |
|
| 988 | + * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 989 | + * |
|
| 990 | + * @return bool TRUE if entry was found in database and an update was necessary |
|
| 991 | + * @since 29.0.0 |
|
| 992 | + */ |
|
| 993 | + public function updateLazy(string $app, string $key, bool $lazy): bool { |
|
| 994 | + $this->assertParams($app, $key); |
|
| 995 | + $this->loadConfigAll(); |
|
| 996 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 997 | + |
|
| 998 | + try { |
|
| 999 | + if ($lazy === $this->isLazy($app, $key)) { |
|
| 1000 | + return false; |
|
| 1001 | + } |
|
| 1002 | + } catch (AppConfigUnknownKeyException $e) { |
|
| 1003 | + return false; |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + $update = $this->connection->getQueryBuilder(); |
|
| 1007 | + $update->update('appconfig') |
|
| 1008 | + ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1009 | + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1010 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1011 | + $update->executeStatement(); |
|
| 1012 | + |
|
| 1013 | + // At this point, it is a lot safer to clean cache |
|
| 1014 | + $this->clearCache(); |
|
| 1015 | + |
|
| 1016 | + return true; |
|
| 1017 | + } |
|
| 1018 | + |
|
| 1019 | + /** |
|
| 1020 | + * @inheritDoc |
|
| 1021 | + * |
|
| 1022 | + * @param string $app id of the app |
|
| 1023 | + * @param string $key config key |
|
| 1024 | + * |
|
| 1025 | + * @return array |
|
| 1026 | + * @throws AppConfigUnknownKeyException if config key is not known in database |
|
| 1027 | + * @since 29.0.0 |
|
| 1028 | + */ |
|
| 1029 | + public function getDetails(string $app, string $key): array { |
|
| 1030 | + $this->assertParams($app, $key); |
|
| 1031 | + $this->loadConfigAll(); |
|
| 1032 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1033 | + $lazy = $this->isLazy($app, $key); |
|
| 1034 | + |
|
| 1035 | + if ($lazy) { |
|
| 1036 | + $cache = $this->lazyCache; |
|
| 1037 | + } else { |
|
| 1038 | + $cache = $this->fastCache; |
|
| 1039 | + } |
|
| 1040 | + |
|
| 1041 | + $type = $this->getValueType($app, $key); |
|
| 1042 | + try { |
|
| 1043 | + $typeString = $this->convertTypeToString($type); |
|
| 1044 | + } catch (AppConfigIncorrectTypeException $e) { |
|
| 1045 | + $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]); |
|
| 1046 | + $typeString = (string)$type; |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + if (!isset($cache[$app][$key])) { |
|
| 1050 | + throw new AppConfigUnknownKeyException('unknown config key'); |
|
| 1051 | + } |
|
| 1052 | + |
|
| 1053 | + $value = $cache[$app][$key]; |
|
| 1054 | + $sensitive = $this->isSensitive($app, $key, null); |
|
| 1055 | + if ($sensitive && str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 1056 | + $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 1057 | + } |
|
| 1058 | + |
|
| 1059 | + return [ |
|
| 1060 | + 'app' => $app, |
|
| 1061 | + 'key' => $key, |
|
| 1062 | + 'value' => $value, |
|
| 1063 | + 'type' => $type, |
|
| 1064 | + 'lazy' => $lazy, |
|
| 1065 | + 'typeString' => $typeString, |
|
| 1066 | + 'sensitive' => $sensitive |
|
| 1067 | + ]; |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + /** |
|
| 1071 | + * @param string $type |
|
| 1072 | + * |
|
| 1073 | + * @return int |
|
| 1074 | + * @throws AppConfigIncorrectTypeException |
|
| 1075 | + * @since 29.0.0 |
|
| 1076 | + */ |
|
| 1077 | + public function convertTypeToInt(string $type): int { |
|
| 1078 | + return match (strtolower($type)) { |
|
| 1079 | + 'mixed' => IAppConfig::VALUE_MIXED, |
|
| 1080 | + 'string' => IAppConfig::VALUE_STRING, |
|
| 1081 | + 'integer' => IAppConfig::VALUE_INT, |
|
| 1082 | + 'float' => IAppConfig::VALUE_FLOAT, |
|
| 1083 | + 'boolean' => IAppConfig::VALUE_BOOL, |
|
| 1084 | + 'array' => IAppConfig::VALUE_ARRAY, |
|
| 1085 | + default => throw new AppConfigIncorrectTypeException('Unknown type ' . $type) |
|
| 1086 | + }; |
|
| 1087 | + } |
|
| 1088 | + |
|
| 1089 | + /** |
|
| 1090 | + * @param int $type |
|
| 1091 | + * |
|
| 1092 | + * @return string |
|
| 1093 | + * @throws AppConfigIncorrectTypeException |
|
| 1094 | + * @since 29.0.0 |
|
| 1095 | + */ |
|
| 1096 | + public function convertTypeToString(int $type): string { |
|
| 1097 | + $type &= ~self::VALUE_SENSITIVE; |
|
| 1098 | + |
|
| 1099 | + return match ($type) { |
|
| 1100 | + IAppConfig::VALUE_MIXED => 'mixed', |
|
| 1101 | + IAppConfig::VALUE_STRING => 'string', |
|
| 1102 | + IAppConfig::VALUE_INT => 'integer', |
|
| 1103 | + IAppConfig::VALUE_FLOAT => 'float', |
|
| 1104 | + IAppConfig::VALUE_BOOL => 'boolean', |
|
| 1105 | + IAppConfig::VALUE_ARRAY => 'array', |
|
| 1106 | + default => throw new AppConfigIncorrectTypeException('Unknown numeric type ' . $type) |
|
| 1107 | + }; |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + /** |
|
| 1111 | + * @inheritDoc |
|
| 1112 | + * |
|
| 1113 | + * @param string $app id of the app |
|
| 1114 | + * @param string $key config key |
|
| 1115 | + * |
|
| 1116 | + * @since 29.0.0 |
|
| 1117 | + */ |
|
| 1118 | + public function deleteKey(string $app, string $key): void { |
|
| 1119 | + $this->assertParams($app, $key); |
|
| 1120 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1121 | + |
|
| 1122 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1123 | + $qb->delete('appconfig') |
|
| 1124 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1125 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1126 | + $qb->executeStatement(); |
|
| 1127 | + |
|
| 1128 | + unset($this->lazyCache[$app][$key]); |
|
| 1129 | + unset($this->fastCache[$app][$key]); |
|
| 1130 | + unset($this->valueTypes[$app][$key]); |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + /** |
|
| 1134 | + * @inheritDoc |
|
| 1135 | + * |
|
| 1136 | + * @param string $app id of the app |
|
| 1137 | + * |
|
| 1138 | + * @since 29.0.0 |
|
| 1139 | + */ |
|
| 1140 | + public function deleteApp(string $app): void { |
|
| 1141 | + $this->assertParams($app); |
|
| 1142 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1143 | + $qb->delete('appconfig') |
|
| 1144 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 1145 | + $qb->executeStatement(); |
|
| 1146 | + |
|
| 1147 | + $this->clearCache(); |
|
| 1148 | + } |
|
| 1149 | + |
|
| 1150 | + /** |
|
| 1151 | + * @inheritDoc |
|
| 1152 | + * |
|
| 1153 | + * @param bool $reload set to TRUE to refill cache instantly after clearing it |
|
| 1154 | + * |
|
| 1155 | + * @since 29.0.0 |
|
| 1156 | + */ |
|
| 1157 | + public function clearCache(bool $reload = false): void { |
|
| 1158 | + $this->lazyLoaded = $this->fastLoaded = false; |
|
| 1159 | + $this->lazyCache = $this->fastCache = $this->valueTypes = $this->configLexiconDetails = []; |
|
| 1160 | + $this->configLexiconPreset = null; |
|
| 1161 | + |
|
| 1162 | + if (!$reload) { |
|
| 1163 | + return; |
|
| 1164 | + } |
|
| 1165 | + |
|
| 1166 | + $this->loadConfigAll(); |
|
| 1167 | + } |
|
| 1168 | + |
|
| 1169 | + |
|
| 1170 | + /** |
|
| 1171 | + * For debug purpose. |
|
| 1172 | + * Returns the cached data. |
|
| 1173 | + * |
|
| 1174 | + * @return array |
|
| 1175 | + * @since 29.0.0 |
|
| 1176 | + * @internal |
|
| 1177 | + */ |
|
| 1178 | + public function statusCache(): array { |
|
| 1179 | + return [ |
|
| 1180 | + 'fastLoaded' => $this->fastLoaded, |
|
| 1181 | + 'fastCache' => $this->fastCache, |
|
| 1182 | + 'lazyLoaded' => $this->lazyLoaded, |
|
| 1183 | + 'lazyCache' => $this->lazyCache, |
|
| 1184 | + ]; |
|
| 1185 | + } |
|
| 1186 | + |
|
| 1187 | + /** |
|
| 1188 | + * @param int $needle bitflag to search |
|
| 1189 | + * @param int $type known value |
|
| 1190 | + * |
|
| 1191 | + * @return bool TRUE if bitflag $needle is set in $type |
|
| 1192 | + */ |
|
| 1193 | + private function isTyped(int $needle, int $type): bool { |
|
| 1194 | + return (($needle & $type) !== 0); |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + /** |
|
| 1198 | + * Confirm the string set for app and key fit the database description |
|
| 1199 | + * |
|
| 1200 | + * @param string $app assert $app fit in database |
|
| 1201 | + * @param string $configKey assert config key fit in database |
|
| 1202 | + * @param bool $allowEmptyApp $app can be empty string |
|
| 1203 | + * @param int $valueType assert value type is only one type |
|
| 1204 | + * |
|
| 1205 | + * @throws InvalidArgumentException |
|
| 1206 | + */ |
|
| 1207 | + private function assertParams(string $app = '', string $configKey = '', bool $allowEmptyApp = false, int $valueType = -1): void { |
|
| 1208 | + if (!$allowEmptyApp && $app === '') { |
|
| 1209 | + throw new InvalidArgumentException('app cannot be an empty string'); |
|
| 1210 | + } |
|
| 1211 | + if (strlen($app) > self::APP_MAX_LENGTH) { |
|
| 1212 | + throw new InvalidArgumentException( |
|
| 1213 | + 'Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')' |
|
| 1214 | + ); |
|
| 1215 | + } |
|
| 1216 | + if (strlen($configKey) > self::KEY_MAX_LENGTH) { |
|
| 1217 | + throw new InvalidArgumentException('Value (' . $configKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); |
|
| 1218 | + } |
|
| 1219 | + if ($valueType > -1) { |
|
| 1220 | + $valueType &= ~self::VALUE_SENSITIVE; |
|
| 1221 | + if (!in_array($valueType, [self::VALUE_MIXED, self::VALUE_STRING, self::VALUE_INT, self::VALUE_FLOAT, self::VALUE_BOOL, self::VALUE_ARRAY])) { |
|
| 1222 | + throw new InvalidArgumentException('Unknown value type'); |
|
| 1223 | + } |
|
| 1224 | + } |
|
| 1225 | + } |
|
| 1226 | + |
|
| 1227 | + private function loadConfigAll(?string $app = null): void { |
|
| 1228 | + $this->loadConfig($app, null); |
|
| 1229 | + } |
|
| 1230 | + |
|
| 1231 | + /** |
|
| 1232 | + * Load normal config or config set as lazy loaded |
|
| 1233 | + * |
|
| 1234 | + * @param bool|null $lazy set to TRUE to load config set as lazy loaded, set to NULL to load all config |
|
| 1235 | + */ |
|
| 1236 | + private function loadConfig(?string $app = null, ?bool $lazy = false): void { |
|
| 1237 | + if ($this->isLoaded($lazy)) { |
|
| 1238 | + return; |
|
| 1239 | + } |
|
| 1240 | + |
|
| 1241 | + // if lazy is null or true, we debug log |
|
| 1242 | + if (($lazy ?? true) !== false && $app !== null) { |
|
| 1243 | + $exception = new \RuntimeException('The loading of lazy AppConfig values have been triggered by app "' . $app . '"'); |
|
| 1244 | + $this->logger->debug($exception->getMessage(), ['exception' => $exception, 'app' => $app]); |
|
| 1245 | + } |
|
| 1246 | + |
|
| 1247 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1248 | + $qb->from('appconfig'); |
|
| 1249 | + |
|
| 1250 | + // we only need value from lazy when loadConfig does not specify it |
|
| 1251 | + $qb->select('appid', 'configkey', 'configvalue', 'type'); |
|
| 1252 | + |
|
| 1253 | + if ($lazy !== null) { |
|
| 1254 | + $qb->where($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))); |
|
| 1255 | + } else { |
|
| 1256 | + $qb->addSelect('lazy'); |
|
| 1257 | + } |
|
| 1258 | + |
|
| 1259 | + $result = $qb->executeQuery(); |
|
| 1260 | + $rows = $result->fetchAll(); |
|
| 1261 | + foreach ($rows as $row) { |
|
| 1262 | + // most of the time, 'lazy' is not in the select because its value is already known |
|
| 1263 | + if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) { |
|
| 1264 | + $this->lazyCache[$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1265 | + } else { |
|
| 1266 | + $this->fastCache[$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1267 | + } |
|
| 1268 | + $this->valueTypes[$row['appid']][$row['configkey']] = (int)($row['type'] ?? 0); |
|
| 1269 | + } |
|
| 1270 | + $result->closeCursor(); |
|
| 1271 | + $this->setAsLoaded($lazy); |
|
| 1272 | + } |
|
| 1273 | + |
|
| 1274 | + /** |
|
| 1275 | + * if $lazy is: |
|
| 1276 | + * - false: will returns true if fast config is loaded |
|
| 1277 | + * - true : will returns true if lazy config is loaded |
|
| 1278 | + * - null : will returns true if both config are loaded |
|
| 1279 | + * |
|
| 1280 | + * @param bool $lazy |
|
| 1281 | + * |
|
| 1282 | + * @return bool |
|
| 1283 | + */ |
|
| 1284 | + private function isLoaded(?bool $lazy): bool { |
|
| 1285 | + if ($lazy === null) { |
|
| 1286 | + return $this->lazyLoaded && $this->fastLoaded; |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + return $lazy ? $this->lazyLoaded : $this->fastLoaded; |
|
| 1290 | + } |
|
| 1291 | + |
|
| 1292 | + /** |
|
| 1293 | + * if $lazy is: |
|
| 1294 | + * - false: set fast config as loaded |
|
| 1295 | + * - true : set lazy config as loaded |
|
| 1296 | + * - null : set both config as loaded |
|
| 1297 | + * |
|
| 1298 | + * @param bool $lazy |
|
| 1299 | + */ |
|
| 1300 | + private function setAsLoaded(?bool $lazy): void { |
|
| 1301 | + if ($lazy === null) { |
|
| 1302 | + $this->fastLoaded = true; |
|
| 1303 | + $this->lazyLoaded = true; |
|
| 1304 | + |
|
| 1305 | + return; |
|
| 1306 | + } |
|
| 1307 | + |
|
| 1308 | + if ($lazy) { |
|
| 1309 | + $this->lazyLoaded = true; |
|
| 1310 | + } else { |
|
| 1311 | + $this->fastLoaded = true; |
|
| 1312 | + } |
|
| 1313 | + } |
|
| 1314 | + |
|
| 1315 | + /** |
|
| 1316 | + * Gets the config value |
|
| 1317 | + * |
|
| 1318 | + * @param string $app app |
|
| 1319 | + * @param string $key key |
|
| 1320 | + * @param string $default = null, default value if the key does not exist |
|
| 1321 | + * |
|
| 1322 | + * @return string the value or $default |
|
| 1323 | + * @deprecated 29.0.0 use getValue*() |
|
| 1324 | + * |
|
| 1325 | + * This function gets a value from the appconfig table. If the key does |
|
| 1326 | + * not exist the default value will be returned |
|
| 1327 | + */ |
|
| 1328 | + public function getValue($app, $key, $default = null) { |
|
| 1329 | + $this->loadConfig($app); |
|
| 1330 | + $this->matchAndApplyLexiconDefinition($app, $key); |
|
| 1331 | + |
|
| 1332 | + return $this->fastCache[$app][$key] ?? $default; |
|
| 1333 | + } |
|
| 1334 | + |
|
| 1335 | + /** |
|
| 1336 | + * Sets a value. If the key did not exist before it will be created. |
|
| 1337 | + * |
|
| 1338 | + * @param string $app app |
|
| 1339 | + * @param string $key key |
|
| 1340 | + * @param string|float|int $value value |
|
| 1341 | + * |
|
| 1342 | + * @return bool True if the value was inserted or updated, false if the value was the same |
|
| 1343 | + * @throws AppConfigTypeConflictException |
|
| 1344 | + * @throws AppConfigUnknownKeyException |
|
| 1345 | + * @deprecated 29.0.0 |
|
| 1346 | + */ |
|
| 1347 | + public function setValue($app, $key, $value) { |
|
| 1348 | + /** |
|
| 1349 | + * TODO: would it be overkill, or decently improve performance, to catch |
|
| 1350 | + * call to this method with $key='enabled' and 'hide' config value related |
|
| 1351 | + * to $app when the app is disabled (by modifying entry in database: lazy=lazy+2) |
|
| 1352 | + * or enabled (lazy=lazy-2) |
|
| 1353 | + * |
|
| 1354 | + * this solution would remove the loading of config values from disabled app |
|
| 1355 | + * unless calling the method {@see loadConfigAll()} |
|
| 1356 | + */ |
|
| 1357 | + return $this->setTypedValue($app, $key, (string)$value, false, self::VALUE_MIXED); |
|
| 1358 | + } |
|
| 1359 | + |
|
| 1360 | + |
|
| 1361 | + /** |
|
| 1362 | + * get multiple values, either the app or key can be used as wildcard by setting it to false |
|
| 1363 | + * |
|
| 1364 | + * @param string|false $app |
|
| 1365 | + * @param string|false $key |
|
| 1366 | + * |
|
| 1367 | + * @return array|false |
|
| 1368 | + * @deprecated 29.0.0 use {@see getAllValues()} |
|
| 1369 | + */ |
|
| 1370 | + public function getValues($app, $key) { |
|
| 1371 | + if (($app !== false) === ($key !== false)) { |
|
| 1372 | + return false; |
|
| 1373 | + } |
|
| 1374 | + |
|
| 1375 | + $key = ($key === false) ? '' : $key; |
|
| 1376 | + if (!$app) { |
|
| 1377 | + return $this->searchValues($key, false, self::VALUE_MIXED); |
|
| 1378 | + } else { |
|
| 1379 | + return $this->getAllValues($app, $key); |
|
| 1380 | + } |
|
| 1381 | + } |
|
| 1382 | + |
|
| 1383 | + /** |
|
| 1384 | + * get all values of the app or and filters out sensitive data |
|
| 1385 | + * |
|
| 1386 | + * @param string $app |
|
| 1387 | + * |
|
| 1388 | + * @return array |
|
| 1389 | + * @deprecated 29.0.0 use {@see getAllValues()} |
|
| 1390 | + */ |
|
| 1391 | + public function getFilteredValues($app) { |
|
| 1392 | + return $this->getAllValues($app, filtered: true); |
|
| 1393 | + } |
|
| 1394 | + |
|
| 1395 | + |
|
| 1396 | + /** |
|
| 1397 | + * **Warning:** avoid default NULL value for $lazy as this will |
|
| 1398 | + * load all lazy values from the database |
|
| 1399 | + * |
|
| 1400 | + * @param string $app |
|
| 1401 | + * @param array<string, string> $values ['key' => 'value'] |
|
| 1402 | + * @param bool|null $lazy |
|
| 1403 | + * |
|
| 1404 | + * @return array<string, string|int|float|bool|array> |
|
| 1405 | + */ |
|
| 1406 | + private function formatAppValues(string $app, array $values, ?bool $lazy = null): array { |
|
| 1407 | + foreach ($values as $key => $value) { |
|
| 1408 | + try { |
|
| 1409 | + $type = $this->getValueType($app, $key, $lazy); |
|
| 1410 | + } catch (AppConfigUnknownKeyException) { |
|
| 1411 | + continue; |
|
| 1412 | + } |
|
| 1413 | + |
|
| 1414 | + $values[$key] = $this->convertTypedValue($value, $type); |
|
| 1415 | + } |
|
| 1416 | + |
|
| 1417 | + return $values; |
|
| 1418 | + } |
|
| 1419 | + |
|
| 1420 | + /** |
|
| 1421 | + * convert string value to the expected type |
|
| 1422 | + * |
|
| 1423 | + * @param string $value |
|
| 1424 | + * @param int $type |
|
| 1425 | + * |
|
| 1426 | + * @return string|int|float|bool|array |
|
| 1427 | + */ |
|
| 1428 | + private function convertTypedValue(string $value, int $type): string|int|float|bool|array { |
|
| 1429 | + switch ($type) { |
|
| 1430 | + case self::VALUE_INT: |
|
| 1431 | + return (int)$value; |
|
| 1432 | + case self::VALUE_FLOAT: |
|
| 1433 | + return (float)$value; |
|
| 1434 | + case self::VALUE_BOOL: |
|
| 1435 | + return in_array(strtolower($value), ['1', 'true', 'yes', 'on']); |
|
| 1436 | + case self::VALUE_ARRAY: |
|
| 1437 | + try { |
|
| 1438 | + return json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 1439 | + } catch (JsonException $e) { |
|
| 1440 | + // ignoreable |
|
| 1441 | + } |
|
| 1442 | + break; |
|
| 1443 | + } |
|
| 1444 | + return $value; |
|
| 1445 | + } |
|
| 1446 | + |
|
| 1447 | + /** |
|
| 1448 | + * @param string $app |
|
| 1449 | + * |
|
| 1450 | + * @return string[] |
|
| 1451 | + * @deprecated 29.0.0 data sensitivity should be set when calling setValue*() |
|
| 1452 | + */ |
|
| 1453 | + private function getSensitiveKeys(string $app): array { |
|
| 1454 | + $sensitiveValues = [ |
|
| 1455 | + 'circles' => [ |
|
| 1456 | + '/^key_pairs$/', |
|
| 1457 | + '/^local_gskey$/', |
|
| 1458 | + ], |
|
| 1459 | + 'call_summary_bot' => [ |
|
| 1460 | + '/^secret_(.*)$/', |
|
| 1461 | + ], |
|
| 1462 | + 'external' => [ |
|
| 1463 | + '/^sites$/', |
|
| 1464 | + '/^jwt_token_privkey_(.*)$/', |
|
| 1465 | + ], |
|
| 1466 | + 'globalsiteselector' => [ |
|
| 1467 | + '/^gss\.jwt\.key$/', |
|
| 1468 | + ], |
|
| 1469 | + 'gpgmailer' => [ |
|
| 1470 | + '/^GpgServerKey$/', |
|
| 1471 | + ], |
|
| 1472 | + 'integration_discourse' => [ |
|
| 1473 | + '/^private_key$/', |
|
| 1474 | + '/^public_key$/', |
|
| 1475 | + ], |
|
| 1476 | + 'integration_dropbox' => [ |
|
| 1477 | + '/^client_id$/', |
|
| 1478 | + '/^client_secret$/', |
|
| 1479 | + ], |
|
| 1480 | + 'integration_github' => [ |
|
| 1481 | + '/^client_id$/', |
|
| 1482 | + '/^client_secret$/', |
|
| 1483 | + ], |
|
| 1484 | + 'integration_gitlab' => [ |
|
| 1485 | + '/^client_id$/', |
|
| 1486 | + '/^client_secret$/', |
|
| 1487 | + '/^oauth_instance_url$/', |
|
| 1488 | + ], |
|
| 1489 | + 'integration_google' => [ |
|
| 1490 | + '/^client_id$/', |
|
| 1491 | + '/^client_secret$/', |
|
| 1492 | + ], |
|
| 1493 | + 'integration_jira' => [ |
|
| 1494 | + '/^client_id$/', |
|
| 1495 | + '/^client_secret$/', |
|
| 1496 | + '/^forced_instance_url$/', |
|
| 1497 | + ], |
|
| 1498 | + 'integration_onedrive' => [ |
|
| 1499 | + '/^client_id$/', |
|
| 1500 | + '/^client_secret$/', |
|
| 1501 | + ], |
|
| 1502 | + 'integration_openproject' => [ |
|
| 1503 | + '/^client_id$/', |
|
| 1504 | + '/^client_secret$/', |
|
| 1505 | + '/^oauth_instance_url$/', |
|
| 1506 | + ], |
|
| 1507 | + 'integration_reddit' => [ |
|
| 1508 | + '/^client_id$/', |
|
| 1509 | + '/^client_secret$/', |
|
| 1510 | + ], |
|
| 1511 | + 'integration_suitecrm' => [ |
|
| 1512 | + '/^client_id$/', |
|
| 1513 | + '/^client_secret$/', |
|
| 1514 | + '/^oauth_instance_url$/', |
|
| 1515 | + ], |
|
| 1516 | + 'integration_twitter' => [ |
|
| 1517 | + '/^consumer_key$/', |
|
| 1518 | + '/^consumer_secret$/', |
|
| 1519 | + '/^followed_user$/', |
|
| 1520 | + ], |
|
| 1521 | + 'integration_zammad' => [ |
|
| 1522 | + '/^client_id$/', |
|
| 1523 | + '/^client_secret$/', |
|
| 1524 | + '/^oauth_instance_url$/', |
|
| 1525 | + ], |
|
| 1526 | + 'maps' => [ |
|
| 1527 | + '/^mapboxAPIKEY$/', |
|
| 1528 | + ], |
|
| 1529 | + 'notify_push' => [ |
|
| 1530 | + '/^cookie$/', |
|
| 1531 | + ], |
|
| 1532 | + 'onlyoffice' => [ |
|
| 1533 | + '/^jwt_secret$/', |
|
| 1534 | + ], |
|
| 1535 | + 'passwords' => [ |
|
| 1536 | + '/^SSEv1ServerKey$/', |
|
| 1537 | + ], |
|
| 1538 | + 'serverinfo' => [ |
|
| 1539 | + '/^token$/', |
|
| 1540 | + ], |
|
| 1541 | + 'spreed' => [ |
|
| 1542 | + '/^bridge_bot_password$/', |
|
| 1543 | + '/^hosted-signaling-server-(.*)$/', |
|
| 1544 | + '/^recording_servers$/', |
|
| 1545 | + '/^signaling_servers$/', |
|
| 1546 | + '/^signaling_ticket_secret$/', |
|
| 1547 | + '/^signaling_token_privkey_(.*)$/', |
|
| 1548 | + '/^signaling_token_pubkey_(.*)$/', |
|
| 1549 | + '/^sip_bridge_dialin_info$/', |
|
| 1550 | + '/^sip_bridge_shared_secret$/', |
|
| 1551 | + '/^stun_servers$/', |
|
| 1552 | + '/^turn_servers$/', |
|
| 1553 | + '/^turn_server_secret$/', |
|
| 1554 | + ], |
|
| 1555 | + 'support' => [ |
|
| 1556 | + '/^last_response$/', |
|
| 1557 | + '/^potential_subscription_key$/', |
|
| 1558 | + '/^subscription_key$/', |
|
| 1559 | + ], |
|
| 1560 | + 'theming' => [ |
|
| 1561 | + '/^imprintUrl$/', |
|
| 1562 | + '/^privacyUrl$/', |
|
| 1563 | + '/^slogan$/', |
|
| 1564 | + '/^url$/', |
|
| 1565 | + ], |
|
| 1566 | + 'twofactor_gateway' => [ |
|
| 1567 | + '/^.*token$/', |
|
| 1568 | + ], |
|
| 1569 | + 'user_ldap' => [ |
|
| 1570 | + '/^(s..)?ldap_agent_password$/', |
|
| 1571 | + ], |
|
| 1572 | + 'user_saml' => [ |
|
| 1573 | + '/^idp-x509cert$/', |
|
| 1574 | + ], |
|
| 1575 | + 'whiteboard' => [ |
|
| 1576 | + '/^jwt_secret_key$/', |
|
| 1577 | + ], |
|
| 1578 | + ]; |
|
| 1579 | + |
|
| 1580 | + return $sensitiveValues[$app] ?? []; |
|
| 1581 | + } |
|
| 1582 | + |
|
| 1583 | + /** |
|
| 1584 | + * Clear all the cached app config values |
|
| 1585 | + * New cache will be generated next time a config value is retrieved |
|
| 1586 | + * |
|
| 1587 | + * @deprecated 29.0.0 use {@see clearCache()} |
|
| 1588 | + */ |
|
| 1589 | + public function clearCachedConfig(): void { |
|
| 1590 | + $this->clearCache(); |
|
| 1591 | + } |
|
| 1592 | + |
|
| 1593 | + /** |
|
| 1594 | + * Match and apply current use of config values with defined lexicon. |
|
| 1595 | + * Set $lazy to NULL only if only interested into checking that $key is alias. |
|
| 1596 | + * |
|
| 1597 | + * @throws AppConfigUnknownKeyException |
|
| 1598 | + * @throws AppConfigTypeConflictException |
|
| 1599 | + * @return bool TRUE if everything is fine compared to lexicon or lexicon does not exist |
|
| 1600 | + */ |
|
| 1601 | + private function matchAndApplyLexiconDefinition( |
|
| 1602 | + string $app, |
|
| 1603 | + string &$key, |
|
| 1604 | + ?bool &$lazy = null, |
|
| 1605 | + int &$type = self::VALUE_MIXED, |
|
| 1606 | + ?string &$default = null, |
|
| 1607 | + ): bool { |
|
| 1608 | + if (in_array($key, |
|
| 1609 | + [ |
|
| 1610 | + 'enabled', |
|
| 1611 | + 'installed_version', |
|
| 1612 | + 'types', |
|
| 1613 | + ])) { |
|
| 1614 | + return true; // we don't break stuff for this list of config keys. |
|
| 1615 | + } |
|
| 1616 | + $configDetails = $this->getConfigDetailsFromLexicon($app); |
|
| 1617 | + if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) { |
|
| 1618 | + // in case '$rename' is set in ConfigLexiconEntry, we use the new config key |
|
| 1619 | + $key = $configDetails['aliases'][$key]; |
|
| 1620 | + } |
|
| 1621 | + |
|
| 1622 | + if (!array_key_exists($key, $configDetails['entries'])) { |
|
| 1623 | + return $this->applyLexiconStrictness($configDetails['strictness'], 'The app config key ' . $app . '/' . $key . ' is not defined in the config lexicon'); |
|
| 1624 | + } |
|
| 1625 | + |
|
| 1626 | + // if lazy is NULL, we ignore all check on the type/lazyness/default from Lexicon |
|
| 1627 | + if ($lazy === null) { |
|
| 1628 | + return true; |
|
| 1629 | + } |
|
| 1630 | + |
|
| 1631 | + /** @var ConfigLexiconEntry $configValue */ |
|
| 1632 | + $configValue = $configDetails['entries'][$key]; |
|
| 1633 | + $type &= ~self::VALUE_SENSITIVE; |
|
| 1634 | + |
|
| 1635 | + $appConfigValueType = $configValue->getValueType()->toAppConfigFlag(); |
|
| 1636 | + if ($type === self::VALUE_MIXED) { |
|
| 1637 | + $type = $appConfigValueType; // we overwrite if value was requested as mixed |
|
| 1638 | + } elseif ($appConfigValueType !== $type) { |
|
| 1639 | + throw new AppConfigTypeConflictException('The app config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon'); |
|
| 1640 | + } |
|
| 1641 | + |
|
| 1642 | + $lazy = $configValue->isLazy(); |
|
| 1643 | + // only look for default if needed, default from Lexicon got priority |
|
| 1644 | + if ($default !== null) { |
|
| 1645 | + $default = $configValue->getDefault($this->getLexiconPreset()) ?? $default; |
|
| 1646 | + } |
|
| 1647 | + |
|
| 1648 | + if ($configValue->isFlagged(self::FLAG_SENSITIVE)) { |
|
| 1649 | + $type |= self::VALUE_SENSITIVE; |
|
| 1650 | + } |
|
| 1651 | + if ($configValue->isDeprecated()) { |
|
| 1652 | + $this->logger->notice('App config key ' . $app . '/' . $key . ' is set as deprecated.'); |
|
| 1653 | + } |
|
| 1654 | + |
|
| 1655 | + return true; |
|
| 1656 | + } |
|
| 1657 | + |
|
| 1658 | + /** |
|
| 1659 | + * manage ConfigLexicon behavior based on strictness set in IConfigLexicon |
|
| 1660 | + * |
|
| 1661 | + * @param ConfigLexiconStrictness|null $strictness |
|
| 1662 | + * @param string $line |
|
| 1663 | + * |
|
| 1664 | + * @return bool TRUE if conflict can be fully ignored, FALSE if action should be not performed |
|
| 1665 | + * @throws AppConfigUnknownKeyException if strictness implies exception |
|
| 1666 | + * @see IConfigLexicon::getStrictness() |
|
| 1667 | + */ |
|
| 1668 | + private function applyLexiconStrictness( |
|
| 1669 | + ?ConfigLexiconStrictness $strictness, |
|
| 1670 | + string $line = '', |
|
| 1671 | + ): bool { |
|
| 1672 | + if ($strictness === null) { |
|
| 1673 | + return true; |
|
| 1674 | + } |
|
| 1675 | + |
|
| 1676 | + switch ($strictness) { |
|
| 1677 | + case ConfigLexiconStrictness::IGNORE: |
|
| 1678 | + return true; |
|
| 1679 | + case ConfigLexiconStrictness::NOTICE: |
|
| 1680 | + $this->logger->notice($line); |
|
| 1681 | + return true; |
|
| 1682 | + case ConfigLexiconStrictness::WARNING: |
|
| 1683 | + $this->logger->warning($line); |
|
| 1684 | + return false; |
|
| 1685 | + } |
|
| 1686 | + |
|
| 1687 | + throw new AppConfigUnknownKeyException($line); |
|
| 1688 | + } |
|
| 1689 | + |
|
| 1690 | + /** |
|
| 1691 | + * extract details from registered $appId's config lexicon |
|
| 1692 | + * |
|
| 1693 | + * @param string $appId |
|
| 1694 | + * @internal |
|
| 1695 | + * |
|
| 1696 | + * @return array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness} |
|
| 1697 | + */ |
|
| 1698 | + public function getConfigDetailsFromLexicon(string $appId): array { |
|
| 1699 | + if (!array_key_exists($appId, $this->configLexiconDetails)) { |
|
| 1700 | + $entries = $aliases = []; |
|
| 1701 | + $bootstrapCoordinator = \OCP\Server::get(Coordinator::class); |
|
| 1702 | + $configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId); |
|
| 1703 | + foreach ($configLexicon?->getAppConfigs() ?? [] as $configEntry) { |
|
| 1704 | + $entries[$configEntry->getKey()] = $configEntry; |
|
| 1705 | + if ($configEntry->getRename() !== null) { |
|
| 1706 | + $aliases[$configEntry->getRename()] = $configEntry->getKey(); |
|
| 1707 | + } |
|
| 1708 | + } |
|
| 1709 | + |
|
| 1710 | + $this->configLexiconDetails[$appId] = [ |
|
| 1711 | + 'entries' => $entries, |
|
| 1712 | + 'aliases' => $aliases, |
|
| 1713 | + 'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE |
|
| 1714 | + ]; |
|
| 1715 | + } |
|
| 1716 | + |
|
| 1717 | + return $this->configLexiconDetails[$appId]; |
|
| 1718 | + } |
|
| 1719 | + |
|
| 1720 | + private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry { |
|
| 1721 | + return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null; |
|
| 1722 | + } |
|
| 1723 | + |
|
| 1724 | + /** |
|
| 1725 | + * if set to TRUE, ignore aliases defined in Config Lexicon during the use of the methods of this class |
|
| 1726 | + * |
|
| 1727 | + * @internal |
|
| 1728 | + */ |
|
| 1729 | + public function ignoreLexiconAliases(bool $ignore): void { |
|
| 1730 | + $this->ignoreLexiconAliases = $ignore; |
|
| 1731 | + } |
|
| 1732 | + |
|
| 1733 | + private function getLexiconPreset(): Preset { |
|
| 1734 | + if ($this->configLexiconPreset === null) { |
|
| 1735 | + $this->configLexiconPreset = Preset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? Preset::NONE; |
|
| 1736 | + } |
|
| 1737 | + |
|
| 1738 | + return $this->configLexiconPreset; |
|
| 1739 | + } |
|
| 1740 | + |
|
| 1741 | + /** |
|
| 1742 | + * Returns the installed versions of all apps |
|
| 1743 | + * |
|
| 1744 | + * @return array<string, string> |
|
| 1745 | + */ |
|
| 1746 | + public function getAppInstalledVersions(bool $onlyEnabled = false): array { |
|
| 1747 | + if ($this->appVersionsCache === null) { |
|
| 1748 | + /** @var array<string, string> */ |
|
| 1749 | + $this->appVersionsCache = $this->searchValues('installed_version', false, IAppConfig::VALUE_STRING); |
|
| 1750 | + } |
|
| 1751 | + if ($onlyEnabled) { |
|
| 1752 | + return array_filter( |
|
| 1753 | + $this->appVersionsCache, |
|
| 1754 | + fn (string $app): bool => $this->getValueString($app, 'enabled', 'no') !== 'no', |
|
| 1755 | + ARRAY_FILTER_USE_KEY |
|
| 1756 | + ); |
|
| 1757 | + } |
|
| 1758 | + return $this->appVersionsCache; |
|
| 1759 | + } |
|
| 1760 | 1760 | } |
@@ -27,241 +27,241 @@ |
||
| 27 | 27 | * @since 32.0.0 |
| 28 | 28 | */ |
| 29 | 29 | class ConfigManager { |
| 30 | - /** @since 32.0.0 */ |
|
| 31 | - public const PRESET_CONFIGKEY = 'config_preset'; |
|
| 32 | - |
|
| 33 | - /** @var AppConfig|null $appConfig */ |
|
| 34 | - private ?IAppConfig $appConfig = null; |
|
| 35 | - /** @var UserConfig|null $userConfig */ |
|
| 36 | - private ?IUserConfig $userConfig = null; |
|
| 37 | - |
|
| 38 | - public function __construct( |
|
| 39 | - private readonly IConfig $config, |
|
| 40 | - private readonly LoggerInterface $logger, |
|
| 41 | - ) { |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * Use the rename values from the list of ConfigLexiconEntry defined in each app ConfigLexicon |
|
| 46 | - * to migrate config value to a new config key. |
|
| 47 | - * Migration will only occur if new config key has no value in database. |
|
| 48 | - * The previous value from the key set in rename will be deleted from the database when migration |
|
| 49 | - * is over. |
|
| 50 | - * |
|
| 51 | - * This method should be mainly called during a new upgrade or when a new app is enabled. |
|
| 52 | - * |
|
| 53 | - * @see ConfigLexiconEntry |
|
| 54 | - * @internal |
|
| 55 | - * @since 32.0.0 |
|
| 56 | - * @param string|null $appId when set to NULL the method will be executed for all enabled apps of the instance |
|
| 57 | - */ |
|
| 58 | - public function migrateConfigLexiconKeys(?string $appId = null): void { |
|
| 59 | - if ($appId === null) { |
|
| 60 | - $this->migrateConfigLexiconKeys('core'); |
|
| 61 | - $appManager = Server::get(IAppManager::class); |
|
| 62 | - foreach ($appManager->getEnabledApps() as $app) { |
|
| 63 | - $this->migrateConfigLexiconKeys($app); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - return; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - $this->loadConfigServices(); |
|
| 70 | - |
|
| 71 | - // it is required to ignore aliases when moving config values |
|
| 72 | - $this->appConfig->ignoreLexiconAliases(true); |
|
| 73 | - $this->userConfig->ignoreLexiconAliases(true); |
|
| 74 | - |
|
| 75 | - $this->migrateAppConfigKeys($appId); |
|
| 76 | - $this->migrateUserConfigKeys($appId); |
|
| 77 | - |
|
| 78 | - // switch back to normal behavior |
|
| 79 | - $this->appConfig->ignoreLexiconAliases(false); |
|
| 80 | - $this->userConfig->ignoreLexiconAliases(false); |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * store in config.php the new preset |
|
| 85 | - * refresh cached preset |
|
| 86 | - */ |
|
| 87 | - public function setLexiconPreset(Preset $preset): void { |
|
| 88 | - $this->config->setSystemValue(self::PRESET_CONFIGKEY, $preset->value); |
|
| 89 | - $this->loadConfigServices(); |
|
| 90 | - $this->appConfig->clearCache(); |
|
| 91 | - $this->userConfig->clearCacheAll(); |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * config services cannot be load at __construct() or install will fail |
|
| 96 | - */ |
|
| 97 | - private function loadConfigServices(): void { |
|
| 98 | - if ($this->appConfig === null) { |
|
| 99 | - $this->appConfig = Server::get(IAppConfig::class); |
|
| 100 | - } |
|
| 101 | - if ($this->userConfig === null) { |
|
| 102 | - $this->userConfig = Server::get(IUserConfig::class); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - /** |
|
| 107 | - * Get details from lexicon related to AppConfig and search for entries with rename to initiate |
|
| 108 | - * a migration to new config key |
|
| 109 | - */ |
|
| 110 | - private function migrateAppConfigKeys(string $appId): void { |
|
| 111 | - $lexicon = $this->appConfig->getConfigDetailsFromLexicon($appId); |
|
| 112 | - foreach ($lexicon['entries'] as $entry) { |
|
| 113 | - // only interested in entries with rename set |
|
| 114 | - if ($entry->getRename() === null) { |
|
| 115 | - continue; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - // only migrate if rename config key has a value and the new config key hasn't |
|
| 119 | - if ($this->appConfig->hasKey($appId, $entry->getRename()) |
|
| 120 | - && !$this->appConfig->hasKey($appId, $entry->getKey())) { |
|
| 121 | - try { |
|
| 122 | - $this->migrateAppConfigValue($appId, $entry); |
|
| 123 | - } catch (TypeConflictException $e) { |
|
| 124 | - $this->logger->error('could not migrate AppConfig value', ['appId' => $appId, 'entry' => $entry, 'exception' => $e]); |
|
| 125 | - continue; |
|
| 126 | - } |
|
| 127 | - } |
|
| 128 | - |
|
| 129 | - // we only delete previous config value if migration went fine. |
|
| 130 | - $this->appConfig->deleteKey($appId, $entry->getRename()); |
|
| 131 | - } |
|
| 132 | - } |
|
| 133 | - |
|
| 134 | - /** |
|
| 135 | - * Get details from lexicon related to UserConfig and search for entries with rename to initiate |
|
| 136 | - * a migration to new config key |
|
| 137 | - */ |
|
| 138 | - private function migrateUserConfigKeys(string $appId): void { |
|
| 139 | - $lexicon = $this->userConfig->getConfigDetailsFromLexicon($appId); |
|
| 140 | - foreach ($lexicon['entries'] as $entry) { |
|
| 141 | - // only interested in keys with rename set |
|
| 142 | - if ($entry->getRename() === null) { |
|
| 143 | - continue; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - foreach ($this->userConfig->getValuesByUsers($appId, $entry->getRename()) as $userId => $value) { |
|
| 147 | - if ($this->userConfig->hasKey($userId, $appId, $entry->getKey())) { |
|
| 148 | - continue; |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - try { |
|
| 152 | - $this->migrateUserConfigValue($userId, $appId, $entry); |
|
| 153 | - } catch (TypeConflictException $e) { |
|
| 154 | - $this->logger->error('could not migrate UserConfig value', ['userId' => $userId, 'appId' => $appId, 'entry' => $entry, 'exception' => $e]); |
|
| 155 | - continue; |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - $this->userConfig->deleteUserConfig($userId, $appId, $entry->getRename()); |
|
| 159 | - } |
|
| 160 | - } |
|
| 161 | - } |
|
| 162 | - |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * converting value from rename to the new key |
|
| 166 | - * |
|
| 167 | - * @throws TypeConflictException if previous value does not fit the expected type |
|
| 168 | - */ |
|
| 169 | - private function migrateAppConfigValue(string $appId, ConfigLexiconEntry $entry): void { |
|
| 170 | - $value = $this->appConfig->getValueMixed($appId, $entry->getRename(), lazy: null); |
|
| 171 | - switch ($entry->getValueType()) { |
|
| 172 | - case ValueType::STRING: |
|
| 173 | - $this->appConfig->setValueString($appId, $entry->getKey(), $value); |
|
| 174 | - return; |
|
| 175 | - |
|
| 176 | - case ValueType::INT: |
|
| 177 | - $this->appConfig->setValueInt($appId, $entry->getKey(), $this->convertToInt($value)); |
|
| 178 | - return; |
|
| 179 | - |
|
| 180 | - case ValueType::FLOAT: |
|
| 181 | - $this->appConfig->setValueFloat($appId, $entry->getKey(), $this->convertToFloat($value)); |
|
| 182 | - return; |
|
| 183 | - |
|
| 184 | - case ValueType::BOOL: |
|
| 185 | - $this->appConfig->setValueBool($appId, $entry->getKey(), $this->convertToBool($value, $entry)); |
|
| 186 | - return; |
|
| 187 | - |
|
| 188 | - case ValueType::ARRAY: |
|
| 189 | - $this->appConfig->setValueArray($appId, $entry->getKey(), $this->convertToArray($value)); |
|
| 190 | - return; |
|
| 191 | - } |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * converting value from rename to the new key |
|
| 196 | - * |
|
| 197 | - * @throws TypeConflictException if previous value does not fit the expected type |
|
| 198 | - */ |
|
| 199 | - private function migrateUserConfigValue(string $userId, string $appId, ConfigLexiconEntry $entry): void { |
|
| 200 | - $value = $this->userConfig->getValueMixed($userId, $appId, $entry->getRename(), lazy: null); |
|
| 201 | - switch ($entry->getValueType()) { |
|
| 202 | - case ValueType::STRING: |
|
| 203 | - $this->userConfig->setValueString($userId, $appId, $entry->getKey(), $value); |
|
| 204 | - return; |
|
| 205 | - |
|
| 206 | - case ValueType::INT: |
|
| 207 | - $this->userConfig->setValueInt($userId, $appId, $entry->getKey(), $this->convertToInt($value)); |
|
| 208 | - return; |
|
| 209 | - |
|
| 210 | - case ValueType::FLOAT: |
|
| 211 | - $this->userConfig->setValueFloat($userId, $appId, $entry->getKey(), $this->convertToFloat($value)); |
|
| 212 | - return; |
|
| 213 | - |
|
| 214 | - case ValueType::BOOL: |
|
| 215 | - $this->userConfig->setValueBool($userId, $appId, $entry->getKey(), $this->convertToBool($value, $entry)); |
|
| 216 | - return; |
|
| 217 | - |
|
| 218 | - case ValueType::ARRAY: |
|
| 219 | - $this->userConfig->setValueArray($userId, $appId, $entry->getKey(), $this->convertToArray($value)); |
|
| 220 | - return; |
|
| 221 | - } |
|
| 222 | - } |
|
| 223 | - |
|
| 224 | - public function convertToInt(string $value): int { |
|
| 225 | - if (!is_numeric($value) || (float)$value <> (int)$value) { |
|
| 226 | - throw new TypeConflictException('Value is not an integer'); |
|
| 227 | - } |
|
| 228 | - |
|
| 229 | - return (int)$value; |
|
| 230 | - } |
|
| 231 | - |
|
| 232 | - public function convertToFloat(string $value): float { |
|
| 233 | - if (!is_numeric($value)) { |
|
| 234 | - throw new TypeConflictException('Value is not a float'); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - return (float)$value; |
|
| 238 | - } |
|
| 239 | - |
|
| 240 | - public function convertToBool(string $value, ?ConfigLexiconEntry $entry = null): bool { |
|
| 241 | - if (in_array(strtolower($value), ['true', '1', 'on', 'yes'])) { |
|
| 242 | - $valueBool = true; |
|
| 243 | - } elseif (in_array(strtolower($value), ['false', '0', 'off', 'no'])) { |
|
| 244 | - $valueBool = false; |
|
| 245 | - } else { |
|
| 246 | - throw new TypeConflictException('Value cannot be converted to boolean'); |
|
| 247 | - } |
|
| 248 | - if ($entry?->hasOption(ConfigLexiconEntry::RENAME_INVERT_BOOLEAN) === true) { |
|
| 249 | - $valueBool = !$valueBool; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - return $valueBool; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - public function convertToArray(string $value): array { |
|
| 256 | - try { |
|
| 257 | - $valueArray = json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 258 | - } catch (JsonException) { |
|
| 259 | - throw new TypeConflictException('Value is not a valid json'); |
|
| 260 | - } |
|
| 261 | - if (!is_array($valueArray)) { |
|
| 262 | - throw new TypeConflictException('Value is not an array'); |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - return $valueArray; |
|
| 266 | - } |
|
| 30 | + /** @since 32.0.0 */ |
|
| 31 | + public const PRESET_CONFIGKEY = 'config_preset'; |
|
| 32 | + |
|
| 33 | + /** @var AppConfig|null $appConfig */ |
|
| 34 | + private ?IAppConfig $appConfig = null; |
|
| 35 | + /** @var UserConfig|null $userConfig */ |
|
| 36 | + private ?IUserConfig $userConfig = null; |
|
| 37 | + |
|
| 38 | + public function __construct( |
|
| 39 | + private readonly IConfig $config, |
|
| 40 | + private readonly LoggerInterface $logger, |
|
| 41 | + ) { |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * Use the rename values from the list of ConfigLexiconEntry defined in each app ConfigLexicon |
|
| 46 | + * to migrate config value to a new config key. |
|
| 47 | + * Migration will only occur if new config key has no value in database. |
|
| 48 | + * The previous value from the key set in rename will be deleted from the database when migration |
|
| 49 | + * is over. |
|
| 50 | + * |
|
| 51 | + * This method should be mainly called during a new upgrade or when a new app is enabled. |
|
| 52 | + * |
|
| 53 | + * @see ConfigLexiconEntry |
|
| 54 | + * @internal |
|
| 55 | + * @since 32.0.0 |
|
| 56 | + * @param string|null $appId when set to NULL the method will be executed for all enabled apps of the instance |
|
| 57 | + */ |
|
| 58 | + public function migrateConfigLexiconKeys(?string $appId = null): void { |
|
| 59 | + if ($appId === null) { |
|
| 60 | + $this->migrateConfigLexiconKeys('core'); |
|
| 61 | + $appManager = Server::get(IAppManager::class); |
|
| 62 | + foreach ($appManager->getEnabledApps() as $app) { |
|
| 63 | + $this->migrateConfigLexiconKeys($app); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + return; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + $this->loadConfigServices(); |
|
| 70 | + |
|
| 71 | + // it is required to ignore aliases when moving config values |
|
| 72 | + $this->appConfig->ignoreLexiconAliases(true); |
|
| 73 | + $this->userConfig->ignoreLexiconAliases(true); |
|
| 74 | + |
|
| 75 | + $this->migrateAppConfigKeys($appId); |
|
| 76 | + $this->migrateUserConfigKeys($appId); |
|
| 77 | + |
|
| 78 | + // switch back to normal behavior |
|
| 79 | + $this->appConfig->ignoreLexiconAliases(false); |
|
| 80 | + $this->userConfig->ignoreLexiconAliases(false); |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * store in config.php the new preset |
|
| 85 | + * refresh cached preset |
|
| 86 | + */ |
|
| 87 | + public function setLexiconPreset(Preset $preset): void { |
|
| 88 | + $this->config->setSystemValue(self::PRESET_CONFIGKEY, $preset->value); |
|
| 89 | + $this->loadConfigServices(); |
|
| 90 | + $this->appConfig->clearCache(); |
|
| 91 | + $this->userConfig->clearCacheAll(); |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * config services cannot be load at __construct() or install will fail |
|
| 96 | + */ |
|
| 97 | + private function loadConfigServices(): void { |
|
| 98 | + if ($this->appConfig === null) { |
|
| 99 | + $this->appConfig = Server::get(IAppConfig::class); |
|
| 100 | + } |
|
| 101 | + if ($this->userConfig === null) { |
|
| 102 | + $this->userConfig = Server::get(IUserConfig::class); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + /** |
|
| 107 | + * Get details from lexicon related to AppConfig and search for entries with rename to initiate |
|
| 108 | + * a migration to new config key |
|
| 109 | + */ |
|
| 110 | + private function migrateAppConfigKeys(string $appId): void { |
|
| 111 | + $lexicon = $this->appConfig->getConfigDetailsFromLexicon($appId); |
|
| 112 | + foreach ($lexicon['entries'] as $entry) { |
|
| 113 | + // only interested in entries with rename set |
|
| 114 | + if ($entry->getRename() === null) { |
|
| 115 | + continue; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + // only migrate if rename config key has a value and the new config key hasn't |
|
| 119 | + if ($this->appConfig->hasKey($appId, $entry->getRename()) |
|
| 120 | + && !$this->appConfig->hasKey($appId, $entry->getKey())) { |
|
| 121 | + try { |
|
| 122 | + $this->migrateAppConfigValue($appId, $entry); |
|
| 123 | + } catch (TypeConflictException $e) { |
|
| 124 | + $this->logger->error('could not migrate AppConfig value', ['appId' => $appId, 'entry' => $entry, 'exception' => $e]); |
|
| 125 | + continue; |
|
| 126 | + } |
|
| 127 | + } |
|
| 128 | + |
|
| 129 | + // we only delete previous config value if migration went fine. |
|
| 130 | + $this->appConfig->deleteKey($appId, $entry->getRename()); |
|
| 131 | + } |
|
| 132 | + } |
|
| 133 | + |
|
| 134 | + /** |
|
| 135 | + * Get details from lexicon related to UserConfig and search for entries with rename to initiate |
|
| 136 | + * a migration to new config key |
|
| 137 | + */ |
|
| 138 | + private function migrateUserConfigKeys(string $appId): void { |
|
| 139 | + $lexicon = $this->userConfig->getConfigDetailsFromLexicon($appId); |
|
| 140 | + foreach ($lexicon['entries'] as $entry) { |
|
| 141 | + // only interested in keys with rename set |
|
| 142 | + if ($entry->getRename() === null) { |
|
| 143 | + continue; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + foreach ($this->userConfig->getValuesByUsers($appId, $entry->getRename()) as $userId => $value) { |
|
| 147 | + if ($this->userConfig->hasKey($userId, $appId, $entry->getKey())) { |
|
| 148 | + continue; |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + try { |
|
| 152 | + $this->migrateUserConfigValue($userId, $appId, $entry); |
|
| 153 | + } catch (TypeConflictException $e) { |
|
| 154 | + $this->logger->error('could not migrate UserConfig value', ['userId' => $userId, 'appId' => $appId, 'entry' => $entry, 'exception' => $e]); |
|
| 155 | + continue; |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + $this->userConfig->deleteUserConfig($userId, $appId, $entry->getRename()); |
|
| 159 | + } |
|
| 160 | + } |
|
| 161 | + } |
|
| 162 | + |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * converting value from rename to the new key |
|
| 166 | + * |
|
| 167 | + * @throws TypeConflictException if previous value does not fit the expected type |
|
| 168 | + */ |
|
| 169 | + private function migrateAppConfigValue(string $appId, ConfigLexiconEntry $entry): void { |
|
| 170 | + $value = $this->appConfig->getValueMixed($appId, $entry->getRename(), lazy: null); |
|
| 171 | + switch ($entry->getValueType()) { |
|
| 172 | + case ValueType::STRING: |
|
| 173 | + $this->appConfig->setValueString($appId, $entry->getKey(), $value); |
|
| 174 | + return; |
|
| 175 | + |
|
| 176 | + case ValueType::INT: |
|
| 177 | + $this->appConfig->setValueInt($appId, $entry->getKey(), $this->convertToInt($value)); |
|
| 178 | + return; |
|
| 179 | + |
|
| 180 | + case ValueType::FLOAT: |
|
| 181 | + $this->appConfig->setValueFloat($appId, $entry->getKey(), $this->convertToFloat($value)); |
|
| 182 | + return; |
|
| 183 | + |
|
| 184 | + case ValueType::BOOL: |
|
| 185 | + $this->appConfig->setValueBool($appId, $entry->getKey(), $this->convertToBool($value, $entry)); |
|
| 186 | + return; |
|
| 187 | + |
|
| 188 | + case ValueType::ARRAY: |
|
| 189 | + $this->appConfig->setValueArray($appId, $entry->getKey(), $this->convertToArray($value)); |
|
| 190 | + return; |
|
| 191 | + } |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * converting value from rename to the new key |
|
| 196 | + * |
|
| 197 | + * @throws TypeConflictException if previous value does not fit the expected type |
|
| 198 | + */ |
|
| 199 | + private function migrateUserConfigValue(string $userId, string $appId, ConfigLexiconEntry $entry): void { |
|
| 200 | + $value = $this->userConfig->getValueMixed($userId, $appId, $entry->getRename(), lazy: null); |
|
| 201 | + switch ($entry->getValueType()) { |
|
| 202 | + case ValueType::STRING: |
|
| 203 | + $this->userConfig->setValueString($userId, $appId, $entry->getKey(), $value); |
|
| 204 | + return; |
|
| 205 | + |
|
| 206 | + case ValueType::INT: |
|
| 207 | + $this->userConfig->setValueInt($userId, $appId, $entry->getKey(), $this->convertToInt($value)); |
|
| 208 | + return; |
|
| 209 | + |
|
| 210 | + case ValueType::FLOAT: |
|
| 211 | + $this->userConfig->setValueFloat($userId, $appId, $entry->getKey(), $this->convertToFloat($value)); |
|
| 212 | + return; |
|
| 213 | + |
|
| 214 | + case ValueType::BOOL: |
|
| 215 | + $this->userConfig->setValueBool($userId, $appId, $entry->getKey(), $this->convertToBool($value, $entry)); |
|
| 216 | + return; |
|
| 217 | + |
|
| 218 | + case ValueType::ARRAY: |
|
| 219 | + $this->userConfig->setValueArray($userId, $appId, $entry->getKey(), $this->convertToArray($value)); |
|
| 220 | + return; |
|
| 221 | + } |
|
| 222 | + } |
|
| 223 | + |
|
| 224 | + public function convertToInt(string $value): int { |
|
| 225 | + if (!is_numeric($value) || (float)$value <> (int)$value) { |
|
| 226 | + throw new TypeConflictException('Value is not an integer'); |
|
| 227 | + } |
|
| 228 | + |
|
| 229 | + return (int)$value; |
|
| 230 | + } |
|
| 231 | + |
|
| 232 | + public function convertToFloat(string $value): float { |
|
| 233 | + if (!is_numeric($value)) { |
|
| 234 | + throw new TypeConflictException('Value is not a float'); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + return (float)$value; |
|
| 238 | + } |
|
| 239 | + |
|
| 240 | + public function convertToBool(string $value, ?ConfigLexiconEntry $entry = null): bool { |
|
| 241 | + if (in_array(strtolower($value), ['true', '1', 'on', 'yes'])) { |
|
| 242 | + $valueBool = true; |
|
| 243 | + } elseif (in_array(strtolower($value), ['false', '0', 'off', 'no'])) { |
|
| 244 | + $valueBool = false; |
|
| 245 | + } else { |
|
| 246 | + throw new TypeConflictException('Value cannot be converted to boolean'); |
|
| 247 | + } |
|
| 248 | + if ($entry?->hasOption(ConfigLexiconEntry::RENAME_INVERT_BOOLEAN) === true) { |
|
| 249 | + $valueBool = !$valueBool; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + return $valueBool; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + public function convertToArray(string $value): array { |
|
| 256 | + try { |
|
| 257 | + $valueArray = json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 258 | + } catch (JsonException) { |
|
| 259 | + throw new TypeConflictException('Value is not a valid json'); |
|
| 260 | + } |
|
| 261 | + if (!is_array($valueArray)) { |
|
| 262 | + throw new TypeConflictException('Value is not an array'); |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + return $valueArray; |
|
| 266 | + } |
|
| 267 | 267 | } |
@@ -47,2002 +47,2002 @@ |
||
| 47 | 47 | * @since 31.0.0 |
| 48 | 48 | */ |
| 49 | 49 | class UserConfig implements IUserConfig { |
| 50 | - private const USER_MAX_LENGTH = 64; |
|
| 51 | - private const APP_MAX_LENGTH = 32; |
|
| 52 | - private const KEY_MAX_LENGTH = 64; |
|
| 53 | - private const INDEX_MAX_LENGTH = 64; |
|
| 54 | - private const ENCRYPTION_PREFIX = '$UserConfigEncryption$'; |
|
| 55 | - private const ENCRYPTION_PREFIX_LENGTH = 22; // strlen(self::ENCRYPTION_PREFIX) |
|
| 56 | - |
|
| 57 | - /** @var array<string, array<string, array<string, mixed>>> [ass'user_id' => ['app_id' => ['key' => 'value']]] */ |
|
| 58 | - private array $fastCache = []; // cache for normal config keys |
|
| 59 | - /** @var array<string, array<string, array<string, mixed>>> ['user_id' => ['app_id' => ['key' => 'value']]] */ |
|
| 60 | - private array $lazyCache = []; // cache for lazy config keys |
|
| 61 | - /** @var array<string, array<string, array<string, array<string, mixed>>>> ['user_id' => ['app_id' => ['key' => ['type' => ValueType, 'flags' => bitflag]]]] */ |
|
| 62 | - private array $valueDetails = []; // type for all config values |
|
| 63 | - /** @var array<string, boolean> ['user_id' => bool] */ |
|
| 64 | - private array $fastLoaded = []; |
|
| 65 | - /** @var array<string, boolean> ['user_id' => bool] */ |
|
| 66 | - private array $lazyLoaded = []; |
|
| 67 | - /** @var array<string, array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ |
|
| 68 | - private array $configLexiconDetails = []; |
|
| 69 | - private bool $ignoreLexiconAliases = false; |
|
| 70 | - private ?Preset $configLexiconPreset = null; |
|
| 71 | - |
|
| 72 | - public function __construct( |
|
| 73 | - protected IDBConnection $connection, |
|
| 74 | - protected IConfig $config, |
|
| 75 | - protected LoggerInterface $logger, |
|
| 76 | - protected ICrypto $crypto, |
|
| 77 | - ) { |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - /** |
|
| 81 | - * @inheritDoc |
|
| 82 | - * |
|
| 83 | - * @param string $appId optional id of app |
|
| 84 | - * |
|
| 85 | - * @return list<string> list of userIds |
|
| 86 | - * @since 31.0.0 |
|
| 87 | - */ |
|
| 88 | - public function getUserIds(string $appId = ''): array { |
|
| 89 | - $this->assertParams(app: $appId, allowEmptyUser: true, allowEmptyApp: true); |
|
| 90 | - |
|
| 91 | - $qb = $this->connection->getQueryBuilder(); |
|
| 92 | - $qb->from('preferences'); |
|
| 93 | - $qb->select('userid'); |
|
| 94 | - $qb->groupBy('userid'); |
|
| 95 | - if ($appId !== '') { |
|
| 96 | - $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($appId))); |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - $result = $qb->executeQuery(); |
|
| 100 | - $rows = $result->fetchAll(); |
|
| 101 | - $userIds = []; |
|
| 102 | - foreach ($rows as $row) { |
|
| 103 | - $userIds[] = $row['userid']; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - return $userIds; |
|
| 107 | - } |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * @inheritDoc |
|
| 111 | - * |
|
| 112 | - * @return list<string> list of app ids |
|
| 113 | - * @since 31.0.0 |
|
| 114 | - */ |
|
| 115 | - public function getApps(string $userId): array { |
|
| 116 | - $this->assertParams($userId, allowEmptyApp: true); |
|
| 117 | - $this->loadConfigAll($userId); |
|
| 118 | - $apps = array_merge(array_keys($this->fastCache[$userId] ?? []), array_keys($this->lazyCache[$userId] ?? [])); |
|
| 119 | - sort($apps); |
|
| 120 | - |
|
| 121 | - return array_values(array_unique($apps)); |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * @inheritDoc |
|
| 126 | - * |
|
| 127 | - * @param string $userId id of the user |
|
| 128 | - * @param string $app id of the app |
|
| 129 | - * |
|
| 130 | - * @return list<string> list of stored config keys |
|
| 131 | - * @since 31.0.0 |
|
| 132 | - */ |
|
| 133 | - public function getKeys(string $userId, string $app): array { |
|
| 134 | - $this->assertParams($userId, $app); |
|
| 135 | - $this->loadConfigAll($userId); |
|
| 136 | - // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 137 | - $keys = array_map('strval', array_keys(($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []))); |
|
| 138 | - sort($keys); |
|
| 139 | - |
|
| 140 | - return array_values(array_unique($keys)); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - /** |
|
| 144 | - * @inheritDoc |
|
| 145 | - * |
|
| 146 | - * @param string $userId id of the user |
|
| 147 | - * @param string $app id of the app |
|
| 148 | - * @param string $key config key |
|
| 149 | - * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 150 | - * |
|
| 151 | - * @return bool TRUE if key exists |
|
| 152 | - * @since 31.0.0 |
|
| 153 | - */ |
|
| 154 | - public function hasKey(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 155 | - $this->assertParams($userId, $app, $key); |
|
| 156 | - $this->loadConfig($userId, $lazy); |
|
| 157 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 158 | - |
|
| 159 | - if ($lazy === null) { |
|
| 160 | - $appCache = $this->getValues($userId, $app); |
|
| 161 | - return isset($appCache[$key]); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - if ($lazy) { |
|
| 165 | - return isset($this->lazyCache[$userId][$app][$key]); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - return isset($this->fastCache[$userId][$app][$key]); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - /** |
|
| 172 | - * @inheritDoc |
|
| 173 | - * |
|
| 174 | - * @param string $userId id of the user |
|
| 175 | - * @param string $app id of the app |
|
| 176 | - * @param string $key config key |
|
| 177 | - * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 178 | - * |
|
| 179 | - * @return bool |
|
| 180 | - * @throws UnknownKeyException if config key is not known |
|
| 181 | - * @since 31.0.0 |
|
| 182 | - */ |
|
| 183 | - public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 184 | - $this->assertParams($userId, $app, $key); |
|
| 185 | - $this->loadConfig($userId, $lazy); |
|
| 186 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 187 | - |
|
| 188 | - if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 189 | - throw new UnknownKeyException('unknown config key'); |
|
| 190 | - } |
|
| 191 | - |
|
| 192 | - return $this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags']); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * @inheritDoc |
|
| 197 | - * |
|
| 198 | - * @param string $userId id of the user |
|
| 199 | - * @param string $app id of the app |
|
| 200 | - * @param string $key config key |
|
| 201 | - * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 202 | - * |
|
| 203 | - * @return bool |
|
| 204 | - * @throws UnknownKeyException if config key is not known |
|
| 205 | - * @since 31.0.0 |
|
| 206 | - */ |
|
| 207 | - public function isIndexed(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 208 | - $this->assertParams($userId, $app, $key); |
|
| 209 | - $this->loadConfig($userId, $lazy); |
|
| 210 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 211 | - |
|
| 212 | - if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 213 | - throw new UnknownKeyException('unknown config key'); |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - return $this->isFlagged(self::FLAG_INDEXED, $this->valueDetails[$userId][$app][$key]['flags']); |
|
| 217 | - } |
|
| 218 | - |
|
| 219 | - /** |
|
| 220 | - * @inheritDoc |
|
| 221 | - * |
|
| 222 | - * @param string $userId id of the user |
|
| 223 | - * @param string $app if of the app |
|
| 224 | - * @param string $key config key |
|
| 225 | - * |
|
| 226 | - * @return bool TRUE if config is lazy loaded |
|
| 227 | - * @throws UnknownKeyException if config key is not known |
|
| 228 | - * @see IUserConfig for details about lazy loading |
|
| 229 | - * @since 31.0.0 |
|
| 230 | - */ |
|
| 231 | - public function isLazy(string $userId, string $app, string $key): bool { |
|
| 232 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 233 | - |
|
| 234 | - // there is a huge probability the non-lazy config are already loaded |
|
| 235 | - // meaning that we can start by only checking if a current non-lazy key exists |
|
| 236 | - if ($this->hasKey($userId, $app, $key, false)) { |
|
| 237 | - // meaning key is not lazy. |
|
| 238 | - return false; |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - // as key is not found as non-lazy, we load and search in the lazy config |
|
| 242 | - if ($this->hasKey($userId, $app, $key, true)) { |
|
| 243 | - return true; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - throw new UnknownKeyException('unknown config key'); |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - /** |
|
| 250 | - * @inheritDoc |
|
| 251 | - * |
|
| 252 | - * @param string $userId id of the user |
|
| 253 | - * @param string $app id of the app |
|
| 254 | - * @param string $prefix config keys prefix to search |
|
| 255 | - * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 256 | - * |
|
| 257 | - * @return array<string, string|int|float|bool|array> [key => value] |
|
| 258 | - * @since 31.0.0 |
|
| 259 | - */ |
|
| 260 | - public function getValues( |
|
| 261 | - string $userId, |
|
| 262 | - string $app, |
|
| 263 | - string $prefix = '', |
|
| 264 | - bool $filtered = false, |
|
| 265 | - ): array { |
|
| 266 | - $this->assertParams($userId, $app, $prefix); |
|
| 267 | - // if we want to filter values, we need to get sensitivity |
|
| 268 | - $this->loadConfigAll($userId); |
|
| 269 | - // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 270 | - $values = array_filter( |
|
| 271 | - $this->formatAppValues($userId, $app, ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []), $filtered), |
|
| 272 | - function (string $key) use ($prefix): bool { |
|
| 273 | - // filter values based on $prefix |
|
| 274 | - return str_starts_with($key, $prefix); |
|
| 275 | - }, ARRAY_FILTER_USE_KEY |
|
| 276 | - ); |
|
| 277 | - |
|
| 278 | - return $values; |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - /** |
|
| 282 | - * @inheritDoc |
|
| 283 | - * |
|
| 284 | - * @param string $userId id of the user |
|
| 285 | - * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 286 | - * |
|
| 287 | - * @return array<string, array<string, string|int|float|bool|array>> [appId => [key => value]] |
|
| 288 | - * @since 31.0.0 |
|
| 289 | - */ |
|
| 290 | - public function getAllValues(string $userId, bool $filtered = false): array { |
|
| 291 | - $this->assertParams($userId, allowEmptyApp: true); |
|
| 292 | - $this->loadConfigAll($userId); |
|
| 293 | - |
|
| 294 | - $result = []; |
|
| 295 | - foreach ($this->getApps($userId) as $app) { |
|
| 296 | - // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 297 | - $cached = ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []); |
|
| 298 | - $result[$app] = $this->formatAppValues($userId, $app, $cached, $filtered); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - return $result; |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - /** |
|
| 305 | - * @inheritDoc |
|
| 306 | - * |
|
| 307 | - * @param string $userId id of the user |
|
| 308 | - * @param string $key config key |
|
| 309 | - * @param bool $lazy search within lazy loaded config |
|
| 310 | - * @param ValueType|null $typedAs enforce type for the returned values |
|
| 311 | - * |
|
| 312 | - * @return array<string, string|int|float|bool|array> [appId => value] |
|
| 313 | - * @since 31.0.0 |
|
| 314 | - */ |
|
| 315 | - public function getValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array { |
|
| 316 | - $this->assertParams($userId, '', $key, allowEmptyApp: true); |
|
| 317 | - $this->loadConfig($userId, $lazy); |
|
| 318 | - |
|
| 319 | - /** @var array<array-key, array<array-key, mixed>> $cache */ |
|
| 320 | - if ($lazy) { |
|
| 321 | - $cache = $this->lazyCache[$userId]; |
|
| 322 | - } else { |
|
| 323 | - $cache = $this->fastCache[$userId]; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - $values = []; |
|
| 327 | - foreach (array_keys($cache) as $app) { |
|
| 328 | - if (isset($cache[$app][$key])) { |
|
| 329 | - $value = $cache[$app][$key]; |
|
| 330 | - try { |
|
| 331 | - $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 332 | - $value = $this->convertTypedValue($value, $typedAs ?? $this->getValueType($userId, $app, $key, $lazy)); |
|
| 333 | - } catch (IncorrectTypeException|UnknownKeyException) { |
|
| 334 | - } |
|
| 335 | - $values[$app] = $value; |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - return $values; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - |
|
| 343 | - /** |
|
| 344 | - * @inheritDoc |
|
| 345 | - * |
|
| 346 | - * @param string $app id of the app |
|
| 347 | - * @param string $key config key |
|
| 348 | - * @param ValueType|null $typedAs enforce type for the returned values |
|
| 349 | - * @param array|null $userIds limit to a list of user ids |
|
| 350 | - * |
|
| 351 | - * @return array<string, string|int|float|bool|array> [userId => value] |
|
| 352 | - * @since 31.0.0 |
|
| 353 | - */ |
|
| 354 | - public function getValuesByUsers( |
|
| 355 | - string $app, |
|
| 356 | - string $key, |
|
| 357 | - ?ValueType $typedAs = null, |
|
| 358 | - ?array $userIds = null, |
|
| 359 | - ): array { |
|
| 360 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 361 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 362 | - |
|
| 363 | - $qb = $this->connection->getQueryBuilder(); |
|
| 364 | - $qb->select('userid', 'configvalue', 'type') |
|
| 365 | - ->from('preferences') |
|
| 366 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 367 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 368 | - |
|
| 369 | - $values = []; |
|
| 370 | - // this nested function will execute current Query and store result within $values. |
|
| 371 | - $executeAndStoreValue = function (IQueryBuilder $qb) use (&$values, $typedAs): IResult { |
|
| 372 | - $result = $qb->executeQuery(); |
|
| 373 | - while ($row = $result->fetch()) { |
|
| 374 | - $value = $row['configvalue']; |
|
| 375 | - try { |
|
| 376 | - $value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int)$row['type'])); |
|
| 377 | - } catch (IncorrectTypeException) { |
|
| 378 | - } |
|
| 379 | - $values[$row['userid']] = $value; |
|
| 380 | - } |
|
| 381 | - return $result; |
|
| 382 | - }; |
|
| 383 | - |
|
| 384 | - // if no userIds to filter, we execute query as it is and returns all values ... |
|
| 385 | - if ($userIds === null) { |
|
| 386 | - $result = $executeAndStoreValue($qb); |
|
| 387 | - $result->closeCursor(); |
|
| 388 | - return $values; |
|
| 389 | - } |
|
| 390 | - |
|
| 391 | - // if userIds to filter, we chunk the list and execute the same query multiple times until we get all values |
|
| 392 | - $result = null; |
|
| 393 | - $qb->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds'))); |
|
| 394 | - foreach (array_chunk($userIds, 50, true) as $chunk) { |
|
| 395 | - $qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 396 | - $result = $executeAndStoreValue($qb); |
|
| 397 | - } |
|
| 398 | - $result?->closeCursor(); |
|
| 399 | - |
|
| 400 | - return $values; |
|
| 401 | - } |
|
| 402 | - |
|
| 403 | - /** |
|
| 404 | - * @inheritDoc |
|
| 405 | - * |
|
| 406 | - * @param string $app id of the app |
|
| 407 | - * @param string $key config key |
|
| 408 | - * @param string $value config value |
|
| 409 | - * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string |
|
| 410 | - * |
|
| 411 | - * @return Generator<string> |
|
| 412 | - * @since 31.0.0 |
|
| 413 | - */ |
|
| 414 | - public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): Generator { |
|
| 415 | - return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - /** |
|
| 419 | - * @inheritDoc |
|
| 420 | - * |
|
| 421 | - * @param string $app id of the app |
|
| 422 | - * @param string $key config key |
|
| 423 | - * @param int $value config value |
|
| 424 | - * |
|
| 425 | - * @return Generator<string> |
|
| 426 | - * @since 31.0.0 |
|
| 427 | - */ |
|
| 428 | - public function searchUsersByValueInt(string $app, string $key, int $value): Generator { |
|
| 429 | - return $this->searchUsersByValueString($app, $key, (string)$value); |
|
| 430 | - } |
|
| 431 | - |
|
| 432 | - /** |
|
| 433 | - * @inheritDoc |
|
| 434 | - * |
|
| 435 | - * @param string $app id of the app |
|
| 436 | - * @param string $key config key |
|
| 437 | - * @param array $values list of config values |
|
| 438 | - * |
|
| 439 | - * @return Generator<string> |
|
| 440 | - * @since 31.0.0 |
|
| 441 | - */ |
|
| 442 | - public function searchUsersByValues(string $app, string $key, array $values): Generator { |
|
| 443 | - return $this->searchUsersByTypedValue($app, $key, $values); |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - /** |
|
| 447 | - * @inheritDoc |
|
| 448 | - * |
|
| 449 | - * @param string $app id of the app |
|
| 450 | - * @param string $key config key |
|
| 451 | - * @param bool $value config value |
|
| 452 | - * |
|
| 453 | - * @return Generator<string> |
|
| 454 | - * @since 31.0.0 |
|
| 455 | - */ |
|
| 456 | - public function searchUsersByValueBool(string $app, string $key, bool $value): Generator { |
|
| 457 | - $values = ['0', 'off', 'false', 'no']; |
|
| 458 | - if ($value) { |
|
| 459 | - $values = ['1', 'on', 'true', 'yes']; |
|
| 460 | - } |
|
| 461 | - return $this->searchUsersByValues($app, $key, $values); |
|
| 462 | - } |
|
| 463 | - |
|
| 464 | - /** |
|
| 465 | - * returns a list of users with config key set to a specific value, or within the list of |
|
| 466 | - * possible values |
|
| 467 | - * |
|
| 468 | - * @param string $app |
|
| 469 | - * @param string $key |
|
| 470 | - * @param string|array $value |
|
| 471 | - * @param bool $caseInsensitive |
|
| 472 | - * |
|
| 473 | - * @return Generator<string> |
|
| 474 | - */ |
|
| 475 | - private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): Generator { |
|
| 476 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 477 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 478 | - |
|
| 479 | - $qb = $this->connection->getQueryBuilder(); |
|
| 480 | - $qb->from('preferences'); |
|
| 481 | - $qb->select('userid'); |
|
| 482 | - $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 483 | - $qb->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 484 | - |
|
| 485 | - // search within 'indexed' OR 'configvalue' only if 'flags' is set as not indexed |
|
| 486 | - // TODO: when implementing config lexicon remove the searches on 'configvalue' if value is set as indexed |
|
| 487 | - $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue'; |
|
| 488 | - if (is_array($value)) { |
|
| 489 | - $where = $qb->expr()->orX( |
|
| 490 | - $qb->expr()->in('indexed', $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)), |
|
| 491 | - $qb->expr()->andX( |
|
| 492 | - $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 493 | - $qb->expr()->in($configValueColumn, $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 494 | - ) |
|
| 495 | - ); |
|
| 496 | - } else { |
|
| 497 | - if ($caseInsensitive) { |
|
| 498 | - $where = $qb->expr()->orX( |
|
| 499 | - $qb->expr()->eq($qb->func()->lower('indexed'), $qb->createNamedParameter(strtolower($value))), |
|
| 500 | - $qb->expr()->andX( |
|
| 501 | - $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 502 | - $qb->expr()->eq($qb->func()->lower($configValueColumn), $qb->createNamedParameter(strtolower($value))) |
|
| 503 | - ) |
|
| 504 | - ); |
|
| 505 | - } else { |
|
| 506 | - $where = $qb->expr()->orX( |
|
| 507 | - $qb->expr()->eq('indexed', $qb->createNamedParameter($value)), |
|
| 508 | - $qb->expr()->andX( |
|
| 509 | - $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 510 | - $qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value)) |
|
| 511 | - ) |
|
| 512 | - ); |
|
| 513 | - } |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - $qb->andWhere($where); |
|
| 517 | - $result = $qb->executeQuery(); |
|
| 518 | - while ($row = $result->fetch()) { |
|
| 519 | - yield $row['userid']; |
|
| 520 | - } |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - /** |
|
| 524 | - * Get the config value as string. |
|
| 525 | - * If the value does not exist the given default will be returned. |
|
| 526 | - * |
|
| 527 | - * Set lazy to `null` to ignore it and get the value from either source. |
|
| 528 | - * |
|
| 529 | - * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type. |
|
| 530 | - * |
|
| 531 | - * @param string $userId id of the user |
|
| 532 | - * @param string $app id of the app |
|
| 533 | - * @param string $key config key |
|
| 534 | - * @param string $default config value |
|
| 535 | - * @param null|bool $lazy get config as lazy loaded or not. can be NULL |
|
| 536 | - * |
|
| 537 | - * @return string the value or $default |
|
| 538 | - * @throws TypeConflictException |
|
| 539 | - * @internal |
|
| 540 | - * @since 31.0.0 |
|
| 541 | - * @see IUserConfig for explanation about lazy loading |
|
| 542 | - * @see getValueString() |
|
| 543 | - * @see getValueInt() |
|
| 544 | - * @see getValueFloat() |
|
| 545 | - * @see getValueBool() |
|
| 546 | - * @see getValueArray() |
|
| 547 | - */ |
|
| 548 | - public function getValueMixed( |
|
| 549 | - string $userId, |
|
| 550 | - string $app, |
|
| 551 | - string $key, |
|
| 552 | - string $default = '', |
|
| 553 | - ?bool $lazy = false, |
|
| 554 | - ): string { |
|
| 555 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 556 | - try { |
|
| 557 | - $lazy ??= $this->isLazy($userId, $app, $key); |
|
| 558 | - } catch (UnknownKeyException) { |
|
| 559 | - return $default; |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - return $this->getTypedValue( |
|
| 563 | - $userId, |
|
| 564 | - $app, |
|
| 565 | - $key, |
|
| 566 | - $default, |
|
| 567 | - $lazy, |
|
| 568 | - ValueType::MIXED |
|
| 569 | - ); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * @inheritDoc |
|
| 574 | - * |
|
| 575 | - * @param string $userId id of the user |
|
| 576 | - * @param string $app id of the app |
|
| 577 | - * @param string $key config key |
|
| 578 | - * @param string $default default value |
|
| 579 | - * @param bool $lazy search within lazy loaded config |
|
| 580 | - * |
|
| 581 | - * @return string stored config value or $default if not set in database |
|
| 582 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 583 | - * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 584 | - * @since 31.0.0 |
|
| 585 | - * @see IUserConfig for explanation about lazy loading |
|
| 586 | - */ |
|
| 587 | - public function getValueString( |
|
| 588 | - string $userId, |
|
| 589 | - string $app, |
|
| 590 | - string $key, |
|
| 591 | - string $default = '', |
|
| 592 | - bool $lazy = false, |
|
| 593 | - ): string { |
|
| 594 | - return $this->getTypedValue($userId, $app, $key, $default, $lazy, ValueType::STRING); |
|
| 595 | - } |
|
| 596 | - |
|
| 597 | - /** |
|
| 598 | - * @inheritDoc |
|
| 599 | - * |
|
| 600 | - * @param string $userId id of the user |
|
| 601 | - * @param string $app id of the app |
|
| 602 | - * @param string $key config key |
|
| 603 | - * @param int $default default value |
|
| 604 | - * @param bool $lazy search within lazy loaded config |
|
| 605 | - * |
|
| 606 | - * @return int stored config value or $default if not set in database |
|
| 607 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 608 | - * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 609 | - * @since 31.0.0 |
|
| 610 | - * @see IUserConfig for explanation about lazy loading |
|
| 611 | - */ |
|
| 612 | - public function getValueInt( |
|
| 613 | - string $userId, |
|
| 614 | - string $app, |
|
| 615 | - string $key, |
|
| 616 | - int $default = 0, |
|
| 617 | - bool $lazy = false, |
|
| 618 | - ): int { |
|
| 619 | - return (int)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::INT); |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - /** |
|
| 623 | - * @inheritDoc |
|
| 624 | - * |
|
| 625 | - * @param string $userId id of the user |
|
| 626 | - * @param string $app id of the app |
|
| 627 | - * @param string $key config key |
|
| 628 | - * @param float $default default value |
|
| 629 | - * @param bool $lazy search within lazy loaded config |
|
| 630 | - * |
|
| 631 | - * @return float stored config value or $default if not set in database |
|
| 632 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 633 | - * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 634 | - * @since 31.0.0 |
|
| 635 | - * @see IUserConfig for explanation about lazy loading |
|
| 636 | - */ |
|
| 637 | - public function getValueFloat( |
|
| 638 | - string $userId, |
|
| 639 | - string $app, |
|
| 640 | - string $key, |
|
| 641 | - float $default = 0, |
|
| 642 | - bool $lazy = false, |
|
| 643 | - ): float { |
|
| 644 | - return (float)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::FLOAT); |
|
| 645 | - } |
|
| 646 | - |
|
| 647 | - /** |
|
| 648 | - * @inheritDoc |
|
| 649 | - * |
|
| 650 | - * @param string $userId id of the user |
|
| 651 | - * @param string $app id of the app |
|
| 652 | - * @param string $key config key |
|
| 653 | - * @param bool $default default value |
|
| 654 | - * @param bool $lazy search within lazy loaded config |
|
| 655 | - * |
|
| 656 | - * @return bool stored config value or $default if not set in database |
|
| 657 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 658 | - * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 659 | - * @since 31.0.0 |
|
| 660 | - * @see IUserConfig for explanation about lazy loading |
|
| 661 | - */ |
|
| 662 | - public function getValueBool( |
|
| 663 | - string $userId, |
|
| 664 | - string $app, |
|
| 665 | - string $key, |
|
| 666 | - bool $default = false, |
|
| 667 | - bool $lazy = false, |
|
| 668 | - ): bool { |
|
| 669 | - $b = strtolower($this->getTypedValue($userId, $app, $key, $default ? 'true' : 'false', $lazy, ValueType::BOOL)); |
|
| 670 | - return in_array($b, ['1', 'true', 'yes', 'on']); |
|
| 671 | - } |
|
| 672 | - |
|
| 673 | - /** |
|
| 674 | - * @inheritDoc |
|
| 675 | - * |
|
| 676 | - * @param string $userId id of the user |
|
| 677 | - * @param string $app id of the app |
|
| 678 | - * @param string $key config key |
|
| 679 | - * @param array $default default value |
|
| 680 | - * @param bool $lazy search within lazy loaded config |
|
| 681 | - * |
|
| 682 | - * @return array stored config value or $default if not set in database |
|
| 683 | - * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 684 | - * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 685 | - * @since 31.0.0 |
|
| 686 | - * @see IUserConfig for explanation about lazy loading |
|
| 687 | - */ |
|
| 688 | - public function getValueArray( |
|
| 689 | - string $userId, |
|
| 690 | - string $app, |
|
| 691 | - string $key, |
|
| 692 | - array $default = [], |
|
| 693 | - bool $lazy = false, |
|
| 694 | - ): array { |
|
| 695 | - try { |
|
| 696 | - $defaultJson = json_encode($default, JSON_THROW_ON_ERROR); |
|
| 697 | - $value = json_decode($this->getTypedValue($userId, $app, $key, $defaultJson, $lazy, ValueType::ARRAY), true, flags: JSON_THROW_ON_ERROR); |
|
| 698 | - |
|
| 699 | - return is_array($value) ? $value : []; |
|
| 700 | - } catch (JsonException) { |
|
| 701 | - return []; |
|
| 702 | - } |
|
| 703 | - } |
|
| 704 | - |
|
| 705 | - /** |
|
| 706 | - * @param string $userId |
|
| 707 | - * @param string $app id of the app |
|
| 708 | - * @param string $key config key |
|
| 709 | - * @param string $default default value |
|
| 710 | - * @param bool $lazy search within lazy loaded config |
|
| 711 | - * @param ValueType $type value type |
|
| 712 | - * |
|
| 713 | - * @return string |
|
| 714 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 715 | - */ |
|
| 716 | - private function getTypedValue( |
|
| 717 | - string $userId, |
|
| 718 | - string $app, |
|
| 719 | - string $key, |
|
| 720 | - string $default, |
|
| 721 | - bool $lazy, |
|
| 722 | - ValueType $type, |
|
| 723 | - ): string { |
|
| 724 | - $this->assertParams($userId, $app, $key); |
|
| 725 | - $origKey = $key; |
|
| 726 | - $matched = $this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, default: $default); |
|
| 727 | - if ($default === null) { |
|
| 728 | - // there is no logical reason for it to be null |
|
| 729 | - throw new \Exception('default cannot be null'); |
|
| 730 | - } |
|
| 731 | - |
|
| 732 | - // returns default if strictness of lexicon is set to WARNING (block and report) |
|
| 733 | - if (!$matched) { |
|
| 734 | - return $default; |
|
| 735 | - } |
|
| 736 | - |
|
| 737 | - $this->loadConfig($userId, $lazy); |
|
| 738 | - |
|
| 739 | - /** |
|
| 740 | - * We ignore check if mixed type is requested. |
|
| 741 | - * If type of stored value is set as mixed, we don't filter. |
|
| 742 | - * If type of stored value is defined, we compare with the one requested. |
|
| 743 | - */ |
|
| 744 | - $knownType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; |
|
| 745 | - if ($type !== ValueType::MIXED |
|
| 746 | - && $knownType !== null |
|
| 747 | - && $knownType !== ValueType::MIXED |
|
| 748 | - && $type !== $knownType) { |
|
| 749 | - $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]); |
|
| 750 | - throw new TypeConflictException('conflict with value type from database'); |
|
| 751 | - } |
|
| 752 | - |
|
| 753 | - /** |
|
| 754 | - * - the pair $app/$key cannot exist in both array, |
|
| 755 | - * - we should still return an existing non-lazy value even if current method |
|
| 756 | - * is called with $lazy is true |
|
| 757 | - * |
|
| 758 | - * This way, lazyCache will be empty until the load for lazy config value is requested. |
|
| 759 | - */ |
|
| 760 | - if (isset($this->lazyCache[$userId][$app][$key])) { |
|
| 761 | - $value = $this->lazyCache[$userId][$app][$key]; |
|
| 762 | - } elseif (isset($this->fastCache[$userId][$app][$key])) { |
|
| 763 | - $value = $this->fastCache[$userId][$app][$key]; |
|
| 764 | - } else { |
|
| 765 | - return $default; |
|
| 766 | - } |
|
| 767 | - |
|
| 768 | - $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 769 | - |
|
| 770 | - // in case the key was modified while running matchAndApplyLexiconDefinition() we are |
|
| 771 | - // interested to check options in case a modification of the value is needed |
|
| 772 | - // ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN |
|
| 773 | - if ($origKey !== $key && $type === ValueType::BOOL) { |
|
| 774 | - $configManager = Server::get(ConfigManager::class); |
|
| 775 | - $value = ($configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0'; |
|
| 776 | - } |
|
| 777 | - |
|
| 778 | - return $value; |
|
| 779 | - } |
|
| 780 | - |
|
| 781 | - /** |
|
| 782 | - * @inheritDoc |
|
| 783 | - * |
|
| 784 | - * @param string $userId id of the user |
|
| 785 | - * @param string $app id of the app |
|
| 786 | - * @param string $key config key |
|
| 787 | - * |
|
| 788 | - * @return ValueType type of the value |
|
| 789 | - * @throws UnknownKeyException if config key is not known |
|
| 790 | - * @throws IncorrectTypeException if config value type is not known |
|
| 791 | - * @since 31.0.0 |
|
| 792 | - */ |
|
| 793 | - public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType { |
|
| 794 | - $this->assertParams($userId, $app, $key); |
|
| 795 | - $this->loadConfig($userId, $lazy); |
|
| 796 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 797 | - |
|
| 798 | - if (!isset($this->valueDetails[$userId][$app][$key]['type'])) { |
|
| 799 | - throw new UnknownKeyException('unknown config key'); |
|
| 800 | - } |
|
| 801 | - |
|
| 802 | - return $this->valueDetails[$userId][$app][$key]['type']; |
|
| 803 | - } |
|
| 804 | - |
|
| 805 | - /** |
|
| 806 | - * @inheritDoc |
|
| 807 | - * |
|
| 808 | - * @param string $userId id of the user |
|
| 809 | - * @param string $app id of the app |
|
| 810 | - * @param string $key config key |
|
| 811 | - * @param bool $lazy lazy loading |
|
| 812 | - * |
|
| 813 | - * @return int flags applied to value |
|
| 814 | - * @throws UnknownKeyException if config key is not known |
|
| 815 | - * @throws IncorrectTypeException if config value type is not known |
|
| 816 | - * @since 31.0.0 |
|
| 817 | - */ |
|
| 818 | - public function getValueFlags(string $userId, string $app, string $key, bool $lazy = false): int { |
|
| 819 | - $this->assertParams($userId, $app, $key); |
|
| 820 | - $this->loadConfig($userId, $lazy); |
|
| 821 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 822 | - |
|
| 823 | - if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 824 | - throw new UnknownKeyException('unknown config key'); |
|
| 825 | - } |
|
| 826 | - |
|
| 827 | - return $this->valueDetails[$userId][$app][$key]['flags']; |
|
| 828 | - } |
|
| 829 | - |
|
| 830 | - /** |
|
| 831 | - * Store a config key and its value in database as VALUE_MIXED |
|
| 832 | - * |
|
| 833 | - * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type |
|
| 834 | - * |
|
| 835 | - * @param string $userId id of the user |
|
| 836 | - * @param string $app id of the app |
|
| 837 | - * @param string $key config key |
|
| 838 | - * @param string $value config value |
|
| 839 | - * @param bool $lazy set config as lazy loaded |
|
| 840 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 841 | - * |
|
| 842 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 843 | - * @throws TypeConflictException if type from database is not VALUE_MIXED |
|
| 844 | - * @internal |
|
| 845 | - * @since 31.0.0 |
|
| 846 | - * @see IUserConfig for explanation about lazy loading |
|
| 847 | - * @see setValueString() |
|
| 848 | - * @see setValueInt() |
|
| 849 | - * @see setValueFloat() |
|
| 850 | - * @see setValueBool() |
|
| 851 | - * @see setValueArray() |
|
| 852 | - */ |
|
| 853 | - public function setValueMixed( |
|
| 854 | - string $userId, |
|
| 855 | - string $app, |
|
| 856 | - string $key, |
|
| 857 | - string $value, |
|
| 858 | - bool $lazy = false, |
|
| 859 | - int $flags = 0, |
|
| 860 | - ): bool { |
|
| 861 | - return $this->setTypedValue( |
|
| 862 | - $userId, |
|
| 863 | - $app, |
|
| 864 | - $key, |
|
| 865 | - $value, |
|
| 866 | - $lazy, |
|
| 867 | - $flags, |
|
| 868 | - ValueType::MIXED |
|
| 869 | - ); |
|
| 870 | - } |
|
| 871 | - |
|
| 872 | - |
|
| 873 | - /** |
|
| 874 | - * @inheritDoc |
|
| 875 | - * |
|
| 876 | - * @param string $userId id of the user |
|
| 877 | - * @param string $app id of the app |
|
| 878 | - * @param string $key config key |
|
| 879 | - * @param string $value config value |
|
| 880 | - * @param bool $lazy set config as lazy loaded |
|
| 881 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 882 | - * |
|
| 883 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 884 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 885 | - * @since 31.0.0 |
|
| 886 | - * @see IUserConfig for explanation about lazy loading |
|
| 887 | - */ |
|
| 888 | - public function setValueString( |
|
| 889 | - string $userId, |
|
| 890 | - string $app, |
|
| 891 | - string $key, |
|
| 892 | - string $value, |
|
| 893 | - bool $lazy = false, |
|
| 894 | - int $flags = 0, |
|
| 895 | - ): bool { |
|
| 896 | - return $this->setTypedValue( |
|
| 897 | - $userId, |
|
| 898 | - $app, |
|
| 899 | - $key, |
|
| 900 | - $value, |
|
| 901 | - $lazy, |
|
| 902 | - $flags, |
|
| 903 | - ValueType::STRING |
|
| 904 | - ); |
|
| 905 | - } |
|
| 906 | - |
|
| 907 | - /** |
|
| 908 | - * @inheritDoc |
|
| 909 | - * |
|
| 910 | - * @param string $userId id of the user |
|
| 911 | - * @param string $app id of the app |
|
| 912 | - * @param string $key config key |
|
| 913 | - * @param int $value config value |
|
| 914 | - * @param bool $lazy set config as lazy loaded |
|
| 915 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 916 | - * |
|
| 917 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 918 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 919 | - * @since 31.0.0 |
|
| 920 | - * @see IUserConfig for explanation about lazy loading |
|
| 921 | - */ |
|
| 922 | - public function setValueInt( |
|
| 923 | - string $userId, |
|
| 924 | - string $app, |
|
| 925 | - string $key, |
|
| 926 | - int $value, |
|
| 927 | - bool $lazy = false, |
|
| 928 | - int $flags = 0, |
|
| 929 | - ): bool { |
|
| 930 | - if ($value > 2000000000) { |
|
| 931 | - $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.'); |
|
| 932 | - } |
|
| 933 | - |
|
| 934 | - return $this->setTypedValue( |
|
| 935 | - $userId, |
|
| 936 | - $app, |
|
| 937 | - $key, |
|
| 938 | - (string)$value, |
|
| 939 | - $lazy, |
|
| 940 | - $flags, |
|
| 941 | - ValueType::INT |
|
| 942 | - ); |
|
| 943 | - } |
|
| 944 | - |
|
| 945 | - /** |
|
| 946 | - * @inheritDoc |
|
| 947 | - * |
|
| 948 | - * @param string $userId id of the user |
|
| 949 | - * @param string $app id of the app |
|
| 950 | - * @param string $key config key |
|
| 951 | - * @param float $value config value |
|
| 952 | - * @param bool $lazy set config as lazy loaded |
|
| 953 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 954 | - * |
|
| 955 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 956 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 957 | - * @since 31.0.0 |
|
| 958 | - * @see IUserConfig for explanation about lazy loading |
|
| 959 | - */ |
|
| 960 | - public function setValueFloat( |
|
| 961 | - string $userId, |
|
| 962 | - string $app, |
|
| 963 | - string $key, |
|
| 964 | - float $value, |
|
| 965 | - bool $lazy = false, |
|
| 966 | - int $flags = 0, |
|
| 967 | - ): bool { |
|
| 968 | - return $this->setTypedValue( |
|
| 969 | - $userId, |
|
| 970 | - $app, |
|
| 971 | - $key, |
|
| 972 | - (string)$value, |
|
| 973 | - $lazy, |
|
| 974 | - $flags, |
|
| 975 | - ValueType::FLOAT |
|
| 976 | - ); |
|
| 977 | - } |
|
| 978 | - |
|
| 979 | - /** |
|
| 980 | - * @inheritDoc |
|
| 981 | - * |
|
| 982 | - * @param string $userId id of the user |
|
| 983 | - * @param string $app id of the app |
|
| 984 | - * @param string $key config key |
|
| 985 | - * @param bool $value config value |
|
| 986 | - * @param bool $lazy set config as lazy loaded |
|
| 987 | - * |
|
| 988 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 989 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 990 | - * @since 31.0.0 |
|
| 991 | - * @see IUserConfig for explanation about lazy loading |
|
| 992 | - */ |
|
| 993 | - public function setValueBool( |
|
| 994 | - string $userId, |
|
| 995 | - string $app, |
|
| 996 | - string $key, |
|
| 997 | - bool $value, |
|
| 998 | - bool $lazy = false, |
|
| 999 | - int $flags = 0, |
|
| 1000 | - ): bool { |
|
| 1001 | - return $this->setTypedValue( |
|
| 1002 | - $userId, |
|
| 1003 | - $app, |
|
| 1004 | - $key, |
|
| 1005 | - ($value) ? '1' : '0', |
|
| 1006 | - $lazy, |
|
| 1007 | - $flags, |
|
| 1008 | - ValueType::BOOL |
|
| 1009 | - ); |
|
| 1010 | - } |
|
| 1011 | - |
|
| 1012 | - /** |
|
| 1013 | - * @inheritDoc |
|
| 1014 | - * |
|
| 1015 | - * @param string $userId id of the user |
|
| 1016 | - * @param string $app id of the app |
|
| 1017 | - * @param string $key config key |
|
| 1018 | - * @param array $value config value |
|
| 1019 | - * @param bool $lazy set config as lazy loaded |
|
| 1020 | - * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 1021 | - * |
|
| 1022 | - * @return bool TRUE if value was different, therefor updated in database |
|
| 1023 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 1024 | - * @throws JsonException |
|
| 1025 | - * @since 31.0.0 |
|
| 1026 | - * @see IUserConfig for explanation about lazy loading |
|
| 1027 | - */ |
|
| 1028 | - public function setValueArray( |
|
| 1029 | - string $userId, |
|
| 1030 | - string $app, |
|
| 1031 | - string $key, |
|
| 1032 | - array $value, |
|
| 1033 | - bool $lazy = false, |
|
| 1034 | - int $flags = 0, |
|
| 1035 | - ): bool { |
|
| 1036 | - try { |
|
| 1037 | - return $this->setTypedValue( |
|
| 1038 | - $userId, |
|
| 1039 | - $app, |
|
| 1040 | - $key, |
|
| 1041 | - json_encode($value, JSON_THROW_ON_ERROR), |
|
| 1042 | - $lazy, |
|
| 1043 | - $flags, |
|
| 1044 | - ValueType::ARRAY |
|
| 1045 | - ); |
|
| 1046 | - } catch (JsonException $e) { |
|
| 1047 | - $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]); |
|
| 1048 | - throw $e; |
|
| 1049 | - } |
|
| 1050 | - } |
|
| 1051 | - |
|
| 1052 | - /** |
|
| 1053 | - * Store a config key and its value in database |
|
| 1054 | - * |
|
| 1055 | - * If config key is already known with the exact same config value and same sensitive/lazy status, the |
|
| 1056 | - * database is not updated. If config value was previously stored as sensitive, status will not be |
|
| 1057 | - * altered. |
|
| 1058 | - * |
|
| 1059 | - * @param string $userId id of the user |
|
| 1060 | - * @param string $app id of the app |
|
| 1061 | - * @param string $key config key |
|
| 1062 | - * @param string $value config value |
|
| 1063 | - * @param bool $lazy config set as lazy loaded |
|
| 1064 | - * @param ValueType $type value type |
|
| 1065 | - * |
|
| 1066 | - * @return bool TRUE if value was updated in database |
|
| 1067 | - * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 1068 | - * @see IUserConfig for explanation about lazy loading |
|
| 1069 | - */ |
|
| 1070 | - private function setTypedValue( |
|
| 1071 | - string $userId, |
|
| 1072 | - string $app, |
|
| 1073 | - string $key, |
|
| 1074 | - string $value, |
|
| 1075 | - bool $lazy, |
|
| 1076 | - int $flags, |
|
| 1077 | - ValueType $type, |
|
| 1078 | - ): bool { |
|
| 1079 | - // Primary email addresses are always(!) expected to be lowercase |
|
| 1080 | - if ($app === 'settings' && $key === 'email') { |
|
| 1081 | - $value = strtolower($value); |
|
| 1082 | - } |
|
| 1083 | - |
|
| 1084 | - $this->assertParams($userId, $app, $key); |
|
| 1085 | - if (!$this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, $flags)) { |
|
| 1086 | - // returns false as database is not updated |
|
| 1087 | - return false; |
|
| 1088 | - } |
|
| 1089 | - $this->loadConfig($userId, $lazy); |
|
| 1090 | - |
|
| 1091 | - $inserted = $refreshCache = false; |
|
| 1092 | - $origValue = $value; |
|
| 1093 | - $sensitive = $this->isFlagged(self::FLAG_SENSITIVE, $flags); |
|
| 1094 | - if ($sensitive || ($this->hasKey($userId, $app, $key, $lazy) && $this->isSensitive($userId, $app, $key, $lazy))) { |
|
| 1095 | - $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 1096 | - $flags |= self::FLAG_SENSITIVE; |
|
| 1097 | - } |
|
| 1098 | - |
|
| 1099 | - // if requested, we fill the 'indexed' field with current value |
|
| 1100 | - $indexed = ''; |
|
| 1101 | - if ($type !== ValueType::ARRAY && $this->isFlagged(self::FLAG_INDEXED, $flags)) { |
|
| 1102 | - if ($this->isFlagged(self::FLAG_SENSITIVE, $flags)) { |
|
| 1103 | - $this->logger->warning('sensitive value are not to be indexed'); |
|
| 1104 | - } elseif (strlen($value) > self::USER_MAX_LENGTH) { |
|
| 1105 | - $this->logger->warning('value is too lengthy to be indexed'); |
|
| 1106 | - } else { |
|
| 1107 | - $indexed = $value; |
|
| 1108 | - } |
|
| 1109 | - } |
|
| 1110 | - |
|
| 1111 | - if ($this->hasKey($userId, $app, $key, $lazy)) { |
|
| 1112 | - /** |
|
| 1113 | - * no update if key is already known with set lazy status and value is |
|
| 1114 | - * not different, unless sensitivity is switched from false to true. |
|
| 1115 | - */ |
|
| 1116 | - if ($origValue === $this->getTypedValue($userId, $app, $key, $value, $lazy, $type) |
|
| 1117 | - && (!$sensitive || $this->isSensitive($userId, $app, $key, $lazy))) { |
|
| 1118 | - return false; |
|
| 1119 | - } |
|
| 1120 | - } else { |
|
| 1121 | - /** |
|
| 1122 | - * if key is not known yet, we try to insert. |
|
| 1123 | - * It might fail if the key exists with a different lazy flag. |
|
| 1124 | - */ |
|
| 1125 | - try { |
|
| 1126 | - $insert = $this->connection->getQueryBuilder(); |
|
| 1127 | - $insert->insert('preferences') |
|
| 1128 | - ->setValue('userid', $insert->createNamedParameter($userId)) |
|
| 1129 | - ->setValue('appid', $insert->createNamedParameter($app)) |
|
| 1130 | - ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1131 | - ->setValue('type', $insert->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1132 | - ->setValue('flags', $insert->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1133 | - ->setValue('indexed', $insert->createNamedParameter($indexed)) |
|
| 1134 | - ->setValue('configkey', $insert->createNamedParameter($key)) |
|
| 1135 | - ->setValue('configvalue', $insert->createNamedParameter($value)); |
|
| 1136 | - $insert->executeStatement(); |
|
| 1137 | - $inserted = true; |
|
| 1138 | - } catch (DBException $e) { |
|
| 1139 | - if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { |
|
| 1140 | - // TODO: throw exception or just log and returns false !? |
|
| 1141 | - throw $e; |
|
| 1142 | - } |
|
| 1143 | - } |
|
| 1144 | - } |
|
| 1145 | - |
|
| 1146 | - /** |
|
| 1147 | - * We cannot insert a new row, meaning we need to update an already existing one |
|
| 1148 | - */ |
|
| 1149 | - if (!$inserted) { |
|
| 1150 | - $currType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; |
|
| 1151 | - if ($currType === null) { // this might happen when switching lazy loading status |
|
| 1152 | - $this->loadConfigAll($userId); |
|
| 1153 | - $currType = $this->valueDetails[$userId][$app][$key]['type']; |
|
| 1154 | - } |
|
| 1155 | - |
|
| 1156 | - /** |
|
| 1157 | - * We only log a warning and set it to VALUE_MIXED. |
|
| 1158 | - */ |
|
| 1159 | - if ($currType === null) { |
|
| 1160 | - $this->logger->warning('Value type is set to zero (0) in database. This is not supposed to happens', ['app' => $app, 'key' => $key]); |
|
| 1161 | - $currType = ValueType::MIXED; |
|
| 1162 | - } |
|
| 1163 | - |
|
| 1164 | - /** |
|
| 1165 | - * we only accept a different type from the one stored in database |
|
| 1166 | - * if the one stored in database is not-defined (VALUE_MIXED) |
|
| 1167 | - */ |
|
| 1168 | - if ($currType !== ValueType::MIXED |
|
| 1169 | - && $currType !== $type) { |
|
| 1170 | - try { |
|
| 1171 | - $currTypeDef = $currType->getDefinition(); |
|
| 1172 | - $typeDef = $type->getDefinition(); |
|
| 1173 | - } catch (IncorrectTypeException) { |
|
| 1174 | - $currTypeDef = $currType->value; |
|
| 1175 | - $typeDef = $type->value; |
|
| 1176 | - } |
|
| 1177 | - throw new TypeConflictException('conflict between new type (' . $typeDef . ') and old type (' . $currTypeDef . ')'); |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - if ($lazy !== $this->isLazy($userId, $app, $key)) { |
|
| 1181 | - $refreshCache = true; |
|
| 1182 | - } |
|
| 1183 | - |
|
| 1184 | - $update = $this->connection->getQueryBuilder(); |
|
| 1185 | - $update->update('preferences') |
|
| 1186 | - ->set('configvalue', $update->createNamedParameter($value)) |
|
| 1187 | - ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1188 | - ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1189 | - ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1190 | - ->set('indexed', $update->createNamedParameter($indexed)) |
|
| 1191 | - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1192 | - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1193 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1194 | - |
|
| 1195 | - $update->executeStatement(); |
|
| 1196 | - } |
|
| 1197 | - |
|
| 1198 | - if ($refreshCache) { |
|
| 1199 | - $this->clearCache($userId); |
|
| 1200 | - return true; |
|
| 1201 | - } |
|
| 1202 | - |
|
| 1203 | - // update local cache |
|
| 1204 | - if ($lazy) { |
|
| 1205 | - $this->lazyCache[$userId][$app][$key] = $value; |
|
| 1206 | - } else { |
|
| 1207 | - $this->fastCache[$userId][$app][$key] = $value; |
|
| 1208 | - } |
|
| 1209 | - $this->valueDetails[$userId][$app][$key] = [ |
|
| 1210 | - 'type' => $type, |
|
| 1211 | - 'flags' => $flags |
|
| 1212 | - ]; |
|
| 1213 | - |
|
| 1214 | - return true; |
|
| 1215 | - } |
|
| 1216 | - |
|
| 1217 | - /** |
|
| 1218 | - * Change the type of config value. |
|
| 1219 | - * |
|
| 1220 | - * **WARNING:** Method is internal and **MUST** not be used as it may break things. |
|
| 1221 | - * |
|
| 1222 | - * @param string $userId id of the user |
|
| 1223 | - * @param string $app id of the app |
|
| 1224 | - * @param string $key config key |
|
| 1225 | - * @param ValueType $type value type |
|
| 1226 | - * |
|
| 1227 | - * @return bool TRUE if database update were necessary |
|
| 1228 | - * @throws UnknownKeyException if $key is now known in database |
|
| 1229 | - * @throws IncorrectTypeException if $type is not valid |
|
| 1230 | - * @internal |
|
| 1231 | - * @since 31.0.0 |
|
| 1232 | - */ |
|
| 1233 | - public function updateType(string $userId, string $app, string $key, ValueType $type = ValueType::MIXED): bool { |
|
| 1234 | - $this->assertParams($userId, $app, $key); |
|
| 1235 | - $this->loadConfigAll($userId); |
|
| 1236 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1237 | - $this->isLazy($userId, $app, $key); // confirm key exists |
|
| 1238 | - |
|
| 1239 | - $update = $this->connection->getQueryBuilder(); |
|
| 1240 | - $update->update('preferences') |
|
| 1241 | - ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1242 | - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1243 | - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1244 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1245 | - $update->executeStatement(); |
|
| 1246 | - |
|
| 1247 | - $this->valueDetails[$userId][$app][$key]['type'] = $type; |
|
| 1248 | - |
|
| 1249 | - return true; |
|
| 1250 | - } |
|
| 1251 | - |
|
| 1252 | - /** |
|
| 1253 | - * @inheritDoc |
|
| 1254 | - * |
|
| 1255 | - * @param string $userId id of the user |
|
| 1256 | - * @param string $app id of the app |
|
| 1257 | - * @param string $key config key |
|
| 1258 | - * @param bool $sensitive TRUE to set as sensitive, FALSE to unset |
|
| 1259 | - * |
|
| 1260 | - * @return bool TRUE if entry was found in database and an update was necessary |
|
| 1261 | - * @since 31.0.0 |
|
| 1262 | - */ |
|
| 1263 | - public function updateSensitive(string $userId, string $app, string $key, bool $sensitive): bool { |
|
| 1264 | - $this->assertParams($userId, $app, $key); |
|
| 1265 | - $this->loadConfigAll($userId); |
|
| 1266 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1267 | - |
|
| 1268 | - try { |
|
| 1269 | - if ($sensitive === $this->isSensitive($userId, $app, $key, null)) { |
|
| 1270 | - return false; |
|
| 1271 | - } |
|
| 1272 | - } catch (UnknownKeyException) { |
|
| 1273 | - return false; |
|
| 1274 | - } |
|
| 1275 | - |
|
| 1276 | - $lazy = $this->isLazy($userId, $app, $key); |
|
| 1277 | - if ($lazy) { |
|
| 1278 | - $cache = $this->lazyCache; |
|
| 1279 | - } else { |
|
| 1280 | - $cache = $this->fastCache; |
|
| 1281 | - } |
|
| 1282 | - |
|
| 1283 | - if (!isset($cache[$userId][$app][$key])) { |
|
| 1284 | - throw new UnknownKeyException('unknown config key'); |
|
| 1285 | - } |
|
| 1286 | - |
|
| 1287 | - $value = $cache[$userId][$app][$key]; |
|
| 1288 | - $flags = $this->getValueFlags($userId, $app, $key); |
|
| 1289 | - if ($sensitive) { |
|
| 1290 | - $flags |= self::FLAG_SENSITIVE; |
|
| 1291 | - $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 1292 | - } else { |
|
| 1293 | - $flags &= ~self::FLAG_SENSITIVE; |
|
| 1294 | - $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 1295 | - } |
|
| 1296 | - |
|
| 1297 | - $update = $this->connection->getQueryBuilder(); |
|
| 1298 | - $update->update('preferences') |
|
| 1299 | - ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1300 | - ->set('configvalue', $update->createNamedParameter($value)) |
|
| 1301 | - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1302 | - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1303 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1304 | - $update->executeStatement(); |
|
| 1305 | - |
|
| 1306 | - $this->valueDetails[$userId][$app][$key]['flags'] = $flags; |
|
| 1307 | - |
|
| 1308 | - return true; |
|
| 1309 | - } |
|
| 1310 | - |
|
| 1311 | - /** |
|
| 1312 | - * @inheritDoc |
|
| 1313 | - * |
|
| 1314 | - * @param string $app |
|
| 1315 | - * @param string $key |
|
| 1316 | - * @param bool $sensitive |
|
| 1317 | - * |
|
| 1318 | - * @since 31.0.0 |
|
| 1319 | - */ |
|
| 1320 | - public function updateGlobalSensitive(string $app, string $key, bool $sensitive): void { |
|
| 1321 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1322 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1323 | - |
|
| 1324 | - foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) { |
|
| 1325 | - try { |
|
| 1326 | - $this->updateSensitive($userId, $app, $key, $sensitive); |
|
| 1327 | - } catch (UnknownKeyException) { |
|
| 1328 | - // should not happen and can be ignored |
|
| 1329 | - } |
|
| 1330 | - } |
|
| 1331 | - |
|
| 1332 | - // we clear all cache |
|
| 1333 | - $this->clearCacheAll(); |
|
| 1334 | - } |
|
| 1335 | - |
|
| 1336 | - /** |
|
| 1337 | - * @inheritDoc |
|
| 1338 | - * |
|
| 1339 | - * @param string $userId |
|
| 1340 | - * @param string $app |
|
| 1341 | - * @param string $key |
|
| 1342 | - * @param bool $indexed |
|
| 1343 | - * |
|
| 1344 | - * @return bool |
|
| 1345 | - * @throws DBException |
|
| 1346 | - * @throws IncorrectTypeException |
|
| 1347 | - * @throws UnknownKeyException |
|
| 1348 | - * @since 31.0.0 |
|
| 1349 | - */ |
|
| 1350 | - public function updateIndexed(string $userId, string $app, string $key, bool $indexed): bool { |
|
| 1351 | - $this->assertParams($userId, $app, $key); |
|
| 1352 | - $this->loadConfigAll($userId); |
|
| 1353 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1354 | - |
|
| 1355 | - try { |
|
| 1356 | - if ($indexed === $this->isIndexed($userId, $app, $key, null)) { |
|
| 1357 | - return false; |
|
| 1358 | - } |
|
| 1359 | - } catch (UnknownKeyException) { |
|
| 1360 | - return false; |
|
| 1361 | - } |
|
| 1362 | - |
|
| 1363 | - $lazy = $this->isLazy($userId, $app, $key); |
|
| 1364 | - if ($lazy) { |
|
| 1365 | - $cache = $this->lazyCache; |
|
| 1366 | - } else { |
|
| 1367 | - $cache = $this->fastCache; |
|
| 1368 | - } |
|
| 1369 | - |
|
| 1370 | - if (!isset($cache[$userId][$app][$key])) { |
|
| 1371 | - throw new UnknownKeyException('unknown config key'); |
|
| 1372 | - } |
|
| 1373 | - |
|
| 1374 | - $value = $cache[$userId][$app][$key]; |
|
| 1375 | - $flags = $this->getValueFlags($userId, $app, $key); |
|
| 1376 | - if ($indexed) { |
|
| 1377 | - $indexed = $value; |
|
| 1378 | - } else { |
|
| 1379 | - $flags &= ~self::FLAG_INDEXED; |
|
| 1380 | - $indexed = ''; |
|
| 1381 | - } |
|
| 1382 | - |
|
| 1383 | - $update = $this->connection->getQueryBuilder(); |
|
| 1384 | - $update->update('preferences') |
|
| 1385 | - ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1386 | - ->set('indexed', $update->createNamedParameter($indexed)) |
|
| 1387 | - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1388 | - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1389 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1390 | - $update->executeStatement(); |
|
| 1391 | - |
|
| 1392 | - $this->valueDetails[$userId][$app][$key]['flags'] = $flags; |
|
| 1393 | - |
|
| 1394 | - return true; |
|
| 1395 | - } |
|
| 1396 | - |
|
| 1397 | - |
|
| 1398 | - /** |
|
| 1399 | - * @inheritDoc |
|
| 1400 | - * |
|
| 1401 | - * @param string $app |
|
| 1402 | - * @param string $key |
|
| 1403 | - * @param bool $indexed |
|
| 1404 | - * |
|
| 1405 | - * @since 31.0.0 |
|
| 1406 | - */ |
|
| 1407 | - public function updateGlobalIndexed(string $app, string $key, bool $indexed): void { |
|
| 1408 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1409 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1410 | - |
|
| 1411 | - foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) { |
|
| 1412 | - try { |
|
| 1413 | - $this->updateIndexed($userId, $app, $key, $indexed); |
|
| 1414 | - } catch (UnknownKeyException) { |
|
| 1415 | - // should not happen and can be ignored |
|
| 1416 | - } |
|
| 1417 | - } |
|
| 1418 | - |
|
| 1419 | - // we clear all cache |
|
| 1420 | - $this->clearCacheAll(); |
|
| 1421 | - } |
|
| 1422 | - |
|
| 1423 | - /** |
|
| 1424 | - * @inheritDoc |
|
| 1425 | - * |
|
| 1426 | - * @param string $userId id of the user |
|
| 1427 | - * @param string $app id of the app |
|
| 1428 | - * @param string $key config key |
|
| 1429 | - * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 1430 | - * |
|
| 1431 | - * @return bool TRUE if entry was found in database and an update was necessary |
|
| 1432 | - * @since 31.0.0 |
|
| 1433 | - */ |
|
| 1434 | - public function updateLazy(string $userId, string $app, string $key, bool $lazy): bool { |
|
| 1435 | - $this->assertParams($userId, $app, $key); |
|
| 1436 | - $this->loadConfigAll($userId); |
|
| 1437 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1438 | - |
|
| 1439 | - try { |
|
| 1440 | - if ($lazy === $this->isLazy($userId, $app, $key)) { |
|
| 1441 | - return false; |
|
| 1442 | - } |
|
| 1443 | - } catch (UnknownKeyException) { |
|
| 1444 | - return false; |
|
| 1445 | - } |
|
| 1446 | - |
|
| 1447 | - $update = $this->connection->getQueryBuilder(); |
|
| 1448 | - $update->update('preferences') |
|
| 1449 | - ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1450 | - ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1451 | - ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1452 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1453 | - $update->executeStatement(); |
|
| 1454 | - |
|
| 1455 | - // At this point, it is a lot safer to clean cache |
|
| 1456 | - $this->clearCache($userId); |
|
| 1457 | - |
|
| 1458 | - return true; |
|
| 1459 | - } |
|
| 1460 | - |
|
| 1461 | - /** |
|
| 1462 | - * @inheritDoc |
|
| 1463 | - * |
|
| 1464 | - * @param string $app id of the app |
|
| 1465 | - * @param string $key config key |
|
| 1466 | - * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 1467 | - * |
|
| 1468 | - * @since 31.0.0 |
|
| 1469 | - */ |
|
| 1470 | - public function updateGlobalLazy(string $app, string $key, bool $lazy): void { |
|
| 1471 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1472 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1473 | - |
|
| 1474 | - $update = $this->connection->getQueryBuilder(); |
|
| 1475 | - $update->update('preferences') |
|
| 1476 | - ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1477 | - ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1478 | - ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1479 | - $update->executeStatement(); |
|
| 1480 | - |
|
| 1481 | - $this->clearCacheAll(); |
|
| 1482 | - } |
|
| 1483 | - |
|
| 1484 | - /** |
|
| 1485 | - * @inheritDoc |
|
| 1486 | - * |
|
| 1487 | - * @param string $userId id of the user |
|
| 1488 | - * @param string $app id of the app |
|
| 1489 | - * @param string $key config key |
|
| 1490 | - * |
|
| 1491 | - * @return array |
|
| 1492 | - * @throws UnknownKeyException if config key is not known in database |
|
| 1493 | - * @since 31.0.0 |
|
| 1494 | - */ |
|
| 1495 | - public function getDetails(string $userId, string $app, string $key): array { |
|
| 1496 | - $this->assertParams($userId, $app, $key); |
|
| 1497 | - $this->loadConfigAll($userId); |
|
| 1498 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1499 | - |
|
| 1500 | - $lazy = $this->isLazy($userId, $app, $key); |
|
| 1501 | - |
|
| 1502 | - if ($lazy) { |
|
| 1503 | - $cache = $this->lazyCache[$userId]; |
|
| 1504 | - } else { |
|
| 1505 | - $cache = $this->fastCache[$userId]; |
|
| 1506 | - } |
|
| 1507 | - |
|
| 1508 | - $type = $this->getValueType($userId, $app, $key); |
|
| 1509 | - try { |
|
| 1510 | - $typeString = $type->getDefinition(); |
|
| 1511 | - } catch (IncorrectTypeException $e) { |
|
| 1512 | - $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]); |
|
| 1513 | - $typeString = (string)$type->value; |
|
| 1514 | - } |
|
| 1515 | - |
|
| 1516 | - if (!isset($cache[$app][$key])) { |
|
| 1517 | - throw new UnknownKeyException('unknown config key'); |
|
| 1518 | - } |
|
| 1519 | - |
|
| 1520 | - $value = $cache[$app][$key]; |
|
| 1521 | - $sensitive = $this->isSensitive($userId, $app, $key, null); |
|
| 1522 | - $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 1523 | - |
|
| 1524 | - return [ |
|
| 1525 | - 'userId' => $userId, |
|
| 1526 | - 'app' => $app, |
|
| 1527 | - 'key' => $key, |
|
| 1528 | - 'value' => $value, |
|
| 1529 | - 'type' => $type->value, |
|
| 1530 | - 'lazy' => $lazy, |
|
| 1531 | - 'typeString' => $typeString, |
|
| 1532 | - 'sensitive' => $sensitive |
|
| 1533 | - ]; |
|
| 1534 | - } |
|
| 1535 | - |
|
| 1536 | - /** |
|
| 1537 | - * @inheritDoc |
|
| 1538 | - * |
|
| 1539 | - * @param string $userId id of the user |
|
| 1540 | - * @param string $app id of the app |
|
| 1541 | - * @param string $key config key |
|
| 1542 | - * |
|
| 1543 | - * @since 31.0.0 |
|
| 1544 | - */ |
|
| 1545 | - public function deleteUserConfig(string $userId, string $app, string $key): void { |
|
| 1546 | - $this->assertParams($userId, $app, $key); |
|
| 1547 | - $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1548 | - |
|
| 1549 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1550 | - $qb->delete('preferences') |
|
| 1551 | - ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))) |
|
| 1552 | - ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1553 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1554 | - $qb->executeStatement(); |
|
| 1555 | - |
|
| 1556 | - unset($this->lazyCache[$userId][$app][$key]); |
|
| 1557 | - unset($this->fastCache[$userId][$app][$key]); |
|
| 1558 | - unset($this->valueDetails[$userId][$app][$key]); |
|
| 1559 | - } |
|
| 1560 | - |
|
| 1561 | - /** |
|
| 1562 | - * @inheritDoc |
|
| 1563 | - * |
|
| 1564 | - * @param string $app id of the app |
|
| 1565 | - * @param string $key config key |
|
| 1566 | - * |
|
| 1567 | - * @since 31.0.0 |
|
| 1568 | - */ |
|
| 1569 | - public function deleteKey(string $app, string $key): void { |
|
| 1570 | - $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1571 | - $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1572 | - |
|
| 1573 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1574 | - $qb->delete('preferences') |
|
| 1575 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1576 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1577 | - $qb->executeStatement(); |
|
| 1578 | - |
|
| 1579 | - $this->clearCacheAll(); |
|
| 1580 | - } |
|
| 1581 | - |
|
| 1582 | - /** |
|
| 1583 | - * @inheritDoc |
|
| 1584 | - * |
|
| 1585 | - * @param string $app id of the app |
|
| 1586 | - * |
|
| 1587 | - * @since 31.0.0 |
|
| 1588 | - */ |
|
| 1589 | - public function deleteApp(string $app): void { |
|
| 1590 | - $this->assertParams('', $app, allowEmptyUser: true); |
|
| 1591 | - |
|
| 1592 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1593 | - $qb->delete('preferences') |
|
| 1594 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 1595 | - $qb->executeStatement(); |
|
| 1596 | - |
|
| 1597 | - $this->clearCacheAll(); |
|
| 1598 | - } |
|
| 1599 | - |
|
| 1600 | - public function deleteAllUserConfig(string $userId): void { |
|
| 1601 | - $this->assertParams($userId, '', allowEmptyApp: true); |
|
| 1602 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1603 | - $qb->delete('preferences') |
|
| 1604 | - ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); |
|
| 1605 | - $qb->executeStatement(); |
|
| 1606 | - |
|
| 1607 | - $this->clearCache($userId); |
|
| 1608 | - } |
|
| 1609 | - |
|
| 1610 | - /** |
|
| 1611 | - * @inheritDoc |
|
| 1612 | - * |
|
| 1613 | - * @param string $userId id of the user |
|
| 1614 | - * @param bool $reload set to TRUE to refill cache instantly after clearing it. |
|
| 1615 | - * |
|
| 1616 | - * @since 31.0.0 |
|
| 1617 | - */ |
|
| 1618 | - public function clearCache(string $userId, bool $reload = false): void { |
|
| 1619 | - $this->assertParams($userId, allowEmptyApp: true); |
|
| 1620 | - $this->lazyLoaded[$userId] = $this->fastLoaded[$userId] = false; |
|
| 1621 | - $this->lazyCache[$userId] = $this->fastCache[$userId] = $this->valueDetails[$userId] = []; |
|
| 1622 | - |
|
| 1623 | - if (!$reload) { |
|
| 1624 | - return; |
|
| 1625 | - } |
|
| 1626 | - |
|
| 1627 | - $this->loadConfigAll($userId); |
|
| 1628 | - } |
|
| 1629 | - |
|
| 1630 | - /** |
|
| 1631 | - * @inheritDoc |
|
| 1632 | - * |
|
| 1633 | - * @since 31.0.0 |
|
| 1634 | - */ |
|
| 1635 | - public function clearCacheAll(): void { |
|
| 1636 | - $this->lazyLoaded = $this->fastLoaded = []; |
|
| 1637 | - $this->lazyCache = $this->fastCache = $this->valueDetails = $this->configLexiconDetails = []; |
|
| 1638 | - $this->configLexiconPreset = null; |
|
| 1639 | - } |
|
| 1640 | - |
|
| 1641 | - /** |
|
| 1642 | - * For debug purpose. |
|
| 1643 | - * Returns the cached data. |
|
| 1644 | - * |
|
| 1645 | - * @return array |
|
| 1646 | - * @since 31.0.0 |
|
| 1647 | - * @internal |
|
| 1648 | - */ |
|
| 1649 | - public function statusCache(): array { |
|
| 1650 | - return [ |
|
| 1651 | - 'fastLoaded' => $this->fastLoaded, |
|
| 1652 | - 'fastCache' => $this->fastCache, |
|
| 1653 | - 'lazyLoaded' => $this->lazyLoaded, |
|
| 1654 | - 'lazyCache' => $this->lazyCache, |
|
| 1655 | - 'valueDetails' => $this->valueDetails, |
|
| 1656 | - ]; |
|
| 1657 | - } |
|
| 1658 | - |
|
| 1659 | - /** |
|
| 1660 | - * @param int $needle bitflag to search |
|
| 1661 | - * @param int $flags all flags |
|
| 1662 | - * |
|
| 1663 | - * @return bool TRUE if bitflag $needle is set in $flags |
|
| 1664 | - */ |
|
| 1665 | - private function isFlagged(int $needle, int $flags): bool { |
|
| 1666 | - return (($needle & $flags) !== 0); |
|
| 1667 | - } |
|
| 1668 | - |
|
| 1669 | - /** |
|
| 1670 | - * Confirm the string set for app and key fit the database description |
|
| 1671 | - * |
|
| 1672 | - * @param string $userId |
|
| 1673 | - * @param string $app assert $app fit in database |
|
| 1674 | - * @param string $prefKey assert config key fit in database |
|
| 1675 | - * @param bool $allowEmptyUser |
|
| 1676 | - * @param bool $allowEmptyApp $app can be empty string |
|
| 1677 | - * @param ValueType|null $valueType assert value type is only one type |
|
| 1678 | - */ |
|
| 1679 | - private function assertParams( |
|
| 1680 | - string $userId = '', |
|
| 1681 | - string $app = '', |
|
| 1682 | - string $prefKey = '', |
|
| 1683 | - bool $allowEmptyUser = false, |
|
| 1684 | - bool $allowEmptyApp = false, |
|
| 1685 | - ): void { |
|
| 1686 | - if (!$allowEmptyUser && $userId === '') { |
|
| 1687 | - throw new InvalidArgumentException('userId cannot be an empty string'); |
|
| 1688 | - } |
|
| 1689 | - if (!$allowEmptyApp && $app === '') { |
|
| 1690 | - throw new InvalidArgumentException('app cannot be an empty string'); |
|
| 1691 | - } |
|
| 1692 | - if (strlen($userId) > self::USER_MAX_LENGTH) { |
|
| 1693 | - throw new InvalidArgumentException('Value (' . $userId . ') for userId is too long (' . self::USER_MAX_LENGTH . ')'); |
|
| 1694 | - } |
|
| 1695 | - if (strlen($app) > self::APP_MAX_LENGTH) { |
|
| 1696 | - throw new InvalidArgumentException('Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')'); |
|
| 1697 | - } |
|
| 1698 | - if (strlen($prefKey) > self::KEY_MAX_LENGTH) { |
|
| 1699 | - throw new InvalidArgumentException('Value (' . $prefKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); |
|
| 1700 | - } |
|
| 1701 | - } |
|
| 1702 | - |
|
| 1703 | - private function loadConfigAll(string $userId): void { |
|
| 1704 | - $this->loadConfig($userId, null); |
|
| 1705 | - } |
|
| 1706 | - |
|
| 1707 | - /** |
|
| 1708 | - * Load normal config or config set as lazy loaded |
|
| 1709 | - * |
|
| 1710 | - * @param bool|null $lazy set to TRUE to load config set as lazy loaded, set to NULL to load all config |
|
| 1711 | - */ |
|
| 1712 | - private function loadConfig(string $userId, ?bool $lazy = false): void { |
|
| 1713 | - if ($this->isLoaded($userId, $lazy)) { |
|
| 1714 | - return; |
|
| 1715 | - } |
|
| 1716 | - |
|
| 1717 | - if (($lazy ?? true) !== false) { // if lazy is null or true, we debug log |
|
| 1718 | - $this->logger->debug('The loading of lazy UserConfig values have been requested', ['exception' => new \RuntimeException('ignorable exception')]); |
|
| 1719 | - } |
|
| 1720 | - |
|
| 1721 | - $qb = $this->connection->getQueryBuilder(); |
|
| 1722 | - $qb->from('preferences'); |
|
| 1723 | - $qb->select('appid', 'configkey', 'configvalue', 'type', 'flags'); |
|
| 1724 | - $qb->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); |
|
| 1725 | - |
|
| 1726 | - // we only need value from lazy when loadConfig does not specify it |
|
| 1727 | - if ($lazy !== null) { |
|
| 1728 | - $qb->andWhere($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))); |
|
| 1729 | - } else { |
|
| 1730 | - $qb->addSelect('lazy'); |
|
| 1731 | - } |
|
| 1732 | - |
|
| 1733 | - $result = $qb->executeQuery(); |
|
| 1734 | - $rows = $result->fetchAll(); |
|
| 1735 | - foreach ($rows as $row) { |
|
| 1736 | - if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) { |
|
| 1737 | - $this->lazyCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1738 | - } else { |
|
| 1739 | - $this->fastCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1740 | - } |
|
| 1741 | - $this->valueDetails[$userId][$row['appid']][$row['configkey']] = ['type' => ValueType::from((int)($row['type'] ?? 0)), 'flags' => (int)$row['flags']]; |
|
| 1742 | - } |
|
| 1743 | - $result->closeCursor(); |
|
| 1744 | - $this->setAsLoaded($userId, $lazy); |
|
| 1745 | - } |
|
| 1746 | - |
|
| 1747 | - /** |
|
| 1748 | - * if $lazy is: |
|
| 1749 | - * - false: will returns true if fast config are loaded |
|
| 1750 | - * - true : will returns true if lazy config are loaded |
|
| 1751 | - * - null : will returns true if both config are loaded |
|
| 1752 | - * |
|
| 1753 | - * @param string $userId |
|
| 1754 | - * @param bool $lazy |
|
| 1755 | - * |
|
| 1756 | - * @return bool |
|
| 1757 | - */ |
|
| 1758 | - private function isLoaded(string $userId, ?bool $lazy): bool { |
|
| 1759 | - if ($lazy === null) { |
|
| 1760 | - return ($this->lazyLoaded[$userId] ?? false) && ($this->fastLoaded[$userId] ?? false); |
|
| 1761 | - } |
|
| 1762 | - |
|
| 1763 | - return $lazy ? $this->lazyLoaded[$userId] ?? false : $this->fastLoaded[$userId] ?? false; |
|
| 1764 | - } |
|
| 1765 | - |
|
| 1766 | - /** |
|
| 1767 | - * if $lazy is: |
|
| 1768 | - * - false: set fast config as loaded |
|
| 1769 | - * - true : set lazy config as loaded |
|
| 1770 | - * - null : set both config as loaded |
|
| 1771 | - * |
|
| 1772 | - * @param string $userId |
|
| 1773 | - * @param bool $lazy |
|
| 1774 | - */ |
|
| 1775 | - private function setAsLoaded(string $userId, ?bool $lazy): void { |
|
| 1776 | - if ($lazy === null) { |
|
| 1777 | - $this->fastLoaded[$userId] = $this->lazyLoaded[$userId] = true; |
|
| 1778 | - return; |
|
| 1779 | - } |
|
| 1780 | - |
|
| 1781 | - // We also create empty entry to keep both fastLoaded/lazyLoaded synced |
|
| 1782 | - if ($lazy) { |
|
| 1783 | - $this->lazyLoaded[$userId] = true; |
|
| 1784 | - $this->fastLoaded[$userId] = $this->fastLoaded[$userId] ?? false; |
|
| 1785 | - $this->fastCache[$userId] = $this->fastCache[$userId] ?? []; |
|
| 1786 | - } else { |
|
| 1787 | - $this->fastLoaded[$userId] = true; |
|
| 1788 | - $this->lazyLoaded[$userId] = $this->lazyLoaded[$userId] ?? false; |
|
| 1789 | - $this->lazyCache[$userId] = $this->lazyCache[$userId] ?? []; |
|
| 1790 | - } |
|
| 1791 | - } |
|
| 1792 | - |
|
| 1793 | - /** |
|
| 1794 | - * **Warning:** this will load all lazy values from the database |
|
| 1795 | - * |
|
| 1796 | - * @param string $userId id of the user |
|
| 1797 | - * @param string $app id of the app |
|
| 1798 | - * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 1799 | - * |
|
| 1800 | - * @return array<string, string|int|float|bool|array> |
|
| 1801 | - */ |
|
| 1802 | - private function formatAppValues(string $userId, string $app, array $values, bool $filtered = false): array { |
|
| 1803 | - foreach ($values as $key => $value) { |
|
| 1804 | - //$key = (string)$key; |
|
| 1805 | - try { |
|
| 1806 | - $type = $this->getValueType($userId, $app, (string)$key); |
|
| 1807 | - } catch (UnknownKeyException) { |
|
| 1808 | - continue; |
|
| 1809 | - } |
|
| 1810 | - |
|
| 1811 | - if ($this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) { |
|
| 1812 | - if ($filtered) { |
|
| 1813 | - $value = IConfig::SENSITIVE_VALUE; |
|
| 1814 | - $type = ValueType::STRING; |
|
| 1815 | - } else { |
|
| 1816 | - $this->decryptSensitiveValue($userId, $app, (string)$key, $value); |
|
| 1817 | - } |
|
| 1818 | - } |
|
| 1819 | - |
|
| 1820 | - $values[$key] = $this->convertTypedValue($value, $type); |
|
| 1821 | - } |
|
| 1822 | - |
|
| 1823 | - return $values; |
|
| 1824 | - } |
|
| 1825 | - |
|
| 1826 | - /** |
|
| 1827 | - * convert string value to the expected type |
|
| 1828 | - * |
|
| 1829 | - * @param string $value |
|
| 1830 | - * @param ValueType $type |
|
| 1831 | - * |
|
| 1832 | - * @return string|int|float|bool|array |
|
| 1833 | - */ |
|
| 1834 | - private function convertTypedValue(string $value, ValueType $type): string|int|float|bool|array { |
|
| 1835 | - switch ($type) { |
|
| 1836 | - case ValueType::INT: |
|
| 1837 | - return (int)$value; |
|
| 1838 | - case ValueType::FLOAT: |
|
| 1839 | - return (float)$value; |
|
| 1840 | - case ValueType::BOOL: |
|
| 1841 | - return in_array(strtolower($value), ['1', 'true', 'yes', 'on']); |
|
| 1842 | - case ValueType::ARRAY: |
|
| 1843 | - try { |
|
| 1844 | - return json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 1845 | - } catch (JsonException) { |
|
| 1846 | - // ignoreable |
|
| 1847 | - } |
|
| 1848 | - break; |
|
| 1849 | - } |
|
| 1850 | - return $value; |
|
| 1851 | - } |
|
| 1852 | - |
|
| 1853 | - |
|
| 1854 | - /** |
|
| 1855 | - * will change referenced $value with the decrypted value in case of encrypted (sensitive value) |
|
| 1856 | - * |
|
| 1857 | - * @param string $userId |
|
| 1858 | - * @param string $app |
|
| 1859 | - * @param string $key |
|
| 1860 | - * @param string $value |
|
| 1861 | - */ |
|
| 1862 | - private function decryptSensitiveValue(string $userId, string $app, string $key, string &$value): void { |
|
| 1863 | - if (!$this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) { |
|
| 1864 | - return; |
|
| 1865 | - } |
|
| 1866 | - |
|
| 1867 | - if (!str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 1868 | - return; |
|
| 1869 | - } |
|
| 1870 | - |
|
| 1871 | - try { |
|
| 1872 | - $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 1873 | - } catch (\Exception $e) { |
|
| 1874 | - $this->logger->warning('could not decrypt sensitive value', [ |
|
| 1875 | - 'userId' => $userId, |
|
| 1876 | - 'app' => $app, |
|
| 1877 | - 'key' => $key, |
|
| 1878 | - 'value' => $value, |
|
| 1879 | - 'exception' => $e |
|
| 1880 | - ]); |
|
| 1881 | - } |
|
| 1882 | - } |
|
| 1883 | - |
|
| 1884 | - /** |
|
| 1885 | - * Match and apply current use of config values with defined lexicon. |
|
| 1886 | - * Set $lazy to NULL only if only interested into checking that $key is alias. |
|
| 1887 | - * |
|
| 1888 | - * @throws UnknownKeyException |
|
| 1889 | - * @throws TypeConflictException |
|
| 1890 | - * @return bool FALSE if conflict with defined lexicon were observed in the process |
|
| 1891 | - */ |
|
| 1892 | - private function matchAndApplyLexiconDefinition( |
|
| 1893 | - string $userId, |
|
| 1894 | - string $app, |
|
| 1895 | - string &$key, |
|
| 1896 | - ?bool &$lazy = null, |
|
| 1897 | - ValueType &$type = ValueType::MIXED, |
|
| 1898 | - int &$flags = 0, |
|
| 1899 | - ?string &$default = null, |
|
| 1900 | - ): bool { |
|
| 1901 | - $configDetails = $this->getConfigDetailsFromLexicon($app); |
|
| 1902 | - if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) { |
|
| 1903 | - // in case '$rename' is set in ConfigLexiconEntry, we use the new config key |
|
| 1904 | - $key = $configDetails['aliases'][$key]; |
|
| 1905 | - } |
|
| 1906 | - |
|
| 1907 | - if (!array_key_exists($key, $configDetails['entries'])) { |
|
| 1908 | - return $this->applyLexiconStrictness($configDetails['strictness'], 'The user config key ' . $app . '/' . $key . ' is not defined in the config lexicon'); |
|
| 1909 | - } |
|
| 1910 | - |
|
| 1911 | - // if lazy is NULL, we ignore all check on the type/lazyness/default from Lexicon |
|
| 1912 | - if ($lazy === null) { |
|
| 1913 | - return true; |
|
| 1914 | - } |
|
| 1915 | - |
|
| 1916 | - /** @var ConfigLexiconEntry $configValue */ |
|
| 1917 | - $configValue = $configDetails['entries'][$key]; |
|
| 1918 | - if ($type === ValueType::MIXED) { |
|
| 1919 | - // we overwrite if value was requested as mixed |
|
| 1920 | - $type = $configValue->getValueType(); |
|
| 1921 | - } elseif ($configValue->getValueType() !== $type) { |
|
| 1922 | - throw new TypeConflictException('The user config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon'); |
|
| 1923 | - } |
|
| 1924 | - |
|
| 1925 | - $lazy = $configValue->isLazy(); |
|
| 1926 | - $flags = $configValue->getFlags(); |
|
| 1927 | - if ($configValue->isDeprecated()) { |
|
| 1928 | - $this->logger->notice('User config key ' . $app . '/' . $key . ' is set as deprecated.'); |
|
| 1929 | - } |
|
| 1930 | - |
|
| 1931 | - $enforcedValue = $this->config->getSystemValue('lexicon.default.userconfig.enforced', [])[$app][$key] ?? false; |
|
| 1932 | - if (!$enforcedValue && $this->hasKey($userId, $app, $key, $lazy)) { |
|
| 1933 | - // if key exists there should be no need to extract default |
|
| 1934 | - return true; |
|
| 1935 | - } |
|
| 1936 | - |
|
| 1937 | - // only look for default if needed, default from Lexicon got priority if not overwritten by admin |
|
| 1938 | - if ($default !== null) { |
|
| 1939 | - $default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault($this->getLexiconPreset()) ?? $default; |
|
| 1940 | - } |
|
| 1941 | - |
|
| 1942 | - // returning false will make get() returning $default and set() not changing value in database |
|
| 1943 | - return !$enforcedValue; |
|
| 1944 | - } |
|
| 1945 | - |
|
| 1946 | - /** |
|
| 1947 | - * get default value set in config/config.php if stored in key: |
|
| 1948 | - * |
|
| 1949 | - * 'lexicon.default.userconfig' => [ |
|
| 1950 | - * <appId> => [ |
|
| 1951 | - * <configKey> => 'my value', |
|
| 1952 | - * ] |
|
| 1953 | - * ], |
|
| 1954 | - * |
|
| 1955 | - * The entry is converted to string to fit the expected type when managing default value |
|
| 1956 | - */ |
|
| 1957 | - private function getSystemDefault(string $appId, ConfigLexiconEntry $configValue): ?string { |
|
| 1958 | - $default = $this->config->getSystemValue('lexicon.default.userconfig', [])[$appId][$configValue->getKey()] ?? null; |
|
| 1959 | - if ($default === null) { |
|
| 1960 | - // no system default, using default default. |
|
| 1961 | - return null; |
|
| 1962 | - } |
|
| 1963 | - |
|
| 1964 | - return $configValue->convertToString($default); |
|
| 1965 | - } |
|
| 1966 | - |
|
| 1967 | - /** |
|
| 1968 | - * manage ConfigLexicon behavior based on strictness set in IConfigLexicon |
|
| 1969 | - * |
|
| 1970 | - * @see IConfigLexicon::getStrictness() |
|
| 1971 | - * @param ConfigLexiconStrictness|null $strictness |
|
| 1972 | - * @param string $line |
|
| 1973 | - * |
|
| 1974 | - * @return bool TRUE if conflict can be fully ignored |
|
| 1975 | - * @throws UnknownKeyException |
|
| 1976 | - */ |
|
| 1977 | - private function applyLexiconStrictness(?ConfigLexiconStrictness $strictness, string $line = ''): bool { |
|
| 1978 | - if ($strictness === null) { |
|
| 1979 | - return true; |
|
| 1980 | - } |
|
| 1981 | - |
|
| 1982 | - switch ($strictness) { |
|
| 1983 | - case ConfigLexiconStrictness::IGNORE: |
|
| 1984 | - return true; |
|
| 1985 | - case ConfigLexiconStrictness::NOTICE: |
|
| 1986 | - $this->logger->notice($line); |
|
| 1987 | - return true; |
|
| 1988 | - case ConfigLexiconStrictness::WARNING: |
|
| 1989 | - $this->logger->warning($line); |
|
| 1990 | - return false; |
|
| 1991 | - case ConfigLexiconStrictness::EXCEPTION: |
|
| 1992 | - throw new UnknownKeyException($line); |
|
| 1993 | - } |
|
| 1994 | - |
|
| 1995 | - throw new UnknownKeyException($line); |
|
| 1996 | - } |
|
| 1997 | - |
|
| 1998 | - /** |
|
| 1999 | - * extract details from registered $appId's config lexicon |
|
| 2000 | - * |
|
| 2001 | - * @param string $appId |
|
| 2002 | - * @internal |
|
| 2003 | - * |
|
| 2004 | - * @return array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness} |
|
| 2005 | - */ |
|
| 2006 | - public function getConfigDetailsFromLexicon(string $appId): array { |
|
| 2007 | - if (!array_key_exists($appId, $this->configLexiconDetails)) { |
|
| 2008 | - $entries = $aliases = []; |
|
| 2009 | - $bootstrapCoordinator = \OCP\Server::get(Coordinator::class); |
|
| 2010 | - $configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId); |
|
| 2011 | - foreach ($configLexicon?->getUserConfigs() ?? [] as $configEntry) { |
|
| 2012 | - $entries[$configEntry->getKey()] = $configEntry; |
|
| 2013 | - if ($configEntry->getRename() !== null) { |
|
| 2014 | - $aliases[$configEntry->getRename()] = $configEntry->getKey(); |
|
| 2015 | - } |
|
| 2016 | - } |
|
| 2017 | - |
|
| 2018 | - $this->configLexiconDetails[$appId] = [ |
|
| 2019 | - 'entries' => $entries, |
|
| 2020 | - 'aliases' => $aliases, |
|
| 2021 | - 'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE |
|
| 2022 | - ]; |
|
| 2023 | - } |
|
| 2024 | - |
|
| 2025 | - return $this->configLexiconDetails[$appId]; |
|
| 2026 | - } |
|
| 2027 | - |
|
| 2028 | - private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry { |
|
| 2029 | - return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null; |
|
| 2030 | - } |
|
| 2031 | - |
|
| 2032 | - /** |
|
| 2033 | - * if set to TRUE, ignore aliases defined in Config Lexicon during the use of the methods of this class |
|
| 2034 | - * |
|
| 2035 | - * @internal |
|
| 2036 | - */ |
|
| 2037 | - public function ignoreLexiconAliases(bool $ignore): void { |
|
| 2038 | - $this->ignoreLexiconAliases = $ignore; |
|
| 2039 | - } |
|
| 2040 | - |
|
| 2041 | - private function getLexiconPreset(): Preset { |
|
| 2042 | - if ($this->configLexiconPreset === null) { |
|
| 2043 | - $this->configLexiconPreset = Preset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? Preset::NONE; |
|
| 2044 | - } |
|
| 2045 | - |
|
| 2046 | - return $this->configLexiconPreset; |
|
| 2047 | - } |
|
| 50 | + private const USER_MAX_LENGTH = 64; |
|
| 51 | + private const APP_MAX_LENGTH = 32; |
|
| 52 | + private const KEY_MAX_LENGTH = 64; |
|
| 53 | + private const INDEX_MAX_LENGTH = 64; |
|
| 54 | + private const ENCRYPTION_PREFIX = '$UserConfigEncryption$'; |
|
| 55 | + private const ENCRYPTION_PREFIX_LENGTH = 22; // strlen(self::ENCRYPTION_PREFIX) |
|
| 56 | + |
|
| 57 | + /** @var array<string, array<string, array<string, mixed>>> [ass'user_id' => ['app_id' => ['key' => 'value']]] */ |
|
| 58 | + private array $fastCache = []; // cache for normal config keys |
|
| 59 | + /** @var array<string, array<string, array<string, mixed>>> ['user_id' => ['app_id' => ['key' => 'value']]] */ |
|
| 60 | + private array $lazyCache = []; // cache for lazy config keys |
|
| 61 | + /** @var array<string, array<string, array<string, array<string, mixed>>>> ['user_id' => ['app_id' => ['key' => ['type' => ValueType, 'flags' => bitflag]]]] */ |
|
| 62 | + private array $valueDetails = []; // type for all config values |
|
| 63 | + /** @var array<string, boolean> ['user_id' => bool] */ |
|
| 64 | + private array $fastLoaded = []; |
|
| 65 | + /** @var array<string, boolean> ['user_id' => bool] */ |
|
| 66 | + private array $lazyLoaded = []; |
|
| 67 | + /** @var array<string, array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ |
|
| 68 | + private array $configLexiconDetails = []; |
|
| 69 | + private bool $ignoreLexiconAliases = false; |
|
| 70 | + private ?Preset $configLexiconPreset = null; |
|
| 71 | + |
|
| 72 | + public function __construct( |
|
| 73 | + protected IDBConnection $connection, |
|
| 74 | + protected IConfig $config, |
|
| 75 | + protected LoggerInterface $logger, |
|
| 76 | + protected ICrypto $crypto, |
|
| 77 | + ) { |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + /** |
|
| 81 | + * @inheritDoc |
|
| 82 | + * |
|
| 83 | + * @param string $appId optional id of app |
|
| 84 | + * |
|
| 85 | + * @return list<string> list of userIds |
|
| 86 | + * @since 31.0.0 |
|
| 87 | + */ |
|
| 88 | + public function getUserIds(string $appId = ''): array { |
|
| 89 | + $this->assertParams(app: $appId, allowEmptyUser: true, allowEmptyApp: true); |
|
| 90 | + |
|
| 91 | + $qb = $this->connection->getQueryBuilder(); |
|
| 92 | + $qb->from('preferences'); |
|
| 93 | + $qb->select('userid'); |
|
| 94 | + $qb->groupBy('userid'); |
|
| 95 | + if ($appId !== '') { |
|
| 96 | + $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($appId))); |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + $result = $qb->executeQuery(); |
|
| 100 | + $rows = $result->fetchAll(); |
|
| 101 | + $userIds = []; |
|
| 102 | + foreach ($rows as $row) { |
|
| 103 | + $userIds[] = $row['userid']; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + return $userIds; |
|
| 107 | + } |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * @inheritDoc |
|
| 111 | + * |
|
| 112 | + * @return list<string> list of app ids |
|
| 113 | + * @since 31.0.0 |
|
| 114 | + */ |
|
| 115 | + public function getApps(string $userId): array { |
|
| 116 | + $this->assertParams($userId, allowEmptyApp: true); |
|
| 117 | + $this->loadConfigAll($userId); |
|
| 118 | + $apps = array_merge(array_keys($this->fastCache[$userId] ?? []), array_keys($this->lazyCache[$userId] ?? [])); |
|
| 119 | + sort($apps); |
|
| 120 | + |
|
| 121 | + return array_values(array_unique($apps)); |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * @inheritDoc |
|
| 126 | + * |
|
| 127 | + * @param string $userId id of the user |
|
| 128 | + * @param string $app id of the app |
|
| 129 | + * |
|
| 130 | + * @return list<string> list of stored config keys |
|
| 131 | + * @since 31.0.0 |
|
| 132 | + */ |
|
| 133 | + public function getKeys(string $userId, string $app): array { |
|
| 134 | + $this->assertParams($userId, $app); |
|
| 135 | + $this->loadConfigAll($userId); |
|
| 136 | + // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 137 | + $keys = array_map('strval', array_keys(($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []))); |
|
| 138 | + sort($keys); |
|
| 139 | + |
|
| 140 | + return array_values(array_unique($keys)); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + /** |
|
| 144 | + * @inheritDoc |
|
| 145 | + * |
|
| 146 | + * @param string $userId id of the user |
|
| 147 | + * @param string $app id of the app |
|
| 148 | + * @param string $key config key |
|
| 149 | + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 150 | + * |
|
| 151 | + * @return bool TRUE if key exists |
|
| 152 | + * @since 31.0.0 |
|
| 153 | + */ |
|
| 154 | + public function hasKey(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 155 | + $this->assertParams($userId, $app, $key); |
|
| 156 | + $this->loadConfig($userId, $lazy); |
|
| 157 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 158 | + |
|
| 159 | + if ($lazy === null) { |
|
| 160 | + $appCache = $this->getValues($userId, $app); |
|
| 161 | + return isset($appCache[$key]); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + if ($lazy) { |
|
| 165 | + return isset($this->lazyCache[$userId][$app][$key]); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + return isset($this->fastCache[$userId][$app][$key]); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + /** |
|
| 172 | + * @inheritDoc |
|
| 173 | + * |
|
| 174 | + * @param string $userId id of the user |
|
| 175 | + * @param string $app id of the app |
|
| 176 | + * @param string $key config key |
|
| 177 | + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 178 | + * |
|
| 179 | + * @return bool |
|
| 180 | + * @throws UnknownKeyException if config key is not known |
|
| 181 | + * @since 31.0.0 |
|
| 182 | + */ |
|
| 183 | + public function isSensitive(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 184 | + $this->assertParams($userId, $app, $key); |
|
| 185 | + $this->loadConfig($userId, $lazy); |
|
| 186 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 187 | + |
|
| 188 | + if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 189 | + throw new UnknownKeyException('unknown config key'); |
|
| 190 | + } |
|
| 191 | + |
|
| 192 | + return $this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags']); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * @inheritDoc |
|
| 197 | + * |
|
| 198 | + * @param string $userId id of the user |
|
| 199 | + * @param string $app id of the app |
|
| 200 | + * @param string $key config key |
|
| 201 | + * @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config |
|
| 202 | + * |
|
| 203 | + * @return bool |
|
| 204 | + * @throws UnknownKeyException if config key is not known |
|
| 205 | + * @since 31.0.0 |
|
| 206 | + */ |
|
| 207 | + public function isIndexed(string $userId, string $app, string $key, ?bool $lazy = false): bool { |
|
| 208 | + $this->assertParams($userId, $app, $key); |
|
| 209 | + $this->loadConfig($userId, $lazy); |
|
| 210 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 211 | + |
|
| 212 | + if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 213 | + throw new UnknownKeyException('unknown config key'); |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + return $this->isFlagged(self::FLAG_INDEXED, $this->valueDetails[$userId][$app][$key]['flags']); |
|
| 217 | + } |
|
| 218 | + |
|
| 219 | + /** |
|
| 220 | + * @inheritDoc |
|
| 221 | + * |
|
| 222 | + * @param string $userId id of the user |
|
| 223 | + * @param string $app if of the app |
|
| 224 | + * @param string $key config key |
|
| 225 | + * |
|
| 226 | + * @return bool TRUE if config is lazy loaded |
|
| 227 | + * @throws UnknownKeyException if config key is not known |
|
| 228 | + * @see IUserConfig for details about lazy loading |
|
| 229 | + * @since 31.0.0 |
|
| 230 | + */ |
|
| 231 | + public function isLazy(string $userId, string $app, string $key): bool { |
|
| 232 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 233 | + |
|
| 234 | + // there is a huge probability the non-lazy config are already loaded |
|
| 235 | + // meaning that we can start by only checking if a current non-lazy key exists |
|
| 236 | + if ($this->hasKey($userId, $app, $key, false)) { |
|
| 237 | + // meaning key is not lazy. |
|
| 238 | + return false; |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + // as key is not found as non-lazy, we load and search in the lazy config |
|
| 242 | + if ($this->hasKey($userId, $app, $key, true)) { |
|
| 243 | + return true; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + throw new UnknownKeyException('unknown config key'); |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + /** |
|
| 250 | + * @inheritDoc |
|
| 251 | + * |
|
| 252 | + * @param string $userId id of the user |
|
| 253 | + * @param string $app id of the app |
|
| 254 | + * @param string $prefix config keys prefix to search |
|
| 255 | + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 256 | + * |
|
| 257 | + * @return array<string, string|int|float|bool|array> [key => value] |
|
| 258 | + * @since 31.0.0 |
|
| 259 | + */ |
|
| 260 | + public function getValues( |
|
| 261 | + string $userId, |
|
| 262 | + string $app, |
|
| 263 | + string $prefix = '', |
|
| 264 | + bool $filtered = false, |
|
| 265 | + ): array { |
|
| 266 | + $this->assertParams($userId, $app, $prefix); |
|
| 267 | + // if we want to filter values, we need to get sensitivity |
|
| 268 | + $this->loadConfigAll($userId); |
|
| 269 | + // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 270 | + $values = array_filter( |
|
| 271 | + $this->formatAppValues($userId, $app, ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []), $filtered), |
|
| 272 | + function (string $key) use ($prefix): bool { |
|
| 273 | + // filter values based on $prefix |
|
| 274 | + return str_starts_with($key, $prefix); |
|
| 275 | + }, ARRAY_FILTER_USE_KEY |
|
| 276 | + ); |
|
| 277 | + |
|
| 278 | + return $values; |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + /** |
|
| 282 | + * @inheritDoc |
|
| 283 | + * |
|
| 284 | + * @param string $userId id of the user |
|
| 285 | + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 286 | + * |
|
| 287 | + * @return array<string, array<string, string|int|float|bool|array>> [appId => [key => value]] |
|
| 288 | + * @since 31.0.0 |
|
| 289 | + */ |
|
| 290 | + public function getAllValues(string $userId, bool $filtered = false): array { |
|
| 291 | + $this->assertParams($userId, allowEmptyApp: true); |
|
| 292 | + $this->loadConfigAll($userId); |
|
| 293 | + |
|
| 294 | + $result = []; |
|
| 295 | + foreach ($this->getApps($userId) as $app) { |
|
| 296 | + // array_merge() will remove numeric keys (here config keys), so addition arrays instead |
|
| 297 | + $cached = ($this->fastCache[$userId][$app] ?? []) + ($this->lazyCache[$userId][$app] ?? []); |
|
| 298 | + $result[$app] = $this->formatAppValues($userId, $app, $cached, $filtered); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + return $result; |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + /** |
|
| 305 | + * @inheritDoc |
|
| 306 | + * |
|
| 307 | + * @param string $userId id of the user |
|
| 308 | + * @param string $key config key |
|
| 309 | + * @param bool $lazy search within lazy loaded config |
|
| 310 | + * @param ValueType|null $typedAs enforce type for the returned values |
|
| 311 | + * |
|
| 312 | + * @return array<string, string|int|float|bool|array> [appId => value] |
|
| 313 | + * @since 31.0.0 |
|
| 314 | + */ |
|
| 315 | + public function getValuesByApps(string $userId, string $key, bool $lazy = false, ?ValueType $typedAs = null): array { |
|
| 316 | + $this->assertParams($userId, '', $key, allowEmptyApp: true); |
|
| 317 | + $this->loadConfig($userId, $lazy); |
|
| 318 | + |
|
| 319 | + /** @var array<array-key, array<array-key, mixed>> $cache */ |
|
| 320 | + if ($lazy) { |
|
| 321 | + $cache = $this->lazyCache[$userId]; |
|
| 322 | + } else { |
|
| 323 | + $cache = $this->fastCache[$userId]; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + $values = []; |
|
| 327 | + foreach (array_keys($cache) as $app) { |
|
| 328 | + if (isset($cache[$app][$key])) { |
|
| 329 | + $value = $cache[$app][$key]; |
|
| 330 | + try { |
|
| 331 | + $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 332 | + $value = $this->convertTypedValue($value, $typedAs ?? $this->getValueType($userId, $app, $key, $lazy)); |
|
| 333 | + } catch (IncorrectTypeException|UnknownKeyException) { |
|
| 334 | + } |
|
| 335 | + $values[$app] = $value; |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + return $values; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + |
|
| 343 | + /** |
|
| 344 | + * @inheritDoc |
|
| 345 | + * |
|
| 346 | + * @param string $app id of the app |
|
| 347 | + * @param string $key config key |
|
| 348 | + * @param ValueType|null $typedAs enforce type for the returned values |
|
| 349 | + * @param array|null $userIds limit to a list of user ids |
|
| 350 | + * |
|
| 351 | + * @return array<string, string|int|float|bool|array> [userId => value] |
|
| 352 | + * @since 31.0.0 |
|
| 353 | + */ |
|
| 354 | + public function getValuesByUsers( |
|
| 355 | + string $app, |
|
| 356 | + string $key, |
|
| 357 | + ?ValueType $typedAs = null, |
|
| 358 | + ?array $userIds = null, |
|
| 359 | + ): array { |
|
| 360 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 361 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 362 | + |
|
| 363 | + $qb = $this->connection->getQueryBuilder(); |
|
| 364 | + $qb->select('userid', 'configvalue', 'type') |
|
| 365 | + ->from('preferences') |
|
| 366 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 367 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 368 | + |
|
| 369 | + $values = []; |
|
| 370 | + // this nested function will execute current Query and store result within $values. |
|
| 371 | + $executeAndStoreValue = function (IQueryBuilder $qb) use (&$values, $typedAs): IResult { |
|
| 372 | + $result = $qb->executeQuery(); |
|
| 373 | + while ($row = $result->fetch()) { |
|
| 374 | + $value = $row['configvalue']; |
|
| 375 | + try { |
|
| 376 | + $value = $this->convertTypedValue($value, $typedAs ?? ValueType::from((int)$row['type'])); |
|
| 377 | + } catch (IncorrectTypeException) { |
|
| 378 | + } |
|
| 379 | + $values[$row['userid']] = $value; |
|
| 380 | + } |
|
| 381 | + return $result; |
|
| 382 | + }; |
|
| 383 | + |
|
| 384 | + // if no userIds to filter, we execute query as it is and returns all values ... |
|
| 385 | + if ($userIds === null) { |
|
| 386 | + $result = $executeAndStoreValue($qb); |
|
| 387 | + $result->closeCursor(); |
|
| 388 | + return $values; |
|
| 389 | + } |
|
| 390 | + |
|
| 391 | + // if userIds to filter, we chunk the list and execute the same query multiple times until we get all values |
|
| 392 | + $result = null; |
|
| 393 | + $qb->andWhere($qb->expr()->in('userid', $qb->createParameter('userIds'))); |
|
| 394 | + foreach (array_chunk($userIds, 50, true) as $chunk) { |
|
| 395 | + $qb->setParameter('userIds', $chunk, IQueryBuilder::PARAM_STR_ARRAY); |
|
| 396 | + $result = $executeAndStoreValue($qb); |
|
| 397 | + } |
|
| 398 | + $result?->closeCursor(); |
|
| 399 | + |
|
| 400 | + return $values; |
|
| 401 | + } |
|
| 402 | + |
|
| 403 | + /** |
|
| 404 | + * @inheritDoc |
|
| 405 | + * |
|
| 406 | + * @param string $app id of the app |
|
| 407 | + * @param string $key config key |
|
| 408 | + * @param string $value config value |
|
| 409 | + * @param bool $caseInsensitive non-case-sensitive search, only works if $value is a string |
|
| 410 | + * |
|
| 411 | + * @return Generator<string> |
|
| 412 | + * @since 31.0.0 |
|
| 413 | + */ |
|
| 414 | + public function searchUsersByValueString(string $app, string $key, string $value, bool $caseInsensitive = false): Generator { |
|
| 415 | + return $this->searchUsersByTypedValue($app, $key, $value, $caseInsensitive); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * @inheritDoc |
|
| 420 | + * |
|
| 421 | + * @param string $app id of the app |
|
| 422 | + * @param string $key config key |
|
| 423 | + * @param int $value config value |
|
| 424 | + * |
|
| 425 | + * @return Generator<string> |
|
| 426 | + * @since 31.0.0 |
|
| 427 | + */ |
|
| 428 | + public function searchUsersByValueInt(string $app, string $key, int $value): Generator { |
|
| 429 | + return $this->searchUsersByValueString($app, $key, (string)$value); |
|
| 430 | + } |
|
| 431 | + |
|
| 432 | + /** |
|
| 433 | + * @inheritDoc |
|
| 434 | + * |
|
| 435 | + * @param string $app id of the app |
|
| 436 | + * @param string $key config key |
|
| 437 | + * @param array $values list of config values |
|
| 438 | + * |
|
| 439 | + * @return Generator<string> |
|
| 440 | + * @since 31.0.0 |
|
| 441 | + */ |
|
| 442 | + public function searchUsersByValues(string $app, string $key, array $values): Generator { |
|
| 443 | + return $this->searchUsersByTypedValue($app, $key, $values); |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + /** |
|
| 447 | + * @inheritDoc |
|
| 448 | + * |
|
| 449 | + * @param string $app id of the app |
|
| 450 | + * @param string $key config key |
|
| 451 | + * @param bool $value config value |
|
| 452 | + * |
|
| 453 | + * @return Generator<string> |
|
| 454 | + * @since 31.0.0 |
|
| 455 | + */ |
|
| 456 | + public function searchUsersByValueBool(string $app, string $key, bool $value): Generator { |
|
| 457 | + $values = ['0', 'off', 'false', 'no']; |
|
| 458 | + if ($value) { |
|
| 459 | + $values = ['1', 'on', 'true', 'yes']; |
|
| 460 | + } |
|
| 461 | + return $this->searchUsersByValues($app, $key, $values); |
|
| 462 | + } |
|
| 463 | + |
|
| 464 | + /** |
|
| 465 | + * returns a list of users with config key set to a specific value, or within the list of |
|
| 466 | + * possible values |
|
| 467 | + * |
|
| 468 | + * @param string $app |
|
| 469 | + * @param string $key |
|
| 470 | + * @param string|array $value |
|
| 471 | + * @param bool $caseInsensitive |
|
| 472 | + * |
|
| 473 | + * @return Generator<string> |
|
| 474 | + */ |
|
| 475 | + private function searchUsersByTypedValue(string $app, string $key, string|array $value, bool $caseInsensitive = false): Generator { |
|
| 476 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 477 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 478 | + |
|
| 479 | + $qb = $this->connection->getQueryBuilder(); |
|
| 480 | + $qb->from('preferences'); |
|
| 481 | + $qb->select('userid'); |
|
| 482 | + $qb->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 483 | + $qb->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 484 | + |
|
| 485 | + // search within 'indexed' OR 'configvalue' only if 'flags' is set as not indexed |
|
| 486 | + // TODO: when implementing config lexicon remove the searches on 'configvalue' if value is set as indexed |
|
| 487 | + $configValueColumn = ($this->connection->getDatabaseProvider() === IDBConnection::PLATFORM_ORACLE) ? $qb->expr()->castColumn('configvalue', IQueryBuilder::PARAM_STR) : 'configvalue'; |
|
| 488 | + if (is_array($value)) { |
|
| 489 | + $where = $qb->expr()->orX( |
|
| 490 | + $qb->expr()->in('indexed', $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)), |
|
| 491 | + $qb->expr()->andX( |
|
| 492 | + $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 493 | + $qb->expr()->in($configValueColumn, $qb->createNamedParameter($value, IQueryBuilder::PARAM_STR_ARRAY)) |
|
| 494 | + ) |
|
| 495 | + ); |
|
| 496 | + } else { |
|
| 497 | + if ($caseInsensitive) { |
|
| 498 | + $where = $qb->expr()->orX( |
|
| 499 | + $qb->expr()->eq($qb->func()->lower('indexed'), $qb->createNamedParameter(strtolower($value))), |
|
| 500 | + $qb->expr()->andX( |
|
| 501 | + $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 502 | + $qb->expr()->eq($qb->func()->lower($configValueColumn), $qb->createNamedParameter(strtolower($value))) |
|
| 503 | + ) |
|
| 504 | + ); |
|
| 505 | + } else { |
|
| 506 | + $where = $qb->expr()->orX( |
|
| 507 | + $qb->expr()->eq('indexed', $qb->createNamedParameter($value)), |
|
| 508 | + $qb->expr()->andX( |
|
| 509 | + $qb->expr()->neq($qb->expr()->bitwiseAnd('flags', self::FLAG_INDEXED), $qb->createNamedParameter(self::FLAG_INDEXED, IQueryBuilder::PARAM_INT)), |
|
| 510 | + $qb->expr()->eq($configValueColumn, $qb->createNamedParameter($value)) |
|
| 511 | + ) |
|
| 512 | + ); |
|
| 513 | + } |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + $qb->andWhere($where); |
|
| 517 | + $result = $qb->executeQuery(); |
|
| 518 | + while ($row = $result->fetch()) { |
|
| 519 | + yield $row['userid']; |
|
| 520 | + } |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + /** |
|
| 524 | + * Get the config value as string. |
|
| 525 | + * If the value does not exist the given default will be returned. |
|
| 526 | + * |
|
| 527 | + * Set lazy to `null` to ignore it and get the value from either source. |
|
| 528 | + * |
|
| 529 | + * **WARNING:** Method is internal and **SHOULD** not be used, as it is better to get the value with a type. |
|
| 530 | + * |
|
| 531 | + * @param string $userId id of the user |
|
| 532 | + * @param string $app id of the app |
|
| 533 | + * @param string $key config key |
|
| 534 | + * @param string $default config value |
|
| 535 | + * @param null|bool $lazy get config as lazy loaded or not. can be NULL |
|
| 536 | + * |
|
| 537 | + * @return string the value or $default |
|
| 538 | + * @throws TypeConflictException |
|
| 539 | + * @internal |
|
| 540 | + * @since 31.0.0 |
|
| 541 | + * @see IUserConfig for explanation about lazy loading |
|
| 542 | + * @see getValueString() |
|
| 543 | + * @see getValueInt() |
|
| 544 | + * @see getValueFloat() |
|
| 545 | + * @see getValueBool() |
|
| 546 | + * @see getValueArray() |
|
| 547 | + */ |
|
| 548 | + public function getValueMixed( |
|
| 549 | + string $userId, |
|
| 550 | + string $app, |
|
| 551 | + string $key, |
|
| 552 | + string $default = '', |
|
| 553 | + ?bool $lazy = false, |
|
| 554 | + ): string { |
|
| 555 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 556 | + try { |
|
| 557 | + $lazy ??= $this->isLazy($userId, $app, $key); |
|
| 558 | + } catch (UnknownKeyException) { |
|
| 559 | + return $default; |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + return $this->getTypedValue( |
|
| 563 | + $userId, |
|
| 564 | + $app, |
|
| 565 | + $key, |
|
| 566 | + $default, |
|
| 567 | + $lazy, |
|
| 568 | + ValueType::MIXED |
|
| 569 | + ); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * @inheritDoc |
|
| 574 | + * |
|
| 575 | + * @param string $userId id of the user |
|
| 576 | + * @param string $app id of the app |
|
| 577 | + * @param string $key config key |
|
| 578 | + * @param string $default default value |
|
| 579 | + * @param bool $lazy search within lazy loaded config |
|
| 580 | + * |
|
| 581 | + * @return string stored config value or $default if not set in database |
|
| 582 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 583 | + * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 584 | + * @since 31.0.0 |
|
| 585 | + * @see IUserConfig for explanation about lazy loading |
|
| 586 | + */ |
|
| 587 | + public function getValueString( |
|
| 588 | + string $userId, |
|
| 589 | + string $app, |
|
| 590 | + string $key, |
|
| 591 | + string $default = '', |
|
| 592 | + bool $lazy = false, |
|
| 593 | + ): string { |
|
| 594 | + return $this->getTypedValue($userId, $app, $key, $default, $lazy, ValueType::STRING); |
|
| 595 | + } |
|
| 596 | + |
|
| 597 | + /** |
|
| 598 | + * @inheritDoc |
|
| 599 | + * |
|
| 600 | + * @param string $userId id of the user |
|
| 601 | + * @param string $app id of the app |
|
| 602 | + * @param string $key config key |
|
| 603 | + * @param int $default default value |
|
| 604 | + * @param bool $lazy search within lazy loaded config |
|
| 605 | + * |
|
| 606 | + * @return int stored config value or $default if not set in database |
|
| 607 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 608 | + * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 609 | + * @since 31.0.0 |
|
| 610 | + * @see IUserConfig for explanation about lazy loading |
|
| 611 | + */ |
|
| 612 | + public function getValueInt( |
|
| 613 | + string $userId, |
|
| 614 | + string $app, |
|
| 615 | + string $key, |
|
| 616 | + int $default = 0, |
|
| 617 | + bool $lazy = false, |
|
| 618 | + ): int { |
|
| 619 | + return (int)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::INT); |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + /** |
|
| 623 | + * @inheritDoc |
|
| 624 | + * |
|
| 625 | + * @param string $userId id of the user |
|
| 626 | + * @param string $app id of the app |
|
| 627 | + * @param string $key config key |
|
| 628 | + * @param float $default default value |
|
| 629 | + * @param bool $lazy search within lazy loaded config |
|
| 630 | + * |
|
| 631 | + * @return float stored config value or $default if not set in database |
|
| 632 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 633 | + * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 634 | + * @since 31.0.0 |
|
| 635 | + * @see IUserConfig for explanation about lazy loading |
|
| 636 | + */ |
|
| 637 | + public function getValueFloat( |
|
| 638 | + string $userId, |
|
| 639 | + string $app, |
|
| 640 | + string $key, |
|
| 641 | + float $default = 0, |
|
| 642 | + bool $lazy = false, |
|
| 643 | + ): float { |
|
| 644 | + return (float)$this->getTypedValue($userId, $app, $key, (string)$default, $lazy, ValueType::FLOAT); |
|
| 645 | + } |
|
| 646 | + |
|
| 647 | + /** |
|
| 648 | + * @inheritDoc |
|
| 649 | + * |
|
| 650 | + * @param string $userId id of the user |
|
| 651 | + * @param string $app id of the app |
|
| 652 | + * @param string $key config key |
|
| 653 | + * @param bool $default default value |
|
| 654 | + * @param bool $lazy search within lazy loaded config |
|
| 655 | + * |
|
| 656 | + * @return bool stored config value or $default if not set in database |
|
| 657 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 658 | + * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 659 | + * @since 31.0.0 |
|
| 660 | + * @see IUserConfig for explanation about lazy loading |
|
| 661 | + */ |
|
| 662 | + public function getValueBool( |
|
| 663 | + string $userId, |
|
| 664 | + string $app, |
|
| 665 | + string $key, |
|
| 666 | + bool $default = false, |
|
| 667 | + bool $lazy = false, |
|
| 668 | + ): bool { |
|
| 669 | + $b = strtolower($this->getTypedValue($userId, $app, $key, $default ? 'true' : 'false', $lazy, ValueType::BOOL)); |
|
| 670 | + return in_array($b, ['1', 'true', 'yes', 'on']); |
|
| 671 | + } |
|
| 672 | + |
|
| 673 | + /** |
|
| 674 | + * @inheritDoc |
|
| 675 | + * |
|
| 676 | + * @param string $userId id of the user |
|
| 677 | + * @param string $app id of the app |
|
| 678 | + * @param string $key config key |
|
| 679 | + * @param array $default default value |
|
| 680 | + * @param bool $lazy search within lazy loaded config |
|
| 681 | + * |
|
| 682 | + * @return array stored config value or $default if not set in database |
|
| 683 | + * @throws InvalidArgumentException if one of the argument format is invalid |
|
| 684 | + * @throws TypeConflictException in case of conflict with the value type set in database |
|
| 685 | + * @since 31.0.0 |
|
| 686 | + * @see IUserConfig for explanation about lazy loading |
|
| 687 | + */ |
|
| 688 | + public function getValueArray( |
|
| 689 | + string $userId, |
|
| 690 | + string $app, |
|
| 691 | + string $key, |
|
| 692 | + array $default = [], |
|
| 693 | + bool $lazy = false, |
|
| 694 | + ): array { |
|
| 695 | + try { |
|
| 696 | + $defaultJson = json_encode($default, JSON_THROW_ON_ERROR); |
|
| 697 | + $value = json_decode($this->getTypedValue($userId, $app, $key, $defaultJson, $lazy, ValueType::ARRAY), true, flags: JSON_THROW_ON_ERROR); |
|
| 698 | + |
|
| 699 | + return is_array($value) ? $value : []; |
|
| 700 | + } catch (JsonException) { |
|
| 701 | + return []; |
|
| 702 | + } |
|
| 703 | + } |
|
| 704 | + |
|
| 705 | + /** |
|
| 706 | + * @param string $userId |
|
| 707 | + * @param string $app id of the app |
|
| 708 | + * @param string $key config key |
|
| 709 | + * @param string $default default value |
|
| 710 | + * @param bool $lazy search within lazy loaded config |
|
| 711 | + * @param ValueType $type value type |
|
| 712 | + * |
|
| 713 | + * @return string |
|
| 714 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 715 | + */ |
|
| 716 | + private function getTypedValue( |
|
| 717 | + string $userId, |
|
| 718 | + string $app, |
|
| 719 | + string $key, |
|
| 720 | + string $default, |
|
| 721 | + bool $lazy, |
|
| 722 | + ValueType $type, |
|
| 723 | + ): string { |
|
| 724 | + $this->assertParams($userId, $app, $key); |
|
| 725 | + $origKey = $key; |
|
| 726 | + $matched = $this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, default: $default); |
|
| 727 | + if ($default === null) { |
|
| 728 | + // there is no logical reason for it to be null |
|
| 729 | + throw new \Exception('default cannot be null'); |
|
| 730 | + } |
|
| 731 | + |
|
| 732 | + // returns default if strictness of lexicon is set to WARNING (block and report) |
|
| 733 | + if (!$matched) { |
|
| 734 | + return $default; |
|
| 735 | + } |
|
| 736 | + |
|
| 737 | + $this->loadConfig($userId, $lazy); |
|
| 738 | + |
|
| 739 | + /** |
|
| 740 | + * We ignore check if mixed type is requested. |
|
| 741 | + * If type of stored value is set as mixed, we don't filter. |
|
| 742 | + * If type of stored value is defined, we compare with the one requested. |
|
| 743 | + */ |
|
| 744 | + $knownType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; |
|
| 745 | + if ($type !== ValueType::MIXED |
|
| 746 | + && $knownType !== null |
|
| 747 | + && $knownType !== ValueType::MIXED |
|
| 748 | + && $type !== $knownType) { |
|
| 749 | + $this->logger->warning('conflict with value type from database', ['app' => $app, 'key' => $key, 'type' => $type, 'knownType' => $knownType]); |
|
| 750 | + throw new TypeConflictException('conflict with value type from database'); |
|
| 751 | + } |
|
| 752 | + |
|
| 753 | + /** |
|
| 754 | + * - the pair $app/$key cannot exist in both array, |
|
| 755 | + * - we should still return an existing non-lazy value even if current method |
|
| 756 | + * is called with $lazy is true |
|
| 757 | + * |
|
| 758 | + * This way, lazyCache will be empty until the load for lazy config value is requested. |
|
| 759 | + */ |
|
| 760 | + if (isset($this->lazyCache[$userId][$app][$key])) { |
|
| 761 | + $value = $this->lazyCache[$userId][$app][$key]; |
|
| 762 | + } elseif (isset($this->fastCache[$userId][$app][$key])) { |
|
| 763 | + $value = $this->fastCache[$userId][$app][$key]; |
|
| 764 | + } else { |
|
| 765 | + return $default; |
|
| 766 | + } |
|
| 767 | + |
|
| 768 | + $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 769 | + |
|
| 770 | + // in case the key was modified while running matchAndApplyLexiconDefinition() we are |
|
| 771 | + // interested to check options in case a modification of the value is needed |
|
| 772 | + // ie inverting value from previous key when using lexicon option RENAME_INVERT_BOOLEAN |
|
| 773 | + if ($origKey !== $key && $type === ValueType::BOOL) { |
|
| 774 | + $configManager = Server::get(ConfigManager::class); |
|
| 775 | + $value = ($configManager->convertToBool($value, $this->getLexiconEntry($app, $key))) ? '1' : '0'; |
|
| 776 | + } |
|
| 777 | + |
|
| 778 | + return $value; |
|
| 779 | + } |
|
| 780 | + |
|
| 781 | + /** |
|
| 782 | + * @inheritDoc |
|
| 783 | + * |
|
| 784 | + * @param string $userId id of the user |
|
| 785 | + * @param string $app id of the app |
|
| 786 | + * @param string $key config key |
|
| 787 | + * |
|
| 788 | + * @return ValueType type of the value |
|
| 789 | + * @throws UnknownKeyException if config key is not known |
|
| 790 | + * @throws IncorrectTypeException if config value type is not known |
|
| 791 | + * @since 31.0.0 |
|
| 792 | + */ |
|
| 793 | + public function getValueType(string $userId, string $app, string $key, ?bool $lazy = null): ValueType { |
|
| 794 | + $this->assertParams($userId, $app, $key); |
|
| 795 | + $this->loadConfig($userId, $lazy); |
|
| 796 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 797 | + |
|
| 798 | + if (!isset($this->valueDetails[$userId][$app][$key]['type'])) { |
|
| 799 | + throw new UnknownKeyException('unknown config key'); |
|
| 800 | + } |
|
| 801 | + |
|
| 802 | + return $this->valueDetails[$userId][$app][$key]['type']; |
|
| 803 | + } |
|
| 804 | + |
|
| 805 | + /** |
|
| 806 | + * @inheritDoc |
|
| 807 | + * |
|
| 808 | + * @param string $userId id of the user |
|
| 809 | + * @param string $app id of the app |
|
| 810 | + * @param string $key config key |
|
| 811 | + * @param bool $lazy lazy loading |
|
| 812 | + * |
|
| 813 | + * @return int flags applied to value |
|
| 814 | + * @throws UnknownKeyException if config key is not known |
|
| 815 | + * @throws IncorrectTypeException if config value type is not known |
|
| 816 | + * @since 31.0.0 |
|
| 817 | + */ |
|
| 818 | + public function getValueFlags(string $userId, string $app, string $key, bool $lazy = false): int { |
|
| 819 | + $this->assertParams($userId, $app, $key); |
|
| 820 | + $this->loadConfig($userId, $lazy); |
|
| 821 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 822 | + |
|
| 823 | + if (!isset($this->valueDetails[$userId][$app][$key])) { |
|
| 824 | + throw new UnknownKeyException('unknown config key'); |
|
| 825 | + } |
|
| 826 | + |
|
| 827 | + return $this->valueDetails[$userId][$app][$key]['flags']; |
|
| 828 | + } |
|
| 829 | + |
|
| 830 | + /** |
|
| 831 | + * Store a config key and its value in database as VALUE_MIXED |
|
| 832 | + * |
|
| 833 | + * **WARNING:** Method is internal and **MUST** not be used as it is best to set a real value type |
|
| 834 | + * |
|
| 835 | + * @param string $userId id of the user |
|
| 836 | + * @param string $app id of the app |
|
| 837 | + * @param string $key config key |
|
| 838 | + * @param string $value config value |
|
| 839 | + * @param bool $lazy set config as lazy loaded |
|
| 840 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 841 | + * |
|
| 842 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 843 | + * @throws TypeConflictException if type from database is not VALUE_MIXED |
|
| 844 | + * @internal |
|
| 845 | + * @since 31.0.0 |
|
| 846 | + * @see IUserConfig for explanation about lazy loading |
|
| 847 | + * @see setValueString() |
|
| 848 | + * @see setValueInt() |
|
| 849 | + * @see setValueFloat() |
|
| 850 | + * @see setValueBool() |
|
| 851 | + * @see setValueArray() |
|
| 852 | + */ |
|
| 853 | + public function setValueMixed( |
|
| 854 | + string $userId, |
|
| 855 | + string $app, |
|
| 856 | + string $key, |
|
| 857 | + string $value, |
|
| 858 | + bool $lazy = false, |
|
| 859 | + int $flags = 0, |
|
| 860 | + ): bool { |
|
| 861 | + return $this->setTypedValue( |
|
| 862 | + $userId, |
|
| 863 | + $app, |
|
| 864 | + $key, |
|
| 865 | + $value, |
|
| 866 | + $lazy, |
|
| 867 | + $flags, |
|
| 868 | + ValueType::MIXED |
|
| 869 | + ); |
|
| 870 | + } |
|
| 871 | + |
|
| 872 | + |
|
| 873 | + /** |
|
| 874 | + * @inheritDoc |
|
| 875 | + * |
|
| 876 | + * @param string $userId id of the user |
|
| 877 | + * @param string $app id of the app |
|
| 878 | + * @param string $key config key |
|
| 879 | + * @param string $value config value |
|
| 880 | + * @param bool $lazy set config as lazy loaded |
|
| 881 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 882 | + * |
|
| 883 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 884 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 885 | + * @since 31.0.0 |
|
| 886 | + * @see IUserConfig for explanation about lazy loading |
|
| 887 | + */ |
|
| 888 | + public function setValueString( |
|
| 889 | + string $userId, |
|
| 890 | + string $app, |
|
| 891 | + string $key, |
|
| 892 | + string $value, |
|
| 893 | + bool $lazy = false, |
|
| 894 | + int $flags = 0, |
|
| 895 | + ): bool { |
|
| 896 | + return $this->setTypedValue( |
|
| 897 | + $userId, |
|
| 898 | + $app, |
|
| 899 | + $key, |
|
| 900 | + $value, |
|
| 901 | + $lazy, |
|
| 902 | + $flags, |
|
| 903 | + ValueType::STRING |
|
| 904 | + ); |
|
| 905 | + } |
|
| 906 | + |
|
| 907 | + /** |
|
| 908 | + * @inheritDoc |
|
| 909 | + * |
|
| 910 | + * @param string $userId id of the user |
|
| 911 | + * @param string $app id of the app |
|
| 912 | + * @param string $key config key |
|
| 913 | + * @param int $value config value |
|
| 914 | + * @param bool $lazy set config as lazy loaded |
|
| 915 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 916 | + * |
|
| 917 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 918 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 919 | + * @since 31.0.0 |
|
| 920 | + * @see IUserConfig for explanation about lazy loading |
|
| 921 | + */ |
|
| 922 | + public function setValueInt( |
|
| 923 | + string $userId, |
|
| 924 | + string $app, |
|
| 925 | + string $key, |
|
| 926 | + int $value, |
|
| 927 | + bool $lazy = false, |
|
| 928 | + int $flags = 0, |
|
| 929 | + ): bool { |
|
| 930 | + if ($value > 2000000000) { |
|
| 931 | + $this->logger->debug('You are trying to store an integer value around/above 2,147,483,647. This is a reminder that reaching this theoretical limit on 32 bits system will throw an exception.'); |
|
| 932 | + } |
|
| 933 | + |
|
| 934 | + return $this->setTypedValue( |
|
| 935 | + $userId, |
|
| 936 | + $app, |
|
| 937 | + $key, |
|
| 938 | + (string)$value, |
|
| 939 | + $lazy, |
|
| 940 | + $flags, |
|
| 941 | + ValueType::INT |
|
| 942 | + ); |
|
| 943 | + } |
|
| 944 | + |
|
| 945 | + /** |
|
| 946 | + * @inheritDoc |
|
| 947 | + * |
|
| 948 | + * @param string $userId id of the user |
|
| 949 | + * @param string $app id of the app |
|
| 950 | + * @param string $key config key |
|
| 951 | + * @param float $value config value |
|
| 952 | + * @param bool $lazy set config as lazy loaded |
|
| 953 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 954 | + * |
|
| 955 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 956 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 957 | + * @since 31.0.0 |
|
| 958 | + * @see IUserConfig for explanation about lazy loading |
|
| 959 | + */ |
|
| 960 | + public function setValueFloat( |
|
| 961 | + string $userId, |
|
| 962 | + string $app, |
|
| 963 | + string $key, |
|
| 964 | + float $value, |
|
| 965 | + bool $lazy = false, |
|
| 966 | + int $flags = 0, |
|
| 967 | + ): bool { |
|
| 968 | + return $this->setTypedValue( |
|
| 969 | + $userId, |
|
| 970 | + $app, |
|
| 971 | + $key, |
|
| 972 | + (string)$value, |
|
| 973 | + $lazy, |
|
| 974 | + $flags, |
|
| 975 | + ValueType::FLOAT |
|
| 976 | + ); |
|
| 977 | + } |
|
| 978 | + |
|
| 979 | + /** |
|
| 980 | + * @inheritDoc |
|
| 981 | + * |
|
| 982 | + * @param string $userId id of the user |
|
| 983 | + * @param string $app id of the app |
|
| 984 | + * @param string $key config key |
|
| 985 | + * @param bool $value config value |
|
| 986 | + * @param bool $lazy set config as lazy loaded |
|
| 987 | + * |
|
| 988 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 989 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 990 | + * @since 31.0.0 |
|
| 991 | + * @see IUserConfig for explanation about lazy loading |
|
| 992 | + */ |
|
| 993 | + public function setValueBool( |
|
| 994 | + string $userId, |
|
| 995 | + string $app, |
|
| 996 | + string $key, |
|
| 997 | + bool $value, |
|
| 998 | + bool $lazy = false, |
|
| 999 | + int $flags = 0, |
|
| 1000 | + ): bool { |
|
| 1001 | + return $this->setTypedValue( |
|
| 1002 | + $userId, |
|
| 1003 | + $app, |
|
| 1004 | + $key, |
|
| 1005 | + ($value) ? '1' : '0', |
|
| 1006 | + $lazy, |
|
| 1007 | + $flags, |
|
| 1008 | + ValueType::BOOL |
|
| 1009 | + ); |
|
| 1010 | + } |
|
| 1011 | + |
|
| 1012 | + /** |
|
| 1013 | + * @inheritDoc |
|
| 1014 | + * |
|
| 1015 | + * @param string $userId id of the user |
|
| 1016 | + * @param string $app id of the app |
|
| 1017 | + * @param string $key config key |
|
| 1018 | + * @param array $value config value |
|
| 1019 | + * @param bool $lazy set config as lazy loaded |
|
| 1020 | + * @param bool $sensitive if TRUE value will be hidden when listing config values. |
|
| 1021 | + * |
|
| 1022 | + * @return bool TRUE if value was different, therefor updated in database |
|
| 1023 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 1024 | + * @throws JsonException |
|
| 1025 | + * @since 31.0.0 |
|
| 1026 | + * @see IUserConfig for explanation about lazy loading |
|
| 1027 | + */ |
|
| 1028 | + public function setValueArray( |
|
| 1029 | + string $userId, |
|
| 1030 | + string $app, |
|
| 1031 | + string $key, |
|
| 1032 | + array $value, |
|
| 1033 | + bool $lazy = false, |
|
| 1034 | + int $flags = 0, |
|
| 1035 | + ): bool { |
|
| 1036 | + try { |
|
| 1037 | + return $this->setTypedValue( |
|
| 1038 | + $userId, |
|
| 1039 | + $app, |
|
| 1040 | + $key, |
|
| 1041 | + json_encode($value, JSON_THROW_ON_ERROR), |
|
| 1042 | + $lazy, |
|
| 1043 | + $flags, |
|
| 1044 | + ValueType::ARRAY |
|
| 1045 | + ); |
|
| 1046 | + } catch (JsonException $e) { |
|
| 1047 | + $this->logger->warning('could not setValueArray', ['app' => $app, 'key' => $key, 'exception' => $e]); |
|
| 1048 | + throw $e; |
|
| 1049 | + } |
|
| 1050 | + } |
|
| 1051 | + |
|
| 1052 | + /** |
|
| 1053 | + * Store a config key and its value in database |
|
| 1054 | + * |
|
| 1055 | + * If config key is already known with the exact same config value and same sensitive/lazy status, the |
|
| 1056 | + * database is not updated. If config value was previously stored as sensitive, status will not be |
|
| 1057 | + * altered. |
|
| 1058 | + * |
|
| 1059 | + * @param string $userId id of the user |
|
| 1060 | + * @param string $app id of the app |
|
| 1061 | + * @param string $key config key |
|
| 1062 | + * @param string $value config value |
|
| 1063 | + * @param bool $lazy config set as lazy loaded |
|
| 1064 | + * @param ValueType $type value type |
|
| 1065 | + * |
|
| 1066 | + * @return bool TRUE if value was updated in database |
|
| 1067 | + * @throws TypeConflictException if type from database is not VALUE_MIXED and different from the requested one |
|
| 1068 | + * @see IUserConfig for explanation about lazy loading |
|
| 1069 | + */ |
|
| 1070 | + private function setTypedValue( |
|
| 1071 | + string $userId, |
|
| 1072 | + string $app, |
|
| 1073 | + string $key, |
|
| 1074 | + string $value, |
|
| 1075 | + bool $lazy, |
|
| 1076 | + int $flags, |
|
| 1077 | + ValueType $type, |
|
| 1078 | + ): bool { |
|
| 1079 | + // Primary email addresses are always(!) expected to be lowercase |
|
| 1080 | + if ($app === 'settings' && $key === 'email') { |
|
| 1081 | + $value = strtolower($value); |
|
| 1082 | + } |
|
| 1083 | + |
|
| 1084 | + $this->assertParams($userId, $app, $key); |
|
| 1085 | + if (!$this->matchAndApplyLexiconDefinition($userId, $app, $key, $lazy, $type, $flags)) { |
|
| 1086 | + // returns false as database is not updated |
|
| 1087 | + return false; |
|
| 1088 | + } |
|
| 1089 | + $this->loadConfig($userId, $lazy); |
|
| 1090 | + |
|
| 1091 | + $inserted = $refreshCache = false; |
|
| 1092 | + $origValue = $value; |
|
| 1093 | + $sensitive = $this->isFlagged(self::FLAG_SENSITIVE, $flags); |
|
| 1094 | + if ($sensitive || ($this->hasKey($userId, $app, $key, $lazy) && $this->isSensitive($userId, $app, $key, $lazy))) { |
|
| 1095 | + $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 1096 | + $flags |= self::FLAG_SENSITIVE; |
|
| 1097 | + } |
|
| 1098 | + |
|
| 1099 | + // if requested, we fill the 'indexed' field with current value |
|
| 1100 | + $indexed = ''; |
|
| 1101 | + if ($type !== ValueType::ARRAY && $this->isFlagged(self::FLAG_INDEXED, $flags)) { |
|
| 1102 | + if ($this->isFlagged(self::FLAG_SENSITIVE, $flags)) { |
|
| 1103 | + $this->logger->warning('sensitive value are not to be indexed'); |
|
| 1104 | + } elseif (strlen($value) > self::USER_MAX_LENGTH) { |
|
| 1105 | + $this->logger->warning('value is too lengthy to be indexed'); |
|
| 1106 | + } else { |
|
| 1107 | + $indexed = $value; |
|
| 1108 | + } |
|
| 1109 | + } |
|
| 1110 | + |
|
| 1111 | + if ($this->hasKey($userId, $app, $key, $lazy)) { |
|
| 1112 | + /** |
|
| 1113 | + * no update if key is already known with set lazy status and value is |
|
| 1114 | + * not different, unless sensitivity is switched from false to true. |
|
| 1115 | + */ |
|
| 1116 | + if ($origValue === $this->getTypedValue($userId, $app, $key, $value, $lazy, $type) |
|
| 1117 | + && (!$sensitive || $this->isSensitive($userId, $app, $key, $lazy))) { |
|
| 1118 | + return false; |
|
| 1119 | + } |
|
| 1120 | + } else { |
|
| 1121 | + /** |
|
| 1122 | + * if key is not known yet, we try to insert. |
|
| 1123 | + * It might fail if the key exists with a different lazy flag. |
|
| 1124 | + */ |
|
| 1125 | + try { |
|
| 1126 | + $insert = $this->connection->getQueryBuilder(); |
|
| 1127 | + $insert->insert('preferences') |
|
| 1128 | + ->setValue('userid', $insert->createNamedParameter($userId)) |
|
| 1129 | + ->setValue('appid', $insert->createNamedParameter($app)) |
|
| 1130 | + ->setValue('lazy', $insert->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1131 | + ->setValue('type', $insert->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1132 | + ->setValue('flags', $insert->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1133 | + ->setValue('indexed', $insert->createNamedParameter($indexed)) |
|
| 1134 | + ->setValue('configkey', $insert->createNamedParameter($key)) |
|
| 1135 | + ->setValue('configvalue', $insert->createNamedParameter($value)); |
|
| 1136 | + $insert->executeStatement(); |
|
| 1137 | + $inserted = true; |
|
| 1138 | + } catch (DBException $e) { |
|
| 1139 | + if ($e->getReason() !== DBException::REASON_UNIQUE_CONSTRAINT_VIOLATION) { |
|
| 1140 | + // TODO: throw exception or just log and returns false !? |
|
| 1141 | + throw $e; |
|
| 1142 | + } |
|
| 1143 | + } |
|
| 1144 | + } |
|
| 1145 | + |
|
| 1146 | + /** |
|
| 1147 | + * We cannot insert a new row, meaning we need to update an already existing one |
|
| 1148 | + */ |
|
| 1149 | + if (!$inserted) { |
|
| 1150 | + $currType = $this->valueDetails[$userId][$app][$key]['type'] ?? null; |
|
| 1151 | + if ($currType === null) { // this might happen when switching lazy loading status |
|
| 1152 | + $this->loadConfigAll($userId); |
|
| 1153 | + $currType = $this->valueDetails[$userId][$app][$key]['type']; |
|
| 1154 | + } |
|
| 1155 | + |
|
| 1156 | + /** |
|
| 1157 | + * We only log a warning and set it to VALUE_MIXED. |
|
| 1158 | + */ |
|
| 1159 | + if ($currType === null) { |
|
| 1160 | + $this->logger->warning('Value type is set to zero (0) in database. This is not supposed to happens', ['app' => $app, 'key' => $key]); |
|
| 1161 | + $currType = ValueType::MIXED; |
|
| 1162 | + } |
|
| 1163 | + |
|
| 1164 | + /** |
|
| 1165 | + * we only accept a different type from the one stored in database |
|
| 1166 | + * if the one stored in database is not-defined (VALUE_MIXED) |
|
| 1167 | + */ |
|
| 1168 | + if ($currType !== ValueType::MIXED |
|
| 1169 | + && $currType !== $type) { |
|
| 1170 | + try { |
|
| 1171 | + $currTypeDef = $currType->getDefinition(); |
|
| 1172 | + $typeDef = $type->getDefinition(); |
|
| 1173 | + } catch (IncorrectTypeException) { |
|
| 1174 | + $currTypeDef = $currType->value; |
|
| 1175 | + $typeDef = $type->value; |
|
| 1176 | + } |
|
| 1177 | + throw new TypeConflictException('conflict between new type (' . $typeDef . ') and old type (' . $currTypeDef . ')'); |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + if ($lazy !== $this->isLazy($userId, $app, $key)) { |
|
| 1181 | + $refreshCache = true; |
|
| 1182 | + } |
|
| 1183 | + |
|
| 1184 | + $update = $this->connection->getQueryBuilder(); |
|
| 1185 | + $update->update('preferences') |
|
| 1186 | + ->set('configvalue', $update->createNamedParameter($value)) |
|
| 1187 | + ->set('lazy', $update->createNamedParameter(($lazy) ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1188 | + ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1189 | + ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1190 | + ->set('indexed', $update->createNamedParameter($indexed)) |
|
| 1191 | + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1192 | + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1193 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1194 | + |
|
| 1195 | + $update->executeStatement(); |
|
| 1196 | + } |
|
| 1197 | + |
|
| 1198 | + if ($refreshCache) { |
|
| 1199 | + $this->clearCache($userId); |
|
| 1200 | + return true; |
|
| 1201 | + } |
|
| 1202 | + |
|
| 1203 | + // update local cache |
|
| 1204 | + if ($lazy) { |
|
| 1205 | + $this->lazyCache[$userId][$app][$key] = $value; |
|
| 1206 | + } else { |
|
| 1207 | + $this->fastCache[$userId][$app][$key] = $value; |
|
| 1208 | + } |
|
| 1209 | + $this->valueDetails[$userId][$app][$key] = [ |
|
| 1210 | + 'type' => $type, |
|
| 1211 | + 'flags' => $flags |
|
| 1212 | + ]; |
|
| 1213 | + |
|
| 1214 | + return true; |
|
| 1215 | + } |
|
| 1216 | + |
|
| 1217 | + /** |
|
| 1218 | + * Change the type of config value. |
|
| 1219 | + * |
|
| 1220 | + * **WARNING:** Method is internal and **MUST** not be used as it may break things. |
|
| 1221 | + * |
|
| 1222 | + * @param string $userId id of the user |
|
| 1223 | + * @param string $app id of the app |
|
| 1224 | + * @param string $key config key |
|
| 1225 | + * @param ValueType $type value type |
|
| 1226 | + * |
|
| 1227 | + * @return bool TRUE if database update were necessary |
|
| 1228 | + * @throws UnknownKeyException if $key is now known in database |
|
| 1229 | + * @throws IncorrectTypeException if $type is not valid |
|
| 1230 | + * @internal |
|
| 1231 | + * @since 31.0.0 |
|
| 1232 | + */ |
|
| 1233 | + public function updateType(string $userId, string $app, string $key, ValueType $type = ValueType::MIXED): bool { |
|
| 1234 | + $this->assertParams($userId, $app, $key); |
|
| 1235 | + $this->loadConfigAll($userId); |
|
| 1236 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1237 | + $this->isLazy($userId, $app, $key); // confirm key exists |
|
| 1238 | + |
|
| 1239 | + $update = $this->connection->getQueryBuilder(); |
|
| 1240 | + $update->update('preferences') |
|
| 1241 | + ->set('type', $update->createNamedParameter($type->value, IQueryBuilder::PARAM_INT)) |
|
| 1242 | + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1243 | + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1244 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1245 | + $update->executeStatement(); |
|
| 1246 | + |
|
| 1247 | + $this->valueDetails[$userId][$app][$key]['type'] = $type; |
|
| 1248 | + |
|
| 1249 | + return true; |
|
| 1250 | + } |
|
| 1251 | + |
|
| 1252 | + /** |
|
| 1253 | + * @inheritDoc |
|
| 1254 | + * |
|
| 1255 | + * @param string $userId id of the user |
|
| 1256 | + * @param string $app id of the app |
|
| 1257 | + * @param string $key config key |
|
| 1258 | + * @param bool $sensitive TRUE to set as sensitive, FALSE to unset |
|
| 1259 | + * |
|
| 1260 | + * @return bool TRUE if entry was found in database and an update was necessary |
|
| 1261 | + * @since 31.0.0 |
|
| 1262 | + */ |
|
| 1263 | + public function updateSensitive(string $userId, string $app, string $key, bool $sensitive): bool { |
|
| 1264 | + $this->assertParams($userId, $app, $key); |
|
| 1265 | + $this->loadConfigAll($userId); |
|
| 1266 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1267 | + |
|
| 1268 | + try { |
|
| 1269 | + if ($sensitive === $this->isSensitive($userId, $app, $key, null)) { |
|
| 1270 | + return false; |
|
| 1271 | + } |
|
| 1272 | + } catch (UnknownKeyException) { |
|
| 1273 | + return false; |
|
| 1274 | + } |
|
| 1275 | + |
|
| 1276 | + $lazy = $this->isLazy($userId, $app, $key); |
|
| 1277 | + if ($lazy) { |
|
| 1278 | + $cache = $this->lazyCache; |
|
| 1279 | + } else { |
|
| 1280 | + $cache = $this->fastCache; |
|
| 1281 | + } |
|
| 1282 | + |
|
| 1283 | + if (!isset($cache[$userId][$app][$key])) { |
|
| 1284 | + throw new UnknownKeyException('unknown config key'); |
|
| 1285 | + } |
|
| 1286 | + |
|
| 1287 | + $value = $cache[$userId][$app][$key]; |
|
| 1288 | + $flags = $this->getValueFlags($userId, $app, $key); |
|
| 1289 | + if ($sensitive) { |
|
| 1290 | + $flags |= self::FLAG_SENSITIVE; |
|
| 1291 | + $value = self::ENCRYPTION_PREFIX . $this->crypto->encrypt($value); |
|
| 1292 | + } else { |
|
| 1293 | + $flags &= ~self::FLAG_SENSITIVE; |
|
| 1294 | + $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 1295 | + } |
|
| 1296 | + |
|
| 1297 | + $update = $this->connection->getQueryBuilder(); |
|
| 1298 | + $update->update('preferences') |
|
| 1299 | + ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1300 | + ->set('configvalue', $update->createNamedParameter($value)) |
|
| 1301 | + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1302 | + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1303 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1304 | + $update->executeStatement(); |
|
| 1305 | + |
|
| 1306 | + $this->valueDetails[$userId][$app][$key]['flags'] = $flags; |
|
| 1307 | + |
|
| 1308 | + return true; |
|
| 1309 | + } |
|
| 1310 | + |
|
| 1311 | + /** |
|
| 1312 | + * @inheritDoc |
|
| 1313 | + * |
|
| 1314 | + * @param string $app |
|
| 1315 | + * @param string $key |
|
| 1316 | + * @param bool $sensitive |
|
| 1317 | + * |
|
| 1318 | + * @since 31.0.0 |
|
| 1319 | + */ |
|
| 1320 | + public function updateGlobalSensitive(string $app, string $key, bool $sensitive): void { |
|
| 1321 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1322 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1323 | + |
|
| 1324 | + foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) { |
|
| 1325 | + try { |
|
| 1326 | + $this->updateSensitive($userId, $app, $key, $sensitive); |
|
| 1327 | + } catch (UnknownKeyException) { |
|
| 1328 | + // should not happen and can be ignored |
|
| 1329 | + } |
|
| 1330 | + } |
|
| 1331 | + |
|
| 1332 | + // we clear all cache |
|
| 1333 | + $this->clearCacheAll(); |
|
| 1334 | + } |
|
| 1335 | + |
|
| 1336 | + /** |
|
| 1337 | + * @inheritDoc |
|
| 1338 | + * |
|
| 1339 | + * @param string $userId |
|
| 1340 | + * @param string $app |
|
| 1341 | + * @param string $key |
|
| 1342 | + * @param bool $indexed |
|
| 1343 | + * |
|
| 1344 | + * @return bool |
|
| 1345 | + * @throws DBException |
|
| 1346 | + * @throws IncorrectTypeException |
|
| 1347 | + * @throws UnknownKeyException |
|
| 1348 | + * @since 31.0.0 |
|
| 1349 | + */ |
|
| 1350 | + public function updateIndexed(string $userId, string $app, string $key, bool $indexed): bool { |
|
| 1351 | + $this->assertParams($userId, $app, $key); |
|
| 1352 | + $this->loadConfigAll($userId); |
|
| 1353 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1354 | + |
|
| 1355 | + try { |
|
| 1356 | + if ($indexed === $this->isIndexed($userId, $app, $key, null)) { |
|
| 1357 | + return false; |
|
| 1358 | + } |
|
| 1359 | + } catch (UnknownKeyException) { |
|
| 1360 | + return false; |
|
| 1361 | + } |
|
| 1362 | + |
|
| 1363 | + $lazy = $this->isLazy($userId, $app, $key); |
|
| 1364 | + if ($lazy) { |
|
| 1365 | + $cache = $this->lazyCache; |
|
| 1366 | + } else { |
|
| 1367 | + $cache = $this->fastCache; |
|
| 1368 | + } |
|
| 1369 | + |
|
| 1370 | + if (!isset($cache[$userId][$app][$key])) { |
|
| 1371 | + throw new UnknownKeyException('unknown config key'); |
|
| 1372 | + } |
|
| 1373 | + |
|
| 1374 | + $value = $cache[$userId][$app][$key]; |
|
| 1375 | + $flags = $this->getValueFlags($userId, $app, $key); |
|
| 1376 | + if ($indexed) { |
|
| 1377 | + $indexed = $value; |
|
| 1378 | + } else { |
|
| 1379 | + $flags &= ~self::FLAG_INDEXED; |
|
| 1380 | + $indexed = ''; |
|
| 1381 | + } |
|
| 1382 | + |
|
| 1383 | + $update = $this->connection->getQueryBuilder(); |
|
| 1384 | + $update->update('preferences') |
|
| 1385 | + ->set('flags', $update->createNamedParameter($flags, IQueryBuilder::PARAM_INT)) |
|
| 1386 | + ->set('indexed', $update->createNamedParameter($indexed)) |
|
| 1387 | + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1388 | + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1389 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1390 | + $update->executeStatement(); |
|
| 1391 | + |
|
| 1392 | + $this->valueDetails[$userId][$app][$key]['flags'] = $flags; |
|
| 1393 | + |
|
| 1394 | + return true; |
|
| 1395 | + } |
|
| 1396 | + |
|
| 1397 | + |
|
| 1398 | + /** |
|
| 1399 | + * @inheritDoc |
|
| 1400 | + * |
|
| 1401 | + * @param string $app |
|
| 1402 | + * @param string $key |
|
| 1403 | + * @param bool $indexed |
|
| 1404 | + * |
|
| 1405 | + * @since 31.0.0 |
|
| 1406 | + */ |
|
| 1407 | + public function updateGlobalIndexed(string $app, string $key, bool $indexed): void { |
|
| 1408 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1409 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1410 | + |
|
| 1411 | + foreach (array_keys($this->getValuesByUsers($app, $key)) as $userId) { |
|
| 1412 | + try { |
|
| 1413 | + $this->updateIndexed($userId, $app, $key, $indexed); |
|
| 1414 | + } catch (UnknownKeyException) { |
|
| 1415 | + // should not happen and can be ignored |
|
| 1416 | + } |
|
| 1417 | + } |
|
| 1418 | + |
|
| 1419 | + // we clear all cache |
|
| 1420 | + $this->clearCacheAll(); |
|
| 1421 | + } |
|
| 1422 | + |
|
| 1423 | + /** |
|
| 1424 | + * @inheritDoc |
|
| 1425 | + * |
|
| 1426 | + * @param string $userId id of the user |
|
| 1427 | + * @param string $app id of the app |
|
| 1428 | + * @param string $key config key |
|
| 1429 | + * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 1430 | + * |
|
| 1431 | + * @return bool TRUE if entry was found in database and an update was necessary |
|
| 1432 | + * @since 31.0.0 |
|
| 1433 | + */ |
|
| 1434 | + public function updateLazy(string $userId, string $app, string $key, bool $lazy): bool { |
|
| 1435 | + $this->assertParams($userId, $app, $key); |
|
| 1436 | + $this->loadConfigAll($userId); |
|
| 1437 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1438 | + |
|
| 1439 | + try { |
|
| 1440 | + if ($lazy === $this->isLazy($userId, $app, $key)) { |
|
| 1441 | + return false; |
|
| 1442 | + } |
|
| 1443 | + } catch (UnknownKeyException) { |
|
| 1444 | + return false; |
|
| 1445 | + } |
|
| 1446 | + |
|
| 1447 | + $update = $this->connection->getQueryBuilder(); |
|
| 1448 | + $update->update('preferences') |
|
| 1449 | + ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1450 | + ->where($update->expr()->eq('userid', $update->createNamedParameter($userId))) |
|
| 1451 | + ->andWhere($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1452 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1453 | + $update->executeStatement(); |
|
| 1454 | + |
|
| 1455 | + // At this point, it is a lot safer to clean cache |
|
| 1456 | + $this->clearCache($userId); |
|
| 1457 | + |
|
| 1458 | + return true; |
|
| 1459 | + } |
|
| 1460 | + |
|
| 1461 | + /** |
|
| 1462 | + * @inheritDoc |
|
| 1463 | + * |
|
| 1464 | + * @param string $app id of the app |
|
| 1465 | + * @param string $key config key |
|
| 1466 | + * @param bool $lazy TRUE to set as lazy loaded, FALSE to unset |
|
| 1467 | + * |
|
| 1468 | + * @since 31.0.0 |
|
| 1469 | + */ |
|
| 1470 | + public function updateGlobalLazy(string $app, string $key, bool $lazy): void { |
|
| 1471 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1472 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1473 | + |
|
| 1474 | + $update = $this->connection->getQueryBuilder(); |
|
| 1475 | + $update->update('preferences') |
|
| 1476 | + ->set('lazy', $update->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT)) |
|
| 1477 | + ->where($update->expr()->eq('appid', $update->createNamedParameter($app))) |
|
| 1478 | + ->andWhere($update->expr()->eq('configkey', $update->createNamedParameter($key))); |
|
| 1479 | + $update->executeStatement(); |
|
| 1480 | + |
|
| 1481 | + $this->clearCacheAll(); |
|
| 1482 | + } |
|
| 1483 | + |
|
| 1484 | + /** |
|
| 1485 | + * @inheritDoc |
|
| 1486 | + * |
|
| 1487 | + * @param string $userId id of the user |
|
| 1488 | + * @param string $app id of the app |
|
| 1489 | + * @param string $key config key |
|
| 1490 | + * |
|
| 1491 | + * @return array |
|
| 1492 | + * @throws UnknownKeyException if config key is not known in database |
|
| 1493 | + * @since 31.0.0 |
|
| 1494 | + */ |
|
| 1495 | + public function getDetails(string $userId, string $app, string $key): array { |
|
| 1496 | + $this->assertParams($userId, $app, $key); |
|
| 1497 | + $this->loadConfigAll($userId); |
|
| 1498 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1499 | + |
|
| 1500 | + $lazy = $this->isLazy($userId, $app, $key); |
|
| 1501 | + |
|
| 1502 | + if ($lazy) { |
|
| 1503 | + $cache = $this->lazyCache[$userId]; |
|
| 1504 | + } else { |
|
| 1505 | + $cache = $this->fastCache[$userId]; |
|
| 1506 | + } |
|
| 1507 | + |
|
| 1508 | + $type = $this->getValueType($userId, $app, $key); |
|
| 1509 | + try { |
|
| 1510 | + $typeString = $type->getDefinition(); |
|
| 1511 | + } catch (IncorrectTypeException $e) { |
|
| 1512 | + $this->logger->warning('type stored in database is not correct', ['exception' => $e, 'type' => $type]); |
|
| 1513 | + $typeString = (string)$type->value; |
|
| 1514 | + } |
|
| 1515 | + |
|
| 1516 | + if (!isset($cache[$app][$key])) { |
|
| 1517 | + throw new UnknownKeyException('unknown config key'); |
|
| 1518 | + } |
|
| 1519 | + |
|
| 1520 | + $value = $cache[$app][$key]; |
|
| 1521 | + $sensitive = $this->isSensitive($userId, $app, $key, null); |
|
| 1522 | + $this->decryptSensitiveValue($userId, $app, $key, $value); |
|
| 1523 | + |
|
| 1524 | + return [ |
|
| 1525 | + 'userId' => $userId, |
|
| 1526 | + 'app' => $app, |
|
| 1527 | + 'key' => $key, |
|
| 1528 | + 'value' => $value, |
|
| 1529 | + 'type' => $type->value, |
|
| 1530 | + 'lazy' => $lazy, |
|
| 1531 | + 'typeString' => $typeString, |
|
| 1532 | + 'sensitive' => $sensitive |
|
| 1533 | + ]; |
|
| 1534 | + } |
|
| 1535 | + |
|
| 1536 | + /** |
|
| 1537 | + * @inheritDoc |
|
| 1538 | + * |
|
| 1539 | + * @param string $userId id of the user |
|
| 1540 | + * @param string $app id of the app |
|
| 1541 | + * @param string $key config key |
|
| 1542 | + * |
|
| 1543 | + * @since 31.0.0 |
|
| 1544 | + */ |
|
| 1545 | + public function deleteUserConfig(string $userId, string $app, string $key): void { |
|
| 1546 | + $this->assertParams($userId, $app, $key); |
|
| 1547 | + $this->matchAndApplyLexiconDefinition($userId, $app, $key); |
|
| 1548 | + |
|
| 1549 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1550 | + $qb->delete('preferences') |
|
| 1551 | + ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))) |
|
| 1552 | + ->andWhere($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1553 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1554 | + $qb->executeStatement(); |
|
| 1555 | + |
|
| 1556 | + unset($this->lazyCache[$userId][$app][$key]); |
|
| 1557 | + unset($this->fastCache[$userId][$app][$key]); |
|
| 1558 | + unset($this->valueDetails[$userId][$app][$key]); |
|
| 1559 | + } |
|
| 1560 | + |
|
| 1561 | + /** |
|
| 1562 | + * @inheritDoc |
|
| 1563 | + * |
|
| 1564 | + * @param string $app id of the app |
|
| 1565 | + * @param string $key config key |
|
| 1566 | + * |
|
| 1567 | + * @since 31.0.0 |
|
| 1568 | + */ |
|
| 1569 | + public function deleteKey(string $app, string $key): void { |
|
| 1570 | + $this->assertParams('', $app, $key, allowEmptyUser: true); |
|
| 1571 | + $this->matchAndApplyLexiconDefinition('', $app, $key); |
|
| 1572 | + |
|
| 1573 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1574 | + $qb->delete('preferences') |
|
| 1575 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))) |
|
| 1576 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))); |
|
| 1577 | + $qb->executeStatement(); |
|
| 1578 | + |
|
| 1579 | + $this->clearCacheAll(); |
|
| 1580 | + } |
|
| 1581 | + |
|
| 1582 | + /** |
|
| 1583 | + * @inheritDoc |
|
| 1584 | + * |
|
| 1585 | + * @param string $app id of the app |
|
| 1586 | + * |
|
| 1587 | + * @since 31.0.0 |
|
| 1588 | + */ |
|
| 1589 | + public function deleteApp(string $app): void { |
|
| 1590 | + $this->assertParams('', $app, allowEmptyUser: true); |
|
| 1591 | + |
|
| 1592 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1593 | + $qb->delete('preferences') |
|
| 1594 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter($app))); |
|
| 1595 | + $qb->executeStatement(); |
|
| 1596 | + |
|
| 1597 | + $this->clearCacheAll(); |
|
| 1598 | + } |
|
| 1599 | + |
|
| 1600 | + public function deleteAllUserConfig(string $userId): void { |
|
| 1601 | + $this->assertParams($userId, '', allowEmptyApp: true); |
|
| 1602 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1603 | + $qb->delete('preferences') |
|
| 1604 | + ->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); |
|
| 1605 | + $qb->executeStatement(); |
|
| 1606 | + |
|
| 1607 | + $this->clearCache($userId); |
|
| 1608 | + } |
|
| 1609 | + |
|
| 1610 | + /** |
|
| 1611 | + * @inheritDoc |
|
| 1612 | + * |
|
| 1613 | + * @param string $userId id of the user |
|
| 1614 | + * @param bool $reload set to TRUE to refill cache instantly after clearing it. |
|
| 1615 | + * |
|
| 1616 | + * @since 31.0.0 |
|
| 1617 | + */ |
|
| 1618 | + public function clearCache(string $userId, bool $reload = false): void { |
|
| 1619 | + $this->assertParams($userId, allowEmptyApp: true); |
|
| 1620 | + $this->lazyLoaded[$userId] = $this->fastLoaded[$userId] = false; |
|
| 1621 | + $this->lazyCache[$userId] = $this->fastCache[$userId] = $this->valueDetails[$userId] = []; |
|
| 1622 | + |
|
| 1623 | + if (!$reload) { |
|
| 1624 | + return; |
|
| 1625 | + } |
|
| 1626 | + |
|
| 1627 | + $this->loadConfigAll($userId); |
|
| 1628 | + } |
|
| 1629 | + |
|
| 1630 | + /** |
|
| 1631 | + * @inheritDoc |
|
| 1632 | + * |
|
| 1633 | + * @since 31.0.0 |
|
| 1634 | + */ |
|
| 1635 | + public function clearCacheAll(): void { |
|
| 1636 | + $this->lazyLoaded = $this->fastLoaded = []; |
|
| 1637 | + $this->lazyCache = $this->fastCache = $this->valueDetails = $this->configLexiconDetails = []; |
|
| 1638 | + $this->configLexiconPreset = null; |
|
| 1639 | + } |
|
| 1640 | + |
|
| 1641 | + /** |
|
| 1642 | + * For debug purpose. |
|
| 1643 | + * Returns the cached data. |
|
| 1644 | + * |
|
| 1645 | + * @return array |
|
| 1646 | + * @since 31.0.0 |
|
| 1647 | + * @internal |
|
| 1648 | + */ |
|
| 1649 | + public function statusCache(): array { |
|
| 1650 | + return [ |
|
| 1651 | + 'fastLoaded' => $this->fastLoaded, |
|
| 1652 | + 'fastCache' => $this->fastCache, |
|
| 1653 | + 'lazyLoaded' => $this->lazyLoaded, |
|
| 1654 | + 'lazyCache' => $this->lazyCache, |
|
| 1655 | + 'valueDetails' => $this->valueDetails, |
|
| 1656 | + ]; |
|
| 1657 | + } |
|
| 1658 | + |
|
| 1659 | + /** |
|
| 1660 | + * @param int $needle bitflag to search |
|
| 1661 | + * @param int $flags all flags |
|
| 1662 | + * |
|
| 1663 | + * @return bool TRUE if bitflag $needle is set in $flags |
|
| 1664 | + */ |
|
| 1665 | + private function isFlagged(int $needle, int $flags): bool { |
|
| 1666 | + return (($needle & $flags) !== 0); |
|
| 1667 | + } |
|
| 1668 | + |
|
| 1669 | + /** |
|
| 1670 | + * Confirm the string set for app and key fit the database description |
|
| 1671 | + * |
|
| 1672 | + * @param string $userId |
|
| 1673 | + * @param string $app assert $app fit in database |
|
| 1674 | + * @param string $prefKey assert config key fit in database |
|
| 1675 | + * @param bool $allowEmptyUser |
|
| 1676 | + * @param bool $allowEmptyApp $app can be empty string |
|
| 1677 | + * @param ValueType|null $valueType assert value type is only one type |
|
| 1678 | + */ |
|
| 1679 | + private function assertParams( |
|
| 1680 | + string $userId = '', |
|
| 1681 | + string $app = '', |
|
| 1682 | + string $prefKey = '', |
|
| 1683 | + bool $allowEmptyUser = false, |
|
| 1684 | + bool $allowEmptyApp = false, |
|
| 1685 | + ): void { |
|
| 1686 | + if (!$allowEmptyUser && $userId === '') { |
|
| 1687 | + throw new InvalidArgumentException('userId cannot be an empty string'); |
|
| 1688 | + } |
|
| 1689 | + if (!$allowEmptyApp && $app === '') { |
|
| 1690 | + throw new InvalidArgumentException('app cannot be an empty string'); |
|
| 1691 | + } |
|
| 1692 | + if (strlen($userId) > self::USER_MAX_LENGTH) { |
|
| 1693 | + throw new InvalidArgumentException('Value (' . $userId . ') for userId is too long (' . self::USER_MAX_LENGTH . ')'); |
|
| 1694 | + } |
|
| 1695 | + if (strlen($app) > self::APP_MAX_LENGTH) { |
|
| 1696 | + throw new InvalidArgumentException('Value (' . $app . ') for app is too long (' . self::APP_MAX_LENGTH . ')'); |
|
| 1697 | + } |
|
| 1698 | + if (strlen($prefKey) > self::KEY_MAX_LENGTH) { |
|
| 1699 | + throw new InvalidArgumentException('Value (' . $prefKey . ') for key is too long (' . self::KEY_MAX_LENGTH . ')'); |
|
| 1700 | + } |
|
| 1701 | + } |
|
| 1702 | + |
|
| 1703 | + private function loadConfigAll(string $userId): void { |
|
| 1704 | + $this->loadConfig($userId, null); |
|
| 1705 | + } |
|
| 1706 | + |
|
| 1707 | + /** |
|
| 1708 | + * Load normal config or config set as lazy loaded |
|
| 1709 | + * |
|
| 1710 | + * @param bool|null $lazy set to TRUE to load config set as lazy loaded, set to NULL to load all config |
|
| 1711 | + */ |
|
| 1712 | + private function loadConfig(string $userId, ?bool $lazy = false): void { |
|
| 1713 | + if ($this->isLoaded($userId, $lazy)) { |
|
| 1714 | + return; |
|
| 1715 | + } |
|
| 1716 | + |
|
| 1717 | + if (($lazy ?? true) !== false) { // if lazy is null or true, we debug log |
|
| 1718 | + $this->logger->debug('The loading of lazy UserConfig values have been requested', ['exception' => new \RuntimeException('ignorable exception')]); |
|
| 1719 | + } |
|
| 1720 | + |
|
| 1721 | + $qb = $this->connection->getQueryBuilder(); |
|
| 1722 | + $qb->from('preferences'); |
|
| 1723 | + $qb->select('appid', 'configkey', 'configvalue', 'type', 'flags'); |
|
| 1724 | + $qb->where($qb->expr()->eq('userid', $qb->createNamedParameter($userId))); |
|
| 1725 | + |
|
| 1726 | + // we only need value from lazy when loadConfig does not specify it |
|
| 1727 | + if ($lazy !== null) { |
|
| 1728 | + $qb->andWhere($qb->expr()->eq('lazy', $qb->createNamedParameter($lazy ? 1 : 0, IQueryBuilder::PARAM_INT))); |
|
| 1729 | + } else { |
|
| 1730 | + $qb->addSelect('lazy'); |
|
| 1731 | + } |
|
| 1732 | + |
|
| 1733 | + $result = $qb->executeQuery(); |
|
| 1734 | + $rows = $result->fetchAll(); |
|
| 1735 | + foreach ($rows as $row) { |
|
| 1736 | + if (($row['lazy'] ?? ($lazy ?? 0) ? 1 : 0) === 1) { |
|
| 1737 | + $this->lazyCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1738 | + } else { |
|
| 1739 | + $this->fastCache[$userId][$row['appid']][$row['configkey']] = $row['configvalue'] ?? ''; |
|
| 1740 | + } |
|
| 1741 | + $this->valueDetails[$userId][$row['appid']][$row['configkey']] = ['type' => ValueType::from((int)($row['type'] ?? 0)), 'flags' => (int)$row['flags']]; |
|
| 1742 | + } |
|
| 1743 | + $result->closeCursor(); |
|
| 1744 | + $this->setAsLoaded($userId, $lazy); |
|
| 1745 | + } |
|
| 1746 | + |
|
| 1747 | + /** |
|
| 1748 | + * if $lazy is: |
|
| 1749 | + * - false: will returns true if fast config are loaded |
|
| 1750 | + * - true : will returns true if lazy config are loaded |
|
| 1751 | + * - null : will returns true if both config are loaded |
|
| 1752 | + * |
|
| 1753 | + * @param string $userId |
|
| 1754 | + * @param bool $lazy |
|
| 1755 | + * |
|
| 1756 | + * @return bool |
|
| 1757 | + */ |
|
| 1758 | + private function isLoaded(string $userId, ?bool $lazy): bool { |
|
| 1759 | + if ($lazy === null) { |
|
| 1760 | + return ($this->lazyLoaded[$userId] ?? false) && ($this->fastLoaded[$userId] ?? false); |
|
| 1761 | + } |
|
| 1762 | + |
|
| 1763 | + return $lazy ? $this->lazyLoaded[$userId] ?? false : $this->fastLoaded[$userId] ?? false; |
|
| 1764 | + } |
|
| 1765 | + |
|
| 1766 | + /** |
|
| 1767 | + * if $lazy is: |
|
| 1768 | + * - false: set fast config as loaded |
|
| 1769 | + * - true : set lazy config as loaded |
|
| 1770 | + * - null : set both config as loaded |
|
| 1771 | + * |
|
| 1772 | + * @param string $userId |
|
| 1773 | + * @param bool $lazy |
|
| 1774 | + */ |
|
| 1775 | + private function setAsLoaded(string $userId, ?bool $lazy): void { |
|
| 1776 | + if ($lazy === null) { |
|
| 1777 | + $this->fastLoaded[$userId] = $this->lazyLoaded[$userId] = true; |
|
| 1778 | + return; |
|
| 1779 | + } |
|
| 1780 | + |
|
| 1781 | + // We also create empty entry to keep both fastLoaded/lazyLoaded synced |
|
| 1782 | + if ($lazy) { |
|
| 1783 | + $this->lazyLoaded[$userId] = true; |
|
| 1784 | + $this->fastLoaded[$userId] = $this->fastLoaded[$userId] ?? false; |
|
| 1785 | + $this->fastCache[$userId] = $this->fastCache[$userId] ?? []; |
|
| 1786 | + } else { |
|
| 1787 | + $this->fastLoaded[$userId] = true; |
|
| 1788 | + $this->lazyLoaded[$userId] = $this->lazyLoaded[$userId] ?? false; |
|
| 1789 | + $this->lazyCache[$userId] = $this->lazyCache[$userId] ?? []; |
|
| 1790 | + } |
|
| 1791 | + } |
|
| 1792 | + |
|
| 1793 | + /** |
|
| 1794 | + * **Warning:** this will load all lazy values from the database |
|
| 1795 | + * |
|
| 1796 | + * @param string $userId id of the user |
|
| 1797 | + * @param string $app id of the app |
|
| 1798 | + * @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} |
|
| 1799 | + * |
|
| 1800 | + * @return array<string, string|int|float|bool|array> |
|
| 1801 | + */ |
|
| 1802 | + private function formatAppValues(string $userId, string $app, array $values, bool $filtered = false): array { |
|
| 1803 | + foreach ($values as $key => $value) { |
|
| 1804 | + //$key = (string)$key; |
|
| 1805 | + try { |
|
| 1806 | + $type = $this->getValueType($userId, $app, (string)$key); |
|
| 1807 | + } catch (UnknownKeyException) { |
|
| 1808 | + continue; |
|
| 1809 | + } |
|
| 1810 | + |
|
| 1811 | + if ($this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) { |
|
| 1812 | + if ($filtered) { |
|
| 1813 | + $value = IConfig::SENSITIVE_VALUE; |
|
| 1814 | + $type = ValueType::STRING; |
|
| 1815 | + } else { |
|
| 1816 | + $this->decryptSensitiveValue($userId, $app, (string)$key, $value); |
|
| 1817 | + } |
|
| 1818 | + } |
|
| 1819 | + |
|
| 1820 | + $values[$key] = $this->convertTypedValue($value, $type); |
|
| 1821 | + } |
|
| 1822 | + |
|
| 1823 | + return $values; |
|
| 1824 | + } |
|
| 1825 | + |
|
| 1826 | + /** |
|
| 1827 | + * convert string value to the expected type |
|
| 1828 | + * |
|
| 1829 | + * @param string $value |
|
| 1830 | + * @param ValueType $type |
|
| 1831 | + * |
|
| 1832 | + * @return string|int|float|bool|array |
|
| 1833 | + */ |
|
| 1834 | + private function convertTypedValue(string $value, ValueType $type): string|int|float|bool|array { |
|
| 1835 | + switch ($type) { |
|
| 1836 | + case ValueType::INT: |
|
| 1837 | + return (int)$value; |
|
| 1838 | + case ValueType::FLOAT: |
|
| 1839 | + return (float)$value; |
|
| 1840 | + case ValueType::BOOL: |
|
| 1841 | + return in_array(strtolower($value), ['1', 'true', 'yes', 'on']); |
|
| 1842 | + case ValueType::ARRAY: |
|
| 1843 | + try { |
|
| 1844 | + return json_decode($value, true, flags: JSON_THROW_ON_ERROR); |
|
| 1845 | + } catch (JsonException) { |
|
| 1846 | + // ignoreable |
|
| 1847 | + } |
|
| 1848 | + break; |
|
| 1849 | + } |
|
| 1850 | + return $value; |
|
| 1851 | + } |
|
| 1852 | + |
|
| 1853 | + |
|
| 1854 | + /** |
|
| 1855 | + * will change referenced $value with the decrypted value in case of encrypted (sensitive value) |
|
| 1856 | + * |
|
| 1857 | + * @param string $userId |
|
| 1858 | + * @param string $app |
|
| 1859 | + * @param string $key |
|
| 1860 | + * @param string $value |
|
| 1861 | + */ |
|
| 1862 | + private function decryptSensitiveValue(string $userId, string $app, string $key, string &$value): void { |
|
| 1863 | + if (!$this->isFlagged(self::FLAG_SENSITIVE, $this->valueDetails[$userId][$app][$key]['flags'] ?? 0)) { |
|
| 1864 | + return; |
|
| 1865 | + } |
|
| 1866 | + |
|
| 1867 | + if (!str_starts_with($value, self::ENCRYPTION_PREFIX)) { |
|
| 1868 | + return; |
|
| 1869 | + } |
|
| 1870 | + |
|
| 1871 | + try { |
|
| 1872 | + $value = $this->crypto->decrypt(substr($value, self::ENCRYPTION_PREFIX_LENGTH)); |
|
| 1873 | + } catch (\Exception $e) { |
|
| 1874 | + $this->logger->warning('could not decrypt sensitive value', [ |
|
| 1875 | + 'userId' => $userId, |
|
| 1876 | + 'app' => $app, |
|
| 1877 | + 'key' => $key, |
|
| 1878 | + 'value' => $value, |
|
| 1879 | + 'exception' => $e |
|
| 1880 | + ]); |
|
| 1881 | + } |
|
| 1882 | + } |
|
| 1883 | + |
|
| 1884 | + /** |
|
| 1885 | + * Match and apply current use of config values with defined lexicon. |
|
| 1886 | + * Set $lazy to NULL only if only interested into checking that $key is alias. |
|
| 1887 | + * |
|
| 1888 | + * @throws UnknownKeyException |
|
| 1889 | + * @throws TypeConflictException |
|
| 1890 | + * @return bool FALSE if conflict with defined lexicon were observed in the process |
|
| 1891 | + */ |
|
| 1892 | + private function matchAndApplyLexiconDefinition( |
|
| 1893 | + string $userId, |
|
| 1894 | + string $app, |
|
| 1895 | + string &$key, |
|
| 1896 | + ?bool &$lazy = null, |
|
| 1897 | + ValueType &$type = ValueType::MIXED, |
|
| 1898 | + int &$flags = 0, |
|
| 1899 | + ?string &$default = null, |
|
| 1900 | + ): bool { |
|
| 1901 | + $configDetails = $this->getConfigDetailsFromLexicon($app); |
|
| 1902 | + if (array_key_exists($key, $configDetails['aliases']) && !$this->ignoreLexiconAliases) { |
|
| 1903 | + // in case '$rename' is set in ConfigLexiconEntry, we use the new config key |
|
| 1904 | + $key = $configDetails['aliases'][$key]; |
|
| 1905 | + } |
|
| 1906 | + |
|
| 1907 | + if (!array_key_exists($key, $configDetails['entries'])) { |
|
| 1908 | + return $this->applyLexiconStrictness($configDetails['strictness'], 'The user config key ' . $app . '/' . $key . ' is not defined in the config lexicon'); |
|
| 1909 | + } |
|
| 1910 | + |
|
| 1911 | + // if lazy is NULL, we ignore all check on the type/lazyness/default from Lexicon |
|
| 1912 | + if ($lazy === null) { |
|
| 1913 | + return true; |
|
| 1914 | + } |
|
| 1915 | + |
|
| 1916 | + /** @var ConfigLexiconEntry $configValue */ |
|
| 1917 | + $configValue = $configDetails['entries'][$key]; |
|
| 1918 | + if ($type === ValueType::MIXED) { |
|
| 1919 | + // we overwrite if value was requested as mixed |
|
| 1920 | + $type = $configValue->getValueType(); |
|
| 1921 | + } elseif ($configValue->getValueType() !== $type) { |
|
| 1922 | + throw new TypeConflictException('The user config key ' . $app . '/' . $key . ' is typed incorrectly in relation to the config lexicon'); |
|
| 1923 | + } |
|
| 1924 | + |
|
| 1925 | + $lazy = $configValue->isLazy(); |
|
| 1926 | + $flags = $configValue->getFlags(); |
|
| 1927 | + if ($configValue->isDeprecated()) { |
|
| 1928 | + $this->logger->notice('User config key ' . $app . '/' . $key . ' is set as deprecated.'); |
|
| 1929 | + } |
|
| 1930 | + |
|
| 1931 | + $enforcedValue = $this->config->getSystemValue('lexicon.default.userconfig.enforced', [])[$app][$key] ?? false; |
|
| 1932 | + if (!$enforcedValue && $this->hasKey($userId, $app, $key, $lazy)) { |
|
| 1933 | + // if key exists there should be no need to extract default |
|
| 1934 | + return true; |
|
| 1935 | + } |
|
| 1936 | + |
|
| 1937 | + // only look for default if needed, default from Lexicon got priority if not overwritten by admin |
|
| 1938 | + if ($default !== null) { |
|
| 1939 | + $default = $this->getSystemDefault($app, $configValue) ?? $configValue->getDefault($this->getLexiconPreset()) ?? $default; |
|
| 1940 | + } |
|
| 1941 | + |
|
| 1942 | + // returning false will make get() returning $default and set() not changing value in database |
|
| 1943 | + return !$enforcedValue; |
|
| 1944 | + } |
|
| 1945 | + |
|
| 1946 | + /** |
|
| 1947 | + * get default value set in config/config.php if stored in key: |
|
| 1948 | + * |
|
| 1949 | + * 'lexicon.default.userconfig' => [ |
|
| 1950 | + * <appId> => [ |
|
| 1951 | + * <configKey> => 'my value', |
|
| 1952 | + * ] |
|
| 1953 | + * ], |
|
| 1954 | + * |
|
| 1955 | + * The entry is converted to string to fit the expected type when managing default value |
|
| 1956 | + */ |
|
| 1957 | + private function getSystemDefault(string $appId, ConfigLexiconEntry $configValue): ?string { |
|
| 1958 | + $default = $this->config->getSystemValue('lexicon.default.userconfig', [])[$appId][$configValue->getKey()] ?? null; |
|
| 1959 | + if ($default === null) { |
|
| 1960 | + // no system default, using default default. |
|
| 1961 | + return null; |
|
| 1962 | + } |
|
| 1963 | + |
|
| 1964 | + return $configValue->convertToString($default); |
|
| 1965 | + } |
|
| 1966 | + |
|
| 1967 | + /** |
|
| 1968 | + * manage ConfigLexicon behavior based on strictness set in IConfigLexicon |
|
| 1969 | + * |
|
| 1970 | + * @see IConfigLexicon::getStrictness() |
|
| 1971 | + * @param ConfigLexiconStrictness|null $strictness |
|
| 1972 | + * @param string $line |
|
| 1973 | + * |
|
| 1974 | + * @return bool TRUE if conflict can be fully ignored |
|
| 1975 | + * @throws UnknownKeyException |
|
| 1976 | + */ |
|
| 1977 | + private function applyLexiconStrictness(?ConfigLexiconStrictness $strictness, string $line = ''): bool { |
|
| 1978 | + if ($strictness === null) { |
|
| 1979 | + return true; |
|
| 1980 | + } |
|
| 1981 | + |
|
| 1982 | + switch ($strictness) { |
|
| 1983 | + case ConfigLexiconStrictness::IGNORE: |
|
| 1984 | + return true; |
|
| 1985 | + case ConfigLexiconStrictness::NOTICE: |
|
| 1986 | + $this->logger->notice($line); |
|
| 1987 | + return true; |
|
| 1988 | + case ConfigLexiconStrictness::WARNING: |
|
| 1989 | + $this->logger->warning($line); |
|
| 1990 | + return false; |
|
| 1991 | + case ConfigLexiconStrictness::EXCEPTION: |
|
| 1992 | + throw new UnknownKeyException($line); |
|
| 1993 | + } |
|
| 1994 | + |
|
| 1995 | + throw new UnknownKeyException($line); |
|
| 1996 | + } |
|
| 1997 | + |
|
| 1998 | + /** |
|
| 1999 | + * extract details from registered $appId's config lexicon |
|
| 2000 | + * |
|
| 2001 | + * @param string $appId |
|
| 2002 | + * @internal |
|
| 2003 | + * |
|
| 2004 | + * @return array{entries: array<string, ConfigLexiconEntry>, aliases: array<string, string>, strictness: ConfigLexiconStrictness} |
|
| 2005 | + */ |
|
| 2006 | + public function getConfigDetailsFromLexicon(string $appId): array { |
|
| 2007 | + if (!array_key_exists($appId, $this->configLexiconDetails)) { |
|
| 2008 | + $entries = $aliases = []; |
|
| 2009 | + $bootstrapCoordinator = \OCP\Server::get(Coordinator::class); |
|
| 2010 | + $configLexicon = $bootstrapCoordinator->getRegistrationContext()?->getConfigLexicon($appId); |
|
| 2011 | + foreach ($configLexicon?->getUserConfigs() ?? [] as $configEntry) { |
|
| 2012 | + $entries[$configEntry->getKey()] = $configEntry; |
|
| 2013 | + if ($configEntry->getRename() !== null) { |
|
| 2014 | + $aliases[$configEntry->getRename()] = $configEntry->getKey(); |
|
| 2015 | + } |
|
| 2016 | + } |
|
| 2017 | + |
|
| 2018 | + $this->configLexiconDetails[$appId] = [ |
|
| 2019 | + 'entries' => $entries, |
|
| 2020 | + 'aliases' => $aliases, |
|
| 2021 | + 'strictness' => $configLexicon?->getStrictness() ?? ConfigLexiconStrictness::IGNORE |
|
| 2022 | + ]; |
|
| 2023 | + } |
|
| 2024 | + |
|
| 2025 | + return $this->configLexiconDetails[$appId]; |
|
| 2026 | + } |
|
| 2027 | + |
|
| 2028 | + private function getLexiconEntry(string $appId, string $key): ?ConfigLexiconEntry { |
|
| 2029 | + return $this->getConfigDetailsFromLexicon($appId)['entries'][$key] ?? null; |
|
| 2030 | + } |
|
| 2031 | + |
|
| 2032 | + /** |
|
| 2033 | + * if set to TRUE, ignore aliases defined in Config Lexicon during the use of the methods of this class |
|
| 2034 | + * |
|
| 2035 | + * @internal |
|
| 2036 | + */ |
|
| 2037 | + public function ignoreLexiconAliases(bool $ignore): void { |
|
| 2038 | + $this->ignoreLexiconAliases = $ignore; |
|
| 2039 | + } |
|
| 2040 | + |
|
| 2041 | + private function getLexiconPreset(): Preset { |
|
| 2042 | + if ($this->configLexiconPreset === null) { |
|
| 2043 | + $this->configLexiconPreset = Preset::tryFrom($this->config->getSystemValueInt(ConfigManager::PRESET_CONFIGKEY, 0)) ?? Preset::NONE; |
|
| 2044 | + } |
|
| 2045 | + |
|
| 2046 | + return $this->configLexiconPreset; |
|
| 2047 | + } |
|
| 2048 | 2048 | } |
@@ -26,1454 +26,1454 @@ |
||
| 26 | 26 | * @package Test |
| 27 | 27 | */ |
| 28 | 28 | class AppConfigTest extends TestCase { |
| 29 | - protected IAppConfig $appConfig; |
|
| 30 | - protected IDBConnection $connection; |
|
| 31 | - private IConfig $config; |
|
| 32 | - private LoggerInterface $logger; |
|
| 33 | - private ICrypto $crypto; |
|
| 34 | - |
|
| 35 | - private array $originalConfig; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var array<string, array<string, array<string, string, int, bool, bool>>> |
|
| 39 | - * [appId => [configKey, configValue, valueType, lazy, sensitive]] |
|
| 40 | - */ |
|
| 41 | - private static array $baseStruct |
|
| 42 | - = [ |
|
| 43 | - 'testapp' => [ |
|
| 44 | - 'enabled' => ['enabled', 'yes'], |
|
| 45 | - 'installed_version' => ['installed_version', '1.2.3'], |
|
| 46 | - 'depends_on' => ['depends_on', 'someapp'], |
|
| 47 | - 'deletethis' => ['deletethis', 'deletethis'], |
|
| 48 | - 'key' => ['key', 'value'] |
|
| 49 | - ], |
|
| 50 | - 'someapp' => [ |
|
| 51 | - 'key' => ['key', 'value'], |
|
| 52 | - 'otherkey' => ['otherkey', 'othervalue'] |
|
| 53 | - ], |
|
| 54 | - '123456' => [ |
|
| 55 | - 'enabled' => ['enabled', 'yes'], |
|
| 56 | - 'key' => ['key', 'value'] |
|
| 57 | - ], |
|
| 58 | - 'anotherapp' => [ |
|
| 59 | - 'enabled' => ['enabled', 'no'], |
|
| 60 | - 'installed_version' => ['installed_version', '3.2.1'], |
|
| 61 | - 'key' => ['key', 'value'] |
|
| 62 | - ], |
|
| 63 | - 'non-sensitive-app' => [ |
|
| 64 | - 'lazy-key' => ['lazy-key', 'value', IAppConfig::VALUE_STRING, true, false], |
|
| 65 | - 'non-lazy-key' => ['non-lazy-key', 'value', IAppConfig::VALUE_STRING, false, false], |
|
| 66 | - ], |
|
| 67 | - 'sensitive-app' => [ |
|
| 68 | - 'lazy-key' => ['lazy-key', 'value', IAppConfig::VALUE_STRING, true, true], |
|
| 69 | - 'non-lazy-key' => ['non-lazy-key', 'value', IAppConfig::VALUE_STRING, false, true], |
|
| 70 | - ], |
|
| 71 | - 'only-lazy' => [ |
|
| 72 | - 'lazy-key' => ['lazy-key', 'value', IAppConfig::VALUE_STRING, true] |
|
| 73 | - ], |
|
| 74 | - 'typed' => [ |
|
| 75 | - 'mixed' => ['mixed', 'mix', IAppConfig::VALUE_MIXED], |
|
| 76 | - 'string' => ['string', 'value', IAppConfig::VALUE_STRING], |
|
| 77 | - 'int' => ['int', '42', IAppConfig::VALUE_INT], |
|
| 78 | - 'float' => ['float', '3.14', IAppConfig::VALUE_FLOAT], |
|
| 79 | - 'bool' => ['bool', '1', IAppConfig::VALUE_BOOL], |
|
| 80 | - 'array' => ['array', '{"test": 1}', IAppConfig::VALUE_ARRAY], |
|
| 81 | - ], |
|
| 82 | - 'prefix-app' => [ |
|
| 83 | - 'key1' => ['key1', 'value'], |
|
| 84 | - 'prefix1' => ['prefix1', 'value'], |
|
| 85 | - 'prefix-2' => ['prefix-2', 'value'], |
|
| 86 | - 'key-2' => ['key-2', 'value'], |
|
| 87 | - ] |
|
| 88 | - ]; |
|
| 89 | - |
|
| 90 | - protected function setUp(): void { |
|
| 91 | - parent::setUp(); |
|
| 92 | - |
|
| 93 | - $this->connection = Server::get(IDBConnection::class); |
|
| 94 | - $this->config = Server::get(IConfig::class); |
|
| 95 | - $this->logger = Server::get(LoggerInterface::class); |
|
| 96 | - $this->crypto = Server::get(ICrypto::class); |
|
| 97 | - |
|
| 98 | - // storing current config and emptying the data table |
|
| 99 | - $sql = $this->connection->getQueryBuilder(); |
|
| 100 | - $sql->select('*') |
|
| 101 | - ->from('appconfig'); |
|
| 102 | - $result = $sql->executeQuery(); |
|
| 103 | - $this->originalConfig = $result->fetchAll(); |
|
| 104 | - $result->closeCursor(); |
|
| 105 | - |
|
| 106 | - $sql = $this->connection->getQueryBuilder(); |
|
| 107 | - $sql->delete('appconfig'); |
|
| 108 | - $sql->executeStatement(); |
|
| 109 | - |
|
| 110 | - $sql = $this->connection->getQueryBuilder(); |
|
| 111 | - $sql->insert('appconfig') |
|
| 112 | - ->values( |
|
| 113 | - [ |
|
| 114 | - 'appid' => $sql->createParameter('appid'), |
|
| 115 | - 'configkey' => $sql->createParameter('configkey'), |
|
| 116 | - 'configvalue' => $sql->createParameter('configvalue'), |
|
| 117 | - 'type' => $sql->createParameter('type'), |
|
| 118 | - 'lazy' => $sql->createParameter('lazy') |
|
| 119 | - ] |
|
| 120 | - ); |
|
| 121 | - |
|
| 122 | - foreach (self::$baseStruct as $appId => $appData) { |
|
| 123 | - foreach ($appData as $key => $row) { |
|
| 124 | - $value = $row[1]; |
|
| 125 | - $type = $row[2] ?? IAppConfig::VALUE_MIXED; |
|
| 126 | - if (($row[4] ?? false) === true) { |
|
| 127 | - $type |= IAppConfig::VALUE_SENSITIVE; |
|
| 128 | - $value = self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX') . $this->crypto->encrypt($value); |
|
| 129 | - self::$baseStruct[$appId][$key]['encrypted'] = $value; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - $sql->setParameters( |
|
| 133 | - [ |
|
| 134 | - 'appid' => $appId, |
|
| 135 | - 'configkey' => $row[0], |
|
| 136 | - 'configvalue' => $value, |
|
| 137 | - 'type' => $type, |
|
| 138 | - 'lazy' => (($row[3] ?? false) === true) ? 1 : 0 |
|
| 139 | - ] |
|
| 140 | - )->executeStatement(); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - } |
|
| 144 | - |
|
| 145 | - protected function tearDown(): void { |
|
| 146 | - $sql = $this->connection->getQueryBuilder(); |
|
| 147 | - $sql->delete('appconfig'); |
|
| 148 | - $sql->executeStatement(); |
|
| 149 | - |
|
| 150 | - $sql = $this->connection->getQueryBuilder(); |
|
| 151 | - $sql->insert('appconfig') |
|
| 152 | - ->values( |
|
| 153 | - [ |
|
| 154 | - 'appid' => $sql->createParameter('appid'), |
|
| 155 | - 'configkey' => $sql->createParameter('configkey'), |
|
| 156 | - 'configvalue' => $sql->createParameter('configvalue'), |
|
| 157 | - 'lazy' => $sql->createParameter('lazy'), |
|
| 158 | - 'type' => $sql->createParameter('type'), |
|
| 159 | - ] |
|
| 160 | - ); |
|
| 161 | - |
|
| 162 | - foreach ($this->originalConfig as $key => $configs) { |
|
| 163 | - $sql->setParameter('appid', $configs['appid']) |
|
| 164 | - ->setParameter('configkey', $configs['configkey']) |
|
| 165 | - ->setParameter('configvalue', $configs['configvalue']) |
|
| 166 | - ->setParameter('lazy', ($configs['lazy'] === '1') ? '1' : '0') |
|
| 167 | - ->setParameter('type', $configs['type']); |
|
| 168 | - $sql->executeStatement(); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - // $this->restoreService(AppConfig::class); |
|
| 172 | - parent::tearDown(); |
|
| 173 | - } |
|
| 174 | - |
|
| 175 | - /** |
|
| 176 | - * @param bool $preLoading TRUE will preload the 'fast' cache, which is the normal behavior of usual |
|
| 177 | - * IAppConfig |
|
| 178 | - * |
|
| 179 | - * @return IAppConfig |
|
| 180 | - */ |
|
| 181 | - private function generateAppConfig(bool $preLoading = true): IAppConfig { |
|
| 182 | - /** @var AppConfig $config */ |
|
| 183 | - $config = new AppConfig( |
|
| 184 | - $this->connection, |
|
| 185 | - $this->config, |
|
| 186 | - $this->logger, |
|
| 187 | - $this->crypto, |
|
| 188 | - ); |
|
| 189 | - $msg = ' generateAppConfig() failed to confirm cache status'; |
|
| 190 | - |
|
| 191 | - // confirm cache status |
|
| 192 | - $status = $config->statusCache(); |
|
| 193 | - $this->assertSame(false, $status['fastLoaded'], $msg); |
|
| 194 | - $this->assertSame(false, $status['lazyLoaded'], $msg); |
|
| 195 | - $this->assertSame([], $status['fastCache'], $msg); |
|
| 196 | - $this->assertSame([], $status['lazyCache'], $msg); |
|
| 197 | - if ($preLoading) { |
|
| 198 | - // simple way to initiate the load of non-lazy config values in cache |
|
| 199 | - $config->getValueString('core', 'preload', ''); |
|
| 200 | - |
|
| 201 | - // confirm cache status |
|
| 202 | - $status = $config->statusCache(); |
|
| 203 | - $this->assertSame(true, $status['fastLoaded'], $msg); |
|
| 204 | - $this->assertSame(false, $status['lazyLoaded'], $msg); |
|
| 205 | - |
|
| 206 | - $apps = array_values(array_diff(array_keys(self::$baseStruct), ['only-lazy'])); |
|
| 207 | - $this->assertEqualsCanonicalizing($apps, array_keys($status['fastCache']), $msg); |
|
| 208 | - $this->assertSame([], array_keys($status['lazyCache']), $msg); |
|
| 209 | - } |
|
| 210 | - |
|
| 211 | - return $config; |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - public function testGetApps(): void { |
|
| 215 | - $config = $this->generateAppConfig(false); |
|
| 216 | - |
|
| 217 | - $this->assertEqualsCanonicalizing(array_keys(self::$baseStruct), $config->getApps()); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - public function testGetAppInstalledVersions(): void { |
|
| 221 | - $config = $this->generateAppConfig(false); |
|
| 222 | - |
|
| 223 | - $this->assertEquals( |
|
| 224 | - ['testapp' => '1.2.3', 'anotherapp' => '3.2.1'], |
|
| 225 | - $config->getAppInstalledVersions(false) |
|
| 226 | - ); |
|
| 227 | - $this->assertEquals( |
|
| 228 | - ['testapp' => '1.2.3'], |
|
| 229 | - $config->getAppInstalledVersions(true) |
|
| 230 | - ); |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - /** |
|
| 234 | - * returns list of app and their keys |
|
| 235 | - * |
|
| 236 | - * @return array<string, string[]> ['appId' => ['key1', 'key2', ]] |
|
| 237 | - * @see testGetKeys |
|
| 238 | - */ |
|
| 239 | - public static function providerGetAppKeys(): array { |
|
| 240 | - $appKeys = []; |
|
| 241 | - foreach (self::$baseStruct as $appId => $appData) { |
|
| 242 | - $keys = []; |
|
| 243 | - foreach ($appData as $row) { |
|
| 244 | - $keys[] = $row[0]; |
|
| 245 | - } |
|
| 246 | - $appKeys[] = [(string)$appId, $keys]; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - return $appKeys; |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - /** |
|
| 253 | - * returns list of config keys |
|
| 254 | - * |
|
| 255 | - * @return array<string, string, string, int, bool, bool> [appId, key, value, type, lazy, sensitive] |
|
| 256 | - * @see testIsSensitive |
|
| 257 | - * @see testIsLazy |
|
| 258 | - * @see testGetKeys |
|
| 259 | - */ |
|
| 260 | - public static function providerGetKeys(): array { |
|
| 261 | - $appKeys = []; |
|
| 262 | - foreach (self::$baseStruct as $appId => $appData) { |
|
| 263 | - foreach ($appData as $row) { |
|
| 264 | - $appKeys[] = [ |
|
| 265 | - (string)$appId, $row[0], $row[1], $row[2] ?? IAppConfig::VALUE_MIXED, $row[3] ?? false, |
|
| 266 | - $row[4] ?? false |
|
| 267 | - ]; |
|
| 268 | - } |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - return $appKeys; |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - /** |
|
| 275 | - * |
|
| 276 | - * @param string $appId |
|
| 277 | - * @param array $expectedKeys |
|
| 278 | - */ |
|
| 279 | - #[\PHPUnit\Framework\Attributes\DataProvider('providerGetAppKeys')] |
|
| 280 | - public function testGetKeys(string $appId, array $expectedKeys): void { |
|
| 281 | - $config = $this->generateAppConfig(); |
|
| 282 | - $this->assertEqualsCanonicalizing($expectedKeys, $config->getKeys($appId)); |
|
| 283 | - } |
|
| 284 | - |
|
| 285 | - public function testGetKeysOnUnknownAppShouldReturnsEmptyArray(): void { |
|
| 286 | - $config = $this->generateAppConfig(); |
|
| 287 | - $this->assertEqualsCanonicalizing([], $config->getKeys('unknown-app')); |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * |
|
| 292 | - * @param string $appId |
|
| 293 | - * @param string $configKey |
|
| 294 | - * @param string $value |
|
| 295 | - * @param bool $lazy |
|
| 296 | - */ |
|
| 297 | - #[\PHPUnit\Framework\Attributes\DataProvider('providerGetKeys')] |
|
| 298 | - public function testHasKey(string $appId, string $configKey, string $value, int $type, bool $lazy): void { |
|
| 299 | - $config = $this->generateAppConfig(); |
|
| 300 | - $this->assertEquals(true, $config->hasKey($appId, $configKey, $lazy)); |
|
| 301 | - } |
|
| 302 | - |
|
| 303 | - public function testHasKeyOnNonExistentKeyReturnsFalse(): void { |
|
| 304 | - $config = $this->generateAppConfig(); |
|
| 305 | - $this->assertEquals(false, $config->hasKey(array_keys(self::$baseStruct)[0], 'inexistant-key')); |
|
| 306 | - } |
|
| 307 | - |
|
| 308 | - public function testHasKeyOnUnknownAppReturnsFalse(): void { |
|
| 309 | - $config = $this->generateAppConfig(); |
|
| 310 | - $this->assertEquals(false, $config->hasKey('inexistant-app', 'inexistant-key')); |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - public function testHasKeyOnMistypedAsLazyReturnsFalse(): void { |
|
| 314 | - $config = $this->generateAppConfig(); |
|
| 315 | - $this->assertSame(false, $config->hasKey('non-sensitive-app', 'non-lazy-key', true)); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - public function testHasKeyOnMistypeAsNonLazyReturnsFalse(): void { |
|
| 319 | - $config = $this->generateAppConfig(); |
|
| 320 | - $this->assertSame(false, $config->hasKey('non-sensitive-app', 'lazy-key', false)); |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - public function testHasKeyOnMistypeAsNonLazyReturnsTrueWithLazyArgumentIsNull(): void { |
|
| 324 | - $config = $this->generateAppConfig(); |
|
| 325 | - $this->assertSame(true, $config->hasKey('non-sensitive-app', 'lazy-key', null)); |
|
| 326 | - } |
|
| 327 | - |
|
| 328 | - #[\PHPUnit\Framework\Attributes\DataProvider('providerGetKeys')] |
|
| 329 | - public function testIsSensitive( |
|
| 330 | - string $appId, string $configKey, string $configValue, int $type, bool $lazy, bool $sensitive, |
|
| 331 | - ): void { |
|
| 332 | - $config = $this->generateAppConfig(); |
|
| 333 | - $this->assertEquals($sensitive, $config->isSensitive($appId, $configKey, $lazy)); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - public function testIsSensitiveOnNonExistentKeyThrowsException(): void { |
|
| 337 | - $config = $this->generateAppConfig(); |
|
| 338 | - $this->expectException(AppConfigUnknownKeyException::class); |
|
| 339 | - $config->isSensitive(array_keys(self::$baseStruct)[0], 'inexistant-key'); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - public function testIsSensitiveOnUnknownAppThrowsException(): void { |
|
| 343 | - $config = $this->generateAppConfig(); |
|
| 344 | - $this->expectException(AppConfigUnknownKeyException::class); |
|
| 345 | - $config->isSensitive('unknown-app', 'inexistant-key'); |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - public function testIsSensitiveOnSensitiveMistypedAsLazy(): void { |
|
| 349 | - $config = $this->generateAppConfig(); |
|
| 350 | - $this->assertSame(true, $config->isSensitive('sensitive-app', 'non-lazy-key', true)); |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - public function testIsSensitiveOnNonSensitiveMistypedAsLazy(): void { |
|
| 354 | - $config = $this->generateAppConfig(); |
|
| 355 | - $this->assertSame(false, $config->isSensitive('non-sensitive-app', 'non-lazy-key', true)); |
|
| 356 | - } |
|
| 357 | - |
|
| 358 | - public function testIsSensitiveOnSensitiveMistypedAsNonLazyThrowsException(): void { |
|
| 359 | - $config = $this->generateAppConfig(); |
|
| 360 | - $this->expectException(AppConfigUnknownKeyException::class); |
|
| 361 | - $config->isSensitive('sensitive-app', 'lazy-key', false); |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - public function testIsSensitiveOnNonSensitiveMistypedAsNonLazyThrowsException(): void { |
|
| 365 | - $config = $this->generateAppConfig(); |
|
| 366 | - $this->expectException(AppConfigUnknownKeyException::class); |
|
| 367 | - $config->isSensitive('non-sensitive-app', 'lazy-key', false); |
|
| 368 | - } |
|
| 369 | - |
|
| 370 | - #[\PHPUnit\Framework\Attributes\DataProvider('providerGetKeys')] |
|
| 371 | - public function testIsLazy(string $appId, string $configKey, string $configValue, int $type, bool $lazy, |
|
| 372 | - ): void { |
|
| 373 | - $config = $this->generateAppConfig(); |
|
| 374 | - $this->assertEquals($lazy, $config->isLazy($appId, $configKey)); |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - public function testIsLazyOnNonExistentKeyThrowsException(): void { |
|
| 378 | - $config = $this->generateAppConfig(); |
|
| 379 | - $this->expectException(AppConfigUnknownKeyException::class); |
|
| 380 | - $config->isLazy(array_keys(self::$baseStruct)[0], 'inexistant-key'); |
|
| 381 | - } |
|
| 382 | - |
|
| 383 | - public function testIsLazyOnUnknownAppThrowsException(): void { |
|
| 384 | - $config = $this->generateAppConfig(); |
|
| 385 | - $this->expectException(AppConfigUnknownKeyException::class); |
|
| 386 | - $config->isLazy('unknown-app', 'inexistant-key'); |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - public function testGetAllValues(): void { |
|
| 390 | - $config = $this->generateAppConfig(); |
|
| 391 | - $this->assertEquals( |
|
| 392 | - [ |
|
| 393 | - 'array' => ['test' => 1], |
|
| 394 | - 'bool' => true, |
|
| 395 | - 'float' => 3.14, |
|
| 396 | - 'int' => 42, |
|
| 397 | - 'mixed' => 'mix', |
|
| 398 | - 'string' => 'value', |
|
| 399 | - ], |
|
| 400 | - $config->getAllValues('typed') |
|
| 401 | - ); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - public function testGetAllValuesWithEmptyApp(): void { |
|
| 405 | - $config = $this->generateAppConfig(); |
|
| 406 | - $this->expectException(InvalidArgumentException::class); |
|
| 407 | - $config->getAllValues(''); |
|
| 408 | - } |
|
| 409 | - |
|
| 410 | - /** |
|
| 411 | - * |
|
| 412 | - * @param string $appId |
|
| 413 | - * @param array $keys |
|
| 414 | - */ |
|
| 415 | - #[\PHPUnit\Framework\Attributes\DataProvider('providerGetAppKeys')] |
|
| 416 | - public function testGetAllValuesWithEmptyKey(string $appId, array $keys): void { |
|
| 417 | - $config = $this->generateAppConfig(); |
|
| 418 | - $this->assertEqualsCanonicalizing($keys, array_keys($config->getAllValues($appId, ''))); |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - public function testGetAllValuesWithPrefix(): void { |
|
| 422 | - $config = $this->generateAppConfig(); |
|
| 423 | - $this->assertEqualsCanonicalizing(['prefix1', 'prefix-2'], array_keys($config->getAllValues('prefix-app', 'prefix'))); |
|
| 424 | - } |
|
| 425 | - |
|
| 426 | - public function testSearchValues(): void { |
|
| 427 | - $config = $this->generateAppConfig(); |
|
| 428 | - $this->assertEqualsCanonicalizing(['testapp' => 'yes', '123456' => 'yes', 'anotherapp' => 'no'], $config->searchValues('enabled')); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - public function testGetValueString(): void { |
|
| 432 | - $config = $this->generateAppConfig(); |
|
| 433 | - $this->assertSame('value', $config->getValueString('typed', 'string', '')); |
|
| 434 | - } |
|
| 435 | - |
|
| 436 | - public function testGetValueStringOnUnknownAppReturnsDefault(): void { |
|
| 437 | - $config = $this->generateAppConfig(); |
|
| 438 | - $this->assertSame('default-1', $config->getValueString('typed-1', 'string', 'default-1')); |
|
| 439 | - } |
|
| 440 | - |
|
| 441 | - public function testGetValueStringOnNonExistentKeyReturnsDefault(): void { |
|
| 442 | - $config = $this->generateAppConfig(); |
|
| 443 | - $this->assertSame('default-2', $config->getValueString('typed', 'string-2', 'default-2')); |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - public function testGetValueStringOnWrongType(): void { |
|
| 447 | - $config = $this->generateAppConfig(); |
|
| 448 | - $this->expectException(AppConfigTypeConflictException::class); |
|
| 449 | - $config->getValueString('typed', 'int'); |
|
| 450 | - } |
|
| 451 | - |
|
| 452 | - public function testGetNonLazyValueStringAsLazy(): void { |
|
| 453 | - $config = $this->generateAppConfig(); |
|
| 454 | - $this->assertSame('value', $config->getValueString('non-sensitive-app', 'non-lazy-key', 'default', lazy: true)); |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - public function testGetValueInt(): void { |
|
| 458 | - $config = $this->generateAppConfig(); |
|
| 459 | - $this->assertSame(42, $config->getValueInt('typed', 'int', 0)); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - public function testGetValueIntOnUnknownAppReturnsDefault(): void { |
|
| 463 | - $config = $this->generateAppConfig(); |
|
| 464 | - $this->assertSame(1, $config->getValueInt('typed-1', 'int', 1)); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - public function testGetValueIntOnNonExistentKeyReturnsDefault(): void { |
|
| 468 | - $config = $this->generateAppConfig(); |
|
| 469 | - $this->assertSame(2, $config->getValueInt('typed', 'int-2', 2)); |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - public function testGetValueIntOnWrongType(): void { |
|
| 473 | - $config = $this->generateAppConfig(); |
|
| 474 | - $this->expectException(AppConfigTypeConflictException::class); |
|
| 475 | - $config->getValueInt('typed', 'float'); |
|
| 476 | - } |
|
| 477 | - |
|
| 478 | - public function testGetValueFloat(): void { |
|
| 479 | - $config = $this->generateAppConfig(); |
|
| 480 | - $this->assertSame(3.14, $config->getValueFloat('typed', 'float', 0)); |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - public function testGetValueFloatOnNonUnknownAppReturnsDefault(): void { |
|
| 484 | - $config = $this->generateAppConfig(); |
|
| 485 | - $this->assertSame(1.11, $config->getValueFloat('typed-1', 'float', 1.11)); |
|
| 486 | - } |
|
| 487 | - |
|
| 488 | - public function testGetValueFloatOnNonExistentKeyReturnsDefault(): void { |
|
| 489 | - $config = $this->generateAppConfig(); |
|
| 490 | - $this->assertSame(2.22, $config->getValueFloat('typed', 'float-2', 2.22)); |
|
| 491 | - } |
|
| 492 | - |
|
| 493 | - public function testGetValueFloatOnWrongType(): void { |
|
| 494 | - $config = $this->generateAppConfig(); |
|
| 495 | - $this->expectException(AppConfigTypeConflictException::class); |
|
| 496 | - $config->getValueFloat('typed', 'bool'); |
|
| 497 | - } |
|
| 498 | - |
|
| 499 | - public function testGetValueBool(): void { |
|
| 500 | - $config = $this->generateAppConfig(); |
|
| 501 | - $this->assertSame(true, $config->getValueBool('typed', 'bool')); |
|
| 502 | - } |
|
| 503 | - |
|
| 504 | - public function testGetValueBoolOnUnknownAppReturnsDefault(): void { |
|
| 505 | - $config = $this->generateAppConfig(); |
|
| 506 | - $this->assertSame(false, $config->getValueBool('typed-1', 'bool', false)); |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - public function testGetValueBoolOnNonExistentKeyReturnsDefault(): void { |
|
| 510 | - $config = $this->generateAppConfig(); |
|
| 511 | - $this->assertSame(false, $config->getValueBool('typed', 'bool-2')); |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - public function testGetValueBoolOnWrongType(): void { |
|
| 515 | - $config = $this->generateAppConfig(); |
|
| 516 | - $this->expectException(AppConfigTypeConflictException::class); |
|
| 517 | - $config->getValueBool('typed', 'array'); |
|
| 518 | - } |
|
| 519 | - |
|
| 520 | - public function testGetValueArray(): void { |
|
| 521 | - $config = $this->generateAppConfig(); |
|
| 522 | - $this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('typed', 'array', [])); |
|
| 523 | - } |
|
| 524 | - |
|
| 525 | - public function testGetValueArrayOnUnknownAppReturnsDefault(): void { |
|
| 526 | - $config = $this->generateAppConfig(); |
|
| 527 | - $this->assertSame([1], $config->getValueArray('typed-1', 'array', [1])); |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - public function testGetValueArrayOnNonExistentKeyReturnsDefault(): void { |
|
| 531 | - $config = $this->generateAppConfig(); |
|
| 532 | - $this->assertSame([1, 2], $config->getValueArray('typed', 'array-2', [1, 2])); |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - public function testGetValueArrayOnWrongType(): void { |
|
| 536 | - $config = $this->generateAppConfig(); |
|
| 537 | - $this->expectException(AppConfigTypeConflictException::class); |
|
| 538 | - $config->getValueArray('typed', 'string'); |
|
| 539 | - } |
|
| 540 | - |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * @return array |
|
| 544 | - * @see testGetValueType |
|
| 545 | - * |
|
| 546 | - * @see testGetValueMixed |
|
| 547 | - */ |
|
| 548 | - public static function providerGetValueMixed(): array { |
|
| 549 | - return [ |
|
| 550 | - // key, value, type |
|
| 551 | - ['mixed', 'mix', IAppConfig::VALUE_MIXED], |
|
| 552 | - ['string', 'value', IAppConfig::VALUE_STRING], |
|
| 553 | - ['int', '42', IAppConfig::VALUE_INT], |
|
| 554 | - ['float', '3.14', IAppConfig::VALUE_FLOAT], |
|
| 555 | - ['bool', '1', IAppConfig::VALUE_BOOL], |
|
| 556 | - ['array', '{"test": 1}', IAppConfig::VALUE_ARRAY], |
|
| 557 | - ]; |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - /** |
|
| 561 | - * |
|
| 562 | - * @param string $key |
|
| 563 | - * @param string $value |
|
| 564 | - */ |
|
| 565 | - #[\PHPUnit\Framework\Attributes\DataProvider('providerGetValueMixed')] |
|
| 566 | - public function testGetValueMixed(string $key, string $value): void { |
|
| 567 | - $config = $this->generateAppConfig(); |
|
| 568 | - $this->assertSame($value, $config->getValueMixed('typed', $key)); |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * |
|
| 573 | - * @param string $key |
|
| 574 | - * @param string $value |
|
| 575 | - * @param int $type |
|
| 576 | - */ |
|
| 577 | - #[\PHPUnit\Framework\Attributes\DataProvider('providerGetValueMixed')] |
|
| 578 | - public function testGetValueType(string $key, string $value, int $type): void { |
|
| 579 | - $config = $this->generateAppConfig(); |
|
| 580 | - $this->assertSame($type, $config->getValueType('typed', $key)); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - public function testGetValueTypeOnUnknownApp(): void { |
|
| 584 | - $config = $this->generateAppConfig(); |
|
| 585 | - $this->expectException(AppConfigUnknownKeyException::class); |
|
| 586 | - $config->getValueType('typed-1', 'string'); |
|
| 587 | - } |
|
| 588 | - |
|
| 589 | - public function testGetValueTypeOnNonExistentKey(): void { |
|
| 590 | - $config = $this->generateAppConfig(); |
|
| 591 | - $this->expectException(AppConfigUnknownKeyException::class); |
|
| 592 | - $config->getValueType('typed', 'string-2'); |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - public function testSetValueString(): void { |
|
| 596 | - $config = $this->generateAppConfig(); |
|
| 597 | - $config->setValueString('feed', 'string', 'value-1'); |
|
| 598 | - $this->assertSame('value-1', $config->getValueString('feed', 'string', '')); |
|
| 599 | - } |
|
| 600 | - |
|
| 601 | - public function testSetValueStringCache(): void { |
|
| 602 | - $config = $this->generateAppConfig(); |
|
| 603 | - $config->setValueString('feed', 'string', 'value-1'); |
|
| 604 | - $status = $config->statusCache(); |
|
| 605 | - $this->assertSame('value-1', $status['fastCache']['feed']['string']); |
|
| 606 | - } |
|
| 607 | - |
|
| 608 | - public function testSetValueStringDatabase(): void { |
|
| 609 | - $config = $this->generateAppConfig(); |
|
| 610 | - $config->setValueString('feed', 'string', 'value-1'); |
|
| 611 | - $config->clearCache(); |
|
| 612 | - $this->assertSame('value-1', $config->getValueString('feed', 'string', '')); |
|
| 613 | - } |
|
| 614 | - |
|
| 615 | - public function testSetValueStringIsUpdated(): void { |
|
| 616 | - $config = $this->generateAppConfig(); |
|
| 617 | - $config->setValueString('feed', 'string', 'value-1'); |
|
| 618 | - $this->assertSame(true, $config->setValueString('feed', 'string', 'value-2')); |
|
| 619 | - } |
|
| 620 | - |
|
| 621 | - public function testSetValueStringIsNotUpdated(): void { |
|
| 622 | - $config = $this->generateAppConfig(); |
|
| 623 | - $config->setValueString('feed', 'string', 'value-1'); |
|
| 624 | - $this->assertSame(false, $config->setValueString('feed', 'string', 'value-1')); |
|
| 625 | - } |
|
| 626 | - |
|
| 627 | - public function testSetValueStringIsUpdatedCache(): void { |
|
| 628 | - $config = $this->generateAppConfig(); |
|
| 629 | - $config->setValueString('feed', 'string', 'value-1'); |
|
| 630 | - $config->setValueString('feed', 'string', 'value-2'); |
|
| 631 | - $status = $config->statusCache(); |
|
| 632 | - $this->assertSame('value-2', $status['fastCache']['feed']['string']); |
|
| 633 | - } |
|
| 634 | - |
|
| 635 | - public function testSetValueStringIsUpdatedDatabase(): void { |
|
| 636 | - $config = $this->generateAppConfig(); |
|
| 637 | - $config->setValueString('feed', 'string', 'value-1'); |
|
| 638 | - $config->setValueString('feed', 'string', 'value-2'); |
|
| 639 | - $config->clearCache(); |
|
| 640 | - $this->assertSame('value-2', $config->getValueString('feed', 'string', '')); |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - public function testSetValueInt(): void { |
|
| 644 | - $config = $this->generateAppConfig(); |
|
| 645 | - $config->setValueInt('feed', 'int', 42); |
|
| 646 | - $this->assertSame(42, $config->getValueInt('feed', 'int', 0)); |
|
| 647 | - } |
|
| 648 | - |
|
| 649 | - public function testSetValueIntCache(): void { |
|
| 650 | - $config = $this->generateAppConfig(); |
|
| 651 | - $config->setValueInt('feed', 'int', 42); |
|
| 652 | - $status = $config->statusCache(); |
|
| 653 | - $this->assertSame('42', $status['fastCache']['feed']['int']); |
|
| 654 | - } |
|
| 655 | - |
|
| 656 | - public function testSetValueIntDatabase(): void { |
|
| 657 | - $config = $this->generateAppConfig(); |
|
| 658 | - $config->setValueInt('feed', 'int', 42); |
|
| 659 | - $config->clearCache(); |
|
| 660 | - $this->assertSame(42, $config->getValueInt('feed', 'int', 0)); |
|
| 661 | - } |
|
| 662 | - |
|
| 663 | - public function testSetValueIntIsUpdated(): void { |
|
| 664 | - $config = $this->generateAppConfig(); |
|
| 665 | - $config->setValueInt('feed', 'int', 42); |
|
| 666 | - $this->assertSame(true, $config->setValueInt('feed', 'int', 17)); |
|
| 667 | - } |
|
| 668 | - |
|
| 669 | - public function testSetValueIntIsNotUpdated(): void { |
|
| 670 | - $config = $this->generateAppConfig(); |
|
| 671 | - $config->setValueInt('feed', 'int', 42); |
|
| 672 | - $this->assertSame(false, $config->setValueInt('feed', 'int', 42)); |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - public function testSetValueIntIsUpdatedCache(): void { |
|
| 676 | - $config = $this->generateAppConfig(); |
|
| 677 | - $config->setValueInt('feed', 'int', 42); |
|
| 678 | - $config->setValueInt('feed', 'int', 17); |
|
| 679 | - $status = $config->statusCache(); |
|
| 680 | - $this->assertSame('17', $status['fastCache']['feed']['int']); |
|
| 681 | - } |
|
| 682 | - |
|
| 683 | - public function testSetValueIntIsUpdatedDatabase(): void { |
|
| 684 | - $config = $this->generateAppConfig(); |
|
| 685 | - $config->setValueInt('feed', 'int', 42); |
|
| 686 | - $config->setValueInt('feed', 'int', 17); |
|
| 687 | - $config->clearCache(); |
|
| 688 | - $this->assertSame(17, $config->getValueInt('feed', 'int', 0)); |
|
| 689 | - } |
|
| 690 | - |
|
| 691 | - public function testSetValueFloat(): void { |
|
| 692 | - $config = $this->generateAppConfig(); |
|
| 693 | - $config->setValueFloat('feed', 'float', 3.14); |
|
| 694 | - $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0)); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - public function testSetValueFloatCache(): void { |
|
| 698 | - $config = $this->generateAppConfig(); |
|
| 699 | - $config->setValueFloat('feed', 'float', 3.14); |
|
| 700 | - $status = $config->statusCache(); |
|
| 701 | - $this->assertSame('3.14', $status['fastCache']['feed']['float']); |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - public function testSetValueFloatDatabase(): void { |
|
| 705 | - $config = $this->generateAppConfig(); |
|
| 706 | - $config->setValueFloat('feed', 'float', 3.14); |
|
| 707 | - $config->clearCache(); |
|
| 708 | - $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0)); |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - public function testSetValueFloatIsUpdated(): void { |
|
| 712 | - $config = $this->generateAppConfig(); |
|
| 713 | - $config->setValueFloat('feed', 'float', 3.14); |
|
| 714 | - $this->assertSame(true, $config->setValueFloat('feed', 'float', 1.23)); |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - public function testSetValueFloatIsNotUpdated(): void { |
|
| 718 | - $config = $this->generateAppConfig(); |
|
| 719 | - $config->setValueFloat('feed', 'float', 3.14); |
|
| 720 | - $this->assertSame(false, $config->setValueFloat('feed', 'float', 3.14)); |
|
| 721 | - } |
|
| 722 | - |
|
| 723 | - public function testSetValueFloatIsUpdatedCache(): void { |
|
| 724 | - $config = $this->generateAppConfig(); |
|
| 725 | - $config->setValueFloat('feed', 'float', 3.14); |
|
| 726 | - $config->setValueFloat('feed', 'float', 1.23); |
|
| 727 | - $status = $config->statusCache(); |
|
| 728 | - $this->assertSame('1.23', $status['fastCache']['feed']['float']); |
|
| 729 | - } |
|
| 730 | - |
|
| 731 | - public function testSetValueFloatIsUpdatedDatabase(): void { |
|
| 732 | - $config = $this->generateAppConfig(); |
|
| 733 | - $config->setValueFloat('feed', 'float', 3.14); |
|
| 734 | - $config->setValueFloat('feed', 'float', 1.23); |
|
| 735 | - $config->clearCache(); |
|
| 736 | - $this->assertSame(1.23, $config->getValueFloat('feed', 'float', 0)); |
|
| 737 | - } |
|
| 738 | - |
|
| 739 | - public function testSetValueBool(): void { |
|
| 740 | - $config = $this->generateAppConfig(); |
|
| 741 | - $config->setValueBool('feed', 'bool', true); |
|
| 742 | - $this->assertSame(true, $config->getValueBool('feed', 'bool', false)); |
|
| 743 | - } |
|
| 744 | - |
|
| 745 | - public function testSetValueBoolCache(): void { |
|
| 746 | - $config = $this->generateAppConfig(); |
|
| 747 | - $config->setValueBool('feed', 'bool', true); |
|
| 748 | - $status = $config->statusCache(); |
|
| 749 | - $this->assertSame('1', $status['fastCache']['feed']['bool']); |
|
| 750 | - } |
|
| 751 | - |
|
| 752 | - public function testSetValueBoolDatabase(): void { |
|
| 753 | - $config = $this->generateAppConfig(); |
|
| 754 | - $config->setValueBool('feed', 'bool', true); |
|
| 755 | - $config->clearCache(); |
|
| 756 | - $this->assertSame(true, $config->getValueBool('feed', 'bool', false)); |
|
| 757 | - } |
|
| 758 | - |
|
| 759 | - public function testSetValueBoolIsUpdated(): void { |
|
| 760 | - $config = $this->generateAppConfig(); |
|
| 761 | - $config->setValueBool('feed', 'bool', true); |
|
| 762 | - $this->assertSame(true, $config->setValueBool('feed', 'bool', false)); |
|
| 763 | - } |
|
| 764 | - |
|
| 765 | - public function testSetValueBoolIsNotUpdated(): void { |
|
| 766 | - $config = $this->generateAppConfig(); |
|
| 767 | - $config->setValueBool('feed', 'bool', true); |
|
| 768 | - $this->assertSame(false, $config->setValueBool('feed', 'bool', true)); |
|
| 769 | - } |
|
| 770 | - |
|
| 771 | - public function testSetValueBoolIsUpdatedCache(): void { |
|
| 772 | - $config = $this->generateAppConfig(); |
|
| 773 | - $config->setValueBool('feed', 'bool', true); |
|
| 774 | - $config->setValueBool('feed', 'bool', false); |
|
| 775 | - $status = $config->statusCache(); |
|
| 776 | - $this->assertSame('0', $status['fastCache']['feed']['bool']); |
|
| 777 | - } |
|
| 778 | - |
|
| 779 | - public function testSetValueBoolIsUpdatedDatabase(): void { |
|
| 780 | - $config = $this->generateAppConfig(); |
|
| 781 | - $config->setValueBool('feed', 'bool', true); |
|
| 782 | - $config->setValueBool('feed', 'bool', false); |
|
| 783 | - $config->clearCache(); |
|
| 784 | - $this->assertSame(false, $config->getValueBool('feed', 'bool', true)); |
|
| 785 | - } |
|
| 786 | - |
|
| 787 | - |
|
| 788 | - public function testSetValueArray(): void { |
|
| 789 | - $config = $this->generateAppConfig(); |
|
| 790 | - $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 791 | - $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [])); |
|
| 792 | - } |
|
| 793 | - |
|
| 794 | - public function testSetValueArrayCache(): void { |
|
| 795 | - $config = $this->generateAppConfig(); |
|
| 796 | - $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 797 | - $status = $config->statusCache(); |
|
| 798 | - $this->assertSame('{"test":1}', $status['fastCache']['feed']['array']); |
|
| 799 | - } |
|
| 800 | - |
|
| 801 | - public function testSetValueArrayDatabase(): void { |
|
| 802 | - $config = $this->generateAppConfig(); |
|
| 803 | - $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 804 | - $config->clearCache(); |
|
| 805 | - $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [])); |
|
| 806 | - } |
|
| 807 | - |
|
| 808 | - public function testSetValueArrayIsUpdated(): void { |
|
| 809 | - $config = $this->generateAppConfig(); |
|
| 810 | - $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 811 | - $this->assertSame(true, $config->setValueArray('feed', 'array', ['test' => 2])); |
|
| 812 | - } |
|
| 813 | - |
|
| 814 | - public function testSetValueArrayIsNotUpdated(): void { |
|
| 815 | - $config = $this->generateAppConfig(); |
|
| 816 | - $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 817 | - $this->assertSame(false, $config->setValueArray('feed', 'array', ['test' => 1])); |
|
| 818 | - } |
|
| 819 | - |
|
| 820 | - public function testSetValueArrayIsUpdatedCache(): void { |
|
| 821 | - $config = $this->generateAppConfig(); |
|
| 822 | - $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 823 | - $config->setValueArray('feed', 'array', ['test' => 2]); |
|
| 824 | - $status = $config->statusCache(); |
|
| 825 | - $this->assertSame('{"test":2}', $status['fastCache']['feed']['array']); |
|
| 826 | - } |
|
| 827 | - |
|
| 828 | - public function testSetValueArrayIsUpdatedDatabase(): void { |
|
| 829 | - $config = $this->generateAppConfig(); |
|
| 830 | - $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 831 | - $config->setValueArray('feed', 'array', ['test' => 2]); |
|
| 832 | - $config->clearCache(); |
|
| 833 | - $this->assertSame(['test' => 2], $config->getValueArray('feed', 'array', [])); |
|
| 834 | - } |
|
| 835 | - |
|
| 836 | - public function testSetLazyValueString(): void { |
|
| 837 | - $config = $this->generateAppConfig(); |
|
| 838 | - $config->setValueString('feed', 'string', 'value-1', true); |
|
| 839 | - $this->assertSame('value-1', $config->getValueString('feed', 'string', '', true)); |
|
| 840 | - } |
|
| 841 | - |
|
| 842 | - public function testSetLazyValueStringCache(): void { |
|
| 843 | - $config = $this->generateAppConfig(); |
|
| 844 | - $config->setValueString('feed', 'string', 'value-1', true); |
|
| 845 | - $status = $config->statusCache(); |
|
| 846 | - $this->assertSame('value-1', $status['lazyCache']['feed']['string']); |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - public function testSetLazyValueStringDatabase(): void { |
|
| 850 | - $config = $this->generateAppConfig(); |
|
| 851 | - $config->setValueString('feed', 'string', 'value-1', true); |
|
| 852 | - $config->clearCache(); |
|
| 853 | - $this->assertSame('value-1', $config->getValueString('feed', 'string', '', true)); |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - public function testSetLazyValueStringAsNonLazy(): void { |
|
| 857 | - $config = $this->generateAppConfig(); |
|
| 858 | - $config->setValueString('feed', 'string', 'value-1', true); |
|
| 859 | - $config->setValueString('feed', 'string', 'value-1', false); |
|
| 860 | - $this->assertSame('value-1', $config->getValueString('feed', 'string', '')); |
|
| 861 | - } |
|
| 862 | - |
|
| 863 | - public function testSetNonLazyValueStringAsLazy(): void { |
|
| 864 | - $config = $this->generateAppConfig(); |
|
| 865 | - $config->setValueString('feed', 'string', 'value-1', false); |
|
| 866 | - $config->setValueString('feed', 'string', 'value-1', true); |
|
| 867 | - $this->assertSame('value-1', $config->getValueString('feed', 'string', '', true)); |
|
| 868 | - } |
|
| 869 | - |
|
| 870 | - public function testSetSensitiveValueString(): void { |
|
| 871 | - $config = $this->generateAppConfig(); |
|
| 872 | - $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 873 | - $this->assertSame('value-1', $config->getValueString('feed', 'string', '')); |
|
| 874 | - } |
|
| 875 | - |
|
| 876 | - public function testSetSensitiveValueStringCache(): void { |
|
| 877 | - $config = $this->generateAppConfig(); |
|
| 878 | - $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 879 | - $status = $config->statusCache(); |
|
| 880 | - $this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['string']); |
|
| 881 | - } |
|
| 882 | - |
|
| 883 | - public function testSetSensitiveValueStringDatabase(): void { |
|
| 884 | - $config = $this->generateAppConfig(); |
|
| 885 | - $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 886 | - $config->clearCache(); |
|
| 887 | - $this->assertSame('value-1', $config->getValueString('feed', 'string', '')); |
|
| 888 | - } |
|
| 889 | - |
|
| 890 | - public function testSetNonSensitiveValueStringAsSensitive(): void { |
|
| 891 | - $config = $this->generateAppConfig(); |
|
| 892 | - $config->setValueString('feed', 'string', 'value-1', sensitive: false); |
|
| 893 | - $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 894 | - $this->assertSame(true, $config->isSensitive('feed', 'string')); |
|
| 895 | - |
|
| 896 | - $this->assertConfigValueNotEquals('feed', 'string', 'value-1'); |
|
| 897 | - $this->assertConfigValueNotEquals('feed', 'string', 'value-2'); |
|
| 898 | - } |
|
| 899 | - |
|
| 900 | - public function testSetSensitiveValueStringAsNonSensitiveStaysSensitive(): void { |
|
| 901 | - $config = $this->generateAppConfig(); |
|
| 902 | - $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 903 | - $config->setValueString('feed', 'string', 'value-2', sensitive: false); |
|
| 904 | - $this->assertSame(true, $config->isSensitive('feed', 'string')); |
|
| 905 | - |
|
| 906 | - $this->assertConfigValueNotEquals('feed', 'string', 'value-1'); |
|
| 907 | - $this->assertConfigValueNotEquals('feed', 'string', 'value-2'); |
|
| 908 | - } |
|
| 909 | - |
|
| 910 | - public function testSetSensitiveValueStringAsNonSensitiveAreStillUpdated(): void { |
|
| 911 | - $config = $this->generateAppConfig(); |
|
| 912 | - $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 913 | - $config->setValueString('feed', 'string', 'value-2', sensitive: false); |
|
| 914 | - $this->assertSame('value-2', $config->getValueString('feed', 'string', '')); |
|
| 915 | - |
|
| 916 | - $this->assertConfigValueNotEquals('feed', 'string', 'value-1'); |
|
| 917 | - $this->assertConfigValueNotEquals('feed', 'string', 'value-2'); |
|
| 918 | - } |
|
| 919 | - |
|
| 920 | - public function testSetLazyValueInt(): void { |
|
| 921 | - $config = $this->generateAppConfig(); |
|
| 922 | - $config->setValueInt('feed', 'int', 42, true); |
|
| 923 | - $this->assertSame(42, $config->getValueInt('feed', 'int', 0, true)); |
|
| 924 | - } |
|
| 925 | - |
|
| 926 | - public function testSetLazyValueIntCache(): void { |
|
| 927 | - $config = $this->generateAppConfig(); |
|
| 928 | - $config->setValueInt('feed', 'int', 42, true); |
|
| 929 | - $status = $config->statusCache(); |
|
| 930 | - $this->assertSame('42', $status['lazyCache']['feed']['int']); |
|
| 931 | - } |
|
| 932 | - |
|
| 933 | - public function testSetLazyValueIntDatabase(): void { |
|
| 934 | - $config = $this->generateAppConfig(); |
|
| 935 | - $config->setValueInt('feed', 'int', 42, true); |
|
| 936 | - $config->clearCache(); |
|
| 937 | - $this->assertSame(42, $config->getValueInt('feed', 'int', 0, true)); |
|
| 938 | - } |
|
| 939 | - |
|
| 940 | - public function testSetLazyValueIntAsNonLazy(): void { |
|
| 941 | - $config = $this->generateAppConfig(); |
|
| 942 | - $config->setValueInt('feed', 'int', 42, true); |
|
| 943 | - $config->setValueInt('feed', 'int', 42, false); |
|
| 944 | - $this->assertSame(42, $config->getValueInt('feed', 'int', 0)); |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - public function testSetNonLazyValueIntAsLazy(): void { |
|
| 948 | - $config = $this->generateAppConfig(); |
|
| 949 | - $config->setValueInt('feed', 'int', 42, false); |
|
| 950 | - $config->setValueInt('feed', 'int', 42, true); |
|
| 951 | - $this->assertSame(42, $config->getValueInt('feed', 'int', 0, true)); |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - public function testSetSensitiveValueInt(): void { |
|
| 955 | - $config = $this->generateAppConfig(); |
|
| 956 | - $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 957 | - $this->assertSame(42, $config->getValueInt('feed', 'int', 0)); |
|
| 958 | - } |
|
| 959 | - |
|
| 960 | - public function testSetSensitiveValueIntCache(): void { |
|
| 961 | - $config = $this->generateAppConfig(); |
|
| 962 | - $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 963 | - $status = $config->statusCache(); |
|
| 964 | - $this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['int']); |
|
| 965 | - } |
|
| 966 | - |
|
| 967 | - public function testSetSensitiveValueIntDatabase(): void { |
|
| 968 | - $config = $this->generateAppConfig(); |
|
| 969 | - $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 970 | - $config->clearCache(); |
|
| 971 | - $this->assertSame(42, $config->getValueInt('feed', 'int', 0)); |
|
| 972 | - } |
|
| 973 | - |
|
| 974 | - public function testSetNonSensitiveValueIntAsSensitive(): void { |
|
| 975 | - $config = $this->generateAppConfig(); |
|
| 976 | - $config->setValueInt('feed', 'int', 42); |
|
| 977 | - $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 978 | - $this->assertSame(true, $config->isSensitive('feed', 'int')); |
|
| 979 | - } |
|
| 980 | - |
|
| 981 | - public function testSetSensitiveValueIntAsNonSensitiveStaysSensitive(): void { |
|
| 982 | - $config = $this->generateAppConfig(); |
|
| 983 | - $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 984 | - $config->setValueInt('feed', 'int', 17); |
|
| 985 | - $this->assertSame(true, $config->isSensitive('feed', 'int')); |
|
| 986 | - } |
|
| 987 | - |
|
| 988 | - public function testSetSensitiveValueIntAsNonSensitiveAreStillUpdated(): void { |
|
| 989 | - $config = $this->generateAppConfig(); |
|
| 990 | - $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 991 | - $config->setValueInt('feed', 'int', 17); |
|
| 992 | - $this->assertSame(17, $config->getValueInt('feed', 'int', 0)); |
|
| 993 | - } |
|
| 994 | - |
|
| 995 | - public function testSetLazyValueFloat(): void { |
|
| 996 | - $config = $this->generateAppConfig(); |
|
| 997 | - $config->setValueFloat('feed', 'float', 3.14, true); |
|
| 998 | - $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true)); |
|
| 999 | - } |
|
| 1000 | - |
|
| 1001 | - public function testSetLazyValueFloatCache(): void { |
|
| 1002 | - $config = $this->generateAppConfig(); |
|
| 1003 | - $config->setValueFloat('feed', 'float', 3.14, true); |
|
| 1004 | - $status = $config->statusCache(); |
|
| 1005 | - $this->assertSame('3.14', $status['lazyCache']['feed']['float']); |
|
| 1006 | - } |
|
| 1007 | - |
|
| 1008 | - public function testSetLazyValueFloatDatabase(): void { |
|
| 1009 | - $config = $this->generateAppConfig(); |
|
| 1010 | - $config->setValueFloat('feed', 'float', 3.14, true); |
|
| 1011 | - $config->clearCache(); |
|
| 1012 | - $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true)); |
|
| 1013 | - } |
|
| 1014 | - |
|
| 1015 | - public function testSetLazyValueFloatAsNonLazy(): void { |
|
| 1016 | - $config = $this->generateAppConfig(); |
|
| 1017 | - $config->setValueFloat('feed', 'float', 3.14, true); |
|
| 1018 | - $config->setValueFloat('feed', 'float', 3.14, false); |
|
| 1019 | - $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0)); |
|
| 1020 | - } |
|
| 1021 | - |
|
| 1022 | - public function testSetNonLazyValueFloatAsLazy(): void { |
|
| 1023 | - $config = $this->generateAppConfig(); |
|
| 1024 | - $config->setValueFloat('feed', 'float', 3.14, false); |
|
| 1025 | - $config->setValueFloat('feed', 'float', 3.14, true); |
|
| 1026 | - $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true)); |
|
| 1027 | - } |
|
| 1028 | - |
|
| 1029 | - public function testSetSensitiveValueFloat(): void { |
|
| 1030 | - $config = $this->generateAppConfig(); |
|
| 1031 | - $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1032 | - $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0)); |
|
| 1033 | - } |
|
| 1034 | - |
|
| 1035 | - public function testSetSensitiveValueFloatCache(): void { |
|
| 1036 | - $config = $this->generateAppConfig(); |
|
| 1037 | - $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1038 | - $status = $config->statusCache(); |
|
| 1039 | - $this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['float']); |
|
| 1040 | - } |
|
| 1041 | - |
|
| 1042 | - public function testSetSensitiveValueFloatDatabase(): void { |
|
| 1043 | - $config = $this->generateAppConfig(); |
|
| 1044 | - $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1045 | - $config->clearCache(); |
|
| 1046 | - $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0)); |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - public function testSetNonSensitiveValueFloatAsSensitive(): void { |
|
| 1050 | - $config = $this->generateAppConfig(); |
|
| 1051 | - $config->setValueFloat('feed', 'float', 3.14); |
|
| 1052 | - $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1053 | - $this->assertSame(true, $config->isSensitive('feed', 'float')); |
|
| 1054 | - } |
|
| 1055 | - |
|
| 1056 | - public function testSetSensitiveValueFloatAsNonSensitiveStaysSensitive(): void { |
|
| 1057 | - $config = $this->generateAppConfig(); |
|
| 1058 | - $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1059 | - $config->setValueFloat('feed', 'float', 1.23); |
|
| 1060 | - $this->assertSame(true, $config->isSensitive('feed', 'float')); |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - public function testSetSensitiveValueFloatAsNonSensitiveAreStillUpdated(): void { |
|
| 1064 | - $config = $this->generateAppConfig(); |
|
| 1065 | - $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1066 | - $config->setValueFloat('feed', 'float', 1.23); |
|
| 1067 | - $this->assertSame(1.23, $config->getValueFloat('feed', 'float', 0)); |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - public function testSetLazyValueBool(): void { |
|
| 1071 | - $config = $this->generateAppConfig(); |
|
| 1072 | - $config->setValueBool('feed', 'bool', true, true); |
|
| 1073 | - $this->assertSame(true, $config->getValueBool('feed', 'bool', false, true)); |
|
| 1074 | - } |
|
| 1075 | - |
|
| 1076 | - public function testSetLazyValueBoolCache(): void { |
|
| 1077 | - $config = $this->generateAppConfig(); |
|
| 1078 | - $config->setValueBool('feed', 'bool', true, true); |
|
| 1079 | - $status = $config->statusCache(); |
|
| 1080 | - $this->assertSame('1', $status['lazyCache']['feed']['bool']); |
|
| 1081 | - } |
|
| 1082 | - |
|
| 1083 | - public function testSetLazyValueBoolDatabase(): void { |
|
| 1084 | - $config = $this->generateAppConfig(); |
|
| 1085 | - $config->setValueBool('feed', 'bool', true, true); |
|
| 1086 | - $config->clearCache(); |
|
| 1087 | - $this->assertSame(true, $config->getValueBool('feed', 'bool', false, true)); |
|
| 1088 | - } |
|
| 1089 | - |
|
| 1090 | - public function testSetLazyValueBoolAsNonLazy(): void { |
|
| 1091 | - $config = $this->generateAppConfig(); |
|
| 1092 | - $config->setValueBool('feed', 'bool', true, true); |
|
| 1093 | - $config->setValueBool('feed', 'bool', true, false); |
|
| 1094 | - $this->assertSame(true, $config->getValueBool('feed', 'bool', false)); |
|
| 1095 | - } |
|
| 1096 | - |
|
| 1097 | - public function testSetNonLazyValueBoolAsLazy(): void { |
|
| 1098 | - $config = $this->generateAppConfig(); |
|
| 1099 | - $config->setValueBool('feed', 'bool', true, false); |
|
| 1100 | - $config->setValueBool('feed', 'bool', true, true); |
|
| 1101 | - $this->assertSame(true, $config->getValueBool('feed', 'bool', false, true)); |
|
| 1102 | - } |
|
| 1103 | - |
|
| 1104 | - public function testSetLazyValueArray(): void { |
|
| 1105 | - $config = $this->generateAppConfig(); |
|
| 1106 | - $config->setValueArray('feed', 'array', ['test' => 1], true); |
|
| 1107 | - $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true)); |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - public function testSetLazyValueArrayCache(): void { |
|
| 1111 | - $config = $this->generateAppConfig(); |
|
| 1112 | - $config->setValueArray('feed', 'array', ['test' => 1], true); |
|
| 1113 | - $status = $config->statusCache(); |
|
| 1114 | - $this->assertSame('{"test":1}', $status['lazyCache']['feed']['array']); |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - public function testSetLazyValueArrayDatabase(): void { |
|
| 1118 | - $config = $this->generateAppConfig(); |
|
| 1119 | - $config->setValueArray('feed', 'array', ['test' => 1], true); |
|
| 1120 | - $config->clearCache(); |
|
| 1121 | - $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true)); |
|
| 1122 | - } |
|
| 1123 | - |
|
| 1124 | - public function testSetLazyValueArrayAsNonLazy(): void { |
|
| 1125 | - $config = $this->generateAppConfig(); |
|
| 1126 | - $config->setValueArray('feed', 'array', ['test' => 1], true); |
|
| 1127 | - $config->setValueArray('feed', 'array', ['test' => 1], false); |
|
| 1128 | - $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [])); |
|
| 1129 | - } |
|
| 1130 | - |
|
| 1131 | - public function testSetNonLazyValueArrayAsLazy(): void { |
|
| 1132 | - $config = $this->generateAppConfig(); |
|
| 1133 | - $config->setValueArray('feed', 'array', ['test' => 1], false); |
|
| 1134 | - $config->setValueArray('feed', 'array', ['test' => 1], true); |
|
| 1135 | - $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true)); |
|
| 1136 | - } |
|
| 1137 | - |
|
| 1138 | - |
|
| 1139 | - public function testSetSensitiveValueArray(): void { |
|
| 1140 | - $config = $this->generateAppConfig(); |
|
| 1141 | - $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1142 | - $this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('feed', 'array', [])); |
|
| 1143 | - } |
|
| 1144 | - |
|
| 1145 | - public function testSetSensitiveValueArrayCache(): void { |
|
| 1146 | - $config = $this->generateAppConfig(); |
|
| 1147 | - $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1148 | - $status = $config->statusCache(); |
|
| 1149 | - $this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['array']); |
|
| 1150 | - } |
|
| 1151 | - |
|
| 1152 | - public function testSetSensitiveValueArrayDatabase(): void { |
|
| 1153 | - $config = $this->generateAppConfig(); |
|
| 1154 | - $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1155 | - $config->clearCache(); |
|
| 1156 | - $this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('feed', 'array', [])); |
|
| 1157 | - } |
|
| 1158 | - |
|
| 1159 | - public function testSetNonSensitiveValueArrayAsSensitive(): void { |
|
| 1160 | - $config = $this->generateAppConfig(); |
|
| 1161 | - $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 1162 | - $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1163 | - $this->assertSame(true, $config->isSensitive('feed', 'array')); |
|
| 1164 | - } |
|
| 1165 | - |
|
| 1166 | - public function testSetSensitiveValueArrayAsNonSensitiveStaysSensitive(): void { |
|
| 1167 | - $config = $this->generateAppConfig(); |
|
| 1168 | - $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1169 | - $config->setValueArray('feed', 'array', ['test' => 2]); |
|
| 1170 | - $this->assertSame(true, $config->isSensitive('feed', 'array')); |
|
| 1171 | - } |
|
| 1172 | - |
|
| 1173 | - public function testSetSensitiveValueArrayAsNonSensitiveAreStillUpdated(): void { |
|
| 1174 | - $config = $this->generateAppConfig(); |
|
| 1175 | - $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1176 | - $config->setValueArray('feed', 'array', ['test' => 2]); |
|
| 1177 | - $this->assertEqualsCanonicalizing(['test' => 2], $config->getValueArray('feed', 'array', [])); |
|
| 1178 | - } |
|
| 1179 | - |
|
| 1180 | - public function testUpdateNotSensitiveToSensitive(): void { |
|
| 1181 | - $config = $this->generateAppConfig(); |
|
| 1182 | - $config->updateSensitive('non-sensitive-app', 'lazy-key', true); |
|
| 1183 | - $this->assertSame(true, $config->isSensitive('non-sensitive-app', 'lazy-key', true)); |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - public function testUpdateSensitiveToNotSensitive(): void { |
|
| 1187 | - $config = $this->generateAppConfig(); |
|
| 1188 | - $config->updateSensitive('sensitive-app', 'lazy-key', false); |
|
| 1189 | - $this->assertSame(false, $config->isSensitive('sensitive-app', 'lazy-key', true)); |
|
| 1190 | - } |
|
| 1191 | - |
|
| 1192 | - public function testUpdateSensitiveToSensitiveReturnsFalse(): void { |
|
| 1193 | - $config = $this->generateAppConfig(); |
|
| 1194 | - $this->assertSame(false, $config->updateSensitive('sensitive-app', 'lazy-key', true)); |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - public function testUpdateNotSensitiveToNotSensitiveReturnsFalse(): void { |
|
| 1198 | - $config = $this->generateAppConfig(); |
|
| 1199 | - $this->assertSame(false, $config->updateSensitive('non-sensitive-app', 'lazy-key', false)); |
|
| 1200 | - } |
|
| 1201 | - |
|
| 1202 | - public function testUpdateSensitiveOnUnknownKeyReturnsFalse(): void { |
|
| 1203 | - $config = $this->generateAppConfig(); |
|
| 1204 | - $this->assertSame(false, $config->updateSensitive('non-sensitive-app', 'unknown-key', true)); |
|
| 1205 | - } |
|
| 1206 | - |
|
| 1207 | - public function testUpdateNotLazyToLazy(): void { |
|
| 1208 | - $config = $this->generateAppConfig(); |
|
| 1209 | - $config->updateLazy('non-sensitive-app', 'non-lazy-key', true); |
|
| 1210 | - $this->assertSame(true, $config->isLazy('non-sensitive-app', 'non-lazy-key')); |
|
| 1211 | - } |
|
| 1212 | - |
|
| 1213 | - public function testUpdateLazyToNotLazy(): void { |
|
| 1214 | - $config = $this->generateAppConfig(); |
|
| 1215 | - $config->updateLazy('non-sensitive-app', 'lazy-key', false); |
|
| 1216 | - $this->assertSame(false, $config->isLazy('non-sensitive-app', 'lazy-key')); |
|
| 1217 | - } |
|
| 1218 | - |
|
| 1219 | - public function testUpdateLazyToLazyReturnsFalse(): void { |
|
| 1220 | - $config = $this->generateAppConfig(); |
|
| 1221 | - $this->assertSame(false, $config->updateLazy('non-sensitive-app', 'lazy-key', true)); |
|
| 1222 | - } |
|
| 1223 | - |
|
| 1224 | - public function testUpdateNotLazyToNotLazyReturnsFalse(): void { |
|
| 1225 | - $config = $this->generateAppConfig(); |
|
| 1226 | - $this->assertSame(false, $config->updateLazy('non-sensitive-app', 'non-lazy-key', false)); |
|
| 1227 | - } |
|
| 1228 | - |
|
| 1229 | - public function testUpdateLazyOnUnknownKeyReturnsFalse(): void { |
|
| 1230 | - $config = $this->generateAppConfig(); |
|
| 1231 | - $this->assertSame(false, $config->updateLazy('non-sensitive-app', 'unknown-key', true)); |
|
| 1232 | - } |
|
| 1233 | - |
|
| 1234 | - public function testGetDetails(): void { |
|
| 1235 | - $config = $this->generateAppConfig(); |
|
| 1236 | - $this->assertEquals( |
|
| 1237 | - [ |
|
| 1238 | - 'app' => 'non-sensitive-app', |
|
| 1239 | - 'key' => 'lazy-key', |
|
| 1240 | - 'value' => 'value', |
|
| 1241 | - 'type' => 4, |
|
| 1242 | - 'lazy' => true, |
|
| 1243 | - 'typeString' => 'string', |
|
| 1244 | - 'sensitive' => false, |
|
| 1245 | - ], |
|
| 1246 | - $config->getDetails('non-sensitive-app', 'lazy-key') |
|
| 1247 | - ); |
|
| 1248 | - } |
|
| 1249 | - |
|
| 1250 | - public function testGetDetailsSensitive(): void { |
|
| 1251 | - $config = $this->generateAppConfig(); |
|
| 1252 | - $this->assertEquals( |
|
| 1253 | - [ |
|
| 1254 | - 'app' => 'sensitive-app', |
|
| 1255 | - 'key' => 'lazy-key', |
|
| 1256 | - 'value' => 'value', |
|
| 1257 | - 'type' => 4, |
|
| 1258 | - 'lazy' => true, |
|
| 1259 | - 'typeString' => 'string', |
|
| 1260 | - 'sensitive' => true, |
|
| 1261 | - ], |
|
| 1262 | - $config->getDetails('sensitive-app', 'lazy-key') |
|
| 1263 | - ); |
|
| 1264 | - } |
|
| 1265 | - |
|
| 1266 | - public function testGetDetailsInt(): void { |
|
| 1267 | - $config = $this->generateAppConfig(); |
|
| 1268 | - $this->assertEquals( |
|
| 1269 | - [ |
|
| 1270 | - 'app' => 'typed', |
|
| 1271 | - 'key' => 'int', |
|
| 1272 | - 'value' => '42', |
|
| 1273 | - 'type' => 8, |
|
| 1274 | - 'lazy' => false, |
|
| 1275 | - 'typeString' => 'integer', |
|
| 1276 | - 'sensitive' => false |
|
| 1277 | - ], |
|
| 1278 | - $config->getDetails('typed', 'int') |
|
| 1279 | - ); |
|
| 1280 | - } |
|
| 1281 | - |
|
| 1282 | - public function testGetDetailsFloat(): void { |
|
| 1283 | - $config = $this->generateAppConfig(); |
|
| 1284 | - $this->assertEquals( |
|
| 1285 | - [ |
|
| 1286 | - 'app' => 'typed', |
|
| 1287 | - 'key' => 'float', |
|
| 1288 | - 'value' => '3.14', |
|
| 1289 | - 'type' => 16, |
|
| 1290 | - 'lazy' => false, |
|
| 1291 | - 'typeString' => 'float', |
|
| 1292 | - 'sensitive' => false |
|
| 1293 | - ], |
|
| 1294 | - $config->getDetails('typed', 'float') |
|
| 1295 | - ); |
|
| 1296 | - } |
|
| 1297 | - |
|
| 1298 | - public function testGetDetailsBool(): void { |
|
| 1299 | - $config = $this->generateAppConfig(); |
|
| 1300 | - $this->assertEquals( |
|
| 1301 | - [ |
|
| 1302 | - 'app' => 'typed', |
|
| 1303 | - 'key' => 'bool', |
|
| 1304 | - 'value' => '1', |
|
| 1305 | - 'type' => 32, |
|
| 1306 | - 'lazy' => false, |
|
| 1307 | - 'typeString' => 'boolean', |
|
| 1308 | - 'sensitive' => false |
|
| 1309 | - ], |
|
| 1310 | - $config->getDetails('typed', 'bool') |
|
| 1311 | - ); |
|
| 1312 | - } |
|
| 1313 | - |
|
| 1314 | - public function testGetDetailsArray(): void { |
|
| 1315 | - $config = $this->generateAppConfig(); |
|
| 1316 | - $this->assertEquals( |
|
| 1317 | - [ |
|
| 1318 | - 'app' => 'typed', |
|
| 1319 | - 'key' => 'array', |
|
| 1320 | - 'value' => '{"test": 1}', |
|
| 1321 | - 'type' => 64, |
|
| 1322 | - 'lazy' => false, |
|
| 1323 | - 'typeString' => 'array', |
|
| 1324 | - 'sensitive' => false |
|
| 1325 | - ], |
|
| 1326 | - $config->getDetails('typed', 'array') |
|
| 1327 | - ); |
|
| 1328 | - } |
|
| 1329 | - |
|
| 1330 | - public function testDeleteKey(): void { |
|
| 1331 | - $config = $this->generateAppConfig(); |
|
| 1332 | - $config->deleteKey('anotherapp', 'key'); |
|
| 1333 | - $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default')); |
|
| 1334 | - } |
|
| 1335 | - |
|
| 1336 | - public function testDeleteKeyCache(): void { |
|
| 1337 | - $config = $this->generateAppConfig(); |
|
| 1338 | - $config->deleteKey('anotherapp', 'key'); |
|
| 1339 | - $status = $config->statusCache(); |
|
| 1340 | - $this->assertEqualsCanonicalizing(['enabled' => 'no', 'installed_version' => '3.2.1'], $status['fastCache']['anotherapp']); |
|
| 1341 | - } |
|
| 1342 | - |
|
| 1343 | - public function testDeleteKeyDatabase(): void { |
|
| 1344 | - $config = $this->generateAppConfig(); |
|
| 1345 | - $config->deleteKey('anotherapp', 'key'); |
|
| 1346 | - $config->clearCache(); |
|
| 1347 | - $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default')); |
|
| 1348 | - } |
|
| 1349 | - |
|
| 1350 | - public function testDeleteApp(): void { |
|
| 1351 | - $config = $this->generateAppConfig(); |
|
| 1352 | - $config->deleteApp('anotherapp'); |
|
| 1353 | - $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default')); |
|
| 1354 | - $this->assertSame('default', $config->getValueString('anotherapp', 'enabled', 'default')); |
|
| 1355 | - } |
|
| 1356 | - |
|
| 1357 | - public function testDeleteAppCache(): void { |
|
| 1358 | - $config = $this->generateAppConfig(); |
|
| 1359 | - $status = $config->statusCache(); |
|
| 1360 | - $this->assertSame(true, isset($status['fastCache']['anotherapp'])); |
|
| 1361 | - $config->deleteApp('anotherapp'); |
|
| 1362 | - $status = $config->statusCache(); |
|
| 1363 | - $this->assertSame(false, isset($status['fastCache']['anotherapp'])); |
|
| 1364 | - } |
|
| 1365 | - |
|
| 1366 | - public function testDeleteAppDatabase(): void { |
|
| 1367 | - $config = $this->generateAppConfig(); |
|
| 1368 | - $config->deleteApp('anotherapp'); |
|
| 1369 | - $config->clearCache(); |
|
| 1370 | - $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default')); |
|
| 1371 | - $this->assertSame('default', $config->getValueString('anotherapp', 'enabled', 'default')); |
|
| 1372 | - } |
|
| 1373 | - |
|
| 1374 | - public function testClearCache(): void { |
|
| 1375 | - $config = $this->generateAppConfig(); |
|
| 1376 | - $config->setValueString('feed', 'string', '123454'); |
|
| 1377 | - $config->clearCache(); |
|
| 1378 | - $status = $config->statusCache(); |
|
| 1379 | - $this->assertSame([], $status['fastCache']); |
|
| 1380 | - } |
|
| 1381 | - |
|
| 1382 | - public function testSensitiveValuesAreEncrypted(): void { |
|
| 1383 | - $key = self::getUniqueID('secret'); |
|
| 1384 | - |
|
| 1385 | - $appConfig = $this->generateAppConfig(); |
|
| 1386 | - $secret = md5((string)time()); |
|
| 1387 | - $appConfig->setValueString('testapp', $key, $secret, sensitive: true); |
|
| 1388 | - |
|
| 1389 | - $this->assertConfigValueNotEquals('testapp', $key, $secret); |
|
| 1390 | - |
|
| 1391 | - // Can get in same run |
|
| 1392 | - $actualSecret = $appConfig->getValueString('testapp', $key); |
|
| 1393 | - $this->assertEquals($secret, $actualSecret); |
|
| 1394 | - |
|
| 1395 | - // Can get freshly decrypted from DB |
|
| 1396 | - $newAppConfig = $this->generateAppConfig(); |
|
| 1397 | - $actualSecret = $newAppConfig->getValueString('testapp', $key); |
|
| 1398 | - $this->assertEquals($secret, $actualSecret); |
|
| 1399 | - } |
|
| 1400 | - |
|
| 1401 | - public function testMigratingNonSensitiveValueToSensitiveWithSetValue(): void { |
|
| 1402 | - $key = self::getUniqueID('secret'); |
|
| 1403 | - $appConfig = $this->generateAppConfig(); |
|
| 1404 | - $secret = sha1((string)time()); |
|
| 1405 | - |
|
| 1406 | - // Unencrypted |
|
| 1407 | - $appConfig->setValueString('testapp', $key, $secret); |
|
| 1408 | - $this->assertConfigKey('testapp', $key, $secret); |
|
| 1409 | - |
|
| 1410 | - // Can get freshly decrypted from DB |
|
| 1411 | - $newAppConfig = $this->generateAppConfig(); |
|
| 1412 | - $actualSecret = $newAppConfig->getValueString('testapp', $key); |
|
| 1413 | - $this->assertEquals($secret, $actualSecret); |
|
| 1414 | - |
|
| 1415 | - // Encrypting on change |
|
| 1416 | - $appConfig->setValueString('testapp', $key, $secret, sensitive: true); |
|
| 1417 | - $this->assertConfigValueNotEquals('testapp', $key, $secret); |
|
| 1418 | - |
|
| 1419 | - // Can get in same run |
|
| 1420 | - $actualSecret = $appConfig->getValueString('testapp', $key); |
|
| 1421 | - $this->assertEquals($secret, $actualSecret); |
|
| 1422 | - |
|
| 1423 | - // Can get freshly decrypted from DB |
|
| 1424 | - $newAppConfig = $this->generateAppConfig(); |
|
| 1425 | - $actualSecret = $newAppConfig->getValueString('testapp', $key); |
|
| 1426 | - $this->assertEquals($secret, $actualSecret); |
|
| 1427 | - } |
|
| 1428 | - |
|
| 1429 | - public function testUpdateSensitiveValueToNonSensitiveWithUpdateSensitive(): void { |
|
| 1430 | - $key = self::getUniqueID('secret'); |
|
| 1431 | - $appConfig = $this->generateAppConfig(); |
|
| 1432 | - $secret = sha1((string)time()); |
|
| 1433 | - |
|
| 1434 | - // Encrypted |
|
| 1435 | - $appConfig->setValueString('testapp', $key, $secret, sensitive: true); |
|
| 1436 | - $this->assertConfigValueNotEquals('testapp', $key, $secret); |
|
| 1437 | - |
|
| 1438 | - // Migrate to non-sensitive / non-encrypted |
|
| 1439 | - $appConfig->updateSensitive('testapp', $key, false); |
|
| 1440 | - $this->assertConfigKey('testapp', $key, $secret); |
|
| 1441 | - } |
|
| 1442 | - |
|
| 1443 | - public function testUpdateNonSensitiveValueToSensitiveWithUpdateSensitive(): void { |
|
| 1444 | - $key = self::getUniqueID('secret'); |
|
| 1445 | - $appConfig = $this->generateAppConfig(); |
|
| 1446 | - $secret = sha1((string)time()); |
|
| 1447 | - |
|
| 1448 | - // Unencrypted |
|
| 1449 | - $appConfig->setValueString('testapp', $key, $secret); |
|
| 1450 | - $this->assertConfigKey('testapp', $key, $secret); |
|
| 1451 | - |
|
| 1452 | - // Migrate to sensitive / encrypted |
|
| 1453 | - $appConfig->updateSensitive('testapp', $key, true); |
|
| 1454 | - $this->assertConfigValueNotEquals('testapp', $key, $secret); |
|
| 1455 | - } |
|
| 1456 | - |
|
| 1457 | - protected function loadConfigValueFromDatabase(string $app, string $key): string|false { |
|
| 1458 | - $sql = $this->connection->getQueryBuilder(); |
|
| 1459 | - $sql->select('configvalue') |
|
| 1460 | - ->from('appconfig') |
|
| 1461 | - ->where($sql->expr()->eq('appid', $sql->createParameter('appid'))) |
|
| 1462 | - ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey'))) |
|
| 1463 | - ->setParameter('appid', $app) |
|
| 1464 | - ->setParameter('configkey', $key); |
|
| 1465 | - $query = $sql->executeQuery(); |
|
| 1466 | - $actual = $query->fetchOne(); |
|
| 1467 | - $query->closeCursor(); |
|
| 1468 | - |
|
| 1469 | - return $actual; |
|
| 1470 | - } |
|
| 1471 | - |
|
| 1472 | - protected function assertConfigKey(string $app, string $key, string|false $expected): void { |
|
| 1473 | - $this->assertEquals($expected, $this->loadConfigValueFromDatabase($app, $key)); |
|
| 1474 | - } |
|
| 1475 | - |
|
| 1476 | - protected function assertConfigValueNotEquals(string $app, string $key, string|false $expected): void { |
|
| 1477 | - $this->assertNotEquals($expected, $this->loadConfigValueFromDatabase($app, $key)); |
|
| 1478 | - } |
|
| 29 | + protected IAppConfig $appConfig; |
|
| 30 | + protected IDBConnection $connection; |
|
| 31 | + private IConfig $config; |
|
| 32 | + private LoggerInterface $logger; |
|
| 33 | + private ICrypto $crypto; |
|
| 34 | + |
|
| 35 | + private array $originalConfig; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var array<string, array<string, array<string, string, int, bool, bool>>> |
|
| 39 | + * [appId => [configKey, configValue, valueType, lazy, sensitive]] |
|
| 40 | + */ |
|
| 41 | + private static array $baseStruct |
|
| 42 | + = [ |
|
| 43 | + 'testapp' => [ |
|
| 44 | + 'enabled' => ['enabled', 'yes'], |
|
| 45 | + 'installed_version' => ['installed_version', '1.2.3'], |
|
| 46 | + 'depends_on' => ['depends_on', 'someapp'], |
|
| 47 | + 'deletethis' => ['deletethis', 'deletethis'], |
|
| 48 | + 'key' => ['key', 'value'] |
|
| 49 | + ], |
|
| 50 | + 'someapp' => [ |
|
| 51 | + 'key' => ['key', 'value'], |
|
| 52 | + 'otherkey' => ['otherkey', 'othervalue'] |
|
| 53 | + ], |
|
| 54 | + '123456' => [ |
|
| 55 | + 'enabled' => ['enabled', 'yes'], |
|
| 56 | + 'key' => ['key', 'value'] |
|
| 57 | + ], |
|
| 58 | + 'anotherapp' => [ |
|
| 59 | + 'enabled' => ['enabled', 'no'], |
|
| 60 | + 'installed_version' => ['installed_version', '3.2.1'], |
|
| 61 | + 'key' => ['key', 'value'] |
|
| 62 | + ], |
|
| 63 | + 'non-sensitive-app' => [ |
|
| 64 | + 'lazy-key' => ['lazy-key', 'value', IAppConfig::VALUE_STRING, true, false], |
|
| 65 | + 'non-lazy-key' => ['non-lazy-key', 'value', IAppConfig::VALUE_STRING, false, false], |
|
| 66 | + ], |
|
| 67 | + 'sensitive-app' => [ |
|
| 68 | + 'lazy-key' => ['lazy-key', 'value', IAppConfig::VALUE_STRING, true, true], |
|
| 69 | + 'non-lazy-key' => ['non-lazy-key', 'value', IAppConfig::VALUE_STRING, false, true], |
|
| 70 | + ], |
|
| 71 | + 'only-lazy' => [ |
|
| 72 | + 'lazy-key' => ['lazy-key', 'value', IAppConfig::VALUE_STRING, true] |
|
| 73 | + ], |
|
| 74 | + 'typed' => [ |
|
| 75 | + 'mixed' => ['mixed', 'mix', IAppConfig::VALUE_MIXED], |
|
| 76 | + 'string' => ['string', 'value', IAppConfig::VALUE_STRING], |
|
| 77 | + 'int' => ['int', '42', IAppConfig::VALUE_INT], |
|
| 78 | + 'float' => ['float', '3.14', IAppConfig::VALUE_FLOAT], |
|
| 79 | + 'bool' => ['bool', '1', IAppConfig::VALUE_BOOL], |
|
| 80 | + 'array' => ['array', '{"test": 1}', IAppConfig::VALUE_ARRAY], |
|
| 81 | + ], |
|
| 82 | + 'prefix-app' => [ |
|
| 83 | + 'key1' => ['key1', 'value'], |
|
| 84 | + 'prefix1' => ['prefix1', 'value'], |
|
| 85 | + 'prefix-2' => ['prefix-2', 'value'], |
|
| 86 | + 'key-2' => ['key-2', 'value'], |
|
| 87 | + ] |
|
| 88 | + ]; |
|
| 89 | + |
|
| 90 | + protected function setUp(): void { |
|
| 91 | + parent::setUp(); |
|
| 92 | + |
|
| 93 | + $this->connection = Server::get(IDBConnection::class); |
|
| 94 | + $this->config = Server::get(IConfig::class); |
|
| 95 | + $this->logger = Server::get(LoggerInterface::class); |
|
| 96 | + $this->crypto = Server::get(ICrypto::class); |
|
| 97 | + |
|
| 98 | + // storing current config and emptying the data table |
|
| 99 | + $sql = $this->connection->getQueryBuilder(); |
|
| 100 | + $sql->select('*') |
|
| 101 | + ->from('appconfig'); |
|
| 102 | + $result = $sql->executeQuery(); |
|
| 103 | + $this->originalConfig = $result->fetchAll(); |
|
| 104 | + $result->closeCursor(); |
|
| 105 | + |
|
| 106 | + $sql = $this->connection->getQueryBuilder(); |
|
| 107 | + $sql->delete('appconfig'); |
|
| 108 | + $sql->executeStatement(); |
|
| 109 | + |
|
| 110 | + $sql = $this->connection->getQueryBuilder(); |
|
| 111 | + $sql->insert('appconfig') |
|
| 112 | + ->values( |
|
| 113 | + [ |
|
| 114 | + 'appid' => $sql->createParameter('appid'), |
|
| 115 | + 'configkey' => $sql->createParameter('configkey'), |
|
| 116 | + 'configvalue' => $sql->createParameter('configvalue'), |
|
| 117 | + 'type' => $sql->createParameter('type'), |
|
| 118 | + 'lazy' => $sql->createParameter('lazy') |
|
| 119 | + ] |
|
| 120 | + ); |
|
| 121 | + |
|
| 122 | + foreach (self::$baseStruct as $appId => $appData) { |
|
| 123 | + foreach ($appData as $key => $row) { |
|
| 124 | + $value = $row[1]; |
|
| 125 | + $type = $row[2] ?? IAppConfig::VALUE_MIXED; |
|
| 126 | + if (($row[4] ?? false) === true) { |
|
| 127 | + $type |= IAppConfig::VALUE_SENSITIVE; |
|
| 128 | + $value = self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX') . $this->crypto->encrypt($value); |
|
| 129 | + self::$baseStruct[$appId][$key]['encrypted'] = $value; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + $sql->setParameters( |
|
| 133 | + [ |
|
| 134 | + 'appid' => $appId, |
|
| 135 | + 'configkey' => $row[0], |
|
| 136 | + 'configvalue' => $value, |
|
| 137 | + 'type' => $type, |
|
| 138 | + 'lazy' => (($row[3] ?? false) === true) ? 1 : 0 |
|
| 139 | + ] |
|
| 140 | + )->executeStatement(); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + } |
|
| 144 | + |
|
| 145 | + protected function tearDown(): void { |
|
| 146 | + $sql = $this->connection->getQueryBuilder(); |
|
| 147 | + $sql->delete('appconfig'); |
|
| 148 | + $sql->executeStatement(); |
|
| 149 | + |
|
| 150 | + $sql = $this->connection->getQueryBuilder(); |
|
| 151 | + $sql->insert('appconfig') |
|
| 152 | + ->values( |
|
| 153 | + [ |
|
| 154 | + 'appid' => $sql->createParameter('appid'), |
|
| 155 | + 'configkey' => $sql->createParameter('configkey'), |
|
| 156 | + 'configvalue' => $sql->createParameter('configvalue'), |
|
| 157 | + 'lazy' => $sql->createParameter('lazy'), |
|
| 158 | + 'type' => $sql->createParameter('type'), |
|
| 159 | + ] |
|
| 160 | + ); |
|
| 161 | + |
|
| 162 | + foreach ($this->originalConfig as $key => $configs) { |
|
| 163 | + $sql->setParameter('appid', $configs['appid']) |
|
| 164 | + ->setParameter('configkey', $configs['configkey']) |
|
| 165 | + ->setParameter('configvalue', $configs['configvalue']) |
|
| 166 | + ->setParameter('lazy', ($configs['lazy'] === '1') ? '1' : '0') |
|
| 167 | + ->setParameter('type', $configs['type']); |
|
| 168 | + $sql->executeStatement(); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + // $this->restoreService(AppConfig::class); |
|
| 172 | + parent::tearDown(); |
|
| 173 | + } |
|
| 174 | + |
|
| 175 | + /** |
|
| 176 | + * @param bool $preLoading TRUE will preload the 'fast' cache, which is the normal behavior of usual |
|
| 177 | + * IAppConfig |
|
| 178 | + * |
|
| 179 | + * @return IAppConfig |
|
| 180 | + */ |
|
| 181 | + private function generateAppConfig(bool $preLoading = true): IAppConfig { |
|
| 182 | + /** @var AppConfig $config */ |
|
| 183 | + $config = new AppConfig( |
|
| 184 | + $this->connection, |
|
| 185 | + $this->config, |
|
| 186 | + $this->logger, |
|
| 187 | + $this->crypto, |
|
| 188 | + ); |
|
| 189 | + $msg = ' generateAppConfig() failed to confirm cache status'; |
|
| 190 | + |
|
| 191 | + // confirm cache status |
|
| 192 | + $status = $config->statusCache(); |
|
| 193 | + $this->assertSame(false, $status['fastLoaded'], $msg); |
|
| 194 | + $this->assertSame(false, $status['lazyLoaded'], $msg); |
|
| 195 | + $this->assertSame([], $status['fastCache'], $msg); |
|
| 196 | + $this->assertSame([], $status['lazyCache'], $msg); |
|
| 197 | + if ($preLoading) { |
|
| 198 | + // simple way to initiate the load of non-lazy config values in cache |
|
| 199 | + $config->getValueString('core', 'preload', ''); |
|
| 200 | + |
|
| 201 | + // confirm cache status |
|
| 202 | + $status = $config->statusCache(); |
|
| 203 | + $this->assertSame(true, $status['fastLoaded'], $msg); |
|
| 204 | + $this->assertSame(false, $status['lazyLoaded'], $msg); |
|
| 205 | + |
|
| 206 | + $apps = array_values(array_diff(array_keys(self::$baseStruct), ['only-lazy'])); |
|
| 207 | + $this->assertEqualsCanonicalizing($apps, array_keys($status['fastCache']), $msg); |
|
| 208 | + $this->assertSame([], array_keys($status['lazyCache']), $msg); |
|
| 209 | + } |
|
| 210 | + |
|
| 211 | + return $config; |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + public function testGetApps(): void { |
|
| 215 | + $config = $this->generateAppConfig(false); |
|
| 216 | + |
|
| 217 | + $this->assertEqualsCanonicalizing(array_keys(self::$baseStruct), $config->getApps()); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + public function testGetAppInstalledVersions(): void { |
|
| 221 | + $config = $this->generateAppConfig(false); |
|
| 222 | + |
|
| 223 | + $this->assertEquals( |
|
| 224 | + ['testapp' => '1.2.3', 'anotherapp' => '3.2.1'], |
|
| 225 | + $config->getAppInstalledVersions(false) |
|
| 226 | + ); |
|
| 227 | + $this->assertEquals( |
|
| 228 | + ['testapp' => '1.2.3'], |
|
| 229 | + $config->getAppInstalledVersions(true) |
|
| 230 | + ); |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + /** |
|
| 234 | + * returns list of app and their keys |
|
| 235 | + * |
|
| 236 | + * @return array<string, string[]> ['appId' => ['key1', 'key2', ]] |
|
| 237 | + * @see testGetKeys |
|
| 238 | + */ |
|
| 239 | + public static function providerGetAppKeys(): array { |
|
| 240 | + $appKeys = []; |
|
| 241 | + foreach (self::$baseStruct as $appId => $appData) { |
|
| 242 | + $keys = []; |
|
| 243 | + foreach ($appData as $row) { |
|
| 244 | + $keys[] = $row[0]; |
|
| 245 | + } |
|
| 246 | + $appKeys[] = [(string)$appId, $keys]; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + return $appKeys; |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + /** |
|
| 253 | + * returns list of config keys |
|
| 254 | + * |
|
| 255 | + * @return array<string, string, string, int, bool, bool> [appId, key, value, type, lazy, sensitive] |
|
| 256 | + * @see testIsSensitive |
|
| 257 | + * @see testIsLazy |
|
| 258 | + * @see testGetKeys |
|
| 259 | + */ |
|
| 260 | + public static function providerGetKeys(): array { |
|
| 261 | + $appKeys = []; |
|
| 262 | + foreach (self::$baseStruct as $appId => $appData) { |
|
| 263 | + foreach ($appData as $row) { |
|
| 264 | + $appKeys[] = [ |
|
| 265 | + (string)$appId, $row[0], $row[1], $row[2] ?? IAppConfig::VALUE_MIXED, $row[3] ?? false, |
|
| 266 | + $row[4] ?? false |
|
| 267 | + ]; |
|
| 268 | + } |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + return $appKeys; |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + /** |
|
| 275 | + * |
|
| 276 | + * @param string $appId |
|
| 277 | + * @param array $expectedKeys |
|
| 278 | + */ |
|
| 279 | + #[\PHPUnit\Framework\Attributes\DataProvider('providerGetAppKeys')] |
|
| 280 | + public function testGetKeys(string $appId, array $expectedKeys): void { |
|
| 281 | + $config = $this->generateAppConfig(); |
|
| 282 | + $this->assertEqualsCanonicalizing($expectedKeys, $config->getKeys($appId)); |
|
| 283 | + } |
|
| 284 | + |
|
| 285 | + public function testGetKeysOnUnknownAppShouldReturnsEmptyArray(): void { |
|
| 286 | + $config = $this->generateAppConfig(); |
|
| 287 | + $this->assertEqualsCanonicalizing([], $config->getKeys('unknown-app')); |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * |
|
| 292 | + * @param string $appId |
|
| 293 | + * @param string $configKey |
|
| 294 | + * @param string $value |
|
| 295 | + * @param bool $lazy |
|
| 296 | + */ |
|
| 297 | + #[\PHPUnit\Framework\Attributes\DataProvider('providerGetKeys')] |
|
| 298 | + public function testHasKey(string $appId, string $configKey, string $value, int $type, bool $lazy): void { |
|
| 299 | + $config = $this->generateAppConfig(); |
|
| 300 | + $this->assertEquals(true, $config->hasKey($appId, $configKey, $lazy)); |
|
| 301 | + } |
|
| 302 | + |
|
| 303 | + public function testHasKeyOnNonExistentKeyReturnsFalse(): void { |
|
| 304 | + $config = $this->generateAppConfig(); |
|
| 305 | + $this->assertEquals(false, $config->hasKey(array_keys(self::$baseStruct)[0], 'inexistant-key')); |
|
| 306 | + } |
|
| 307 | + |
|
| 308 | + public function testHasKeyOnUnknownAppReturnsFalse(): void { |
|
| 309 | + $config = $this->generateAppConfig(); |
|
| 310 | + $this->assertEquals(false, $config->hasKey('inexistant-app', 'inexistant-key')); |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + public function testHasKeyOnMistypedAsLazyReturnsFalse(): void { |
|
| 314 | + $config = $this->generateAppConfig(); |
|
| 315 | + $this->assertSame(false, $config->hasKey('non-sensitive-app', 'non-lazy-key', true)); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + public function testHasKeyOnMistypeAsNonLazyReturnsFalse(): void { |
|
| 319 | + $config = $this->generateAppConfig(); |
|
| 320 | + $this->assertSame(false, $config->hasKey('non-sensitive-app', 'lazy-key', false)); |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + public function testHasKeyOnMistypeAsNonLazyReturnsTrueWithLazyArgumentIsNull(): void { |
|
| 324 | + $config = $this->generateAppConfig(); |
|
| 325 | + $this->assertSame(true, $config->hasKey('non-sensitive-app', 'lazy-key', null)); |
|
| 326 | + } |
|
| 327 | + |
|
| 328 | + #[\PHPUnit\Framework\Attributes\DataProvider('providerGetKeys')] |
|
| 329 | + public function testIsSensitive( |
|
| 330 | + string $appId, string $configKey, string $configValue, int $type, bool $lazy, bool $sensitive, |
|
| 331 | + ): void { |
|
| 332 | + $config = $this->generateAppConfig(); |
|
| 333 | + $this->assertEquals($sensitive, $config->isSensitive($appId, $configKey, $lazy)); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + public function testIsSensitiveOnNonExistentKeyThrowsException(): void { |
|
| 337 | + $config = $this->generateAppConfig(); |
|
| 338 | + $this->expectException(AppConfigUnknownKeyException::class); |
|
| 339 | + $config->isSensitive(array_keys(self::$baseStruct)[0], 'inexistant-key'); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + public function testIsSensitiveOnUnknownAppThrowsException(): void { |
|
| 343 | + $config = $this->generateAppConfig(); |
|
| 344 | + $this->expectException(AppConfigUnknownKeyException::class); |
|
| 345 | + $config->isSensitive('unknown-app', 'inexistant-key'); |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + public function testIsSensitiveOnSensitiveMistypedAsLazy(): void { |
|
| 349 | + $config = $this->generateAppConfig(); |
|
| 350 | + $this->assertSame(true, $config->isSensitive('sensitive-app', 'non-lazy-key', true)); |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + public function testIsSensitiveOnNonSensitiveMistypedAsLazy(): void { |
|
| 354 | + $config = $this->generateAppConfig(); |
|
| 355 | + $this->assertSame(false, $config->isSensitive('non-sensitive-app', 'non-lazy-key', true)); |
|
| 356 | + } |
|
| 357 | + |
|
| 358 | + public function testIsSensitiveOnSensitiveMistypedAsNonLazyThrowsException(): void { |
|
| 359 | + $config = $this->generateAppConfig(); |
|
| 360 | + $this->expectException(AppConfigUnknownKeyException::class); |
|
| 361 | + $config->isSensitive('sensitive-app', 'lazy-key', false); |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + public function testIsSensitiveOnNonSensitiveMistypedAsNonLazyThrowsException(): void { |
|
| 365 | + $config = $this->generateAppConfig(); |
|
| 366 | + $this->expectException(AppConfigUnknownKeyException::class); |
|
| 367 | + $config->isSensitive('non-sensitive-app', 'lazy-key', false); |
|
| 368 | + } |
|
| 369 | + |
|
| 370 | + #[\PHPUnit\Framework\Attributes\DataProvider('providerGetKeys')] |
|
| 371 | + public function testIsLazy(string $appId, string $configKey, string $configValue, int $type, bool $lazy, |
|
| 372 | + ): void { |
|
| 373 | + $config = $this->generateAppConfig(); |
|
| 374 | + $this->assertEquals($lazy, $config->isLazy($appId, $configKey)); |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + public function testIsLazyOnNonExistentKeyThrowsException(): void { |
|
| 378 | + $config = $this->generateAppConfig(); |
|
| 379 | + $this->expectException(AppConfigUnknownKeyException::class); |
|
| 380 | + $config->isLazy(array_keys(self::$baseStruct)[0], 'inexistant-key'); |
|
| 381 | + } |
|
| 382 | + |
|
| 383 | + public function testIsLazyOnUnknownAppThrowsException(): void { |
|
| 384 | + $config = $this->generateAppConfig(); |
|
| 385 | + $this->expectException(AppConfigUnknownKeyException::class); |
|
| 386 | + $config->isLazy('unknown-app', 'inexistant-key'); |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + public function testGetAllValues(): void { |
|
| 390 | + $config = $this->generateAppConfig(); |
|
| 391 | + $this->assertEquals( |
|
| 392 | + [ |
|
| 393 | + 'array' => ['test' => 1], |
|
| 394 | + 'bool' => true, |
|
| 395 | + 'float' => 3.14, |
|
| 396 | + 'int' => 42, |
|
| 397 | + 'mixed' => 'mix', |
|
| 398 | + 'string' => 'value', |
|
| 399 | + ], |
|
| 400 | + $config->getAllValues('typed') |
|
| 401 | + ); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + public function testGetAllValuesWithEmptyApp(): void { |
|
| 405 | + $config = $this->generateAppConfig(); |
|
| 406 | + $this->expectException(InvalidArgumentException::class); |
|
| 407 | + $config->getAllValues(''); |
|
| 408 | + } |
|
| 409 | + |
|
| 410 | + /** |
|
| 411 | + * |
|
| 412 | + * @param string $appId |
|
| 413 | + * @param array $keys |
|
| 414 | + */ |
|
| 415 | + #[\PHPUnit\Framework\Attributes\DataProvider('providerGetAppKeys')] |
|
| 416 | + public function testGetAllValuesWithEmptyKey(string $appId, array $keys): void { |
|
| 417 | + $config = $this->generateAppConfig(); |
|
| 418 | + $this->assertEqualsCanonicalizing($keys, array_keys($config->getAllValues($appId, ''))); |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + public function testGetAllValuesWithPrefix(): void { |
|
| 422 | + $config = $this->generateAppConfig(); |
|
| 423 | + $this->assertEqualsCanonicalizing(['prefix1', 'prefix-2'], array_keys($config->getAllValues('prefix-app', 'prefix'))); |
|
| 424 | + } |
|
| 425 | + |
|
| 426 | + public function testSearchValues(): void { |
|
| 427 | + $config = $this->generateAppConfig(); |
|
| 428 | + $this->assertEqualsCanonicalizing(['testapp' => 'yes', '123456' => 'yes', 'anotherapp' => 'no'], $config->searchValues('enabled')); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + public function testGetValueString(): void { |
|
| 432 | + $config = $this->generateAppConfig(); |
|
| 433 | + $this->assertSame('value', $config->getValueString('typed', 'string', '')); |
|
| 434 | + } |
|
| 435 | + |
|
| 436 | + public function testGetValueStringOnUnknownAppReturnsDefault(): void { |
|
| 437 | + $config = $this->generateAppConfig(); |
|
| 438 | + $this->assertSame('default-1', $config->getValueString('typed-1', 'string', 'default-1')); |
|
| 439 | + } |
|
| 440 | + |
|
| 441 | + public function testGetValueStringOnNonExistentKeyReturnsDefault(): void { |
|
| 442 | + $config = $this->generateAppConfig(); |
|
| 443 | + $this->assertSame('default-2', $config->getValueString('typed', 'string-2', 'default-2')); |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + public function testGetValueStringOnWrongType(): void { |
|
| 447 | + $config = $this->generateAppConfig(); |
|
| 448 | + $this->expectException(AppConfigTypeConflictException::class); |
|
| 449 | + $config->getValueString('typed', 'int'); |
|
| 450 | + } |
|
| 451 | + |
|
| 452 | + public function testGetNonLazyValueStringAsLazy(): void { |
|
| 453 | + $config = $this->generateAppConfig(); |
|
| 454 | + $this->assertSame('value', $config->getValueString('non-sensitive-app', 'non-lazy-key', 'default', lazy: true)); |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + public function testGetValueInt(): void { |
|
| 458 | + $config = $this->generateAppConfig(); |
|
| 459 | + $this->assertSame(42, $config->getValueInt('typed', 'int', 0)); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + public function testGetValueIntOnUnknownAppReturnsDefault(): void { |
|
| 463 | + $config = $this->generateAppConfig(); |
|
| 464 | + $this->assertSame(1, $config->getValueInt('typed-1', 'int', 1)); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + public function testGetValueIntOnNonExistentKeyReturnsDefault(): void { |
|
| 468 | + $config = $this->generateAppConfig(); |
|
| 469 | + $this->assertSame(2, $config->getValueInt('typed', 'int-2', 2)); |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + public function testGetValueIntOnWrongType(): void { |
|
| 473 | + $config = $this->generateAppConfig(); |
|
| 474 | + $this->expectException(AppConfigTypeConflictException::class); |
|
| 475 | + $config->getValueInt('typed', 'float'); |
|
| 476 | + } |
|
| 477 | + |
|
| 478 | + public function testGetValueFloat(): void { |
|
| 479 | + $config = $this->generateAppConfig(); |
|
| 480 | + $this->assertSame(3.14, $config->getValueFloat('typed', 'float', 0)); |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + public function testGetValueFloatOnNonUnknownAppReturnsDefault(): void { |
|
| 484 | + $config = $this->generateAppConfig(); |
|
| 485 | + $this->assertSame(1.11, $config->getValueFloat('typed-1', 'float', 1.11)); |
|
| 486 | + } |
|
| 487 | + |
|
| 488 | + public function testGetValueFloatOnNonExistentKeyReturnsDefault(): void { |
|
| 489 | + $config = $this->generateAppConfig(); |
|
| 490 | + $this->assertSame(2.22, $config->getValueFloat('typed', 'float-2', 2.22)); |
|
| 491 | + } |
|
| 492 | + |
|
| 493 | + public function testGetValueFloatOnWrongType(): void { |
|
| 494 | + $config = $this->generateAppConfig(); |
|
| 495 | + $this->expectException(AppConfigTypeConflictException::class); |
|
| 496 | + $config->getValueFloat('typed', 'bool'); |
|
| 497 | + } |
|
| 498 | + |
|
| 499 | + public function testGetValueBool(): void { |
|
| 500 | + $config = $this->generateAppConfig(); |
|
| 501 | + $this->assertSame(true, $config->getValueBool('typed', 'bool')); |
|
| 502 | + } |
|
| 503 | + |
|
| 504 | + public function testGetValueBoolOnUnknownAppReturnsDefault(): void { |
|
| 505 | + $config = $this->generateAppConfig(); |
|
| 506 | + $this->assertSame(false, $config->getValueBool('typed-1', 'bool', false)); |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + public function testGetValueBoolOnNonExistentKeyReturnsDefault(): void { |
|
| 510 | + $config = $this->generateAppConfig(); |
|
| 511 | + $this->assertSame(false, $config->getValueBool('typed', 'bool-2')); |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + public function testGetValueBoolOnWrongType(): void { |
|
| 515 | + $config = $this->generateAppConfig(); |
|
| 516 | + $this->expectException(AppConfigTypeConflictException::class); |
|
| 517 | + $config->getValueBool('typed', 'array'); |
|
| 518 | + } |
|
| 519 | + |
|
| 520 | + public function testGetValueArray(): void { |
|
| 521 | + $config = $this->generateAppConfig(); |
|
| 522 | + $this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('typed', 'array', [])); |
|
| 523 | + } |
|
| 524 | + |
|
| 525 | + public function testGetValueArrayOnUnknownAppReturnsDefault(): void { |
|
| 526 | + $config = $this->generateAppConfig(); |
|
| 527 | + $this->assertSame([1], $config->getValueArray('typed-1', 'array', [1])); |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + public function testGetValueArrayOnNonExistentKeyReturnsDefault(): void { |
|
| 531 | + $config = $this->generateAppConfig(); |
|
| 532 | + $this->assertSame([1, 2], $config->getValueArray('typed', 'array-2', [1, 2])); |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + public function testGetValueArrayOnWrongType(): void { |
|
| 536 | + $config = $this->generateAppConfig(); |
|
| 537 | + $this->expectException(AppConfigTypeConflictException::class); |
|
| 538 | + $config->getValueArray('typed', 'string'); |
|
| 539 | + } |
|
| 540 | + |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * @return array |
|
| 544 | + * @see testGetValueType |
|
| 545 | + * |
|
| 546 | + * @see testGetValueMixed |
|
| 547 | + */ |
|
| 548 | + public static function providerGetValueMixed(): array { |
|
| 549 | + return [ |
|
| 550 | + // key, value, type |
|
| 551 | + ['mixed', 'mix', IAppConfig::VALUE_MIXED], |
|
| 552 | + ['string', 'value', IAppConfig::VALUE_STRING], |
|
| 553 | + ['int', '42', IAppConfig::VALUE_INT], |
|
| 554 | + ['float', '3.14', IAppConfig::VALUE_FLOAT], |
|
| 555 | + ['bool', '1', IAppConfig::VALUE_BOOL], |
|
| 556 | + ['array', '{"test": 1}', IAppConfig::VALUE_ARRAY], |
|
| 557 | + ]; |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + /** |
|
| 561 | + * |
|
| 562 | + * @param string $key |
|
| 563 | + * @param string $value |
|
| 564 | + */ |
|
| 565 | + #[\PHPUnit\Framework\Attributes\DataProvider('providerGetValueMixed')] |
|
| 566 | + public function testGetValueMixed(string $key, string $value): void { |
|
| 567 | + $config = $this->generateAppConfig(); |
|
| 568 | + $this->assertSame($value, $config->getValueMixed('typed', $key)); |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * |
|
| 573 | + * @param string $key |
|
| 574 | + * @param string $value |
|
| 575 | + * @param int $type |
|
| 576 | + */ |
|
| 577 | + #[\PHPUnit\Framework\Attributes\DataProvider('providerGetValueMixed')] |
|
| 578 | + public function testGetValueType(string $key, string $value, int $type): void { |
|
| 579 | + $config = $this->generateAppConfig(); |
|
| 580 | + $this->assertSame($type, $config->getValueType('typed', $key)); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + public function testGetValueTypeOnUnknownApp(): void { |
|
| 584 | + $config = $this->generateAppConfig(); |
|
| 585 | + $this->expectException(AppConfigUnknownKeyException::class); |
|
| 586 | + $config->getValueType('typed-1', 'string'); |
|
| 587 | + } |
|
| 588 | + |
|
| 589 | + public function testGetValueTypeOnNonExistentKey(): void { |
|
| 590 | + $config = $this->generateAppConfig(); |
|
| 591 | + $this->expectException(AppConfigUnknownKeyException::class); |
|
| 592 | + $config->getValueType('typed', 'string-2'); |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + public function testSetValueString(): void { |
|
| 596 | + $config = $this->generateAppConfig(); |
|
| 597 | + $config->setValueString('feed', 'string', 'value-1'); |
|
| 598 | + $this->assertSame('value-1', $config->getValueString('feed', 'string', '')); |
|
| 599 | + } |
|
| 600 | + |
|
| 601 | + public function testSetValueStringCache(): void { |
|
| 602 | + $config = $this->generateAppConfig(); |
|
| 603 | + $config->setValueString('feed', 'string', 'value-1'); |
|
| 604 | + $status = $config->statusCache(); |
|
| 605 | + $this->assertSame('value-1', $status['fastCache']['feed']['string']); |
|
| 606 | + } |
|
| 607 | + |
|
| 608 | + public function testSetValueStringDatabase(): void { |
|
| 609 | + $config = $this->generateAppConfig(); |
|
| 610 | + $config->setValueString('feed', 'string', 'value-1'); |
|
| 611 | + $config->clearCache(); |
|
| 612 | + $this->assertSame('value-1', $config->getValueString('feed', 'string', '')); |
|
| 613 | + } |
|
| 614 | + |
|
| 615 | + public function testSetValueStringIsUpdated(): void { |
|
| 616 | + $config = $this->generateAppConfig(); |
|
| 617 | + $config->setValueString('feed', 'string', 'value-1'); |
|
| 618 | + $this->assertSame(true, $config->setValueString('feed', 'string', 'value-2')); |
|
| 619 | + } |
|
| 620 | + |
|
| 621 | + public function testSetValueStringIsNotUpdated(): void { |
|
| 622 | + $config = $this->generateAppConfig(); |
|
| 623 | + $config->setValueString('feed', 'string', 'value-1'); |
|
| 624 | + $this->assertSame(false, $config->setValueString('feed', 'string', 'value-1')); |
|
| 625 | + } |
|
| 626 | + |
|
| 627 | + public function testSetValueStringIsUpdatedCache(): void { |
|
| 628 | + $config = $this->generateAppConfig(); |
|
| 629 | + $config->setValueString('feed', 'string', 'value-1'); |
|
| 630 | + $config->setValueString('feed', 'string', 'value-2'); |
|
| 631 | + $status = $config->statusCache(); |
|
| 632 | + $this->assertSame('value-2', $status['fastCache']['feed']['string']); |
|
| 633 | + } |
|
| 634 | + |
|
| 635 | + public function testSetValueStringIsUpdatedDatabase(): void { |
|
| 636 | + $config = $this->generateAppConfig(); |
|
| 637 | + $config->setValueString('feed', 'string', 'value-1'); |
|
| 638 | + $config->setValueString('feed', 'string', 'value-2'); |
|
| 639 | + $config->clearCache(); |
|
| 640 | + $this->assertSame('value-2', $config->getValueString('feed', 'string', '')); |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + public function testSetValueInt(): void { |
|
| 644 | + $config = $this->generateAppConfig(); |
|
| 645 | + $config->setValueInt('feed', 'int', 42); |
|
| 646 | + $this->assertSame(42, $config->getValueInt('feed', 'int', 0)); |
|
| 647 | + } |
|
| 648 | + |
|
| 649 | + public function testSetValueIntCache(): void { |
|
| 650 | + $config = $this->generateAppConfig(); |
|
| 651 | + $config->setValueInt('feed', 'int', 42); |
|
| 652 | + $status = $config->statusCache(); |
|
| 653 | + $this->assertSame('42', $status['fastCache']['feed']['int']); |
|
| 654 | + } |
|
| 655 | + |
|
| 656 | + public function testSetValueIntDatabase(): void { |
|
| 657 | + $config = $this->generateAppConfig(); |
|
| 658 | + $config->setValueInt('feed', 'int', 42); |
|
| 659 | + $config->clearCache(); |
|
| 660 | + $this->assertSame(42, $config->getValueInt('feed', 'int', 0)); |
|
| 661 | + } |
|
| 662 | + |
|
| 663 | + public function testSetValueIntIsUpdated(): void { |
|
| 664 | + $config = $this->generateAppConfig(); |
|
| 665 | + $config->setValueInt('feed', 'int', 42); |
|
| 666 | + $this->assertSame(true, $config->setValueInt('feed', 'int', 17)); |
|
| 667 | + } |
|
| 668 | + |
|
| 669 | + public function testSetValueIntIsNotUpdated(): void { |
|
| 670 | + $config = $this->generateAppConfig(); |
|
| 671 | + $config->setValueInt('feed', 'int', 42); |
|
| 672 | + $this->assertSame(false, $config->setValueInt('feed', 'int', 42)); |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + public function testSetValueIntIsUpdatedCache(): void { |
|
| 676 | + $config = $this->generateAppConfig(); |
|
| 677 | + $config->setValueInt('feed', 'int', 42); |
|
| 678 | + $config->setValueInt('feed', 'int', 17); |
|
| 679 | + $status = $config->statusCache(); |
|
| 680 | + $this->assertSame('17', $status['fastCache']['feed']['int']); |
|
| 681 | + } |
|
| 682 | + |
|
| 683 | + public function testSetValueIntIsUpdatedDatabase(): void { |
|
| 684 | + $config = $this->generateAppConfig(); |
|
| 685 | + $config->setValueInt('feed', 'int', 42); |
|
| 686 | + $config->setValueInt('feed', 'int', 17); |
|
| 687 | + $config->clearCache(); |
|
| 688 | + $this->assertSame(17, $config->getValueInt('feed', 'int', 0)); |
|
| 689 | + } |
|
| 690 | + |
|
| 691 | + public function testSetValueFloat(): void { |
|
| 692 | + $config = $this->generateAppConfig(); |
|
| 693 | + $config->setValueFloat('feed', 'float', 3.14); |
|
| 694 | + $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0)); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + public function testSetValueFloatCache(): void { |
|
| 698 | + $config = $this->generateAppConfig(); |
|
| 699 | + $config->setValueFloat('feed', 'float', 3.14); |
|
| 700 | + $status = $config->statusCache(); |
|
| 701 | + $this->assertSame('3.14', $status['fastCache']['feed']['float']); |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + public function testSetValueFloatDatabase(): void { |
|
| 705 | + $config = $this->generateAppConfig(); |
|
| 706 | + $config->setValueFloat('feed', 'float', 3.14); |
|
| 707 | + $config->clearCache(); |
|
| 708 | + $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0)); |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + public function testSetValueFloatIsUpdated(): void { |
|
| 712 | + $config = $this->generateAppConfig(); |
|
| 713 | + $config->setValueFloat('feed', 'float', 3.14); |
|
| 714 | + $this->assertSame(true, $config->setValueFloat('feed', 'float', 1.23)); |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + public function testSetValueFloatIsNotUpdated(): void { |
|
| 718 | + $config = $this->generateAppConfig(); |
|
| 719 | + $config->setValueFloat('feed', 'float', 3.14); |
|
| 720 | + $this->assertSame(false, $config->setValueFloat('feed', 'float', 3.14)); |
|
| 721 | + } |
|
| 722 | + |
|
| 723 | + public function testSetValueFloatIsUpdatedCache(): void { |
|
| 724 | + $config = $this->generateAppConfig(); |
|
| 725 | + $config->setValueFloat('feed', 'float', 3.14); |
|
| 726 | + $config->setValueFloat('feed', 'float', 1.23); |
|
| 727 | + $status = $config->statusCache(); |
|
| 728 | + $this->assertSame('1.23', $status['fastCache']['feed']['float']); |
|
| 729 | + } |
|
| 730 | + |
|
| 731 | + public function testSetValueFloatIsUpdatedDatabase(): void { |
|
| 732 | + $config = $this->generateAppConfig(); |
|
| 733 | + $config->setValueFloat('feed', 'float', 3.14); |
|
| 734 | + $config->setValueFloat('feed', 'float', 1.23); |
|
| 735 | + $config->clearCache(); |
|
| 736 | + $this->assertSame(1.23, $config->getValueFloat('feed', 'float', 0)); |
|
| 737 | + } |
|
| 738 | + |
|
| 739 | + public function testSetValueBool(): void { |
|
| 740 | + $config = $this->generateAppConfig(); |
|
| 741 | + $config->setValueBool('feed', 'bool', true); |
|
| 742 | + $this->assertSame(true, $config->getValueBool('feed', 'bool', false)); |
|
| 743 | + } |
|
| 744 | + |
|
| 745 | + public function testSetValueBoolCache(): void { |
|
| 746 | + $config = $this->generateAppConfig(); |
|
| 747 | + $config->setValueBool('feed', 'bool', true); |
|
| 748 | + $status = $config->statusCache(); |
|
| 749 | + $this->assertSame('1', $status['fastCache']['feed']['bool']); |
|
| 750 | + } |
|
| 751 | + |
|
| 752 | + public function testSetValueBoolDatabase(): void { |
|
| 753 | + $config = $this->generateAppConfig(); |
|
| 754 | + $config->setValueBool('feed', 'bool', true); |
|
| 755 | + $config->clearCache(); |
|
| 756 | + $this->assertSame(true, $config->getValueBool('feed', 'bool', false)); |
|
| 757 | + } |
|
| 758 | + |
|
| 759 | + public function testSetValueBoolIsUpdated(): void { |
|
| 760 | + $config = $this->generateAppConfig(); |
|
| 761 | + $config->setValueBool('feed', 'bool', true); |
|
| 762 | + $this->assertSame(true, $config->setValueBool('feed', 'bool', false)); |
|
| 763 | + } |
|
| 764 | + |
|
| 765 | + public function testSetValueBoolIsNotUpdated(): void { |
|
| 766 | + $config = $this->generateAppConfig(); |
|
| 767 | + $config->setValueBool('feed', 'bool', true); |
|
| 768 | + $this->assertSame(false, $config->setValueBool('feed', 'bool', true)); |
|
| 769 | + } |
|
| 770 | + |
|
| 771 | + public function testSetValueBoolIsUpdatedCache(): void { |
|
| 772 | + $config = $this->generateAppConfig(); |
|
| 773 | + $config->setValueBool('feed', 'bool', true); |
|
| 774 | + $config->setValueBool('feed', 'bool', false); |
|
| 775 | + $status = $config->statusCache(); |
|
| 776 | + $this->assertSame('0', $status['fastCache']['feed']['bool']); |
|
| 777 | + } |
|
| 778 | + |
|
| 779 | + public function testSetValueBoolIsUpdatedDatabase(): void { |
|
| 780 | + $config = $this->generateAppConfig(); |
|
| 781 | + $config->setValueBool('feed', 'bool', true); |
|
| 782 | + $config->setValueBool('feed', 'bool', false); |
|
| 783 | + $config->clearCache(); |
|
| 784 | + $this->assertSame(false, $config->getValueBool('feed', 'bool', true)); |
|
| 785 | + } |
|
| 786 | + |
|
| 787 | + |
|
| 788 | + public function testSetValueArray(): void { |
|
| 789 | + $config = $this->generateAppConfig(); |
|
| 790 | + $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 791 | + $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [])); |
|
| 792 | + } |
|
| 793 | + |
|
| 794 | + public function testSetValueArrayCache(): void { |
|
| 795 | + $config = $this->generateAppConfig(); |
|
| 796 | + $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 797 | + $status = $config->statusCache(); |
|
| 798 | + $this->assertSame('{"test":1}', $status['fastCache']['feed']['array']); |
|
| 799 | + } |
|
| 800 | + |
|
| 801 | + public function testSetValueArrayDatabase(): void { |
|
| 802 | + $config = $this->generateAppConfig(); |
|
| 803 | + $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 804 | + $config->clearCache(); |
|
| 805 | + $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [])); |
|
| 806 | + } |
|
| 807 | + |
|
| 808 | + public function testSetValueArrayIsUpdated(): void { |
|
| 809 | + $config = $this->generateAppConfig(); |
|
| 810 | + $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 811 | + $this->assertSame(true, $config->setValueArray('feed', 'array', ['test' => 2])); |
|
| 812 | + } |
|
| 813 | + |
|
| 814 | + public function testSetValueArrayIsNotUpdated(): void { |
|
| 815 | + $config = $this->generateAppConfig(); |
|
| 816 | + $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 817 | + $this->assertSame(false, $config->setValueArray('feed', 'array', ['test' => 1])); |
|
| 818 | + } |
|
| 819 | + |
|
| 820 | + public function testSetValueArrayIsUpdatedCache(): void { |
|
| 821 | + $config = $this->generateAppConfig(); |
|
| 822 | + $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 823 | + $config->setValueArray('feed', 'array', ['test' => 2]); |
|
| 824 | + $status = $config->statusCache(); |
|
| 825 | + $this->assertSame('{"test":2}', $status['fastCache']['feed']['array']); |
|
| 826 | + } |
|
| 827 | + |
|
| 828 | + public function testSetValueArrayIsUpdatedDatabase(): void { |
|
| 829 | + $config = $this->generateAppConfig(); |
|
| 830 | + $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 831 | + $config->setValueArray('feed', 'array', ['test' => 2]); |
|
| 832 | + $config->clearCache(); |
|
| 833 | + $this->assertSame(['test' => 2], $config->getValueArray('feed', 'array', [])); |
|
| 834 | + } |
|
| 835 | + |
|
| 836 | + public function testSetLazyValueString(): void { |
|
| 837 | + $config = $this->generateAppConfig(); |
|
| 838 | + $config->setValueString('feed', 'string', 'value-1', true); |
|
| 839 | + $this->assertSame('value-1', $config->getValueString('feed', 'string', '', true)); |
|
| 840 | + } |
|
| 841 | + |
|
| 842 | + public function testSetLazyValueStringCache(): void { |
|
| 843 | + $config = $this->generateAppConfig(); |
|
| 844 | + $config->setValueString('feed', 'string', 'value-1', true); |
|
| 845 | + $status = $config->statusCache(); |
|
| 846 | + $this->assertSame('value-1', $status['lazyCache']['feed']['string']); |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + public function testSetLazyValueStringDatabase(): void { |
|
| 850 | + $config = $this->generateAppConfig(); |
|
| 851 | + $config->setValueString('feed', 'string', 'value-1', true); |
|
| 852 | + $config->clearCache(); |
|
| 853 | + $this->assertSame('value-1', $config->getValueString('feed', 'string', '', true)); |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + public function testSetLazyValueStringAsNonLazy(): void { |
|
| 857 | + $config = $this->generateAppConfig(); |
|
| 858 | + $config->setValueString('feed', 'string', 'value-1', true); |
|
| 859 | + $config->setValueString('feed', 'string', 'value-1', false); |
|
| 860 | + $this->assertSame('value-1', $config->getValueString('feed', 'string', '')); |
|
| 861 | + } |
|
| 862 | + |
|
| 863 | + public function testSetNonLazyValueStringAsLazy(): void { |
|
| 864 | + $config = $this->generateAppConfig(); |
|
| 865 | + $config->setValueString('feed', 'string', 'value-1', false); |
|
| 866 | + $config->setValueString('feed', 'string', 'value-1', true); |
|
| 867 | + $this->assertSame('value-1', $config->getValueString('feed', 'string', '', true)); |
|
| 868 | + } |
|
| 869 | + |
|
| 870 | + public function testSetSensitiveValueString(): void { |
|
| 871 | + $config = $this->generateAppConfig(); |
|
| 872 | + $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 873 | + $this->assertSame('value-1', $config->getValueString('feed', 'string', '')); |
|
| 874 | + } |
|
| 875 | + |
|
| 876 | + public function testSetSensitiveValueStringCache(): void { |
|
| 877 | + $config = $this->generateAppConfig(); |
|
| 878 | + $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 879 | + $status = $config->statusCache(); |
|
| 880 | + $this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['string']); |
|
| 881 | + } |
|
| 882 | + |
|
| 883 | + public function testSetSensitiveValueStringDatabase(): void { |
|
| 884 | + $config = $this->generateAppConfig(); |
|
| 885 | + $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 886 | + $config->clearCache(); |
|
| 887 | + $this->assertSame('value-1', $config->getValueString('feed', 'string', '')); |
|
| 888 | + } |
|
| 889 | + |
|
| 890 | + public function testSetNonSensitiveValueStringAsSensitive(): void { |
|
| 891 | + $config = $this->generateAppConfig(); |
|
| 892 | + $config->setValueString('feed', 'string', 'value-1', sensitive: false); |
|
| 893 | + $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 894 | + $this->assertSame(true, $config->isSensitive('feed', 'string')); |
|
| 895 | + |
|
| 896 | + $this->assertConfigValueNotEquals('feed', 'string', 'value-1'); |
|
| 897 | + $this->assertConfigValueNotEquals('feed', 'string', 'value-2'); |
|
| 898 | + } |
|
| 899 | + |
|
| 900 | + public function testSetSensitiveValueStringAsNonSensitiveStaysSensitive(): void { |
|
| 901 | + $config = $this->generateAppConfig(); |
|
| 902 | + $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 903 | + $config->setValueString('feed', 'string', 'value-2', sensitive: false); |
|
| 904 | + $this->assertSame(true, $config->isSensitive('feed', 'string')); |
|
| 905 | + |
|
| 906 | + $this->assertConfigValueNotEquals('feed', 'string', 'value-1'); |
|
| 907 | + $this->assertConfigValueNotEquals('feed', 'string', 'value-2'); |
|
| 908 | + } |
|
| 909 | + |
|
| 910 | + public function testSetSensitiveValueStringAsNonSensitiveAreStillUpdated(): void { |
|
| 911 | + $config = $this->generateAppConfig(); |
|
| 912 | + $config->setValueString('feed', 'string', 'value-1', sensitive: true); |
|
| 913 | + $config->setValueString('feed', 'string', 'value-2', sensitive: false); |
|
| 914 | + $this->assertSame('value-2', $config->getValueString('feed', 'string', '')); |
|
| 915 | + |
|
| 916 | + $this->assertConfigValueNotEquals('feed', 'string', 'value-1'); |
|
| 917 | + $this->assertConfigValueNotEquals('feed', 'string', 'value-2'); |
|
| 918 | + } |
|
| 919 | + |
|
| 920 | + public function testSetLazyValueInt(): void { |
|
| 921 | + $config = $this->generateAppConfig(); |
|
| 922 | + $config->setValueInt('feed', 'int', 42, true); |
|
| 923 | + $this->assertSame(42, $config->getValueInt('feed', 'int', 0, true)); |
|
| 924 | + } |
|
| 925 | + |
|
| 926 | + public function testSetLazyValueIntCache(): void { |
|
| 927 | + $config = $this->generateAppConfig(); |
|
| 928 | + $config->setValueInt('feed', 'int', 42, true); |
|
| 929 | + $status = $config->statusCache(); |
|
| 930 | + $this->assertSame('42', $status['lazyCache']['feed']['int']); |
|
| 931 | + } |
|
| 932 | + |
|
| 933 | + public function testSetLazyValueIntDatabase(): void { |
|
| 934 | + $config = $this->generateAppConfig(); |
|
| 935 | + $config->setValueInt('feed', 'int', 42, true); |
|
| 936 | + $config->clearCache(); |
|
| 937 | + $this->assertSame(42, $config->getValueInt('feed', 'int', 0, true)); |
|
| 938 | + } |
|
| 939 | + |
|
| 940 | + public function testSetLazyValueIntAsNonLazy(): void { |
|
| 941 | + $config = $this->generateAppConfig(); |
|
| 942 | + $config->setValueInt('feed', 'int', 42, true); |
|
| 943 | + $config->setValueInt('feed', 'int', 42, false); |
|
| 944 | + $this->assertSame(42, $config->getValueInt('feed', 'int', 0)); |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + public function testSetNonLazyValueIntAsLazy(): void { |
|
| 948 | + $config = $this->generateAppConfig(); |
|
| 949 | + $config->setValueInt('feed', 'int', 42, false); |
|
| 950 | + $config->setValueInt('feed', 'int', 42, true); |
|
| 951 | + $this->assertSame(42, $config->getValueInt('feed', 'int', 0, true)); |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + public function testSetSensitiveValueInt(): void { |
|
| 955 | + $config = $this->generateAppConfig(); |
|
| 956 | + $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 957 | + $this->assertSame(42, $config->getValueInt('feed', 'int', 0)); |
|
| 958 | + } |
|
| 959 | + |
|
| 960 | + public function testSetSensitiveValueIntCache(): void { |
|
| 961 | + $config = $this->generateAppConfig(); |
|
| 962 | + $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 963 | + $status = $config->statusCache(); |
|
| 964 | + $this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['int']); |
|
| 965 | + } |
|
| 966 | + |
|
| 967 | + public function testSetSensitiveValueIntDatabase(): void { |
|
| 968 | + $config = $this->generateAppConfig(); |
|
| 969 | + $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 970 | + $config->clearCache(); |
|
| 971 | + $this->assertSame(42, $config->getValueInt('feed', 'int', 0)); |
|
| 972 | + } |
|
| 973 | + |
|
| 974 | + public function testSetNonSensitiveValueIntAsSensitive(): void { |
|
| 975 | + $config = $this->generateAppConfig(); |
|
| 976 | + $config->setValueInt('feed', 'int', 42); |
|
| 977 | + $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 978 | + $this->assertSame(true, $config->isSensitive('feed', 'int')); |
|
| 979 | + } |
|
| 980 | + |
|
| 981 | + public function testSetSensitiveValueIntAsNonSensitiveStaysSensitive(): void { |
|
| 982 | + $config = $this->generateAppConfig(); |
|
| 983 | + $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 984 | + $config->setValueInt('feed', 'int', 17); |
|
| 985 | + $this->assertSame(true, $config->isSensitive('feed', 'int')); |
|
| 986 | + } |
|
| 987 | + |
|
| 988 | + public function testSetSensitiveValueIntAsNonSensitiveAreStillUpdated(): void { |
|
| 989 | + $config = $this->generateAppConfig(); |
|
| 990 | + $config->setValueInt('feed', 'int', 42, sensitive: true); |
|
| 991 | + $config->setValueInt('feed', 'int', 17); |
|
| 992 | + $this->assertSame(17, $config->getValueInt('feed', 'int', 0)); |
|
| 993 | + } |
|
| 994 | + |
|
| 995 | + public function testSetLazyValueFloat(): void { |
|
| 996 | + $config = $this->generateAppConfig(); |
|
| 997 | + $config->setValueFloat('feed', 'float', 3.14, true); |
|
| 998 | + $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true)); |
|
| 999 | + } |
|
| 1000 | + |
|
| 1001 | + public function testSetLazyValueFloatCache(): void { |
|
| 1002 | + $config = $this->generateAppConfig(); |
|
| 1003 | + $config->setValueFloat('feed', 'float', 3.14, true); |
|
| 1004 | + $status = $config->statusCache(); |
|
| 1005 | + $this->assertSame('3.14', $status['lazyCache']['feed']['float']); |
|
| 1006 | + } |
|
| 1007 | + |
|
| 1008 | + public function testSetLazyValueFloatDatabase(): void { |
|
| 1009 | + $config = $this->generateAppConfig(); |
|
| 1010 | + $config->setValueFloat('feed', 'float', 3.14, true); |
|
| 1011 | + $config->clearCache(); |
|
| 1012 | + $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true)); |
|
| 1013 | + } |
|
| 1014 | + |
|
| 1015 | + public function testSetLazyValueFloatAsNonLazy(): void { |
|
| 1016 | + $config = $this->generateAppConfig(); |
|
| 1017 | + $config->setValueFloat('feed', 'float', 3.14, true); |
|
| 1018 | + $config->setValueFloat('feed', 'float', 3.14, false); |
|
| 1019 | + $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0)); |
|
| 1020 | + } |
|
| 1021 | + |
|
| 1022 | + public function testSetNonLazyValueFloatAsLazy(): void { |
|
| 1023 | + $config = $this->generateAppConfig(); |
|
| 1024 | + $config->setValueFloat('feed', 'float', 3.14, false); |
|
| 1025 | + $config->setValueFloat('feed', 'float', 3.14, true); |
|
| 1026 | + $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true)); |
|
| 1027 | + } |
|
| 1028 | + |
|
| 1029 | + public function testSetSensitiveValueFloat(): void { |
|
| 1030 | + $config = $this->generateAppConfig(); |
|
| 1031 | + $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1032 | + $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0)); |
|
| 1033 | + } |
|
| 1034 | + |
|
| 1035 | + public function testSetSensitiveValueFloatCache(): void { |
|
| 1036 | + $config = $this->generateAppConfig(); |
|
| 1037 | + $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1038 | + $status = $config->statusCache(); |
|
| 1039 | + $this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['float']); |
|
| 1040 | + } |
|
| 1041 | + |
|
| 1042 | + public function testSetSensitiveValueFloatDatabase(): void { |
|
| 1043 | + $config = $this->generateAppConfig(); |
|
| 1044 | + $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1045 | + $config->clearCache(); |
|
| 1046 | + $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0)); |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + public function testSetNonSensitiveValueFloatAsSensitive(): void { |
|
| 1050 | + $config = $this->generateAppConfig(); |
|
| 1051 | + $config->setValueFloat('feed', 'float', 3.14); |
|
| 1052 | + $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1053 | + $this->assertSame(true, $config->isSensitive('feed', 'float')); |
|
| 1054 | + } |
|
| 1055 | + |
|
| 1056 | + public function testSetSensitiveValueFloatAsNonSensitiveStaysSensitive(): void { |
|
| 1057 | + $config = $this->generateAppConfig(); |
|
| 1058 | + $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1059 | + $config->setValueFloat('feed', 'float', 1.23); |
|
| 1060 | + $this->assertSame(true, $config->isSensitive('feed', 'float')); |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + public function testSetSensitiveValueFloatAsNonSensitiveAreStillUpdated(): void { |
|
| 1064 | + $config = $this->generateAppConfig(); |
|
| 1065 | + $config->setValueFloat('feed', 'float', 3.14, sensitive: true); |
|
| 1066 | + $config->setValueFloat('feed', 'float', 1.23); |
|
| 1067 | + $this->assertSame(1.23, $config->getValueFloat('feed', 'float', 0)); |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + public function testSetLazyValueBool(): void { |
|
| 1071 | + $config = $this->generateAppConfig(); |
|
| 1072 | + $config->setValueBool('feed', 'bool', true, true); |
|
| 1073 | + $this->assertSame(true, $config->getValueBool('feed', 'bool', false, true)); |
|
| 1074 | + } |
|
| 1075 | + |
|
| 1076 | + public function testSetLazyValueBoolCache(): void { |
|
| 1077 | + $config = $this->generateAppConfig(); |
|
| 1078 | + $config->setValueBool('feed', 'bool', true, true); |
|
| 1079 | + $status = $config->statusCache(); |
|
| 1080 | + $this->assertSame('1', $status['lazyCache']['feed']['bool']); |
|
| 1081 | + } |
|
| 1082 | + |
|
| 1083 | + public function testSetLazyValueBoolDatabase(): void { |
|
| 1084 | + $config = $this->generateAppConfig(); |
|
| 1085 | + $config->setValueBool('feed', 'bool', true, true); |
|
| 1086 | + $config->clearCache(); |
|
| 1087 | + $this->assertSame(true, $config->getValueBool('feed', 'bool', false, true)); |
|
| 1088 | + } |
|
| 1089 | + |
|
| 1090 | + public function testSetLazyValueBoolAsNonLazy(): void { |
|
| 1091 | + $config = $this->generateAppConfig(); |
|
| 1092 | + $config->setValueBool('feed', 'bool', true, true); |
|
| 1093 | + $config->setValueBool('feed', 'bool', true, false); |
|
| 1094 | + $this->assertSame(true, $config->getValueBool('feed', 'bool', false)); |
|
| 1095 | + } |
|
| 1096 | + |
|
| 1097 | + public function testSetNonLazyValueBoolAsLazy(): void { |
|
| 1098 | + $config = $this->generateAppConfig(); |
|
| 1099 | + $config->setValueBool('feed', 'bool', true, false); |
|
| 1100 | + $config->setValueBool('feed', 'bool', true, true); |
|
| 1101 | + $this->assertSame(true, $config->getValueBool('feed', 'bool', false, true)); |
|
| 1102 | + } |
|
| 1103 | + |
|
| 1104 | + public function testSetLazyValueArray(): void { |
|
| 1105 | + $config = $this->generateAppConfig(); |
|
| 1106 | + $config->setValueArray('feed', 'array', ['test' => 1], true); |
|
| 1107 | + $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true)); |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + public function testSetLazyValueArrayCache(): void { |
|
| 1111 | + $config = $this->generateAppConfig(); |
|
| 1112 | + $config->setValueArray('feed', 'array', ['test' => 1], true); |
|
| 1113 | + $status = $config->statusCache(); |
|
| 1114 | + $this->assertSame('{"test":1}', $status['lazyCache']['feed']['array']); |
|
| 1115 | + } |
|
| 1116 | + |
|
| 1117 | + public function testSetLazyValueArrayDatabase(): void { |
|
| 1118 | + $config = $this->generateAppConfig(); |
|
| 1119 | + $config->setValueArray('feed', 'array', ['test' => 1], true); |
|
| 1120 | + $config->clearCache(); |
|
| 1121 | + $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true)); |
|
| 1122 | + } |
|
| 1123 | + |
|
| 1124 | + public function testSetLazyValueArrayAsNonLazy(): void { |
|
| 1125 | + $config = $this->generateAppConfig(); |
|
| 1126 | + $config->setValueArray('feed', 'array', ['test' => 1], true); |
|
| 1127 | + $config->setValueArray('feed', 'array', ['test' => 1], false); |
|
| 1128 | + $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [])); |
|
| 1129 | + } |
|
| 1130 | + |
|
| 1131 | + public function testSetNonLazyValueArrayAsLazy(): void { |
|
| 1132 | + $config = $this->generateAppConfig(); |
|
| 1133 | + $config->setValueArray('feed', 'array', ['test' => 1], false); |
|
| 1134 | + $config->setValueArray('feed', 'array', ['test' => 1], true); |
|
| 1135 | + $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true)); |
|
| 1136 | + } |
|
| 1137 | + |
|
| 1138 | + |
|
| 1139 | + public function testSetSensitiveValueArray(): void { |
|
| 1140 | + $config = $this->generateAppConfig(); |
|
| 1141 | + $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1142 | + $this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('feed', 'array', [])); |
|
| 1143 | + } |
|
| 1144 | + |
|
| 1145 | + public function testSetSensitiveValueArrayCache(): void { |
|
| 1146 | + $config = $this->generateAppConfig(); |
|
| 1147 | + $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1148 | + $status = $config->statusCache(); |
|
| 1149 | + $this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['array']); |
|
| 1150 | + } |
|
| 1151 | + |
|
| 1152 | + public function testSetSensitiveValueArrayDatabase(): void { |
|
| 1153 | + $config = $this->generateAppConfig(); |
|
| 1154 | + $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1155 | + $config->clearCache(); |
|
| 1156 | + $this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('feed', 'array', [])); |
|
| 1157 | + } |
|
| 1158 | + |
|
| 1159 | + public function testSetNonSensitiveValueArrayAsSensitive(): void { |
|
| 1160 | + $config = $this->generateAppConfig(); |
|
| 1161 | + $config->setValueArray('feed', 'array', ['test' => 1]); |
|
| 1162 | + $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1163 | + $this->assertSame(true, $config->isSensitive('feed', 'array')); |
|
| 1164 | + } |
|
| 1165 | + |
|
| 1166 | + public function testSetSensitiveValueArrayAsNonSensitiveStaysSensitive(): void { |
|
| 1167 | + $config = $this->generateAppConfig(); |
|
| 1168 | + $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1169 | + $config->setValueArray('feed', 'array', ['test' => 2]); |
|
| 1170 | + $this->assertSame(true, $config->isSensitive('feed', 'array')); |
|
| 1171 | + } |
|
| 1172 | + |
|
| 1173 | + public function testSetSensitiveValueArrayAsNonSensitiveAreStillUpdated(): void { |
|
| 1174 | + $config = $this->generateAppConfig(); |
|
| 1175 | + $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true); |
|
| 1176 | + $config->setValueArray('feed', 'array', ['test' => 2]); |
|
| 1177 | + $this->assertEqualsCanonicalizing(['test' => 2], $config->getValueArray('feed', 'array', [])); |
|
| 1178 | + } |
|
| 1179 | + |
|
| 1180 | + public function testUpdateNotSensitiveToSensitive(): void { |
|
| 1181 | + $config = $this->generateAppConfig(); |
|
| 1182 | + $config->updateSensitive('non-sensitive-app', 'lazy-key', true); |
|
| 1183 | + $this->assertSame(true, $config->isSensitive('non-sensitive-app', 'lazy-key', true)); |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + public function testUpdateSensitiveToNotSensitive(): void { |
|
| 1187 | + $config = $this->generateAppConfig(); |
|
| 1188 | + $config->updateSensitive('sensitive-app', 'lazy-key', false); |
|
| 1189 | + $this->assertSame(false, $config->isSensitive('sensitive-app', 'lazy-key', true)); |
|
| 1190 | + } |
|
| 1191 | + |
|
| 1192 | + public function testUpdateSensitiveToSensitiveReturnsFalse(): void { |
|
| 1193 | + $config = $this->generateAppConfig(); |
|
| 1194 | + $this->assertSame(false, $config->updateSensitive('sensitive-app', 'lazy-key', true)); |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + public function testUpdateNotSensitiveToNotSensitiveReturnsFalse(): void { |
|
| 1198 | + $config = $this->generateAppConfig(); |
|
| 1199 | + $this->assertSame(false, $config->updateSensitive('non-sensitive-app', 'lazy-key', false)); |
|
| 1200 | + } |
|
| 1201 | + |
|
| 1202 | + public function testUpdateSensitiveOnUnknownKeyReturnsFalse(): void { |
|
| 1203 | + $config = $this->generateAppConfig(); |
|
| 1204 | + $this->assertSame(false, $config->updateSensitive('non-sensitive-app', 'unknown-key', true)); |
|
| 1205 | + } |
|
| 1206 | + |
|
| 1207 | + public function testUpdateNotLazyToLazy(): void { |
|
| 1208 | + $config = $this->generateAppConfig(); |
|
| 1209 | + $config->updateLazy('non-sensitive-app', 'non-lazy-key', true); |
|
| 1210 | + $this->assertSame(true, $config->isLazy('non-sensitive-app', 'non-lazy-key')); |
|
| 1211 | + } |
|
| 1212 | + |
|
| 1213 | + public function testUpdateLazyToNotLazy(): void { |
|
| 1214 | + $config = $this->generateAppConfig(); |
|
| 1215 | + $config->updateLazy('non-sensitive-app', 'lazy-key', false); |
|
| 1216 | + $this->assertSame(false, $config->isLazy('non-sensitive-app', 'lazy-key')); |
|
| 1217 | + } |
|
| 1218 | + |
|
| 1219 | + public function testUpdateLazyToLazyReturnsFalse(): void { |
|
| 1220 | + $config = $this->generateAppConfig(); |
|
| 1221 | + $this->assertSame(false, $config->updateLazy('non-sensitive-app', 'lazy-key', true)); |
|
| 1222 | + } |
|
| 1223 | + |
|
| 1224 | + public function testUpdateNotLazyToNotLazyReturnsFalse(): void { |
|
| 1225 | + $config = $this->generateAppConfig(); |
|
| 1226 | + $this->assertSame(false, $config->updateLazy('non-sensitive-app', 'non-lazy-key', false)); |
|
| 1227 | + } |
|
| 1228 | + |
|
| 1229 | + public function testUpdateLazyOnUnknownKeyReturnsFalse(): void { |
|
| 1230 | + $config = $this->generateAppConfig(); |
|
| 1231 | + $this->assertSame(false, $config->updateLazy('non-sensitive-app', 'unknown-key', true)); |
|
| 1232 | + } |
|
| 1233 | + |
|
| 1234 | + public function testGetDetails(): void { |
|
| 1235 | + $config = $this->generateAppConfig(); |
|
| 1236 | + $this->assertEquals( |
|
| 1237 | + [ |
|
| 1238 | + 'app' => 'non-sensitive-app', |
|
| 1239 | + 'key' => 'lazy-key', |
|
| 1240 | + 'value' => 'value', |
|
| 1241 | + 'type' => 4, |
|
| 1242 | + 'lazy' => true, |
|
| 1243 | + 'typeString' => 'string', |
|
| 1244 | + 'sensitive' => false, |
|
| 1245 | + ], |
|
| 1246 | + $config->getDetails('non-sensitive-app', 'lazy-key') |
|
| 1247 | + ); |
|
| 1248 | + } |
|
| 1249 | + |
|
| 1250 | + public function testGetDetailsSensitive(): void { |
|
| 1251 | + $config = $this->generateAppConfig(); |
|
| 1252 | + $this->assertEquals( |
|
| 1253 | + [ |
|
| 1254 | + 'app' => 'sensitive-app', |
|
| 1255 | + 'key' => 'lazy-key', |
|
| 1256 | + 'value' => 'value', |
|
| 1257 | + 'type' => 4, |
|
| 1258 | + 'lazy' => true, |
|
| 1259 | + 'typeString' => 'string', |
|
| 1260 | + 'sensitive' => true, |
|
| 1261 | + ], |
|
| 1262 | + $config->getDetails('sensitive-app', 'lazy-key') |
|
| 1263 | + ); |
|
| 1264 | + } |
|
| 1265 | + |
|
| 1266 | + public function testGetDetailsInt(): void { |
|
| 1267 | + $config = $this->generateAppConfig(); |
|
| 1268 | + $this->assertEquals( |
|
| 1269 | + [ |
|
| 1270 | + 'app' => 'typed', |
|
| 1271 | + 'key' => 'int', |
|
| 1272 | + 'value' => '42', |
|
| 1273 | + 'type' => 8, |
|
| 1274 | + 'lazy' => false, |
|
| 1275 | + 'typeString' => 'integer', |
|
| 1276 | + 'sensitive' => false |
|
| 1277 | + ], |
|
| 1278 | + $config->getDetails('typed', 'int') |
|
| 1279 | + ); |
|
| 1280 | + } |
|
| 1281 | + |
|
| 1282 | + public function testGetDetailsFloat(): void { |
|
| 1283 | + $config = $this->generateAppConfig(); |
|
| 1284 | + $this->assertEquals( |
|
| 1285 | + [ |
|
| 1286 | + 'app' => 'typed', |
|
| 1287 | + 'key' => 'float', |
|
| 1288 | + 'value' => '3.14', |
|
| 1289 | + 'type' => 16, |
|
| 1290 | + 'lazy' => false, |
|
| 1291 | + 'typeString' => 'float', |
|
| 1292 | + 'sensitive' => false |
|
| 1293 | + ], |
|
| 1294 | + $config->getDetails('typed', 'float') |
|
| 1295 | + ); |
|
| 1296 | + } |
|
| 1297 | + |
|
| 1298 | + public function testGetDetailsBool(): void { |
|
| 1299 | + $config = $this->generateAppConfig(); |
|
| 1300 | + $this->assertEquals( |
|
| 1301 | + [ |
|
| 1302 | + 'app' => 'typed', |
|
| 1303 | + 'key' => 'bool', |
|
| 1304 | + 'value' => '1', |
|
| 1305 | + 'type' => 32, |
|
| 1306 | + 'lazy' => false, |
|
| 1307 | + 'typeString' => 'boolean', |
|
| 1308 | + 'sensitive' => false |
|
| 1309 | + ], |
|
| 1310 | + $config->getDetails('typed', 'bool') |
|
| 1311 | + ); |
|
| 1312 | + } |
|
| 1313 | + |
|
| 1314 | + public function testGetDetailsArray(): void { |
|
| 1315 | + $config = $this->generateAppConfig(); |
|
| 1316 | + $this->assertEquals( |
|
| 1317 | + [ |
|
| 1318 | + 'app' => 'typed', |
|
| 1319 | + 'key' => 'array', |
|
| 1320 | + 'value' => '{"test": 1}', |
|
| 1321 | + 'type' => 64, |
|
| 1322 | + 'lazy' => false, |
|
| 1323 | + 'typeString' => 'array', |
|
| 1324 | + 'sensitive' => false |
|
| 1325 | + ], |
|
| 1326 | + $config->getDetails('typed', 'array') |
|
| 1327 | + ); |
|
| 1328 | + } |
|
| 1329 | + |
|
| 1330 | + public function testDeleteKey(): void { |
|
| 1331 | + $config = $this->generateAppConfig(); |
|
| 1332 | + $config->deleteKey('anotherapp', 'key'); |
|
| 1333 | + $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default')); |
|
| 1334 | + } |
|
| 1335 | + |
|
| 1336 | + public function testDeleteKeyCache(): void { |
|
| 1337 | + $config = $this->generateAppConfig(); |
|
| 1338 | + $config->deleteKey('anotherapp', 'key'); |
|
| 1339 | + $status = $config->statusCache(); |
|
| 1340 | + $this->assertEqualsCanonicalizing(['enabled' => 'no', 'installed_version' => '3.2.1'], $status['fastCache']['anotherapp']); |
|
| 1341 | + } |
|
| 1342 | + |
|
| 1343 | + public function testDeleteKeyDatabase(): void { |
|
| 1344 | + $config = $this->generateAppConfig(); |
|
| 1345 | + $config->deleteKey('anotherapp', 'key'); |
|
| 1346 | + $config->clearCache(); |
|
| 1347 | + $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default')); |
|
| 1348 | + } |
|
| 1349 | + |
|
| 1350 | + public function testDeleteApp(): void { |
|
| 1351 | + $config = $this->generateAppConfig(); |
|
| 1352 | + $config->deleteApp('anotherapp'); |
|
| 1353 | + $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default')); |
|
| 1354 | + $this->assertSame('default', $config->getValueString('anotherapp', 'enabled', 'default')); |
|
| 1355 | + } |
|
| 1356 | + |
|
| 1357 | + public function testDeleteAppCache(): void { |
|
| 1358 | + $config = $this->generateAppConfig(); |
|
| 1359 | + $status = $config->statusCache(); |
|
| 1360 | + $this->assertSame(true, isset($status['fastCache']['anotherapp'])); |
|
| 1361 | + $config->deleteApp('anotherapp'); |
|
| 1362 | + $status = $config->statusCache(); |
|
| 1363 | + $this->assertSame(false, isset($status['fastCache']['anotherapp'])); |
|
| 1364 | + } |
|
| 1365 | + |
|
| 1366 | + public function testDeleteAppDatabase(): void { |
|
| 1367 | + $config = $this->generateAppConfig(); |
|
| 1368 | + $config->deleteApp('anotherapp'); |
|
| 1369 | + $config->clearCache(); |
|
| 1370 | + $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default')); |
|
| 1371 | + $this->assertSame('default', $config->getValueString('anotherapp', 'enabled', 'default')); |
|
| 1372 | + } |
|
| 1373 | + |
|
| 1374 | + public function testClearCache(): void { |
|
| 1375 | + $config = $this->generateAppConfig(); |
|
| 1376 | + $config->setValueString('feed', 'string', '123454'); |
|
| 1377 | + $config->clearCache(); |
|
| 1378 | + $status = $config->statusCache(); |
|
| 1379 | + $this->assertSame([], $status['fastCache']); |
|
| 1380 | + } |
|
| 1381 | + |
|
| 1382 | + public function testSensitiveValuesAreEncrypted(): void { |
|
| 1383 | + $key = self::getUniqueID('secret'); |
|
| 1384 | + |
|
| 1385 | + $appConfig = $this->generateAppConfig(); |
|
| 1386 | + $secret = md5((string)time()); |
|
| 1387 | + $appConfig->setValueString('testapp', $key, $secret, sensitive: true); |
|
| 1388 | + |
|
| 1389 | + $this->assertConfigValueNotEquals('testapp', $key, $secret); |
|
| 1390 | + |
|
| 1391 | + // Can get in same run |
|
| 1392 | + $actualSecret = $appConfig->getValueString('testapp', $key); |
|
| 1393 | + $this->assertEquals($secret, $actualSecret); |
|
| 1394 | + |
|
| 1395 | + // Can get freshly decrypted from DB |
|
| 1396 | + $newAppConfig = $this->generateAppConfig(); |
|
| 1397 | + $actualSecret = $newAppConfig->getValueString('testapp', $key); |
|
| 1398 | + $this->assertEquals($secret, $actualSecret); |
|
| 1399 | + } |
|
| 1400 | + |
|
| 1401 | + public function testMigratingNonSensitiveValueToSensitiveWithSetValue(): void { |
|
| 1402 | + $key = self::getUniqueID('secret'); |
|
| 1403 | + $appConfig = $this->generateAppConfig(); |
|
| 1404 | + $secret = sha1((string)time()); |
|
| 1405 | + |
|
| 1406 | + // Unencrypted |
|
| 1407 | + $appConfig->setValueString('testapp', $key, $secret); |
|
| 1408 | + $this->assertConfigKey('testapp', $key, $secret); |
|
| 1409 | + |
|
| 1410 | + // Can get freshly decrypted from DB |
|
| 1411 | + $newAppConfig = $this->generateAppConfig(); |
|
| 1412 | + $actualSecret = $newAppConfig->getValueString('testapp', $key); |
|
| 1413 | + $this->assertEquals($secret, $actualSecret); |
|
| 1414 | + |
|
| 1415 | + // Encrypting on change |
|
| 1416 | + $appConfig->setValueString('testapp', $key, $secret, sensitive: true); |
|
| 1417 | + $this->assertConfigValueNotEquals('testapp', $key, $secret); |
|
| 1418 | + |
|
| 1419 | + // Can get in same run |
|
| 1420 | + $actualSecret = $appConfig->getValueString('testapp', $key); |
|
| 1421 | + $this->assertEquals($secret, $actualSecret); |
|
| 1422 | + |
|
| 1423 | + // Can get freshly decrypted from DB |
|
| 1424 | + $newAppConfig = $this->generateAppConfig(); |
|
| 1425 | + $actualSecret = $newAppConfig->getValueString('testapp', $key); |
|
| 1426 | + $this->assertEquals($secret, $actualSecret); |
|
| 1427 | + } |
|
| 1428 | + |
|
| 1429 | + public function testUpdateSensitiveValueToNonSensitiveWithUpdateSensitive(): void { |
|
| 1430 | + $key = self::getUniqueID('secret'); |
|
| 1431 | + $appConfig = $this->generateAppConfig(); |
|
| 1432 | + $secret = sha1((string)time()); |
|
| 1433 | + |
|
| 1434 | + // Encrypted |
|
| 1435 | + $appConfig->setValueString('testapp', $key, $secret, sensitive: true); |
|
| 1436 | + $this->assertConfigValueNotEquals('testapp', $key, $secret); |
|
| 1437 | + |
|
| 1438 | + // Migrate to non-sensitive / non-encrypted |
|
| 1439 | + $appConfig->updateSensitive('testapp', $key, false); |
|
| 1440 | + $this->assertConfigKey('testapp', $key, $secret); |
|
| 1441 | + } |
|
| 1442 | + |
|
| 1443 | + public function testUpdateNonSensitiveValueToSensitiveWithUpdateSensitive(): void { |
|
| 1444 | + $key = self::getUniqueID('secret'); |
|
| 1445 | + $appConfig = $this->generateAppConfig(); |
|
| 1446 | + $secret = sha1((string)time()); |
|
| 1447 | + |
|
| 1448 | + // Unencrypted |
|
| 1449 | + $appConfig->setValueString('testapp', $key, $secret); |
|
| 1450 | + $this->assertConfigKey('testapp', $key, $secret); |
|
| 1451 | + |
|
| 1452 | + // Migrate to sensitive / encrypted |
|
| 1453 | + $appConfig->updateSensitive('testapp', $key, true); |
|
| 1454 | + $this->assertConfigValueNotEquals('testapp', $key, $secret); |
|
| 1455 | + } |
|
| 1456 | + |
|
| 1457 | + protected function loadConfigValueFromDatabase(string $app, string $key): string|false { |
|
| 1458 | + $sql = $this->connection->getQueryBuilder(); |
|
| 1459 | + $sql->select('configvalue') |
|
| 1460 | + ->from('appconfig') |
|
| 1461 | + ->where($sql->expr()->eq('appid', $sql->createParameter('appid'))) |
|
| 1462 | + ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey'))) |
|
| 1463 | + ->setParameter('appid', $app) |
|
| 1464 | + ->setParameter('configkey', $key); |
|
| 1465 | + $query = $sql->executeQuery(); |
|
| 1466 | + $actual = $query->fetchOne(); |
|
| 1467 | + $query->closeCursor(); |
|
| 1468 | + |
|
| 1469 | + return $actual; |
|
| 1470 | + } |
|
| 1471 | + |
|
| 1472 | + protected function assertConfigKey(string $app, string $key, string|false $expected): void { |
|
| 1473 | + $this->assertEquals($expected, $this->loadConfigValueFromDatabase($app, $key)); |
|
| 1474 | + } |
|
| 1475 | + |
|
| 1476 | + protected function assertConfigValueNotEquals(string $app, string $key, string|false $expected): void { |
|
| 1477 | + $this->assertNotEquals($expected, $this->loadConfigValueFromDatabase($app, $key)); |
|
| 1478 | + } |
|
| 1479 | 1479 | } |
@@ -28,204 +28,204 @@ |
||
| 28 | 28 | * @package Test |
| 29 | 29 | */ |
| 30 | 30 | class LexiconTest extends TestCase { |
| 31 | - /** @var AppConfig */ |
|
| 32 | - private IAppConfig $appConfig; |
|
| 33 | - private IUserConfig $userConfig; |
|
| 34 | - private ConfigManager $configManager; |
|
| 35 | - |
|
| 36 | - protected function setUp(): void { |
|
| 37 | - parent::setUp(); |
|
| 38 | - |
|
| 39 | - $bootstrapCoordinator = Server::get(Coordinator::class); |
|
| 40 | - $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_I::APPID, TestConfigLexicon_I::class); |
|
| 41 | - $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_N::APPID, TestConfigLexicon_N::class); |
|
| 42 | - $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_W::APPID, TestConfigLexicon_W::class); |
|
| 43 | - $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_E::APPID, TestConfigLexicon_E::class); |
|
| 44 | - |
|
| 45 | - $this->appConfig = Server::get(IAppConfig::class); |
|
| 46 | - $this->userConfig = Server::get(IUserConfig::class); |
|
| 47 | - $this->configManager = Server::get(ConfigManager::class); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - protected function tearDown(): void { |
|
| 51 | - parent::tearDown(); |
|
| 52 | - |
|
| 53 | - $this->appConfig->deleteApp(TestConfigLexicon_I::APPID); |
|
| 54 | - $this->appConfig->deleteApp(TestConfigLexicon_N::APPID); |
|
| 55 | - $this->appConfig->deleteApp(TestConfigLexicon_W::APPID); |
|
| 56 | - $this->appConfig->deleteApp(TestConfigLexicon_E::APPID); |
|
| 57 | - |
|
| 58 | - $this->userConfig->deleteApp(TestConfigLexicon_I::APPID); |
|
| 59 | - $this->userConfig->deleteApp(TestConfigLexicon_N::APPID); |
|
| 60 | - $this->userConfig->deleteApp(TestConfigLexicon_W::APPID); |
|
| 61 | - $this->userConfig->deleteApp(TestConfigLexicon_E::APPID); |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - public function testAppLexiconSetCorrect() { |
|
| 65 | - $this->assertSame(true, $this->appConfig->setValueString(TestConfigLexicon_E::APPID, 'key1', 'new_value')); |
|
| 66 | - $this->assertSame(true, $this->appConfig->isLazy(TestConfigLexicon_E::APPID, 'key1')); |
|
| 67 | - $this->assertSame(true, $this->appConfig->isSensitive(TestConfigLexicon_E::APPID, 'key1')); |
|
| 68 | - $this->appConfig->deleteKey(TestConfigLexicon_E::APPID, 'key1'); |
|
| 69 | - } |
|
| 70 | - |
|
| 71 | - public function testAppLexiconGetCorrect() { |
|
| 72 | - $this->assertSame('abcde', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key1', 'default')); |
|
| 73 | - } |
|
| 74 | - |
|
| 75 | - public function testAppLexiconSetIncorrectValueType() { |
|
| 76 | - $this->expectException(AppConfigTypeConflictException::class); |
|
| 77 | - $this->appConfig->setValueInt(TestConfigLexicon_E::APPID, 'key1', -1); |
|
| 78 | - } |
|
| 79 | - |
|
| 80 | - public function testAppLexiconGetIncorrectValueType() { |
|
| 81 | - $this->expectException(AppConfigTypeConflictException::class); |
|
| 82 | - $this->appConfig->getValueInt(TestConfigLexicon_E::APPID, 'key1'); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - public function testAppLexiconIgnore() { |
|
| 86 | - $this->appConfig->setValueString(TestConfigLexicon_I::APPID, 'key_ignore', 'new_value'); |
|
| 87 | - $this->assertSame('new_value', $this->appConfig->getValueString(TestConfigLexicon_I::APPID, 'key_ignore', '')); |
|
| 88 | - } |
|
| 89 | - |
|
| 90 | - public function testAppLexiconNotice() { |
|
| 91 | - $this->appConfig->setValueString(TestConfigLexicon_N::APPID, 'key_notice', 'new_value'); |
|
| 92 | - $this->assertSame('new_value', $this->appConfig->getValueString(TestConfigLexicon_N::APPID, 'key_notice', '')); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - public function testAppLexiconWarning() { |
|
| 96 | - $this->appConfig->setValueString(TestConfigLexicon_W::APPID, 'key_warning', 'new_value'); |
|
| 97 | - $this->assertSame('', $this->appConfig->getValueString(TestConfigLexicon_W::APPID, 'key_warning', '')); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - public function testAppLexiconSetException() { |
|
| 101 | - $this->expectException(AppConfigUnknownKeyException::class); |
|
| 102 | - $this->appConfig->setValueString(TestConfigLexicon_E::APPID, 'key_exception', 'new_value'); |
|
| 103 | - $this->assertSame('', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3', '')); |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - public function testAppLexiconGetException() { |
|
| 107 | - $this->expectException(AppConfigUnknownKeyException::class); |
|
| 108 | - $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key_exception'); |
|
| 109 | - } |
|
| 110 | - |
|
| 111 | - public function testUserLexiconSetCorrect() { |
|
| 112 | - $this->assertSame(true, $this->userConfig->setValueString('user1', TestConfigLexicon_E::APPID, 'key1', 'new_value')); |
|
| 113 | - $this->assertSame(true, $this->userConfig->isLazy('user1', TestConfigLexicon_E::APPID, 'key1')); |
|
| 114 | - $this->assertSame(true, $this->userConfig->isSensitive('user1', TestConfigLexicon_E::APPID, 'key1')); |
|
| 115 | - $this->userConfig->deleteKey(TestConfigLexicon_E::APPID, 'key1'); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - public function testUserLexiconGetCorrect() { |
|
| 119 | - $this->assertSame('abcde', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key1', 'default')); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - public function testUserLexiconSetIncorrectValueType() { |
|
| 123 | - $this->expectException(TypeConflictException::class); |
|
| 124 | - $this->userConfig->setValueInt('user1', TestConfigLexicon_E::APPID, 'key1', -1); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - public function testUserLexiconGetIncorrectValueType() { |
|
| 128 | - $this->expectException(TypeConflictException::class); |
|
| 129 | - $this->userConfig->getValueInt('user1', TestConfigLexicon_E::APPID, 'key1'); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - public function testUserLexiconIgnore() { |
|
| 133 | - $this->userConfig->setValueString('user1', TestConfigLexicon_I::APPID, 'key_ignore', 'new_value'); |
|
| 134 | - $this->assertSame('new_value', $this->userConfig->getValueString('user1', TestConfigLexicon_I::APPID, 'key_ignore', '')); |
|
| 135 | - } |
|
| 136 | - |
|
| 137 | - public function testUserLexiconNotice() { |
|
| 138 | - $this->userConfig->setValueString('user1', TestConfigLexicon_N::APPID, 'key_notice', 'new_value'); |
|
| 139 | - $this->assertSame('new_value', $this->userConfig->getValueString('user1', TestConfigLexicon_N::APPID, 'key_notice', '')); |
|
| 140 | - } |
|
| 141 | - |
|
| 142 | - public function testUserLexiconWarning() { |
|
| 143 | - $this->userConfig->setValueString('user1', TestConfigLexicon_W::APPID, 'key_warning', 'new_value'); |
|
| 144 | - $this->assertSame('', $this->userConfig->getValueString('user1', TestConfigLexicon_W::APPID, 'key_warning', '')); |
|
| 145 | - } |
|
| 146 | - |
|
| 147 | - public function testUserLexiconSetException() { |
|
| 148 | - $this->expectException(UnknownKeyException::class); |
|
| 149 | - $this->userConfig->setValueString('user1', TestConfigLexicon_E::APPID, 'key_exception', 'new_value'); |
|
| 150 | - $this->assertSame('', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key5', '')); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - public function testUserLexiconGetException() { |
|
| 154 | - $this->expectException(UnknownKeyException::class); |
|
| 155 | - $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key_exception'); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - public function testAppConfigLexiconRenameSetNewValue() { |
|
| 159 | - $this->assertSame(12345, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); |
|
| 160 | - $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'old_key3', 994); |
|
| 161 | - $this->assertSame(994, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - public function testAppConfigLexiconRenameSetOldValuePreMigration() { |
|
| 165 | - $this->appConfig->ignoreLexiconAliases(true); |
|
| 166 | - $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'old_key3', 993); |
|
| 167 | - $this->appConfig->ignoreLexiconAliases(false); |
|
| 168 | - $this->assertSame(12345, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); |
|
| 169 | - } |
|
| 170 | - |
|
| 171 | - public function testAppConfigLexiconRenameSetOldValuePostMigration() { |
|
| 172 | - $this->appConfig->ignoreLexiconAliases(true); |
|
| 173 | - $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'old_key3', 994); |
|
| 174 | - $this->appConfig->ignoreLexiconAliases(false); |
|
| 175 | - $this->configManager->migrateConfigLexiconKeys(TestConfigLexicon_I::APPID); |
|
| 176 | - $this->assertSame(994, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - public function testAppConfigLexiconRenameGetNewValue() { |
|
| 180 | - $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'key3', 981); |
|
| 181 | - $this->assertSame(981, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'old_key3', 123)); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - public function testAppConfigLexiconRenameGetOldValuePreMigration() { |
|
| 185 | - $this->appConfig->ignoreLexiconAliases(true); |
|
| 186 | - $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'key3', 984); |
|
| 187 | - $this->assertSame(123, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'old_key3', 123)); |
|
| 188 | - $this->appConfig->ignoreLexiconAliases(false); |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - public function testAppConfigLexiconRenameGetOldValuePostMigration() { |
|
| 192 | - $this->appConfig->ignoreLexiconAliases(true); |
|
| 193 | - $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'key3', 987); |
|
| 194 | - $this->appConfig->ignoreLexiconAliases(false); |
|
| 195 | - $this->configManager->migrateConfigLexiconKeys(TestConfigLexicon_I::APPID); |
|
| 196 | - $this->assertSame(987, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'old_key3', 123)); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - public function testAppConfigLexiconRenameInvertBoolean() { |
|
| 200 | - $this->appConfig->ignoreLexiconAliases(true); |
|
| 201 | - $this->appConfig->setValueBool(TestConfigLexicon_I::APPID, 'old_key4', true); |
|
| 202 | - $this->appConfig->ignoreLexiconAliases(false); |
|
| 203 | - $this->assertSame(true, $this->appConfig->getValueBool(TestConfigLexicon_I::APPID, 'key4')); |
|
| 204 | - $this->configManager->migrateConfigLexiconKeys(TestConfigLexicon_I::APPID); |
|
| 205 | - $this->assertSame(false, $this->appConfig->getValueBool(TestConfigLexicon_I::APPID, 'key4')); |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - public function testAppConfigLexiconPreset() { |
|
| 209 | - $this->configManager->setLexiconPreset(Preset::FAMILY); |
|
| 210 | - $this->assertSame('family', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3')); |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - public function testAppConfigLexiconPresets() { |
|
| 214 | - $this->configManager->setLexiconPreset(Preset::MEDIUM); |
|
| 215 | - $this->assertSame('club+medium', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3')); |
|
| 216 | - $this->configManager->setLexiconPreset(Preset::FAMILY); |
|
| 217 | - $this->assertSame('family', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3')); |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - public function testUserConfigLexiconPreset() { |
|
| 221 | - $this->configManager->setLexiconPreset(Preset::FAMILY); |
|
| 222 | - $this->assertSame('family', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key3')); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - public function testUserConfigLexiconPresets() { |
|
| 226 | - $this->configManager->setLexiconPreset(Preset::MEDIUM); |
|
| 227 | - $this->assertSame('club+medium', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key3')); |
|
| 228 | - $this->configManager->setLexiconPreset(Preset::FAMILY); |
|
| 229 | - $this->assertSame('family', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key3')); |
|
| 230 | - } |
|
| 31 | + /** @var AppConfig */ |
|
| 32 | + private IAppConfig $appConfig; |
|
| 33 | + private IUserConfig $userConfig; |
|
| 34 | + private ConfigManager $configManager; |
|
| 35 | + |
|
| 36 | + protected function setUp(): void { |
|
| 37 | + parent::setUp(); |
|
| 38 | + |
|
| 39 | + $bootstrapCoordinator = Server::get(Coordinator::class); |
|
| 40 | + $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_I::APPID, TestConfigLexicon_I::class); |
|
| 41 | + $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_N::APPID, TestConfigLexicon_N::class); |
|
| 42 | + $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_W::APPID, TestConfigLexicon_W::class); |
|
| 43 | + $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon(TestConfigLexicon_E::APPID, TestConfigLexicon_E::class); |
|
| 44 | + |
|
| 45 | + $this->appConfig = Server::get(IAppConfig::class); |
|
| 46 | + $this->userConfig = Server::get(IUserConfig::class); |
|
| 47 | + $this->configManager = Server::get(ConfigManager::class); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + protected function tearDown(): void { |
|
| 51 | + parent::tearDown(); |
|
| 52 | + |
|
| 53 | + $this->appConfig->deleteApp(TestConfigLexicon_I::APPID); |
|
| 54 | + $this->appConfig->deleteApp(TestConfigLexicon_N::APPID); |
|
| 55 | + $this->appConfig->deleteApp(TestConfigLexicon_W::APPID); |
|
| 56 | + $this->appConfig->deleteApp(TestConfigLexicon_E::APPID); |
|
| 57 | + |
|
| 58 | + $this->userConfig->deleteApp(TestConfigLexicon_I::APPID); |
|
| 59 | + $this->userConfig->deleteApp(TestConfigLexicon_N::APPID); |
|
| 60 | + $this->userConfig->deleteApp(TestConfigLexicon_W::APPID); |
|
| 61 | + $this->userConfig->deleteApp(TestConfigLexicon_E::APPID); |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + public function testAppLexiconSetCorrect() { |
|
| 65 | + $this->assertSame(true, $this->appConfig->setValueString(TestConfigLexicon_E::APPID, 'key1', 'new_value')); |
|
| 66 | + $this->assertSame(true, $this->appConfig->isLazy(TestConfigLexicon_E::APPID, 'key1')); |
|
| 67 | + $this->assertSame(true, $this->appConfig->isSensitive(TestConfigLexicon_E::APPID, 'key1')); |
|
| 68 | + $this->appConfig->deleteKey(TestConfigLexicon_E::APPID, 'key1'); |
|
| 69 | + } |
|
| 70 | + |
|
| 71 | + public function testAppLexiconGetCorrect() { |
|
| 72 | + $this->assertSame('abcde', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key1', 'default')); |
|
| 73 | + } |
|
| 74 | + |
|
| 75 | + public function testAppLexiconSetIncorrectValueType() { |
|
| 76 | + $this->expectException(AppConfigTypeConflictException::class); |
|
| 77 | + $this->appConfig->setValueInt(TestConfigLexicon_E::APPID, 'key1', -1); |
|
| 78 | + } |
|
| 79 | + |
|
| 80 | + public function testAppLexiconGetIncorrectValueType() { |
|
| 81 | + $this->expectException(AppConfigTypeConflictException::class); |
|
| 82 | + $this->appConfig->getValueInt(TestConfigLexicon_E::APPID, 'key1'); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + public function testAppLexiconIgnore() { |
|
| 86 | + $this->appConfig->setValueString(TestConfigLexicon_I::APPID, 'key_ignore', 'new_value'); |
|
| 87 | + $this->assertSame('new_value', $this->appConfig->getValueString(TestConfigLexicon_I::APPID, 'key_ignore', '')); |
|
| 88 | + } |
|
| 89 | + |
|
| 90 | + public function testAppLexiconNotice() { |
|
| 91 | + $this->appConfig->setValueString(TestConfigLexicon_N::APPID, 'key_notice', 'new_value'); |
|
| 92 | + $this->assertSame('new_value', $this->appConfig->getValueString(TestConfigLexicon_N::APPID, 'key_notice', '')); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + public function testAppLexiconWarning() { |
|
| 96 | + $this->appConfig->setValueString(TestConfigLexicon_W::APPID, 'key_warning', 'new_value'); |
|
| 97 | + $this->assertSame('', $this->appConfig->getValueString(TestConfigLexicon_W::APPID, 'key_warning', '')); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + public function testAppLexiconSetException() { |
|
| 101 | + $this->expectException(AppConfigUnknownKeyException::class); |
|
| 102 | + $this->appConfig->setValueString(TestConfigLexicon_E::APPID, 'key_exception', 'new_value'); |
|
| 103 | + $this->assertSame('', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3', '')); |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + public function testAppLexiconGetException() { |
|
| 107 | + $this->expectException(AppConfigUnknownKeyException::class); |
|
| 108 | + $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key_exception'); |
|
| 109 | + } |
|
| 110 | + |
|
| 111 | + public function testUserLexiconSetCorrect() { |
|
| 112 | + $this->assertSame(true, $this->userConfig->setValueString('user1', TestConfigLexicon_E::APPID, 'key1', 'new_value')); |
|
| 113 | + $this->assertSame(true, $this->userConfig->isLazy('user1', TestConfigLexicon_E::APPID, 'key1')); |
|
| 114 | + $this->assertSame(true, $this->userConfig->isSensitive('user1', TestConfigLexicon_E::APPID, 'key1')); |
|
| 115 | + $this->userConfig->deleteKey(TestConfigLexicon_E::APPID, 'key1'); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + public function testUserLexiconGetCorrect() { |
|
| 119 | + $this->assertSame('abcde', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key1', 'default')); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + public function testUserLexiconSetIncorrectValueType() { |
|
| 123 | + $this->expectException(TypeConflictException::class); |
|
| 124 | + $this->userConfig->setValueInt('user1', TestConfigLexicon_E::APPID, 'key1', -1); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + public function testUserLexiconGetIncorrectValueType() { |
|
| 128 | + $this->expectException(TypeConflictException::class); |
|
| 129 | + $this->userConfig->getValueInt('user1', TestConfigLexicon_E::APPID, 'key1'); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + public function testUserLexiconIgnore() { |
|
| 133 | + $this->userConfig->setValueString('user1', TestConfigLexicon_I::APPID, 'key_ignore', 'new_value'); |
|
| 134 | + $this->assertSame('new_value', $this->userConfig->getValueString('user1', TestConfigLexicon_I::APPID, 'key_ignore', '')); |
|
| 135 | + } |
|
| 136 | + |
|
| 137 | + public function testUserLexiconNotice() { |
|
| 138 | + $this->userConfig->setValueString('user1', TestConfigLexicon_N::APPID, 'key_notice', 'new_value'); |
|
| 139 | + $this->assertSame('new_value', $this->userConfig->getValueString('user1', TestConfigLexicon_N::APPID, 'key_notice', '')); |
|
| 140 | + } |
|
| 141 | + |
|
| 142 | + public function testUserLexiconWarning() { |
|
| 143 | + $this->userConfig->setValueString('user1', TestConfigLexicon_W::APPID, 'key_warning', 'new_value'); |
|
| 144 | + $this->assertSame('', $this->userConfig->getValueString('user1', TestConfigLexicon_W::APPID, 'key_warning', '')); |
|
| 145 | + } |
|
| 146 | + |
|
| 147 | + public function testUserLexiconSetException() { |
|
| 148 | + $this->expectException(UnknownKeyException::class); |
|
| 149 | + $this->userConfig->setValueString('user1', TestConfigLexicon_E::APPID, 'key_exception', 'new_value'); |
|
| 150 | + $this->assertSame('', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key5', '')); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + public function testUserLexiconGetException() { |
|
| 154 | + $this->expectException(UnknownKeyException::class); |
|
| 155 | + $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key_exception'); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + public function testAppConfigLexiconRenameSetNewValue() { |
|
| 159 | + $this->assertSame(12345, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); |
|
| 160 | + $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'old_key3', 994); |
|
| 161 | + $this->assertSame(994, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + public function testAppConfigLexiconRenameSetOldValuePreMigration() { |
|
| 165 | + $this->appConfig->ignoreLexiconAliases(true); |
|
| 166 | + $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'old_key3', 993); |
|
| 167 | + $this->appConfig->ignoreLexiconAliases(false); |
|
| 168 | + $this->assertSame(12345, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); |
|
| 169 | + } |
|
| 170 | + |
|
| 171 | + public function testAppConfigLexiconRenameSetOldValuePostMigration() { |
|
| 172 | + $this->appConfig->ignoreLexiconAliases(true); |
|
| 173 | + $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'old_key3', 994); |
|
| 174 | + $this->appConfig->ignoreLexiconAliases(false); |
|
| 175 | + $this->configManager->migrateConfigLexiconKeys(TestConfigLexicon_I::APPID); |
|
| 176 | + $this->assertSame(994, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'key3', 123)); |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + public function testAppConfigLexiconRenameGetNewValue() { |
|
| 180 | + $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'key3', 981); |
|
| 181 | + $this->assertSame(981, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'old_key3', 123)); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + public function testAppConfigLexiconRenameGetOldValuePreMigration() { |
|
| 185 | + $this->appConfig->ignoreLexiconAliases(true); |
|
| 186 | + $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'key3', 984); |
|
| 187 | + $this->assertSame(123, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'old_key3', 123)); |
|
| 188 | + $this->appConfig->ignoreLexiconAliases(false); |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + public function testAppConfigLexiconRenameGetOldValuePostMigration() { |
|
| 192 | + $this->appConfig->ignoreLexiconAliases(true); |
|
| 193 | + $this->appConfig->setValueInt(TestConfigLexicon_I::APPID, 'key3', 987); |
|
| 194 | + $this->appConfig->ignoreLexiconAliases(false); |
|
| 195 | + $this->configManager->migrateConfigLexiconKeys(TestConfigLexicon_I::APPID); |
|
| 196 | + $this->assertSame(987, $this->appConfig->getValueInt(TestConfigLexicon_I::APPID, 'old_key3', 123)); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + public function testAppConfigLexiconRenameInvertBoolean() { |
|
| 200 | + $this->appConfig->ignoreLexiconAliases(true); |
|
| 201 | + $this->appConfig->setValueBool(TestConfigLexicon_I::APPID, 'old_key4', true); |
|
| 202 | + $this->appConfig->ignoreLexiconAliases(false); |
|
| 203 | + $this->assertSame(true, $this->appConfig->getValueBool(TestConfigLexicon_I::APPID, 'key4')); |
|
| 204 | + $this->configManager->migrateConfigLexiconKeys(TestConfigLexicon_I::APPID); |
|
| 205 | + $this->assertSame(false, $this->appConfig->getValueBool(TestConfigLexicon_I::APPID, 'key4')); |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + public function testAppConfigLexiconPreset() { |
|
| 209 | + $this->configManager->setLexiconPreset(Preset::FAMILY); |
|
| 210 | + $this->assertSame('family', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3')); |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + public function testAppConfigLexiconPresets() { |
|
| 214 | + $this->configManager->setLexiconPreset(Preset::MEDIUM); |
|
| 215 | + $this->assertSame('club+medium', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3')); |
|
| 216 | + $this->configManager->setLexiconPreset(Preset::FAMILY); |
|
| 217 | + $this->assertSame('family', $this->appConfig->getValueString(TestConfigLexicon_E::APPID, 'key3')); |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + public function testUserConfigLexiconPreset() { |
|
| 221 | + $this->configManager->setLexiconPreset(Preset::FAMILY); |
|
| 222 | + $this->assertSame('family', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key3')); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + public function testUserConfigLexiconPresets() { |
|
| 226 | + $this->configManager->setLexiconPreset(Preset::MEDIUM); |
|
| 227 | + $this->assertSame('club+medium', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key3')); |
|
| 228 | + $this->configManager->setLexiconPreset(Preset::FAMILY); |
|
| 229 | + $this->assertSame('family', $this->userConfig->getValueString('user1', TestConfigLexicon_E::APPID, 'key3')); |
|
| 230 | + } |
|
| 231 | 231 | } |
@@ -17,33 +17,33 @@ |
||
| 17 | 17 | use OCP\IAppConfig; |
| 18 | 18 | |
| 19 | 19 | class TestConfigLexicon_E implements IConfigLexicon { |
| 20 | - public const APPID = 'lexicon_test_e'; |
|
| 20 | + public const APPID = 'lexicon_test_e'; |
|
| 21 | 21 | |
| 22 | - public function getStrictness(): ConfigLexiconStrictness { |
|
| 23 | - return ConfigLexiconStrictness::EXCEPTION; |
|
| 24 | - } |
|
| 22 | + public function getStrictness(): ConfigLexiconStrictness { |
|
| 23 | + return ConfigLexiconStrictness::EXCEPTION; |
|
| 24 | + } |
|
| 25 | 25 | |
| 26 | - public function getAppConfigs(): array { |
|
| 27 | - return [ |
|
| 28 | - new ConfigLexiconEntry('key1', ValueType::STRING, 'abcde', 'test key', true, IAppConfig::FLAG_SENSITIVE), |
|
| 29 | - new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false), |
|
| 30 | - new ConfigLexiconEntry('key3', ValueType::STRING, fn (Preset $p): string => match ($p) { |
|
| 31 | - Preset::FAMILY => 'family', |
|
| 32 | - Preset::CLUB, Preset::MEDIUM => 'club+medium', |
|
| 33 | - default => 'none', |
|
| 34 | - }, 'test key'), |
|
| 35 | - ]; |
|
| 36 | - } |
|
| 26 | + public function getAppConfigs(): array { |
|
| 27 | + return [ |
|
| 28 | + new ConfigLexiconEntry('key1', ValueType::STRING, 'abcde', 'test key', true, IAppConfig::FLAG_SENSITIVE), |
|
| 29 | + new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false), |
|
| 30 | + new ConfigLexiconEntry('key3', ValueType::STRING, fn (Preset $p): string => match ($p) { |
|
| 31 | + Preset::FAMILY => 'family', |
|
| 32 | + Preset::CLUB, Preset::MEDIUM => 'club+medium', |
|
| 33 | + default => 'none', |
|
| 34 | + }, 'test key'), |
|
| 35 | + ]; |
|
| 36 | + } |
|
| 37 | 37 | |
| 38 | - public function getUserConfigs(): array { |
|
| 39 | - return [ |
|
| 40 | - new ConfigLexiconEntry('key1', ValueType::STRING, 'abcde', 'test key', true, IUserConfig::FLAG_SENSITIVE), |
|
| 41 | - new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false), |
|
| 42 | - new ConfigLexiconEntry('key3', ValueType::STRING, fn (Preset $p): string => match ($p) { |
|
| 43 | - Preset::FAMILY => 'family', |
|
| 44 | - Preset::CLUB, Preset::MEDIUM => 'club+medium', |
|
| 45 | - default => 'none', |
|
| 46 | - }, 'test key'), |
|
| 47 | - ]; |
|
| 48 | - } |
|
| 38 | + public function getUserConfigs(): array { |
|
| 39 | + return [ |
|
| 40 | + new ConfigLexiconEntry('key1', ValueType::STRING, 'abcde', 'test key', true, IUserConfig::FLAG_SENSITIVE), |
|
| 41 | + new ConfigLexiconEntry('key2', ValueType::INT, 12345, 'test key', false), |
|
| 42 | + new ConfigLexiconEntry('key3', ValueType::STRING, fn (Preset $p): string => match ($p) { |
|
| 43 | + Preset::FAMILY => 'family', |
|
| 44 | + Preset::CLUB, Preset::MEDIUM => 'club+medium', |
|
| 45 | + default => 'none', |
|
| 46 | + }, 'test key'), |
|
| 47 | + ]; |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -123,142 +123,142 @@ |
||
| 123 | 123 | $config = Server::get(IConfig::class); |
| 124 | 124 | |
| 125 | 125 | if ($config->getSystemValueBool('installed', false)) { |
| 126 | - $application->add(Server::get(Disable::class)); |
|
| 127 | - $application->add(Server::get(Enable::class)); |
|
| 128 | - $application->add(Server::get(Install::class)); |
|
| 129 | - $application->add(Server::get(GetPath::class)); |
|
| 130 | - $application->add(Server::get(ListApps::class)); |
|
| 131 | - $application->add(Server::get(Remove::class)); |
|
| 132 | - $application->add(Server::get(Update::class)); |
|
| 126 | + $application->add(Server::get(Disable::class)); |
|
| 127 | + $application->add(Server::get(Enable::class)); |
|
| 128 | + $application->add(Server::get(Install::class)); |
|
| 129 | + $application->add(Server::get(GetPath::class)); |
|
| 130 | + $application->add(Server::get(ListApps::class)); |
|
| 131 | + $application->add(Server::get(Remove::class)); |
|
| 132 | + $application->add(Server::get(Update::class)); |
|
| 133 | 133 | |
| 134 | - $application->add(Server::get(Cleanup::class)); |
|
| 135 | - $application->add(Server::get(Enforce::class)); |
|
| 136 | - $application->add(Server::get(Command\TwoFactorAuth\Enable::class)); |
|
| 137 | - $application->add(Server::get(Command\TwoFactorAuth\Disable::class)); |
|
| 138 | - $application->add(Server::get(State::class)); |
|
| 134 | + $application->add(Server::get(Cleanup::class)); |
|
| 135 | + $application->add(Server::get(Enforce::class)); |
|
| 136 | + $application->add(Server::get(Command\TwoFactorAuth\Enable::class)); |
|
| 137 | + $application->add(Server::get(Command\TwoFactorAuth\Disable::class)); |
|
| 138 | + $application->add(Server::get(State::class)); |
|
| 139 | 139 | |
| 140 | - $application->add(Server::get(Mode::class)); |
|
| 141 | - $application->add(Server::get(Job::class)); |
|
| 142 | - $application->add(Server::get(ListCommand::class)); |
|
| 143 | - $application->add(Server::get(Delete::class)); |
|
| 144 | - $application->add(Server::get(JobWorker::class)); |
|
| 140 | + $application->add(Server::get(Mode::class)); |
|
| 141 | + $application->add(Server::get(Job::class)); |
|
| 142 | + $application->add(Server::get(ListCommand::class)); |
|
| 143 | + $application->add(Server::get(Delete::class)); |
|
| 144 | + $application->add(Server::get(JobWorker::class)); |
|
| 145 | 145 | |
| 146 | - $application->add(Server::get(Test::class)); |
|
| 146 | + $application->add(Server::get(Test::class)); |
|
| 147 | 147 | |
| 148 | - $application->add(Server::get(DeleteConfig::class)); |
|
| 149 | - $application->add(Server::get(GetConfig::class)); |
|
| 150 | - $application->add(Server::get(SetConfig::class)); |
|
| 151 | - $application->add(Server::get(Import::class)); |
|
| 152 | - $application->add(Server::get(ListConfigs::class)); |
|
| 153 | - $application->add(Server::get(Preset::class)); |
|
| 154 | - $application->add(Server::get(Command\Config\System\DeleteConfig::class)); |
|
| 155 | - $application->add(Server::get(Command\Config\System\GetConfig::class)); |
|
| 156 | - $application->add(Server::get(Command\Config\System\SetConfig::class)); |
|
| 148 | + $application->add(Server::get(DeleteConfig::class)); |
|
| 149 | + $application->add(Server::get(GetConfig::class)); |
|
| 150 | + $application->add(Server::get(SetConfig::class)); |
|
| 151 | + $application->add(Server::get(Import::class)); |
|
| 152 | + $application->add(Server::get(ListConfigs::class)); |
|
| 153 | + $application->add(Server::get(Preset::class)); |
|
| 154 | + $application->add(Server::get(Command\Config\System\DeleteConfig::class)); |
|
| 155 | + $application->add(Server::get(Command\Config\System\GetConfig::class)); |
|
| 156 | + $application->add(Server::get(Command\Config\System\SetConfig::class)); |
|
| 157 | 157 | |
| 158 | - $application->add(Server::get(File::class)); |
|
| 159 | - $application->add(Server::get(Space::class)); |
|
| 160 | - $application->add(Server::get(Storage::class)); |
|
| 161 | - $application->add(Server::get(Storages::class)); |
|
| 158 | + $application->add(Server::get(File::class)); |
|
| 159 | + $application->add(Server::get(Space::class)); |
|
| 160 | + $application->add(Server::get(Storage::class)); |
|
| 161 | + $application->add(Server::get(Storages::class)); |
|
| 162 | 162 | |
| 163 | - $application->add(Server::get(ConvertType::class)); |
|
| 164 | - $application->add(Server::get(ConvertMysqlToMB4::class)); |
|
| 165 | - $application->add(Server::get(ConvertFilecacheBigInt::class)); |
|
| 166 | - $application->add(Server::get(AddMissingColumns::class)); |
|
| 167 | - $application->add(Server::get(AddMissingIndices::class)); |
|
| 168 | - $application->add(Server::get(AddMissingPrimaryKeys::class)); |
|
| 169 | - $application->add(Server::get(ExpectedSchema::class)); |
|
| 170 | - $application->add(Server::get(ExportSchema::class)); |
|
| 163 | + $application->add(Server::get(ConvertType::class)); |
|
| 164 | + $application->add(Server::get(ConvertMysqlToMB4::class)); |
|
| 165 | + $application->add(Server::get(ConvertFilecacheBigInt::class)); |
|
| 166 | + $application->add(Server::get(AddMissingColumns::class)); |
|
| 167 | + $application->add(Server::get(AddMissingIndices::class)); |
|
| 168 | + $application->add(Server::get(AddMissingPrimaryKeys::class)); |
|
| 169 | + $application->add(Server::get(ExpectedSchema::class)); |
|
| 170 | + $application->add(Server::get(ExportSchema::class)); |
|
| 171 | 171 | |
| 172 | - $application->add(Server::get(GenerateMetadataCommand::class)); |
|
| 173 | - $application->add(Server::get(PreviewCommand::class)); |
|
| 174 | - if ($config->getSystemValueBool('debug', false)) { |
|
| 175 | - $application->add(Server::get(StatusCommand::class)); |
|
| 176 | - $application->add(Server::get(MigrateCommand::class)); |
|
| 177 | - $application->add(Server::get(GenerateCommand::class)); |
|
| 178 | - $application->add(Server::get(ExecuteCommand::class)); |
|
| 179 | - } |
|
| 172 | + $application->add(Server::get(GenerateMetadataCommand::class)); |
|
| 173 | + $application->add(Server::get(PreviewCommand::class)); |
|
| 174 | + if ($config->getSystemValueBool('debug', false)) { |
|
| 175 | + $application->add(Server::get(StatusCommand::class)); |
|
| 176 | + $application->add(Server::get(MigrateCommand::class)); |
|
| 177 | + $application->add(Server::get(GenerateCommand::class)); |
|
| 178 | + $application->add(Server::get(ExecuteCommand::class)); |
|
| 179 | + } |
|
| 180 | 180 | |
| 181 | - $application->add(Server::get(Command\Encryption\Disable::class)); |
|
| 182 | - $application->add(Server::get(Command\Encryption\Enable::class)); |
|
| 183 | - $application->add(Server::get(ListModules::class)); |
|
| 184 | - $application->add(Server::get(SetDefaultModule::class)); |
|
| 185 | - $application->add(Server::get(Command\Encryption\Status::class)); |
|
| 186 | - $application->add(Server::get(EncryptAll::class)); |
|
| 187 | - $application->add(Server::get(DecryptAll::class)); |
|
| 181 | + $application->add(Server::get(Command\Encryption\Disable::class)); |
|
| 182 | + $application->add(Server::get(Command\Encryption\Enable::class)); |
|
| 183 | + $application->add(Server::get(ListModules::class)); |
|
| 184 | + $application->add(Server::get(SetDefaultModule::class)); |
|
| 185 | + $application->add(Server::get(Command\Encryption\Status::class)); |
|
| 186 | + $application->add(Server::get(EncryptAll::class)); |
|
| 187 | + $application->add(Server::get(DecryptAll::class)); |
|
| 188 | 188 | |
| 189 | - $application->add(Server::get(Manage::class)); |
|
| 190 | - $application->add(Server::get(Command\Log\File::class)); |
|
| 189 | + $application->add(Server::get(Manage::class)); |
|
| 190 | + $application->add(Server::get(Command\Log\File::class)); |
|
| 191 | 191 | |
| 192 | - $application->add(Server::get(ChangeKeyStorageRoot::class)); |
|
| 193 | - $application->add(Server::get(ShowKeyStorageRoot::class)); |
|
| 194 | - $application->add(Server::get(MigrateKeyStorage::class)); |
|
| 192 | + $application->add(Server::get(ChangeKeyStorageRoot::class)); |
|
| 193 | + $application->add(Server::get(ShowKeyStorageRoot::class)); |
|
| 194 | + $application->add(Server::get(MigrateKeyStorage::class)); |
|
| 195 | 195 | |
| 196 | - $application->add(Server::get(DataFingerprint::class)); |
|
| 197 | - $application->add(Server::get(UpdateDB::class)); |
|
| 198 | - $application->add(Server::get(UpdateJS::class)); |
|
| 199 | - $application->add(Server::get(Command\Maintenance\Mode::class)); |
|
| 200 | - $application->add(Server::get(UpdateHtaccess::class)); |
|
| 201 | - $application->add(Server::get(UpdateTheme::class)); |
|
| 196 | + $application->add(Server::get(DataFingerprint::class)); |
|
| 197 | + $application->add(Server::get(UpdateDB::class)); |
|
| 198 | + $application->add(Server::get(UpdateJS::class)); |
|
| 199 | + $application->add(Server::get(Command\Maintenance\Mode::class)); |
|
| 200 | + $application->add(Server::get(UpdateHtaccess::class)); |
|
| 201 | + $application->add(Server::get(UpdateTheme::class)); |
|
| 202 | 202 | |
| 203 | - $application->add(Server::get(Upgrade::class)); |
|
| 204 | - $application->add(Server::get(Repair::class)); |
|
| 205 | - $application->add(Server::get(RepairShareOwnership::class)); |
|
| 203 | + $application->add(Server::get(Upgrade::class)); |
|
| 204 | + $application->add(Server::get(Repair::class)); |
|
| 205 | + $application->add(Server::get(RepairShareOwnership::class)); |
|
| 206 | 206 | |
| 207 | - $application->add(Server::get(Command\Preview\Cleanup::class)); |
|
| 208 | - $application->add(Server::get(Generate::class)); |
|
| 209 | - $application->add(Server::get(Command\Preview\Repair::class)); |
|
| 210 | - $application->add(Server::get(ResetRenderedTexts::class)); |
|
| 207 | + $application->add(Server::get(Command\Preview\Cleanup::class)); |
|
| 208 | + $application->add(Server::get(Generate::class)); |
|
| 209 | + $application->add(Server::get(Command\Preview\Repair::class)); |
|
| 210 | + $application->add(Server::get(ResetRenderedTexts::class)); |
|
| 211 | 211 | |
| 212 | - $application->add(Server::get(Add::class)); |
|
| 213 | - $application->add(Server::get(Command\User\Delete::class)); |
|
| 214 | - $application->add(Server::get(Command\User\Disable::class)); |
|
| 215 | - $application->add(Server::get(Command\User\Enable::class)); |
|
| 216 | - $application->add(Server::get(LastSeen::class)); |
|
| 217 | - $application->add(Server::get(Report::class)); |
|
| 218 | - $application->add(Server::get(ResetPassword::class)); |
|
| 219 | - $application->add(Server::get(Setting::class)); |
|
| 220 | - $application->add(Server::get(Profile::class)); |
|
| 221 | - $application->add(Server::get(Command\User\ListCommand::class)); |
|
| 222 | - $application->add(Server::get(ClearGeneratedAvatarCacheCommand::class)); |
|
| 223 | - $application->add(Server::get(Info::class)); |
|
| 224 | - $application->add(Server::get(SyncAccountDataCommand::class)); |
|
| 225 | - $application->add(Server::get(Command\User\AuthTokens\Add::class)); |
|
| 226 | - $application->add(Server::get(Command\User\AuthTokens\ListCommand::class)); |
|
| 227 | - $application->add(Server::get(Command\User\AuthTokens\Delete::class)); |
|
| 228 | - $application->add(Server::get(Verify::class)); |
|
| 229 | - $application->add(Server::get(Welcome::class)); |
|
| 212 | + $application->add(Server::get(Add::class)); |
|
| 213 | + $application->add(Server::get(Command\User\Delete::class)); |
|
| 214 | + $application->add(Server::get(Command\User\Disable::class)); |
|
| 215 | + $application->add(Server::get(Command\User\Enable::class)); |
|
| 216 | + $application->add(Server::get(LastSeen::class)); |
|
| 217 | + $application->add(Server::get(Report::class)); |
|
| 218 | + $application->add(Server::get(ResetPassword::class)); |
|
| 219 | + $application->add(Server::get(Setting::class)); |
|
| 220 | + $application->add(Server::get(Profile::class)); |
|
| 221 | + $application->add(Server::get(Command\User\ListCommand::class)); |
|
| 222 | + $application->add(Server::get(ClearGeneratedAvatarCacheCommand::class)); |
|
| 223 | + $application->add(Server::get(Info::class)); |
|
| 224 | + $application->add(Server::get(SyncAccountDataCommand::class)); |
|
| 225 | + $application->add(Server::get(Command\User\AuthTokens\Add::class)); |
|
| 226 | + $application->add(Server::get(Command\User\AuthTokens\ListCommand::class)); |
|
| 227 | + $application->add(Server::get(Command\User\AuthTokens\Delete::class)); |
|
| 228 | + $application->add(Server::get(Verify::class)); |
|
| 229 | + $application->add(Server::get(Welcome::class)); |
|
| 230 | 230 | |
| 231 | - $application->add(Server::get(Command\Group\Add::class)); |
|
| 232 | - $application->add(Server::get(Command\Group\Delete::class)); |
|
| 233 | - $application->add(Server::get(Command\Group\ListCommand::class)); |
|
| 234 | - $application->add(Server::get(AddUser::class)); |
|
| 235 | - $application->add(Server::get(RemoveUser::class)); |
|
| 236 | - $application->add(Server::get(Command\Group\Info::class)); |
|
| 231 | + $application->add(Server::get(Command\Group\Add::class)); |
|
| 232 | + $application->add(Server::get(Command\Group\Delete::class)); |
|
| 233 | + $application->add(Server::get(Command\Group\ListCommand::class)); |
|
| 234 | + $application->add(Server::get(AddUser::class)); |
|
| 235 | + $application->add(Server::get(RemoveUser::class)); |
|
| 236 | + $application->add(Server::get(Command\Group\Info::class)); |
|
| 237 | 237 | |
| 238 | - $application->add(Server::get(Command\SystemTag\ListCommand::class)); |
|
| 239 | - $application->add(Server::get(Command\SystemTag\Delete::class)); |
|
| 240 | - $application->add(Server::get(Command\SystemTag\Add::class)); |
|
| 241 | - $application->add(Server::get(Edit::class)); |
|
| 238 | + $application->add(Server::get(Command\SystemTag\ListCommand::class)); |
|
| 239 | + $application->add(Server::get(Command\SystemTag\Delete::class)); |
|
| 240 | + $application->add(Server::get(Command\SystemTag\Add::class)); |
|
| 241 | + $application->add(Server::get(Edit::class)); |
|
| 242 | 242 | |
| 243 | - $application->add(Server::get(ListCertificates::class)); |
|
| 244 | - $application->add(Server::get(ExportCertificates::class)); |
|
| 245 | - $application->add(Server::get(ImportCertificate::class)); |
|
| 246 | - $application->add(Server::get(RemoveCertificate::class)); |
|
| 247 | - $application->add(Server::get(BruteforceAttempts::class)); |
|
| 248 | - $application->add(Server::get(BruteforceResetAttempts::class)); |
|
| 249 | - $application->add(Server::get(SetupChecks::class)); |
|
| 250 | - $application->add(Server::get(Get::class)); |
|
| 243 | + $application->add(Server::get(ListCertificates::class)); |
|
| 244 | + $application->add(Server::get(ExportCertificates::class)); |
|
| 245 | + $application->add(Server::get(ImportCertificate::class)); |
|
| 246 | + $application->add(Server::get(RemoveCertificate::class)); |
|
| 247 | + $application->add(Server::get(BruteforceAttempts::class)); |
|
| 248 | + $application->add(Server::get(BruteforceResetAttempts::class)); |
|
| 249 | + $application->add(Server::get(SetupChecks::class)); |
|
| 250 | + $application->add(Server::get(Get::class)); |
|
| 251 | 251 | |
| 252 | - $application->add(Server::get(GetCommand::class)); |
|
| 253 | - $application->add(Server::get(EnabledCommand::class)); |
|
| 254 | - $application->add(Server::get(Command\TaskProcessing\ListCommand::class)); |
|
| 255 | - $application->add(Server::get(Statistics::class)); |
|
| 252 | + $application->add(Server::get(GetCommand::class)); |
|
| 253 | + $application->add(Server::get(EnabledCommand::class)); |
|
| 254 | + $application->add(Server::get(Command\TaskProcessing\ListCommand::class)); |
|
| 255 | + $application->add(Server::get(Statistics::class)); |
|
| 256 | 256 | |
| 257 | - $application->add(Server::get(RedisCommand::class)); |
|
| 258 | - $application->add(Server::get(DistributedClear::class)); |
|
| 259 | - $application->add(Server::get(DistributedDelete::class)); |
|
| 260 | - $application->add(Server::get(DistributedGet::class)); |
|
| 261 | - $application->add(Server::get(DistributedSet::class)); |
|
| 257 | + $application->add(Server::get(RedisCommand::class)); |
|
| 258 | + $application->add(Server::get(DistributedClear::class)); |
|
| 259 | + $application->add(Server::get(DistributedDelete::class)); |
|
| 260 | + $application->add(Server::get(DistributedGet::class)); |
|
| 261 | + $application->add(Server::get(DistributedSet::class)); |
|
| 262 | 262 | } else { |
| 263 | - $application->add(Server::get(Command\Maintenance\Install::class)); |
|
| 263 | + $application->add(Server::get(Command\Maintenance\Install::class)); |
|
| 264 | 264 | } |