ExtensionInstallStorage.php 4.73 KB
Newer Older
1 2 3 4
<?php

namespace Drupal\Core\Config;

5
use Drupal\Core\Site\Settings;
6
use Drupal\Core\Extension\ExtensionDiscovery;
7

8
/**
9 10 11 12
 * Storage to access configuration and schema in enabled extensions.
 *
 * @see \Drupal\Core\Config\ConfigInstaller
 * @see \Drupal\Core\Config\TypedConfigManager
13 14 15
 */
class ExtensionInstallStorage extends InstallStorage {

16 17 18 19 20 21 22
  /**
   * The active configuration store.
   *
   * @var \Drupal\Core\Config\StorageInterface
   */
  protected $configStorage;

23 24 25 26 27 28 29
  /**
   * Flag to include the profile in the list of enabled modules.
   *
   * @var bool
   */
  protected $includeProfile = TRUE;

30 31 32 33 34 35
  /**
   * Overrides \Drupal\Core\Config\InstallStorage::__construct().
   *
   * @param \Drupal\Core\Config\StorageInterface $config_storage
   *   The active configuration store where the list of enabled modules and
   *   themes is stored.
36 37
   * @param string $directory
   *   The directory to scan in each extension to scan for files. Defaults to
38 39 40 41
   *   'config/install'.
   * @param string $collection
   *   (optional) The collection to store configuration in. Defaults to the
   *   default collection.
42 43
   * @param bool $include_profile
   *   (optional) Whether to include the install profile in extensions to
44
   *   search and to get overrides from.
45
   */
46
  public function __construct(StorageInterface $config_storage, $directory = self::CONFIG_INSTALL_DIRECTORY, $collection = StorageInterface::DEFAULT_COLLECTION, $include_profile = TRUE) {
47
    $this->configStorage = $config_storage;
48
    $this->directory = $directory;
49
    $this->collection = $collection;
50
    $this->includeProfile = $include_profile;
51 52 53 54 55 56 57 58 59 60 61
  }

  /**
   * {@inheritdoc}
   */
  public function createCollection($collection) {
    return new static(
      $this->configStorage,
      $this->directory,
      $collection
    );
62 63
  }

64 65 66
  /**
   * Returns a map of all config object names and their folders.
   *
67 68 69 70 71
   * The list is based on enabled modules and themes. The active configuration
   * storage is used rather than \Drupal\Core\Extension\ModuleHandler and
   *  \Drupal\Core\Extension\ThemeHandler in order to resolve circular
   * dependencies between these services and \Drupal\Core\Config\ConfigInstaller
   * and \Drupal\Core\Config\TypedConfigManager.
72 73 74 75 76 77
   *
   * @return array
   *   An array mapping config object names with directories.
   */
  protected function getAllFolders() {
    if (!isset($this->folders)) {
78
      $this->folders = array();
79
      $this->folders += $this->getCoreNames();
80

81
      $install_profile = Settings::get('install_profile');
82
      $profile = drupal_get_profile();
83
      $extensions = $this->configStorage->read('core.extension');
84 85
      // @todo Remove this scan as part of https://www.drupal.org/node/2186491
      $listing = new ExtensionDiscovery(\Drupal::root());
86
      if (!empty($extensions['module'])) {
87
        $modules = $extensions['module'];
88 89
        // Remove the install profile as this is handled later.
        unset($modules[$install_profile]);
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
        $profile_list = $listing->scan('profile');
        if ($profile && isset($profile_list[$profile])) {
          // Prime the drupal_get_filename() static cache with the profile info
          // file location so we can use drupal_get_path() on the active profile
          // during the module scan.
          // @todo Remove as part of https://www.drupal.org/node/2186491
          drupal_get_filename('profile', $profile, $profile_list[$profile]->getPathname());
        }
        $module_list_scan = $listing->scan('module');
        $module_list = array();
        foreach (array_keys($modules) as $module) {
          if (isset($module_list_scan[$module])) {
            $module_list[$module] = $module_list_scan[$module];
          }
        }
        $this->folders += $this->getComponentNames($module_list);
106
      }
107
      if (!empty($extensions['theme'])) {
108 109 110 111 112 113 114
        $theme_list_scan = $listing->scan('theme');
        foreach (array_keys($extensions['theme']) as $theme) {
          if (isset($theme_list_scan[$theme])) {
            $theme_list[$theme] = $theme_list_scan[$theme];
          }
        }
        $this->folders += $this->getComponentNames($theme_list);
115
      }
116

117 118 119 120
      if ($this->includeProfile) {
        // The install profile can override module default configuration. We do
        // this by replacing the config file path from the module/theme with the
        // install profile version if there are any duplicates.
121 122 123 124 125 126 127 128 129
        if (isset($profile)) {
          if (!isset($profile_list)) {
            $profile_list = $listing->scan('profile');
          }
          if (isset($profile_list[$profile])) {
            $profile_folders = $this->getComponentNames(array($profile_list[$profile]));
            $this->folders = $profile_folders + $this->folders;
          }
        }
130
      }
131 132 133 134
    }
    return $this->folders;
  }
}