You are here

class Config in Zircon Profile 8

Same name in this branch
  1. 8 core/lib/Drupal/Core/Config/Config.php \Drupal\Core\Config\Config
  2. 8 core/modules/migrate/src/Plugin/migrate/destination/Config.php \Drupal\migrate\Plugin\migrate\destination\Config
Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Config/Config.php \Drupal\Core\Config\Config

Defines the default configuration object.

Encapsulates all capabilities needed for configuration handling for a specific configuration object, including support for runtime overrides. The overrides are handled on top of the stored configuration so they are not saved back to storage.

Hierarchy

Expanded class hierarchy of Config

Related topics

20 files declare their use of Config
BlockTheme.php in core/modules/block/src/Plugin/migrate/process/BlockTheme.php
Contains \Drupal\block\Plugin\migrate\process\BlockTheme.
ConfigEntityStorage.php in core/lib/Drupal/Core/Config/Entity/ConfigEntityStorage.php
Contains \Drupal\Core\Config\Entity\ConfigEntityStorage.
ConfigEventsTest.php in core/modules/config/src/Tests/ConfigEventsTest.php
Contains \Drupal\config\Tests\ConfigEventsTest.
ConfigFactoryTest.php in core/tests/Drupal/Tests/Core/Config/ConfigFactoryTest.php
Contains \Drupal\Tests\Core\Config\ConfigFactoryTest.
ConfigImportSubscriber.php in core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php
Contains \Drupal\Core\EventSubscriber\ConfigImportSubscriber.

... See full list

1 string reference to 'Config'
migrate.destination.schema.yml in core/modules/migrate/config/schema/migrate.destination.schema.yml
core/modules/migrate/config/schema/migrate.destination.schema.yml

File

core/lib/Drupal/Core/Config/Config.php, line 24
Contains \Drupal\Core\Config\Config.

Namespace

Drupal\Core\Config
View source
class Config extends StorableConfigBase {

  /**
   * An event dispatcher instance to use for configuration events.
   *
   * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
   */
  protected $eventDispatcher;

  /**
   * The current runtime data.
   *
   * The configuration data from storage merged with module and settings
   * overrides.
   *
   * @var array
   */
  protected $overriddenData;

  /**
   * The current module overrides.
   *
   * @var array
   */
  protected $moduleOverrides;

  /**
   * The current settings overrides.
   *
   * @var array
   */
  protected $settingsOverrides;

  /**
   * Constructs a configuration object.
   *
   * @param string $name
   *   The name of the configuration object being constructed.
   * @param \Drupal\Core\Config\StorageInterface $storage
   *   A storage object to use for reading and writing the
   *   configuration data.
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher
   *   An event dispatcher instance to use for configuration events.
   * @param \Drupal\Core\Config\TypedConfigManagerInterface $typed_config
   *   The typed configuration manager service.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  public function initWithData(array $data) {
    parent::initWithData($data);
    $this
      ->resetOverriddenData();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  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 isset($this->overriddenData[$key]) ? $this->overriddenData[$key] : NULL;
      }
      else {
        $value = NestedArray::getValue($this->overriddenData, $parts, $key_exists);
        return $key_exists ? $value : NULL;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function setData(array $data) {
    parent::setData($data);
    $this
      ->resetOverriddenData();
    return $this;
  }

  /**
   * Sets settings.php overrides for this configuration object.
   *
   * The overridden data only applies to this configuration object.
   *
   * @param array $data
   *   The overridden values of the configuration data.
   *
   * @return \Drupal\Core\Config\Config
   *   The configuration object.
   */
  public function setSettingsOverride(array $data) {
    $this->settingsOverrides = $data;
    $this
      ->resetOverriddenData();
    return $this;
  }

  /**
   * Sets module overrides for this configuration object.
   *
   * @param array $data
   *   The overridden values of the configuration data.
   *
   * @return \Drupal\Core\Config\Config
   *   The configuration object.
   */
  public function setModuleOverride(array $data) {
    $this->moduleOverrides = $data;
    $this
      ->resetOverriddenData();
    return $this;
  }

