You are here

abstract class ConfigProviderBase in Configuration Provider 8.2

Same name and namespace in other branches
  1. 8 src/Plugin/ConfigProviderBase.php \Drupal\config_provider\Plugin\ConfigProviderBase

Base class for Configuration provider plugins.

Hierarchy

Expanded class hierarchy of ConfigProviderBase

3 files declare their use of ConfigProviderBase
ConfigProviderInstall.php in src/Plugin/ConfigProvider/ConfigProviderInstall.php
ConfigProviderOptional.php in src/Plugin/ConfigProvider/ConfigProviderOptional.php
Foo.php in tests/modules/config_provider_multi_providers_test/src/Plugin/ConfigProvider/Foo.php

File

src/Plugin/ConfigProviderBase.php, line 17

Namespace

Drupal\config_provider\Plugin
View source
abstract class ConfigProviderBase extends PluginBase implements ConfigProviderInterface {

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

  /**
   * The active configuration storages, keyed by collection.
   *
   * @var \Drupal\Core\Config\StorageInterface[]
   */
  protected $activeStorages;

  /**
   * The configuration manager.
   *
   * @var \Drupal\Core\Config\ConfigManagerInterface
   */
  protected $configManager;

  /**
   * The name of the currently active installation profile.
   *
   * @var string
   */
  protected $installProfile;

