You are here

class InstallStorage in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Config/InstallStorage.php \Drupal\Core\Config\InstallStorage

Storage used by the Drupal installer.

This storage performs a full filesystem scan to discover all available extensions and reads from all default config directories that exist.

This special implementation MUST NOT be used anywhere else than the early installer environment.

Hierarchy

Expanded class hierarchy of InstallStorage

See also

\Drupal\Core\DependencyInjection\InstallServiceProvider

17 files declare their use of InstallStorage
ConfigAfterInstallerTestBase.php in core/tests/Drupal/FunctionalTests/Installer/ConfigAfterInstallerTestBase.php
ConfigImportUITest.php in core/modules/config/tests/src/Functional/ConfigImportUITest.php
ConfigInstallProfileOverrideTest.php in core/modules/config/tests/src/Functional/ConfigInstallProfileOverrideTest.php
ConfigInstallProfileUnmetDependenciesTest.php in core/modules/config/tests/src/Functional/ConfigInstallProfileUnmetDependenciesTest.php
ConfigInstallTest.php in core/tests/Drupal/KernelTests/Core/Config/ConfigInstallTest.php

... See full list

File

core/lib/Drupal/Core/Config/InstallStorage.php, line 19

Namespace

Drupal\Core\Config
View source
class InstallStorage extends FileStorage {

  /**
   * Extension sub-directory containing default configuration for installation.
   */
  const CONFIG_INSTALL_DIRECTORY = 'config/install';

  /**
   * Extension sub-directory containing optional configuration for installation.
   */
  const CONFIG_OPTIONAL_DIRECTORY = 'config/optional';

  /**
   * Extension sub-directory containing configuration schema.
   */
  const CONFIG_SCHEMA_DIRECTORY = 'config/schema';

  /**
   * Folder map indexed by configuration name.
   *
   * @var array
   */
  protected $folders;

  /**
   * The directory to scan in each extension to scan for files.
   *
   * @var string
   */
  protected $directory;

  /**
   * Constructs an InstallStorage object.
   *
   * @param string $directory
   *   The directory to scan in each extension to scan for files. Defaults to
   *   'config/install'.
   * @param string $collection
   *   (optional) The collection to store configuration in. Defaults to the
   *   default collection.
   */
  public function __construct($directory = self::CONFIG_INSTALL_DIRECTORY, $collection = StorageInterface::DEFAULT_COLLECTION) {
    parent::__construct($directory, $collection);
  }

  /**
   * Overrides Drupal\Core\Config\FileStorage::getFilePath().
   *
   * Returns the path to the configuration file.
   *
   * Determines the owner and path to the default configuration file of a
   * requested config object name located in the installation profile, a module,
   * or a theme (in this order).
   *
   * @return string
   *   The path to the configuration file.
   *
   * @todo Improve this when figuring out how we want to handle configuration in
   *   installation profiles. For instance, a config object actually has to be
   *   searched in the profile first (whereas the profile is never the owner);
   *   only afterwards check for a corresponding module or theme.
   */
  public function getFilePath($name) {
    $folders = $this
      ->getAllFolders();
    if (isset($folders[$name])) {
      return $folders[$name] . '/' . $name . '.' . $this
        ->getFileExtension();
    }

    // If any code in the early installer requests a configuration object that
    // does not exist anywhere as default config, then that must be mistake.
    throw new StorageException("Missing configuration file: {$name}");
  }

  /**
   * {@inheritdoc}
   */
  public function exists($name) {
    return array_key_exists($name, $this
      ->getAllFolders());
  }

  /**
   * Overrides Drupal\Core\Config\FileStorage::write().
   *
   * @throws \Drupal\Core\Config\StorageException
   */
  public function write($name, array $data) {
    throw new StorageException('Write operation is not allowed.');
  }

  /**
   * Overrides Drupal\Core\Config\FileStorage::delete().
   *
   * @throws \Drupal\Core\Config\StorageException
   */
  public function delete($name) {
    throw new StorageException('Delete operation is not allowed.');
  }

  /**
   * Overrides Drupal\Core\Config\FileStorage::rename().
   *
   * @throws \Drupal\Core\Config\StorageException
   */
  public function rename($name, $new_name) {
    throw new StorageException('Rename operation is not allowed.');
  }

