Same filename in this branch
- 10 composer/Plugin/VendorHardening/Config.php
- 10 core/lib/Drupal/Core/Config/Config.php
- 10 core/modules/migrate/src/Plugin/migrate/destination/Config.php
- 10 core/modules/migrate_drupal/src/Plugin/migrate/source/d8/Config.php
Same filename and directory in other branches
- 8.9.x core/lib/Drupal/Core/Config/Config.php
- 9 core/lib/Drupal/Core/Config/Config.php
Namespace
Drupal\Core\Config
File
core/lib/Drupal/Core/Config/Config.php
View source
<?php
namespace Drupal\Core\Config;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Cache\Cache;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
class Config extends StorableConfigBase {
protected $eventDispatcher;
protected $overriddenData;
protected $moduleOverrides;
protected $settingsOverrides;
public function __construct($name, StorageInterface $storage, EventDispatcherInterface $event_dispatcher, TypedConfigManagerInterface $typed_config) {
$this->name = $name;
$this->storage = $storage;
$this->eventDispatcher = $event_dispatcher;
$this->typedConfigManager = $typed_config;
}
public function initWithData(array $data) {
parent::initWithData($data);
$this
->resetOverriddenData();
return $this;
}
public function get($key = '') {
if (!isset($this->overriddenData)) {
$this
->setOverriddenData();
}
if (empty($key)) {
return $this->overriddenData;
}
else {
$parts = explode('.', $key);
if (count($parts) == 1) {
return $this->overriddenData[$key] ?? NULL;
}
else {
$value = NestedArray::getValue($this->overriddenData, $parts, $key_exists);
return $key_exists ? $value : NULL;
}
}
}
public function setData(array $data) {
parent::setData($data);
$this
->resetOverriddenData();
return $this;
}
public function setSettingsOverride(array $data) {
$this->settingsOverrides = $data;
$this
->resetOverriddenData();
return $this;
}
public function setModuleOverride(array $data) {
$this->moduleOverrides = $data;
$this
->resetOverriddenData();
return $this;
}
protected function setOverriddenData() {
$this->overriddenData = $this->data;
if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
$this->overriddenData = NestedArray::mergeDeepArray([
$this->overriddenData,
$this->moduleOverrides,
], TRUE);
}
if (isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
$this->overriddenData = NestedArray::mergeDeepArray([
$this->overriddenData,
$this->settingsOverrides,
], TRUE);
}
return $this;
}
protected function resetOverriddenData() {
unset($this->overriddenData);
return $this;
}
public function set($key, $value) {
parent::set($key, $value);
$this
->resetOverriddenData();
return $this;
}
public function clear($key) {
parent::clear($key);
$this
->resetOverriddenData();
return $this;
}
public function save($has_trusted_data = FALSE) {
static::validateName($this->name);
if (!$has_trusted_data) {
if ($this->typedConfigManager
->hasConfigSchema($this->name)) {
$this->schemaWrapper = NULL;
$this->data = $this
->castValue(NULL, $this->data);
}
else {
foreach ($this->data as $key => $value) {
$this
->validateValue($key, $value);
}
}
}
$this
->resetOverriddenData();
$this->storage
->write($this->name, $this->data);
if (!$this->isNew) {
Cache::invalidateTags($this
->getCacheTags());
}
$this->isNew = FALSE;
$event_name = $this
->getStorage()
->getCollectionName() === StorageInterface::DEFAULT_COLLECTION ? ConfigEvents::SAVE : ConfigCollectionEvents::SAVE_IN_COLLECTION;
$this->eventDispatcher
->dispatch(new ConfigCrudEvent($this), $event_name);
$this->originalData = $this->data;
return $this;
}
public function delete() {
$this->data = [];
$this->storage
->delete($this->name);
Cache::invalidateTags($this
->getCacheTags());
$this->isNew = TRUE;
$this
->resetOverriddenData();
$event_name = $this
->getStorage()
->getCollectionName() === StorageInterface::DEFAULT_COLLECTION ? ConfigEvents::DELETE : ConfigCollectionEvents::DELETE_IN_COLLECTION;
$this->eventDispatcher
->dispatch(new ConfigCrudEvent($this), $event_name);
$this->originalData = $this->data;
return $this;
}
public function getOriginal($key = '', $apply_overrides = TRUE) {
$original_data = $this->originalData;
if ($apply_overrides) {
if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
$original_data = NestedArray::mergeDeepArray([
$original_data,
$this->moduleOverrides,
], TRUE);
}
if (isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
$original_data = NestedArray::mergeDeepArray([
$original_data,
$this->settingsOverrides,
], TRUE);
}
}
if (empty($key)) {
return $original_data;
}
else {
$parts = explode('.', $key);
if (count($parts) == 1) {
return $original_data[$key] ?? NULL;
}
else {
$value = NestedArray::getValue($original_data, $parts, $key_exists);
return $key_exists ? $value : NULL;
}
}
}
public function hasOverrides($key = '') {
if (empty($key)) {
return !(empty($this->moduleOverrides) && empty($this->settingsOverrides));
}
else {
$parts = explode('.', $key);
$override_exists = FALSE;
if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
$override_exists = NestedArray::keyExists($this->moduleOverrides, $parts);
}
if (!$override_exists && isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
$override_exists = NestedArray::keyExists($this->settingsOverrides, $parts);
}
return $override_exists;
}
}
}
Classes
Name |
Description |
Config |
Defines the default configuration object. |