class TypedConfigManager

Manages config type plugins.

Hierarchy

Expanded class hierarchy of TypedConfigManager

1 file declares its use of TypedConfigManager
LocaleConfigManager.php in drupal/core/modules/locale/lib/Drupal/locale/LocaleConfigManager.php
Contains \Drupal\locale\LocaleConfigManager.
1 string reference to 'TypedConfigManager'
core.services.yml in drupal/core/core.services.yml
drupal/core/core.services.yml
1 service uses TypedConfigManager

File

drupal/core/lib/Drupal/Core/Config/TypedConfigManager.php, line 16
Contains \Drupal\Core\Config\TypedConfigManager.

Namespace

Drupal\Core\Config
View source
class TypedConfigManager extends TypedDataManager {

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

  /**
   * Creates a new typed configuration manager.
   *
   * @param \Drupal\Core\Config\StorageInterface $configStorage
   *   The storage controller object to use for reading schema data
   * @param \Drupal\Core\Config\StorageInterface $schemaStorage
   *   The storage controller object to use for reading schema data
   */
  public function __construct(StorageInterface $configStorage, StorageInterface $schemaStorage) {
    $this->configStorage = $configStorage;
    $this->discovery = new SchemaDiscovery($schemaStorage);
    $this->factory = new TypedConfigElementFactory($this->discovery);
  }

  /**
   * Gets typed configuration data.
   *
   * @param string $name
   *   Configuration object name.
   *
   * @return \Drupal\Core\Config\Schema\Element
   *   Typed configuration element.
   */
  public function get($name) {
    $data = $this->configStorage
      ->read($name);
    $definition = $this
      ->getDefinition($name);
    return $this
      ->create($definition, $data);
  }

  /**
   * Overrides \Drupal\Core\TypedData\TypedDataManager::create()
   *
   * Fills in default type and does variable replacement.
   */
  public function create(array $definition, $value = NULL, $name = NULL, $parent = NULL) {
    if (!isset($definition['type'])) {

      // Set default type 'string' if possible. If not it will be 'undefined'.
      if (is_string($value)) {
        $definition['type'] = 'string';
      }
      else {
        $definition['type'] = 'undefined';
      }
    }
    elseif (strpos($definition['type'], ']')) {

      // Replace variable names in definition.
      $replace = is_array($value) ? $value : array();
      if (isset($parent)) {
        $replace['%parent'] = $parent
          ->getValue();
      }
      if (isset($name)) {
        $replace['%key'] = $name;
      }
      $definition['type'] = $this
        ->replaceName($definition['type'], $replace);
    }

    // Create typed config object.
    return parent::create($definition, $value, $name, $parent);
  }

  /**
   * Replaces variables in configuration name.
   *
   * The configuration name may contain one or more variables to be replaced,
   * enclosed in square brackets like '[name]' and will follow the replacement
   * rules defined by the replaceVariable() method.
   *
   * @param string $name
   *   Configuration name with variables in square brackets.
   * @param mixed $data
   *   Configuration data for the element.
   * @return string
   *   Configuration name with variables replaced.
   */
  protected static function replaceName($name, $data) {
    if (preg_match_all("/\\[(.*)\\]/U", $name, $matches)) {

      // Build our list of '[value]' => replacement.
      foreach (array_combine($matches[0], $matches[1]) as $key => $value) {
        $replace[$key] = self::replaceVariable($value, $data);
      }
      return strtr($name, $replace);
    }
    else {
      return $name;
    }
  }

  /**
   * Replaces variable values in included names with configuration data.
   *
   * Variable values are nested configuration keys that will be replaced by
   * their value or some of these special strings:
   * - '%key', will be replaced by the element's key.
   * - '%parent', to reference the parent element.
   *
   * There may be nested configuration keys separated by dots or more complex
   * patterns like '%parent.name' which references the 'name' value of the
   * parent element.
   *
   * Example patterns:
   * - 'name.subkey', indicates a nested value of the current element.
   * - '%parent.name', will be replaced by the 'name' value of the parent.
   * - '%parent.%key', will be replaced by the parent element's key.
   *
   * @param string $value
   *   Variable value to be replaced.
   *
   * @return string
   *   The replaced value if a replacement found or the original value if not.
   */
  protected static function replaceVariable($value, $data) {
    $parts = explode('.', $value);

    // Process each value part, one at a time.
    while ($name = array_shift($parts)) {
      if (!is_array($data) || !isset($data[$name])) {

        // Key not found, return original value
        return $value;
      }
      elseif (!$parts) {

        // If no more parts left, this is the final property.
        return (string) $data[$name];
      }
      else {

        // Get nested value and continue processing.
        $data = $data[$name];
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PluginManagerBase::$discovery protected property The object that discovers plugins managed by this manager.
PluginManagerBase::$factory protected property The object that instantiates plugins managed by this manager.
PluginManagerBase::$mapper protected property The object that returns the preconfigured plugin instance appropriate for a particular runtime condition.
PluginManagerBase::clearCachedDefinitions public function Clears static and persistent plugin definition caches. Overrides CachedDiscoveryInterface::clearCachedDefinitions
PluginManagerBase::getDefinition public function Gets a specific plugin definition. Overrides DiscoveryInterface::getDefinition
PluginManagerBase::getDefinitions public function Gets the definition of all plugins for this type. Overrides DiscoveryInterface::getDefinitions
PluginManagerBase::processDefinition public function Performs extra processing on plugin definitions. 2
TypedConfigManager::$configStorage protected property A storage controller instance for reading configuration data.
TypedConfigManager::create public function Overrides \Drupal\Core\TypedData\TypedDataManager::create() Overrides TypedDataManager::create
TypedConfigManager::get public function Gets typed configuration data. 1
TypedConfigManager::replaceName protected static function Replaces variables in configuration name.
TypedConfigManager::replaceVariable protected static function Replaces variable values in included names with configuration data.
TypedConfigManager::__construct public function Creates a new typed configuration manager. Overrides TypedDataManager::__construct 1
TypedDataManager::$constraintManager protected property The validation constraint manager to use for instantiating constraints.
TypedDataManager::$defaults protected property Type definition defaults which are merged in by the ProcessDecorator. Overrides PluginManagerBase::$defaults
TypedDataManager::$prototypes protected property An array of typed data property prototypes.
TypedDataManager::$validator protected property The validator used for validating typed data.
TypedDataManager::createInstance public function Implements \Drupal\Component\Plugin\PluginManagerInterface::createInstance(). Overrides PluginManagerBase::createInstance
TypedDataManager::getConstraints public function Gets configured constraints from a data definition.
TypedDataManager::getInstance public function Implements \Drupal\Component\Plugin\PluginManagerInterface::getInstance(). Overrides PluginManagerBase::getInstance
TypedDataManager::getPropertyInstance public function Get a typed data instance for a property of a given typed data object.
TypedDataManager::getValidationConstraintManager public function Gets the validation constraint manager.
TypedDataManager::getValidator public function Gets the validator for validating typed data.
TypedDataManager::setValidationConstraintManager public function Sets the validation constraint manager.
TypedDataManager::setValidator public function Sets the validator for validating typed data.