  /**
   * {@inheritdoc}
   */
  public function listAll($prefix = '') {
    $names = array_keys($this
      ->getAllFolders());
    if (!$prefix) {
      return $names;
    }
    else {
      $return = [];
      foreach ($names as $index => $name) {
        if (strpos($name, $prefix) === 0) {
          $return[$index] = $names[$index];
        }
      }
      return $return;
    }
  }

  /**
   * Returns a map of all config object names and their folders.
   *
   * @return array
   *   An array mapping config object names with directories.
   */
  protected function getAllFolders() {
    if (!isset($this->folders)) {
      $this->folders = [];
      $this->folders += $this
        ->getCoreNames();

      // Perform an ExtensionDiscovery scan as we cannot use
      // \Drupal\Core\Extension\ExtensionList::getPath() yet because the system
      // module may not yet be enabled during install.
      // @todo Remove as part of https://www.drupal.org/node/2186491
      $listing = new ExtensionDiscovery(\Drupal::root());
      if ($profile = \Drupal::installProfile()) {
        $profile_list = $listing
          ->scan('profile');
        if (isset($profile_list[$profile])) {

          // Prime the \Drupal\Core\Extension\ExtensionList::getPathname static
          // cache with the profile info file location so we can use
          // \Drupal\Core\Extension\ExtensionList::getPath() on the active
          // profile during the module scan.
          // @todo Remove as part of https://www.drupal.org/node/2186491

          /** @var \Drupal\Core\Extension\ProfileExtensionList $profile_extension_list */
          $profile_extension_list = \Drupal::service('extension.list.profile');
          $profile_extension_list
            ->setPathname($profile, $profile_list[$profile]
            ->getPathname());
          $this->folders += $this
            ->getComponentNames([
            $profile_list[$profile],
          ]);
        }
      }

      // @todo Remove as part of https://www.drupal.org/node/2186491
      $this->folders += $this
        ->getComponentNames($listing
        ->scan('module'));
      $this->folders += $this
        ->getComponentNames($listing
        ->scan('theme'));
    }
    return $this->folders;
  }

  /**
   * Get all configuration names and folders for a list of modules or themes.
   *
   * @param \Drupal\Core\Extension\Extension[] $list
   *   An associative array of Extension objects, keyed by extension name.
   *
   * @return array
   *   Folders indexed by configuration name.
   */
  public function getComponentNames(array $list) {
    $extension = '.' . $this
      ->getFileExtension();
    $pattern = '/' . preg_quote($extension, '/') . '$/';
    $folders = [];
    foreach ($list as $extension_object) {

      // We don't have to use ExtensionDiscovery here because our list of
      // extensions was already obtained through an ExtensionDiscovery scan.
      $directory = $this
        ->getComponentFolder($extension_object);
      if (is_dir($directory)) {

        // glob() directly calls into libc glob(), which is not aware of PHP
        // stream wrappers. Same for \GlobIterator (which additionally requires
        // an absolute realpath() on Windows).
        // @see https://github.com/mikey179/vfsStream/issues/2
        $files = scandir($directory);
        foreach ($files as $file) {
          if ($file[0] !== '.' && preg_match($pattern, $file)) {
            $folders[basename($file, $extension)] = $directory;
          }
        }
      }
    }
    return $folders;
  }

  /**
   * Get all configuration names and folders for Drupal core.
   *
   * @return array
   *   Folders indexed by configuration name.
   */
  public function getCoreNames() {
    $extension = '.' . $this
      ->getFileExtension();
    $pattern = '/' . preg_quote($extension, '/') . '$/';
    $folders = [];
    $directory = $this
      ->getCoreFolder();
    if (is_dir($directory)) {

      // glob() directly calls into libc glob(), which is not aware of PHP
      // stream wrappers. Same for \GlobIterator (which additionally requires an
      // absolute realpath() on Windows).
      // @see https://github.com/mikey179/vfsStream/issues/2
      $files = scandir($directory);
      foreach ($files as $file) {
        if ($file[0] !== '.' && preg_match($pattern, $file)) {
          $folders[basename($file, $extension)] = $directory;
        }
      }
    }
    return $folders;
  }

