class ConfigBase
Same name in other branches
- 9 core/lib/Drupal/Core/Config/ConfigBase.php \Drupal\Core\Config\ConfigBase
- 8.9.x core/lib/Drupal/Core/Config/ConfigBase.php \Drupal\Core\Config\ConfigBase
- 10 core/lib/Drupal/Core/Config/ConfigBase.php \Drupal\Core\Config\ConfigBase
Provides a base class for configuration objects with get/set support.
Encapsulates all capabilities needed for runtime configuration handling for a specific configuration object.
Extend directly from this class for non-storable configuration where the configuration API is desired but storage is not possible; for example, if the data is derived at runtime. For storable configuration, extend \Drupal\Core\Config\StorableConfigBase.
Hierarchy
- class \Drupal\Core\Config\ConfigBase implements \Drupal\Core\Cache\RefinableCacheableDependencyInterface uses \Drupal\Core\DependencyInjection\DependencySerializationTrait, \Drupal\Core\Cache\RefinableCacheableDependencyTrait
Expanded class hierarchy of ConfigBase
See also
\Drupal\Core\Config\StorableConfigBase
\Drupal\Core\Theme\ThemeSettings
1 file declares its use of ConfigBase
- ThemeSettings.php in core/
lib/ Drupal/ Core/ Theme/ ThemeSettings.php
File
-
core/
lib/ Drupal/ Core/ Config/ ConfigBase.php, line 27
Namespace
Drupal\Core\ConfigView source
abstract class ConfigBase implements RefinableCacheableDependencyInterface {
use DependencySerializationTrait;
use RefinableCacheableDependencyTrait;
/**
* The name of the configuration object.
*
* @var string
*/
protected $name;
/**
* The data of the configuration object.
*
* @var array
*/
protected $data = [];
/**
* The maximum length of a configuration object name.
*
* Many filesystems (including HFS, NTFS, and ext4) have a maximum file name
* length of 255 characters. To ensure that no configuration objects
* incompatible with this limitation are created, we enforce a maximum name
* length of 250 characters (leaving 5 characters for the file extension).
*
* @see http://wikipedia.org/wiki/Comparison_of_file_systems
*
* Configuration objects not stored on the filesystem should still be
* restricted in name length so name can be used as a cache key.
*/
const MAX_NAME_LENGTH = 250;
/**
* Returns the name of this configuration object.
*
* @return string
* The name of the configuration object.
*/
public function getName() {
return $this->name;
}
/**
* Sets the name of this configuration object.
*
* @param string $name
* The name of the configuration object.
*
* @return $this
* The configuration object.
*/
public function setName($name) {
$this->name = $name;
return $this;
}
/**
* Validates the configuration object name.
*
* @param string $name
* The name of the configuration object.
*
* @throws \Drupal\Core\Config\ConfigNameException
*
* @see Config::MAX_NAME_LENGTH
*/
public static function validateName($name) {
// The name must be namespaced by owner.
if (!str_contains($name, '.')) {
throw new ConfigNameException("Missing namespace in Config object name {$name}.");
}
// The name must be shorter than Config::MAX_NAME_LENGTH characters.
if (strlen($name) > self::MAX_NAME_LENGTH) {
throw new ConfigNameException("Config object name {$name} exceeds maximum allowed length of " . static::MAX_NAME_LENGTH . " characters.");
}
// The name must not contain any of the following characters:
// : ? * < > " ' / \
if (preg_match('/[:?*<>"\'\\/\\\\]/', $name)) {
throw new ConfigNameException("Invalid character in Config object name {$name}.");
}
}
/**
* Gets data from this configuration object.
*
* @param string $key
* A string that maps to a key within the configuration data.
* For instance in the following configuration array:
* @code
* [
* 'foo' => [
* 'bar' => 'baz',
* ],
* ];
* @endcode
* A key of 'foo.bar' would return the string 'baz'. However, a key of 'foo'
* would return ['bar' => 'baz'].
* If no key is specified, then the entire data array is returned.
*
* @return mixed
* The data that was requested.
*/
public function get($key = '') {
if (empty($key)) {
return $this->data;
}
else {
$parts = explode('.', $key);
if (count($parts) == 1) {
return $this->data[$key] ?? NULL;
}
else {
$value = NestedArray::getValue($this->data, $parts, $key_exists);
return $key_exists ? $value : NULL;
}
}
}
/**
* Replaces the data of this configuration object.
*
* @param array $data
* The new configuration data.
*
* @return $this
* The configuration object.
*
* @throws \Drupal\Core\Config\ConfigValueException
* If any key in $data in any depth contains a dot.
*/
public function setData(array $data) {
$data = $this->castSafeStrings($data);
$this->validateKeys($data);
$this->data = $data;
return $this;
}
/**
* Sets a value in this configuration object.
*
* @param string $key
* Identifier to store value in configuration.
* @param mixed $value
* Value to associate with identifier.
*
* @return $this
* The configuration object.
*
* @throws \Drupal\Core\Config\ConfigValueException
* If $value is an array and any of its keys in any depth contains a dot.
*/
public function set($key, $value) {
$value = $this->castSafeStrings($value);
// The dot/period is a reserved character; it may appear between keys, but
// not within keys.
if (is_array($value)) {
$this->validateKeys($value);
}
$parts = explode('.', $key);
if (count($parts) == 1) {
$this->data[$key] = $value;
}
else {
NestedArray::setValue($this->data, $parts, $value);
}
return $this;
}
/**
* Validates all keys in a passed in config array structure.
*
* @param array $data
* Configuration array structure.
*
* @throws \Drupal\Core\Config\ConfigValueException
* If any key in $data in any depth contains a dot.
*/
protected function validateKeys(array $data) {
foreach ($data as $key => $value) {
if (str_contains($key, '.')) {
throw new ConfigValueException("{$key} key contains a dot which is not supported.");
}
if (is_array($value)) {
$this->validateKeys($value);
}
}
}
/**
* Unsets a value in this configuration object.
*
* @param string $key
* Name of the key whose value should be unset.
*
* @return $this
* The configuration object.
*/
public function clear($key) {
$parts = explode('.', $key);
if (count($parts) == 1) {
unset($this->data[$key]);
}
else {
NestedArray::unsetValue($this->data, $parts);
}
return $this;
}
/**
* Merges data into a configuration object.
*
* @param array $data_to_merge
* An array containing data to merge.
*
* @return $this
* The configuration object.
*/
public function merge(array $data_to_merge) {
// Preserve integer keys so that configuration keys are not changed.
$this->setData(NestedArray::mergeDeepArray([
$this->data,
$data_to_merge,
], TRUE));
return $this;
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return $this->cacheContexts;
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
return Cache::mergeTags([
'config:' . $this->name,
], $this->cacheTags);
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return $this->cacheMaxAge;
}
/**
* Casts any objects that implement MarkupInterface to string.
*
* @param mixed $data
* The configuration data.
*
* @return mixed
* The data with any safe strings cast to string.
*/
protected function castSafeStrings($data) {
if ($data instanceof MarkupInterface) {
$data = (string) $data;
}
elseif (is_array($data)) {
array_walk_recursive($data, function (&$value) {
if ($value instanceof MarkupInterface) {
$value = (string) $value;
}
});
}
return $data;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title | Overrides |
---|---|---|---|---|---|
CacheableDependencyTrait::$cacheContexts | protected | property | Cache contexts. | ||
CacheableDependencyTrait::$cacheMaxAge | protected | property | Cache max-age. | ||
CacheableDependencyTrait::$cacheTags | protected | property | Cache tags. | ||
CacheableDependencyTrait::setCacheability | protected | function | Sets cacheability; useful for value object constructors. | ||
ConfigBase::$data | protected | property | The data of the configuration object. | ||
ConfigBase::$name | protected | property | The name of the configuration object. | ||
ConfigBase::castSafeStrings | protected | function | Casts any objects that implement MarkupInterface to string. | ||
ConfigBase::clear | public | function | Unsets a value in this configuration object. | 1 | |
ConfigBase::get | public | function | Gets data from this configuration object. | 1 | |
ConfigBase::getCacheContexts | public | function | The cache contexts associated with this object. | Overrides CacheableDependencyTrait::getCacheContexts | |
ConfigBase::getCacheMaxAge | public | function | The maximum age for which this object may be cached. | Overrides CacheableDependencyTrait::getCacheMaxAge | |
ConfigBase::getCacheTags | public | function | The cache tags associated with this object. | Overrides CacheableDependencyTrait::getCacheTags | 1 |
ConfigBase::getName | public | function | Returns the name of this configuration object. | ||
ConfigBase::MAX_NAME_LENGTH | constant | The maximum length of a configuration object name. | |||
ConfigBase::merge | public | function | Merges data into a configuration object. | ||
ConfigBase::set | public | function | Sets a value in this configuration object. | 1 | |
ConfigBase::setData | public | function | Replaces the data of this configuration object. | 1 | |
ConfigBase::setName | public | function | Sets the name of this configuration object. | ||
ConfigBase::validateKeys | protected | function | Validates all keys in a passed in config array structure. | ||
ConfigBase::validateName | public static | function | Validates the configuration object name. | ||
DependencySerializationTrait::$_entityStorages | protected | property | |||
DependencySerializationTrait::$_serviceIds | protected | property | |||
DependencySerializationTrait::__sleep | public | function | 1 | ||
DependencySerializationTrait::__wakeup | public | function | 2 | ||
RefinableCacheableDependencyTrait::addCacheableDependency | public | function | 1 | ||
RefinableCacheableDependencyTrait::addCacheContexts | public | function | |||
RefinableCacheableDependencyTrait::addCacheTags | public | function | |||
RefinableCacheableDependencyTrait::mergeCacheMaxAge | public | function |
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.