  /**
   * The provider configuration storage.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $providerStorage;

  /**
   * {@inheritdoc}
   */
  public function providesFullConfig() {
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function getDirectory() {
    return static::ID;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfigFactory(ConfigFactoryInterface $config_factory) {
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function setActiveStorages(StorageInterface $active_storage) {
    $this->activeStorages[$active_storage
      ->getCollectionName()] = $active_storage;
  }

  /**
   * {@inheritdoc}
   */
  public function setConfigManager(ConfigManagerInterface $config_manager) {
    $this->configManager = $config_manager;
  }

  /**
   * {@inheritdoc}
   */
  public function setInstallProfile($install_profile) {
    $this->installProfile = $install_profile;
  }

  /**
   * {@inheritdoc}
   */
  public function setProviderStorage(StorageInterface $provider_storage) {
    $this->providerStorage = $provider_storage;
  }

  /**
   * Gets the storage for a designated configuration provider.
   *
   * @param string $directory
   *   The configuration directory (for example, config/install).
   * @param string $collection
   *   (optional) The configuration collection. Defaults to the default
   *   collection.
   *
   * @return \Drupal\Core\Config\StorageInterface
   *   The configuration storage that provides the default configuration.
   */
  protected function getExtensionInstallStorage($directory, $collection = StorageInterface::DEFAULT_COLLECTION) {
    return new ExtensionInstallStorage($this
      ->getActiveStorages($collection), $directory, $collection, TRUE, $this->installProfile);
  }

  /**
   * Gets the configuration storage that provides the active configuration.
   *
   * @param string $collection
   *   (optional) The configuration collection. Defaults to the default
   *   collection.
   *
   * @return \Drupal\Core\Config\StorageInterface
   *   The configuration storage that provides the default configuration.
   */
  protected function getActiveStorages($collection = StorageInterface::DEFAULT_COLLECTION) {
    if (!isset($this->activeStorages[$collection])) {
      $this->activeStorages[$collection] = reset($this->activeStorages)
        ->createCollection($collection);
    }
    return $this->activeStorages[$collection];
  }

  /**
   * Gets the profile storage to use to check for profile overrides.
   *
   * The install profile can override module configuration during a module
   * install. Both the install and optional directories are checked for matching
   * configuration. This allows profiles to override default configuration for
   * modules they do not depend on.
   *
   * This is based on the ConfigInstaller::getProfileStorages(). Contrary to
   * the core method, here we don't use an argument for the extension being
   * installed, since our usage isn't in the context of extension installation.
   *
   * @return \Drupal\Core\Config\StorageInterface[]|null
   *   Storages to access configuration from the installation profile.
   */
  protected function getProfileStorages() {
    $profile = $this
      ->drupalGetProfile();
    $profile_storages = [];
    if ($profile) {
      $profile_path = $this
        ->drupalGetPath('module', $profile);
      foreach ([
        InstallStorage::CONFIG_INSTALL_DIRECTORY,
        InstallStorage::CONFIG_OPTIONAL_DIRECTORY,
      ] as $directory) {
        if (is_dir($profile_path . '/' . $directory)) {
          $profile_storages[] = new FileStorage($profile_path . '/' . $directory, StorageInterface::DEFAULT_COLLECTION);
        }
      }
    }
    return $profile_storages;
  }

  /**
   * Gets the list of enabled extensions including both modules and themes.
   *
   * @return array
   *   A list of enabled extensions which includes both modules and themes.
   */
  protected function getEnabledExtensions() {

    // Read enabled extensions directly from configuration to avoid circular
    // dependencies on ModuleHandler and ThemeHandler.
    $extension_config = $this->configFactory
      ->get('core.extension');
    $enabled_extensions = (array) $extension_config
      ->get('module');
    $enabled_extensions += (array) $extension_config
      ->get('theme');

    // Core can provide configuration.
    $enabled_extensions['core'] = 'core';
    return array_keys($enabled_extensions);
  }

  /**
   * Validates an array of config data that contains dependency information.
   *
   * @param string $config_name
   *   The name of the configuration object that is being validated.
   * @param array $data
   *   Configuration data.
   * @param array $enabled_extensions
   *   A list of all the currently enabled modules and themes.
   * @param array $all_config
   *   A list of all the active configuration names.
   *
   * @return bool
   *   TRUE if the dependencies are met, FALSE if not.
   */
  protected function validateDependencies($config_name, array $data, array $enabled_extensions, array $all_config) {
    if (isset($data['dependencies'])) {
      $all_dependencies = $data['dependencies'];

      // Ensure enforced dependencies are included.
      if (isset($all_dependencies['enforced'])) {
        $all_dependencies = array_merge($all_dependencies, $data['dependencies']['enforced']);
        unset($all_dependencies['enforced']);
      }

      // Ensure the configuration entity type provider is in the list of
      // dependencies.
      list($provider) = explode('.', $config_name, 2);
      if (!isset($all_dependencies['module'])) {
        $all_dependencies['module'][] = $provider;
      }
      elseif (!in_array($provider, $all_dependencies['module'])) {
        $all_dependencies['module'][] = $provider;
      }
      foreach ($all_dependencies as $type => $dependencies) {
        $list_to_check = [];
        switch ($type) {
          case 'module':
          case 'theme':
            $list_to_check = $enabled_extensions;
            break;
          case 'config':
            $list_to_check = $all_config;
            break;
        }
        if (!empty($list_to_check)) {
          $missing = array_diff($dependencies, $list_to_check);
          if (!empty($missing)) {
            return FALSE;
          }
        }
      }
    }
    return TRUE;
  }

  /**
   * Returns a list of all configuration items or those of extensions.
   *
   * @param \Drupal\Core\Config\StorageInterface $storage
   *   A configuration storage.
   * @param \Drupal\Core\Extension\Extension[] $extensions
   *   An associative array of Extension objects, keyed by extension name.
   *
   * @return array
   *   An array containing configuration object names.
   */
  protected function listConfig(StorageInterface $storage, array $extensions = []) {
    if (!empty($extensions)) {
      $config_names = [];

      /* @var \Drupal\Core\Extension\Extension $extension */
      foreach ($extensions as $name => $extension) {
        $config_names = array_merge($config_names, array_keys($storage
          ->getComponentNames([
          $name => $extension,
        ])));
      }
    }
    else {
      $config_names = $storage
        ->listAll();
    }
    return $config_names;
  }

  /**
   * Wrapper for drupal_get_path().
   *
   * @param $type
   *   The type of the item; one of 'core', 'profile', 'module', 'theme', or
   *   'theme_engine'.
   * @param $name
   *   The name of the item for which the path is requested. Ignored for
   *   $type 'core'.
   *
   * @return string
   *   The path to the requested item or an empty string if the item is not
   *   found.
   */
  protected function drupalGetPath($type, $name) {
    return drupal_get_path($type, $name);
  }

  /**
   * Gets the install profile from settings.
   *
   * @return string|null
   *   The name of the installation profile or NULL if no installation profile
   *   is currently active. This is the case for example during the first steps
   *   of the installer or during unit tests.
   */
  protected function drupalGetProfile() {
    return $this->installProfile;
  }

  /**
   * Adds default_config_hash for proper localization of the config objects.
   *
   * Use this method only on unchanged config from config/install or config/optional
   * folders.
   *
   * @param array $data
   *   Config to install read directly from config/install or config/optional.
   *
   * @return array
   *   Config with default_config_hash property.
   */
  public function addDefaultConfigHash(array $data) {
    if (empty($data['_core']['default_config_hash'])) {
      $data['_core']['default_config_hash'] = Crypt::hashBase64(serialize($data));
    }
    return $data;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigProviderBase::$activeStorages protected property The active configuration storages, keyed by collection.
ConfigProviderBase::$configFactory protected property The configuration factory.
ConfigProviderBase::$configManager protected property The configuration manager.
ConfigProviderBase::$installProfile protected property The name of the currently active installation profile.
ConfigProviderBase::$providerStorage protected property The provider configuration storage.
ConfigProviderBase::addDefaultConfigHash public function Adds default_config_hash for proper localization of the config objects.
ConfigProviderBase::drupalGetPath protected function Wrapper for drupal_get_path().
ConfigProviderBase::drupalGetProfile protected function Gets the install profile from settings.
ConfigProviderBase::getActiveStorages protected function Gets the configuration storage that provides the active configuration.
ConfigProviderBase::getDirectory public function Returns the configuration directory. Overrides ConfigProviderInterface::getDirectory
ConfigProviderBase::getEnabledExtensions protected function Gets the list of enabled extensions including both modules and themes.
ConfigProviderBase::getExtensionInstallStorage protected function Gets the storage for a designated configuration provider.
ConfigProviderBase::getProfileStorages protected function Gets the profile storage to use to check for profile overrides.
ConfigProviderBase::listConfig protected function Returns a list of all configuration items or those of extensions.
ConfigProviderBase::providesFullConfig public function Indicates whether the configuration items returned by the provider are full as opposed to partials. Overrides ConfigProviderInterface::providesFullConfig
ConfigProviderBase::setActiveStorages public function Injects the active configuration storage. Overrides ConfigProviderInterface::setActiveStorages
ConfigProviderBase::setConfigFactory public function Injects the active configuration storage. Overrides ConfigProviderInterface::setConfigFactory
ConfigProviderBase::setConfigManager public function Injects the active configuration storage. Overrides ConfigProviderInterface::setConfigManager
ConfigProviderBase::setInstallProfile public function Sets the install profile. Overrides ConfigProviderInterface::setInstallProfile
ConfigProviderBase::setProviderStorage public function
ConfigProviderBase::validateDependencies protected function Validates an array of config data that contains dependency information.
ConfigProviderInterface::addConfigToCreate public function Adds configuration to be installed. 3
ConfigProviderInterface::addInstallableConfig public function Adds configuration that is available to be installed or updated. 3
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 3
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. 92