  /**
   * Get folder inside each component that contains the files.
   *
   * @param \Drupal\Core\Extension\Extension $extension
   *   The Extension object for the component.
   *
   * @return string
   *   The configuration folder name for this component.
   */
  protected function getComponentFolder(Extension $extension) {
    return $extension
      ->getPath() . '/' . $this
      ->getCollectionDirectory();
  }

  /**
   * Get folder inside Drupal core that contains the files.
   *
   * @return string
   *   The configuration folder name for core.
   */
  protected function getCoreFolder() {
    return 'core/' . $this
      ->getCollectionDirectory();
  }

  /**
   * Overrides Drupal\Core\Config\FileStorage::deleteAll().
   *
   * @throws \Drupal\Core\Config\StorageException
   */
  public function deleteAll($prefix = '') {
    throw new StorageException('Delete operation is not allowed.');
  }

  /**
   * Resets the static cache.
   */
  public function reset() {
    $this->folders = NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileStorage::$collection protected property The storage collection.
FileStorage::$fileCache protected property The file cache object.
FileStorage::createCollection public function Creates a collection on the storage. Overrides StorageInterface::createCollection 1
FileStorage::decode public function Decodes configuration data from the storage-specific format. Overrides StorageInterface::decode
FileStorage::encode public function Encodes configuration data into the storage-specific format. Overrides StorageInterface::encode
FileStorage::ensureStorage protected function Check if the directory exists and create it if not.
FileStorage::getAllCollectionNames public function Gets the existing collections. Overrides StorageInterface::getAllCollectionNames
FileStorage::getAllCollectionNamesHelper protected function Helper function for getAllCollectionNames().
FileStorage::getCollectionDirectory protected function Gets the directory for the collection.
FileStorage::getCollectionName public function Gets the name of the current collection the storage is using. Overrides StorageInterface::getCollectionName
FileStorage::getFileExtension public static function Returns the file extension used by the file storage for all configuration files.
FileStorage::getFileSystem private function Returns file system service.
FileStorage::read public function Implements Drupal\Core\Config\StorageInterface::read(). Overrides StorageInterface::read
FileStorage::readMultiple public function Reads configuration data from the storage. Overrides StorageInterface::readMultiple
InstallStorage::$directory protected property The directory to scan in each extension to scan for files. Overrides FileStorage::$directory
InstallStorage::$folders protected property Folder map indexed by configuration name.
InstallStorage::CONFIG_INSTALL_DIRECTORY constant Extension sub-directory containing default configuration for installation.
InstallStorage::CONFIG_OPTIONAL_DIRECTORY constant Extension sub-directory containing optional configuration for installation.
InstallStorage::CONFIG_SCHEMA_DIRECTORY constant Extension sub-directory containing configuration schema.
InstallStorage::delete public function Overrides Drupal\Core\Config\FileStorage::delete(). Overrides FileStorage::delete
InstallStorage::deleteAll public function Overrides Drupal\Core\Config\FileStorage::deleteAll(). Overrides FileStorage::deleteAll
InstallStorage::exists public function Returns whether a configuration object exists. Overrides FileStorage::exists
InstallStorage::getAllFolders protected function Returns a map of all config object names and their folders. 2
InstallStorage::getComponentFolder protected function Get folder inside each component that contains the files.
InstallStorage::getComponentNames public function Get all configuration names and folders for a list of modules or themes.
InstallStorage::getCoreFolder protected function Get folder inside Drupal core that contains the files.
InstallStorage::getCoreNames public function Get all configuration names and folders for Drupal core.
InstallStorage::getFilePath public function Overrides Drupal\Core\Config\FileStorage::getFilePath(). Overrides FileStorage::getFilePath
InstallStorage::listAll public function Gets configuration object names starting with a given prefix. Overrides FileStorage::listAll
InstallStorage::rename public function Overrides Drupal\Core\Config\FileStorage::rename(). Overrides FileStorage::rename
InstallStorage::reset public function Resets the static cache.
InstallStorage::write public function Overrides Drupal\Core\Config\FileStorage::write(). Overrides FileStorage::write
InstallStorage::__construct public function Constructs an InstallStorage object. Overrides FileStorage::__construct 1
StorageInterface::DEFAULT_COLLECTION constant The default collection name.