Chris@0: name = $name; Chris@0: $this->storage = $storage; Chris@0: $this->eventDispatcher = $event_dispatcher; Chris@0: $this->typedConfigManager = $typed_config; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function initWithData(array $data) { Chris@0: parent::initWithData($data); Chris@0: $this->resetOverriddenData(); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function get($key = '') { Chris@0: if (!isset($this->overriddenData)) { Chris@0: $this->setOverriddenData(); Chris@0: } Chris@0: if (empty($key)) { Chris@0: return $this->overriddenData; Chris@0: } Chris@0: else { Chris@0: $parts = explode('.', $key); Chris@0: if (count($parts) == 1) { Chris@0: return isset($this->overriddenData[$key]) ? $this->overriddenData[$key] : NULL; Chris@0: } Chris@0: else { Chris@0: $value = NestedArray::getValue($this->overriddenData, $parts, $key_exists); Chris@0: return $key_exists ? $value : NULL; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setData(array $data) { Chris@0: parent::setData($data); Chris@0: $this->resetOverriddenData(); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets settings.php overrides for this configuration object. Chris@0: * Chris@0: * The overridden data only applies to this configuration object. Chris@0: * Chris@0: * @param array $data Chris@0: * The overridden values of the configuration data. Chris@0: * Chris@0: * @return \Drupal\Core\Config\Config Chris@0: * The configuration object. Chris@0: */ Chris@0: public function setSettingsOverride(array $data) { Chris@0: $this->settingsOverrides = $data; Chris@0: $this->resetOverriddenData(); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets module overrides for this configuration object. Chris@0: * Chris@0: * @param array $data Chris@0: * The overridden values of the configuration data. Chris@0: * Chris@0: * @return \Drupal\Core\Config\Config Chris@0: * The configuration object. Chris@0: */ Chris@0: public function setModuleOverride(array $data) { Chris@0: $this->moduleOverrides = $data; Chris@0: $this->resetOverriddenData(); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the current data for this configuration object. Chris@0: * Chris@0: * Configuration overrides operate at two distinct layers: modules and Chris@0: * settings.php. Overrides in settings.php take precedence over values Chris@0: * provided by modules. Precedence or different module overrides is Chris@0: * determined by the priority of the config.factory.override tagged services. Chris@0: * Chris@0: * @return \Drupal\Core\Config\Config Chris@0: * The configuration object. Chris@0: */ Chris@0: protected function setOverriddenData() { Chris@0: $this->overriddenData = $this->data; Chris@0: if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) { Chris@0: $this->overriddenData = NestedArray::mergeDeepArray([$this->overriddenData, $this->moduleOverrides], TRUE); Chris@0: } Chris@0: if (isset($this->settingsOverrides) && is_array($this->settingsOverrides)) { Chris@0: $this->overriddenData = NestedArray::mergeDeepArray([$this->overriddenData, $this->settingsOverrides], TRUE); Chris@0: } Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resets the current data, so overrides are re-applied. Chris@0: * Chris@0: * This method should be called after the original data or the overridden data Chris@0: * has been changed. Chris@0: * Chris@0: * @return \Drupal\Core\Config\Config Chris@0: * The configuration object. Chris@0: */ Chris@0: protected function resetOverriddenData() { Chris@0: unset($this->overriddenData); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function set($key, $value) { Chris@0: parent::set($key, $value); Chris@0: $this->resetOverriddenData(); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function clear($key) { Chris@0: parent::clear($key); Chris@0: $this->resetOverriddenData(); Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function save($has_trusted_data = FALSE) { Chris@0: // Validate the configuration object name before saving. Chris@0: static::validateName($this->name); Chris@0: Chris@0: // If there is a schema for this configuration object, cast all values to Chris@0: // conform to the schema. Chris@0: if (!$has_trusted_data) { Chris@0: if ($this->typedConfigManager->hasConfigSchema($this->name)) { Chris@0: // Ensure that the schema wrapper has the latest data. Chris@0: $this->schemaWrapper = NULL; Chris@0: foreach ($this->data as $key => $value) { Chris@0: $this->data[$key] = $this->castValue($key, $value); Chris@0: } Chris@0: } Chris@0: else { Chris@0: foreach ($this->data as $key => $value) { Chris@0: $this->validateValue($key, $value); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@16: // Potentially configuration schema could have changed the underlying data's Chris@16: // types. Chris@16: $this->resetOverriddenData(); Chris@16: Chris@0: $this->storage->write($this->name, $this->data); Chris@0: if (!$this->isNew) { Chris@0: Cache::invalidateTags($this->getCacheTags()); Chris@0: } Chris@0: $this->isNew = FALSE; Chris@0: $this->eventDispatcher->dispatch(ConfigEvents::SAVE, new ConfigCrudEvent($this)); Chris@0: $this->originalData = $this->data; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Deletes the configuration object. Chris@0: * Chris@0: * @return \Drupal\Core\Config\Config Chris@0: * The configuration object. Chris@0: */ Chris@0: public function delete() { Chris@0: $this->data = []; Chris@0: $this->storage->delete($this->name); Chris@0: Cache::invalidateTags($this->getCacheTags()); Chris@0: $this->isNew = TRUE; Chris@0: $this->resetOverriddenData(); Chris@0: $this->eventDispatcher->dispatch(ConfigEvents::DELETE, new ConfigCrudEvent($this)); Chris@0: $this->originalData = $this->data; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the raw data without overrides. Chris@0: * Chris@0: * @return array Chris@0: * The raw data. Chris@0: */ Chris@0: public function getRawData() { Chris@0: return $this->data; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets original data from this configuration object. Chris@0: * Chris@0: * Original data is the data as it is immediately after loading from Chris@0: * configuration storage before any changes. If this is a new configuration Chris@0: * object it will be an empty array. Chris@0: * Chris@0: * @see \Drupal\Core\Config\Config::get() Chris@0: * Chris@0: * @param string $key Chris@0: * A string that maps to a key within the configuration data. Chris@0: * @param bool $apply_overrides Chris@0: * Apply any overrides to the original data. Defaults to TRUE. Chris@0: * Chris@0: * @return mixed Chris@0: * The data that was requested. Chris@0: */ Chris@0: public function getOriginal($key = '', $apply_overrides = TRUE) { Chris@0: $original_data = $this->originalData; Chris@0: if ($apply_overrides) { Chris@0: // Apply overrides. Chris@0: if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) { Chris@0: $original_data = NestedArray::mergeDeepArray([$original_data, $this->moduleOverrides], TRUE); Chris@0: } Chris@0: if (isset($this->settingsOverrides) && is_array($this->settingsOverrides)) { Chris@0: $original_data = NestedArray::mergeDeepArray([$original_data, $this->settingsOverrides], TRUE); Chris@0: } Chris@0: } Chris@0: Chris@0: if (empty($key)) { Chris@0: return $original_data; Chris@0: } Chris@0: else { Chris@0: $parts = explode('.', $key); Chris@0: if (count($parts) == 1) { Chris@0: return isset($original_data[$key]) ? $original_data[$key] : NULL; Chris@0: } Chris@0: else { Chris@0: $value = NestedArray::getValue($original_data, $parts, $key_exists); Chris@0: return $key_exists ? $value : NULL; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@14: /** Chris@14: * Determines if overrides are applied to a key for this configuration object. Chris@14: * Chris@14: * @param string $key Chris@14: * (optional) A string that maps to a key within the configuration data. Chris@14: * For instance in the following configuration array: Chris@14: * @code Chris@14: * array( Chris@14: * 'foo' => array( Chris@14: * 'bar' => 'baz', Chris@14: * ), Chris@14: * ); Chris@14: * @endcode Chris@14: * A key of 'foo.bar' would map to the string 'baz'. However, a key of 'foo' Chris@14: * would map to the array('bar' => 'baz'). Chris@14: * If not supplied TRUE will be returned if there are any overrides at all Chris@14: * for this configuration object. Chris@14: * Chris@14: * @return bool Chris@14: * TRUE if there are any overrides for the key, otherwise FALSE. Chris@14: */ Chris@14: public function hasOverrides($key = '') { Chris@14: if (empty($key)) { Chris@14: return !(empty($this->moduleOverrides) && empty($this->settingsOverrides)); Chris@14: } Chris@14: else { Chris@14: $parts = explode('.', $key); Chris@14: $override_exists = FALSE; Chris@14: if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) { Chris@14: $override_exists = NestedArray::keyExists($this->moduleOverrides, $parts); Chris@14: } Chris@14: if (!$override_exists && isset($this->settingsOverrides) && is_array($this->settingsOverrides)) { Chris@14: $override_exists = NestedArray::keyExists($this->settingsOverrides, $parts); Chris@14: } Chris@14: return $override_exists; Chris@14: } Chris@14: } Chris@14: Chris@0: }