class ConfigFactory

Defines the configuration object factory.

The configuration object factory instantiates a Config object for each configuration object name that is accessed and returns it to callers.

Each configuration object gets a storage controller object injected, which is used for reading and writing the configuration data.

A configuration context is an object containing parameters that will be available to the configuration plug-ins for them to customize the configuration data in different ways.

Hierarchy

Expanded class hierarchy of ConfigFactory

See also

Drupal\Core\Config\Config

Drupal\Core\Config\StorageInterface

Drupal\Core\Config\Context\ContextInterface

26 files declare their use of ConfigFactory
AccountSettingsForm.php in drupal/core/modules/user/lib/Drupal/user/AccountSettingsForm.php
Contains \Drupal\user\AccountSettingsForm.
AggregatorController.php in drupal/core/modules/aggregator/lib/Drupal/aggregator/Controller/AggregatorController.php
Contains \Drupal\aggregator\Controller\AggregatorController.
ConfigStorageController.php in drupal/core/lib/Drupal/Core/Config/Entity/ConfigStorageController.php
Definition of Drupal\Core\Config\Entity\ConfigStorageController.
CronForm.php in drupal/core/modules/system/lib/Drupal/system/Form/CronForm.php
Contains \Drupal\system\Form\CronForm.
Date.php in drupal/core/lib/Drupal/Core/Datetime/Date.php
Contains \Drupal\Component\Datetime\Date.

... See full list

1 string reference to 'ConfigFactory'
core.services.yml in drupal/core/core.services.yml
drupal/core/core.services.yml
1 service uses ConfigFactory

File

drupal/core/lib/Drupal/Core/Config/ConfigFactory.php, line 31
Definition of Drupal\Core\Config\ConfigFactory.

Namespace

Drupal\Core\Config
View source
class ConfigFactory {

  /**
   * A storage controller instance for reading and writing configuration data.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $storage;

  /**
   * A stack of configuration contexts the last being the context in use.
   *
   * @var array
   */
  protected $contextStack = array();

  /**
   * Cached configuration objects.
   *
   * @var array
   */
  protected $cache = array();

  /**
   * Constructs the Config factory.
   *
   * @param \Drupal\Core\Config\StorageInterface
   *   The configuration storage engine.
   * @param \Drupal\Core\Config\Context\ContextInterface
   *   Configuration context object.
   */
  public function __construct(StorageInterface $storage, ContextInterface $context) {
    $this->storage = $storage;
    $this
      ->enterContext($context);
  }

  /**
   * Returns a configuration object for a given name and context.
   *
   * @param string $name
   *   The name of the configuration object to construct.
   *
   * @return \Drupal\Core\Config\Config
   *   A configuration object.
   */
  public function get($name) {
    $context = $this
      ->getContext();
    $cache_key = $this
      ->getCacheKey($name, $context);
    if (isset($this->cache[$cache_key])) {
      return $this->cache[$cache_key];
    }
    $this->cache[$cache_key] = new Config($name, $this->storage, $context);
    return $this->cache[$cache_key]
      ->init();
  }

  /**
   * Resets and re-initializes configuration objects. Internal use only.
   *
   * @param string $name
   *   (optional) The name of the configuration object to reset. If omitted, all
   *   configuration objects are reset.
   *
   * @return \Drupal\Core\Config\ConfigFactory
   *   The config factory object.
   */
  public function reset($name = NULL) {
    if ($name) {

      // Clear the cached configuration object in all contexts.
      foreach ($this
        ->getCacheKeys($name) as $cache_key) {
        unset($this->cache[$cache_key]);
      }
    }
    else {
      $this->cache = array();
    }
    return $this;
  }

  /**
   * Renames a configuration object using the storage controller.
   *
   * @param string $old_name
   *   The old name of the configuration object.
   * @param string $new_name
   *   The new name of the configuration object.
   *
   * @return \Drupal\Core\Config\Config
   *   The renamed config object.
   */
  public function rename($old_name, $new_name) {
    $context = $this
      ->getContext();
    $old_cache_key = $this
      ->getCacheKey($old_name, $context);
    $new_cache_key = $this
      ->getCacheKey($new_name, $context);
    if (isset($this->cache[$old_cache_key])) {
      $config = $this->cache[$old_cache_key];
      unset($this->cache[$old_cache_key]);
    }
    else {

      // Create the config object if it's not yet loaded into the static cache.
      $config = new Config($old_name, $this->storage, $context);
    }
    $this->cache[$new_cache_key] = $config;
    $this->storage
      ->rename($old_name, $new_name);
    return $this->cache[$new_cache_key]
      ->setName($new_name)
      ->init();
  }

  /**
   * Sets the config context by adding it to the context stack.
   *
   * @param \Drupal\Core\Config\Context\ContextInterface $context
   *   The configuration context to add.
   *
   * @return \Drupal\Core\Config\ConfigFactory
   *   The config factory object.
   */
  public function enterContext(ContextInterface $context) {

    // Initialize the context as it is being entered.
    $this->contextStack[] = $context
      ->init();
    return $this;
  }

  /**
   * Gets the current config context.
   *
   * @return \Drupal\Core\Config\Context\ContextInterface $context
   *   The current configuration context.
   */
  public function getContext() {
    return end($this->contextStack);
  }

  /**
   * Leaves the current context by removing it from the context stack.
   *
   * @return \Drupal\Core\Config\ConfigFactory
   *   The config factory object.
   */
  public function leaveContext() {

    // Ensure at least one context is left on the stack. We already ensured
    // there is at least one context set by taking the initial one in the
    // constructor.
    if (count($this->contextStack) > 1) {
      array_pop($this->contextStack);
    }
    return $this;
  }

  /**
   * Gets the cache key for a given config name in a particular context.
   *
   * @param string $name
   *   The name of the configuration object.
   * @param \Drupal\Core\Config\Context\ContextInterface $context
   *   The configuration context.
   *
   * @return string
   *   The cache key.
   */
  public function getCacheKey($name, ContextInterface $context) {
    return $name . ':' . $context
      ->getUuid();
  }

  /**
   * Gets all the cache keys that match the provided config name.
   *
   * @param string $name
   *   The name of the configuration object.
   *
   * @return array
   *   An array of cache keys that match the provided config name.
   */
  public function getCacheKeys($name) {
    $cache_keys = array_keys($this->cache);
    return array_filter($cache_keys, function ($key) use ($name) {
      return strpos($key, $name) !== false;
    });
  }

  /**
   * Clears the config factory static cache.
   *
   * @return \Drupal\Core\Config\ConfigFactory
   *   The config factory object.
   */
  public function clearStaticCache() {
    $this->cache = array();
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFactory::$cache protected property Cached configuration objects.
ConfigFactory::$contextStack protected property A stack of configuration contexts the last being the context in use.
ConfigFactory::$storage protected property A storage controller instance for reading and writing configuration data.
ConfigFactory::clearStaticCache public function Clears the config factory static cache.
ConfigFactory::enterContext public function Sets the config context by adding it to the context stack.
ConfigFactory::get public function Returns a configuration object for a given name and context.
ConfigFactory::getCacheKey public function Gets the cache key for a given config name in a particular context.
ConfigFactory::getCacheKeys public function Gets all the cache keys that match the provided config name.
ConfigFactory::getContext public function Gets the current config context.
ConfigFactory::leaveContext public function Leaves the current context by removing it from the context stack.
ConfigFactory::rename public function Renames a configuration object using the storage controller.
ConfigFactory::reset public function Resets and re-initializes configuration objects. Internal use only.
ConfigFactory::__construct public function Constructs the Config factory.