  /**
   * Sets the current data for this configuration object.
   *
   * Configuration overrides operate at two distinct layers: modules and
   * settings.php. Overrides in settings.php take precedence over values
   * provided by modules. Precedence or different module overrides is
   * determined by the priority of the config.factory.override tagged services.
   *
   * @return \Drupal\Core\Config\Config
   *   The configuration object.
   */
  protected function setOverriddenData() {
    $this->overriddenData = $this->data;
    if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
      $this->overriddenData = NestedArray::mergeDeepArray(array(
        $this->overriddenData,
        $this->moduleOverrides,
      ), TRUE);
    }
    if (isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
      $this->overriddenData = NestedArray::mergeDeepArray(array(
        $this->overriddenData,
        $this->settingsOverrides,
      ), TRUE);
    }
    return $this;
  }

  /**
   * Resets the current data, so overrides are re-applied.
   *
   * This method should be called after the original data or the overridden data
   * has been changed.
   *
   * @return \Drupal\Core\Config\Config
   *   The configuration object.
   */
  protected function resetOverriddenData() {
    unset($this->overriddenData);
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function set($key, $value) {
    parent::set($key, $value);
    $this
      ->resetOverriddenData();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function clear($key) {
    parent::clear($key);
    $this
      ->resetOverriddenData();
    return $this;
  }

  /**
   * {@inheritdoc}
   */
  public function save($has_trusted_data = FALSE) {

    // Validate the configuration object name before saving.
    static::validateName($this->name);

    // If there is a schema for this configuration object, cast all values to
    // conform to the schema.
    if (!$has_trusted_data) {
      if ($this->typedConfigManager
        ->hasConfigSchema($this->name)) {

        // Ensure that the schema wrapper has the latest data.
        $this->schemaWrapper = NULL;
        foreach ($this->data as $key => $value) {
          $this->data[$key] = $this
            ->castValue($key, $value);
        }
      }
      else {
        foreach ($this->data as $key => $value) {
          $this
            ->validateValue($key, $value);
        }
      }
    }
    $this->storage
      ->write($this->name, $this->data);
    if (!$this->isNew) {
      Cache::invalidateTags($this
        ->getCacheTags());
    }
    $this->isNew = FALSE;
    $this->eventDispatcher
      ->dispatch(ConfigEvents::SAVE, new ConfigCrudEvent($this));
    $this->originalData = $this->data;

    // Potentially configuration schema could have changed the underlying data's
    // types.
    $this
      ->resetOverriddenData();
    return $this;
  }

  /**
   * Deletes the configuration object.
   *
   * @return \Drupal\Core\Config\Config
   *   The configuration object.
   */
  public function delete() {
    $this->data = array();
    $this->storage
      ->delete($this->name);
    Cache::invalidateTags($this
      ->getCacheTags());
    $this->isNew = TRUE;
    $this
      ->resetOverriddenData();
    $this->eventDispatcher
      ->dispatch(ConfigEvents::DELETE, new ConfigCrudEvent($this));
    $this->originalData = $this->data;
    return $this;
  }

  /**
   * Gets the raw data without overrides.
   *
   * @return array
   *   The raw data.
   */
  public function getRawData() {
    return $this->data;
  }

  /**
   * Gets original data from this configuration object.
   *
   * Original data is the data as it is immediately after loading from
   * configuration storage before any changes. If this is a new configuration
   * object it will be an empty array.
   *
   * @see \Drupal\Core\Config\Config::get()
   *
   * @param string $key
   *   A string that maps to a key within the configuration data.
   * @param bool $apply_overrides
   *   Apply any overrides to the original data. Defaults to TRUE.
   *
   * @return mixed
   *   The data that was requested.
   */
  public function getOriginal($key = '', $apply_overrides = TRUE) {
    $original_data = $this->originalData;
    if ($apply_overrides) {

      // Apply overrides.
      if (isset($this->moduleOverrides) && is_array($this->moduleOverrides)) {
        $original_data = NestedArray::mergeDeepArray(array(
          $original_data,
          $this->moduleOverrides,
        ), TRUE);
      }
      if (isset($this->settingsOverrides) && is_array($this->settingsOverrides)) {
        $original_data = NestedArray::mergeDeepArray(array(
          $original_data,
          $this->settingsOverrides,
        ), TRUE);
      }
    }
    if (empty($key)) {
      return $original_data;
    }
    else {
      $parts = explode('.', $key);
      if (count($parts) == 1) {
        return isset($original_data[$key]) ? $original_data[$key] : NULL;
      }
      else {
        $value = NestedArray::getValue($original_data, $parts, $key_exists);
        return $key_exists ? $value : NULL;
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Config::$eventDispatcher protected property An event dispatcher instance to use for configuration events.
Config::$moduleOverrides protected property The current module overrides.
Config::$overriddenData protected property The current runtime data.
Config::$settingsOverrides protected property The current settings overrides.
Config::clear public function Unsets a value in this configuration object. Overrides ConfigBase::clear 1
Config::delete public function Deletes the configuration object. Overrides StorableConfigBase::delete 1
Config::get public function Gets data from this configuration object. Overrides ConfigBase::get
Config::getOriginal public function Gets original data from this configuration object.
Config::getRawData public function Gets the raw data without overrides.
Config::initWithData public function Initializes a configuration object with pre-loaded data. Overrides StorableConfigBase::initWithData
Config::resetOverriddenData protected function Resets the current data, so overrides are re-applied.
Config::save public function Saves the configuration object. Overrides StorableConfigBase::save 1
Config::set public function Sets a value in this configuration object. Overrides ConfigBase::set 1
Config::setData public function Replaces the data of this configuration object. Overrides ConfigBase::setData
Config::setModuleOverride public function Sets module overrides for this configuration object.
Config::setOverriddenData protected function Sets the current data for this configuration object.
Config::setSettingsOverride public function Sets settings.php overrides for this configuration object.
Config::__construct public function Constructs a configuration object.
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::getCacheContexts public function The cache contexts associated with this object. Overrides RefinableCacheableDependencyTrait::getCacheContexts
ConfigBase::getCacheMaxAge public function The maximum age for which this object may be cached. Overrides RefinableCacheableDependencyTrait::getCacheMaxAge
ConfigBase::getCacheTags public function The cache tags associated with this object. Overrides RefinableCacheableDependencyTrait::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::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::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
RefinableCacheableDependencyTrait::$cacheContexts protected property Cache contexts.
RefinableCacheableDependencyTrait::$cacheMaxAge protected property Cache max-age.
RefinableCacheableDependencyTrait::$cacheTags protected property Cache tags.
RefinableCacheableDependencyTrait::addCacheableDependency public function 1
RefinableCacheableDependencyTrait::addCacheContexts public function
RefinableCacheableDependencyTrait::addCacheTags public function
RefinableCacheableDependencyTrait::mergeCacheMaxAge public function
StorableConfigBase::$isNew protected property Whether the configuration object is new or has been saved to the storage.
StorableConfigBase::$originalData protected property The data of the configuration object.
StorableConfigBase::$schemaWrapper protected property The config schema wrapper object for this configuration object.
StorableConfigBase::$storage protected property The storage used to load and save this configuration object.
StorableConfigBase::$typedConfigManager protected property The typed config manager.
StorableConfigBase::castValue protected function Casts the value to correct data type using the configuration schema.
StorableConfigBase::getSchemaWrapper protected function Gets the schema wrapper for the whole configuration object.
StorableConfigBase::getStorage public function Retrieves the storage used to load and save this configuration object.
StorableConfigBase::isNew public function Returns whether this configuration object is new.
StorableConfigBase::validateValue protected function Validate the values are allowed data types.