mirror of https://github.com/nextcloud/server.git
feat(config): implement config lexicon
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>pull/44371/merge^2
parent
5b85562784
commit
40ca27599f
@ -0,0 +1,197 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2024 Maxence Lange <maxence@artificial-owl.com>
|
||||
*
|
||||
* @author Maxence Lange <maxence@artificial-owl.com>
|
||||
*
|
||||
* @license AGPL-3.0 or later
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCP\ConfigLexicon;
|
||||
|
||||
/**
|
||||
* Model that represent config values within an app config lexicon.
|
||||
*
|
||||
* @see IConfigLexicon
|
||||
* @since 30.0.0
|
||||
*/
|
||||
class ConfigLexiconEntry implements IConfigLexiconEntry {
|
||||
private string $definition = '';
|
||||
private ?string $default = null;
|
||||
|
||||
/**
|
||||
* @param string $key config key
|
||||
* @param int $valueType type of config value ({@see self::TYPE_STRING} and others)
|
||||
* @param string $definition optional description of config key available when using occ command
|
||||
* @param bool $lazy set config value as lazy
|
||||
* @param bool $sensitive set config value as sensitive
|
||||
* @param bool $deprecated set config key as deprecated
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly string $key,
|
||||
private readonly int $valueType,
|
||||
string $definition = '',
|
||||
private readonly bool $lazy = false,
|
||||
private readonly bool $sensitive = false,
|
||||
private readonly bool $deprecated = false
|
||||
) {
|
||||
/** @psalm-suppress UndefinedClass */
|
||||
if (\OC::$CLI) { // only store definition if ran from CLI
|
||||
$this->definition = $definition;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @return string config key
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function getKey(): string {
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @return int
|
||||
* @see self::TYPE_STRING and others
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function getValueType(): int {
|
||||
return $this->valueType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @param string $default
|
||||
*
|
||||
* @return self
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function withDefaultString(string $default): self {
|
||||
$this->default = $default;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @param int $default
|
||||
*
|
||||
* @return self
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function withDefaultInt(int $default): self {
|
||||
$this->default = (string) $default;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @param float $default
|
||||
*
|
||||
* @return self
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function withDefaultFloat(float $default): self {
|
||||
$this->default = (string) $default;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @param bool $default
|
||||
*
|
||||
* @return self
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function withDefaultBool(bool $default): self {
|
||||
$this->default = ($default) ? '1' : '0';
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @param array $default
|
||||
*
|
||||
* @return self
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function withDefaultArray(array $default): self {
|
||||
$this->default = json_encode($default);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @return string|null NULL if no default is set
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function getDefault(): ?string {
|
||||
return $this->default;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @return string
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function getDefinition(): string {
|
||||
return $this->definition;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @see IAppConfig for details on lazy config values
|
||||
* @return bool TRUE if config value is lazy
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function isLazy(): bool {
|
||||
return $this->lazy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @see IAppConfig for details on sensitive config values
|
||||
* @return bool TRUE if config value is sensitive
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function isSensitive(): bool {
|
||||
return $this->sensitive;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*
|
||||
* @return bool TRUE if config si deprecated
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function isDeprecated(): bool {
|
||||
return $this->deprecated;
|
||||
}
|
||||
}
|
@ -0,0 +1,59 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2024 Maxence Lange <maxence@artificial-owl.com>
|
||||
*
|
||||
* @author Maxence Lange <maxence@artificial-owl.com>
|
||||
*
|
||||
* @license AGPL-3.0 or later
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCP\ConfigLexicon;
|
||||
|
||||
/**
|
||||
* This interface needs to be implemented if you want to define a config lexicon for your application
|
||||
* The config lexicon is used to avoid conflicts and problems when storing/retrieving config values
|
||||
*
|
||||
* @since 30.0.0
|
||||
*/
|
||||
interface IConfigLexicon {
|
||||
|
||||
/**
|
||||
* set your application config lexicon as strict or not.
|
||||
* When set as strict, using a config key not set in the lexicon will throw an exception.
|
||||
*
|
||||
* @return bool
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function isStrict(): bool;
|
||||
|
||||
/**
|
||||
* define the list of entries of your application config lexicon, related to AppConfig.
|
||||
*
|
||||
* @return IConfigLexiconEntry[]
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function getAppConfigs(): array;
|
||||
|
||||
/**
|
||||
* define the list of entries of your application config lexicon, related to UserPreference.
|
||||
*
|
||||
* @return IConfigLexiconEntry[]
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function getUserPreferences(): array;
|
||||
}
|
@ -0,0 +1,158 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
/**
|
||||
* @copyright Copyright (c) 2024 Maxence Lange <maxence@artificial-owl.com>
|
||||
*
|
||||
* @author Maxence Lange <maxence@artificial-owl.com>
|
||||
*
|
||||
* @license AGPL-3.0 or later
|
||||
*
|
||||
* This code is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License, version 3,
|
||||
* as published by the Free Software Foundation.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License, version 3,
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
||||
*
|
||||
*/
|
||||
|
||||
namespace OCP\ConfigLexicon;
|
||||
|
||||
use OCP\IAppConfig;
|
||||
|
||||
/**
|
||||
* Model that represent config values within an app config lexicon.
|
||||
*
|
||||
* @see IConfigLexicon
|
||||
* @since 30.0.0
|
||||
*/
|
||||
interface IConfigLexiconEntry {
|
||||
/** @since 30.0.0 */
|
||||
public const TYPE_STRING = 1;
|
||||
/** @since 30.0.0 */
|
||||
public const TYPE_INT = 2;
|
||||
/** @since 30.0.0 */
|
||||
public const TYPE_FLOAT = 3;
|
||||
/** @since 30.0.0 */
|
||||
public const TYPE_BOOL = 4;
|
||||
/** @since 30.0.0 */
|
||||
public const TYPE_ARRAY = 5;
|
||||
|
||||
/**
|
||||
* returns the config key.
|
||||
*
|
||||
* @return string config key
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function getKey(): string;
|
||||
|
||||
/**
|
||||
* returns the type of the config value.
|
||||
*
|
||||
* @return int
|
||||
* @see self::TYPE_STRING and others
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function getValueType(): int;
|
||||
|
||||
/**
|
||||
* set default value (as string) for config value.
|
||||
*
|
||||
* @param string $default
|
||||
*
|
||||
* @return self
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function withDefaultString(string $default): self;
|
||||
|
||||
/**
|
||||
* set default value (as int) for config value.
|
||||
*
|
||||
* @param int $default
|
||||
*
|
||||
* @return self
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function withDefaultInt(int $default): self;
|
||||
|
||||
/**
|
||||
* set default value (as float) for config value.
|
||||
*
|
||||
* @param float $default
|
||||
*
|
||||
* @return self
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function withDefaultFloat(float $default): self;
|
||||
|
||||
/**
|
||||
* set default value (as bool) for config value.
|
||||
*
|
||||
* @param bool $default
|
||||
*
|
||||
* @return self
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function withDefaultBool(bool $default): self;
|
||||
|
||||
/**
|
||||
* set default value (as array) for config value.
|
||||
*
|
||||
* @param array $default
|
||||
*
|
||||
* @return self
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function withDefaultArray(array $default): self;
|
||||
|
||||
/**
|
||||
* returns the default value set for this config key.
|
||||
* default value is returned as string or NULL if not set.
|
||||
*
|
||||
* @return string|null NULL if no default is set
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function getDefault(): ?string;
|
||||
|
||||
|
||||
/**
|
||||
* returns the description for config key, only available when process is initiated from occ.
|
||||
* returns empty string if not set or if process is not initiated from occ.
|
||||
*
|
||||
* @return string
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function getDefinition(): string;
|
||||
|
||||
/**
|
||||
* returns if config value is set as LAZY.
|
||||
*
|
||||
* @see IAppConfig for details on lazy config values
|
||||
* @return bool TRUE if config value is lazy
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function isLazy(): bool;
|
||||
|
||||
/**
|
||||
* returns if config value is set as SENSITIVE.
|
||||
*
|
||||
* @see IAppConfig for details on sensitive config values
|
||||
* @return bool TRUE if config value is sensitive
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function isSensitive(): bool;
|
||||
|
||||
/**
|
||||
* returns if config key is deprecated.
|
||||
*
|
||||
* @return bool TRUE if config si deprecated
|
||||
* @since 30.0.0
|
||||
*/
|
||||
public function isDeprecated(): bool;
|
||||
}
|
Loading…
Reference in New Issue