You are here

class KeyConfigOverrides in Key 8

Provides key overrides for configuration.

Hierarchy

Expanded class hierarchy of KeyConfigOverrides

1 string reference to 'KeyConfigOverrides'
key.services.yml in ./key.services.yml
key.services.yml
1 service uses KeyConfigOverrides
key.config_override in ./key.services.yml
\Drupal\key\KeyConfigOverrides

File

src/KeyConfigOverrides.php, line 15

Namespace

Drupal\key
View source
class KeyConfigOverrides implements ConfigFactoryOverrideInterface {

  /**
   * Config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Cache backend.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cacheBackend;

  /**
   * Mapping.
   *
   * @var array
   */
  protected $mapping;

  /**
   * In override.
   *
   * @var bool
   */
  protected $inOverride = FALSE;

  /**
   * Creates a new ModuleConfigOverrides instance.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface|null $config_factory
   *   The config factory.
   * @param \Drupal\Core\Cache\CacheBackendInterface|null $cache_backend
   *   The cache backend.
   */
  public function __construct(ConfigFactoryInterface $config_factory = NULL, CacheBackendInterface $cache_backend = NULL) {
    $this->configFactory = $config_factory ?: \Drupal::configFactory();
    $this->cacheBackend = $cache_backend ?: \Drupal::cache('data');
  }

  /**
   * {@inheritdoc}
   */
  public function loadOverrides($names) {
    if ($this->inOverride) {
      return [];
    }
    $this->inOverride = TRUE;
    $mapping = $this
      ->getMapping();
    if (!$mapping) {
      return [];
    }
    try {
      $storage = \Drupal::entityTypeManager()
        ->getStorage('key');
    } catch (\Exception $e) {
      return [];
    }
    $overrides = [];
    foreach ($names as $name) {
      if (!array_key_exists($name, $mapping)) {
        continue;
      }
      $override = [];
      foreach ($mapping[$name] as $config_item => $key_id) {
        $key_value = $storage
          ->load($key_id)
          ->getKeyValue();
        if (!isset($key_value)) {
          continue;
        }

        // Turn the dot-separated configuration item name into a nested
        // array and set the value.
        $config_item_parents = explode('.', $config_item);
        NestedArray::setValue($override, $config_item_parents, $key_value);
      }
      if ($override) {
        $overrides[$name] = $override;
      }
    }
    $this->inOverride = FALSE;
    return $overrides;
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheSuffix() {
    return 'key_config_override';
  }

  /**
   * {@inheritdoc}
   */
  public function getCacheableMetadata($name) {
    return new CacheableMetadata();
  }

  /**
   * {@inheritdoc}
   */
  public function createConfigObject($name, $collection = StorageInterface::DEFAULT_COLLECTION) {
    return NULL;
  }

  /**
   * Get a mapping of key configuration overrides.
   *
   * @return array
   *   A mapping of key configuration overrides.
   */
  protected function getMapping() {
    if (!$this->mapping) {
      $mapping = [];
      $override_ids = $this->configFactory
        ->listAll('key.config_override.');
      $overrides = $this->configFactory
        ->loadMultiple($override_ids);
      foreach ($overrides as $id => $override) {
        $override = $override
          ->get();
        $config_id = '';
        if (!empty($override['config_prefix'])) {
          $config_id .= $override['config_prefix'] . '.';
        }
        if (isset($override['config_name'])) {
          $config_id .= $override['config_name'];
        }
        $config_item = $override['config_item'];
        $key_id = $override['key_id'];
        $mapping[$config_id][$config_item] = $key_id;
      }
      $this->mapping = $mapping;
    }
    return $this->mapping;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
KeyConfigOverrides::$cacheBackend protected property Cache backend.
KeyConfigOverrides::$configFactory protected property Config factory.
KeyConfigOverrides::$inOverride protected property In override.
KeyConfigOverrides::$mapping protected property Mapping.
KeyConfigOverrides::createConfigObject public function Creates a configuration object for use during install and synchronization. Overrides ConfigFactoryOverrideInterface::createConfigObject
KeyConfigOverrides::getCacheableMetadata public function Gets the cacheability metadata associated with the config factory override. Overrides ConfigFactoryOverrideInterface::getCacheableMetadata
KeyConfigOverrides::getCacheSuffix public function The string to append to the configuration static cache name. Overrides ConfigFactoryOverrideInterface::getCacheSuffix
KeyConfigOverrides::getMapping protected function Get a mapping of key configuration overrides.
KeyConfigOverrides::loadOverrides public function Returns config overrides. Overrides ConfigFactoryOverrideInterface::loadOverrides
KeyConfigOverrides::__construct public function Creates a new ModuleConfigOverrides instance.