class AutoloadingStorage

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

Defines the autoloading storage.

Allows configuration to be read that contains constants and enum from extensions that are not yet installed. To do so, a class loader is temporarily registered while reading from configuration.

Hierarchy

Expanded class hierarchy of AutoloadingStorage

1 string reference to 'AutoloadingStorage'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses AutoloadingStorage
config.storage.sync in core/core.services.yml
Drupal\Core\Config\AutoloadingStorage

File

core/lib/Drupal/Core/Config/AutoloadingStorage.php, line 17

Namespace

Drupal\Core\Config
View source
class AutoloadingStorage implements StorageInterface, StorageCacheInterface {
  use DependencySerializationTrait;
  
  /**
   * Class loader that loads code from extensions provided to the constructor.
   */
  protected ?ClassLoader $classloader = NULL;
  
  /**
   * Constructs a new AutoloadingStorage.
   *
   * @param \Drupal\Core\Config\StorageInterface $storage
   *   A configuration storage to be wrapped.
   * @param string[] $newExtensions
   *   A list of new extensions in the source storage that we need to allow to
   *   autoload while reading. The values should be absolute paths to the
   *   extension directory.
   */
  public function __construct(protected StorageInterface $storage, array $newExtensions = []) {
    if (!empty($newExtensions)) {
      $this->classloader = new ClassLoader();
      foreach ($newExtensions as $extension => $dir) {
        $this->classloader
          ->addPsr4("Drupal\\{$extension}\\", $dir . '/src');
      }
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function exists($name) : bool {
    return $this->storage
      ->exists($name);
  }
  
  /**
   * {@inheritdoc}
   */
  public function read($name) : array|false {
    $this->classloader?->register(TRUE);
    try {
      $data = $this->storage
        ->read($name);
    } finally {
      $this->classloader?->unregister();
    }
    return $data;
  }
  
  /**
   * {@inheritdoc}
   */
  public function readMultiple(array $names) : array {
    $this->classloader?->register(TRUE);
    try {
      $data = $this->storage
        ->readMultiple($names);
    } finally {
      $this->classloader?->unregister();
    }
    return $data;
  }
  
  /**
   * {@inheritdoc}
   */
  public function write($name, array $data) : bool {
    return $this->storage
      ->write($name, $data);
  }
  
  /**
   * {@inheritdoc}
   */
  public function delete($name) : bool {
    return $this->storage
      ->delete($name);
  }
  
  /**
   * {@inheritdoc}
   */
  public function rename($name, $new_name) : bool {
    return $this->storage
      ->rename($name, $new_name);
  }
  
  /**
   * {@inheritdoc}
   */
  public function encode($data) : string {
    return $this->storage
      ->encode($data);
  }
  
  /**
   * {@inheritdoc}
   */
  public function decode($raw) : array {
    return $this->storage
      ->decode($raw);
  }
  
  /**
   * {@inheritdoc}
   */
  public function listAll($prefix = '') : array {
    return $this->storage
      ->listAll($prefix);
  }
  
  /**
   * {@inheritdoc}
   */
  public function deleteAll($prefix = '') : bool {
    return $this->storage
      ->deleteAll($prefix);
  }
  
  /**
   * Clears the static list cache.
   */
  public function resetListCache() : void {
    if ($this->storage instanceof StorageCacheInterface) {
      $this->storage
        ->resetListCache();
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function createCollection($collection) : static {
    $storage = new static($this->storage
      ->createCollection($collection));
    $storage->classloader = $this->classloader;
    return $storage;
  }
  
  /**
   * {@inheritdoc}
   */
  public function getAllCollectionNames() : array {
    return $this->storage
      ->getAllCollectionNames();
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCollectionName() : string {
    return $this->storage
      ->getCollectionName();
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
AutoloadingStorage::$classloader protected property Class loader that loads code from extensions provided to the constructor.
AutoloadingStorage::createCollection public function Creates a collection on the storage. Overrides StorageInterface::createCollection
AutoloadingStorage::decode public function Decodes configuration data from the storage-specific format. Overrides StorageInterface::decode
AutoloadingStorage::delete public function Deletes a configuration object from the storage. Overrides StorageInterface::delete
AutoloadingStorage::deleteAll public function Deletes configuration objects whose names start with a given prefix. Overrides StorageInterface::deleteAll
AutoloadingStorage::encode public function Encodes configuration data into the storage-specific format. Overrides StorageInterface::encode
AutoloadingStorage::exists public function Returns whether a configuration object exists. Overrides StorageInterface::exists
AutoloadingStorage::getAllCollectionNames public function Gets the existing collections. Overrides StorageInterface::getAllCollectionNames
AutoloadingStorage::getCollectionName public function Gets the name of the current collection the storage is using. Overrides StorageInterface::getCollectionName
AutoloadingStorage::listAll public function Gets configuration object names starting with a given prefix. Overrides StorageInterface::listAll
AutoloadingStorage::read public function Reads configuration data from the storage. Overrides StorageInterface::read
AutoloadingStorage::readMultiple public function Reads configuration data from the storage. Overrides StorageInterface::readMultiple
AutoloadingStorage::rename public function Renames a configuration object in the storage. Overrides StorageInterface::rename
AutoloadingStorage::resetListCache public function Clears the static list cache. Overrides StorageCacheInterface::resetListCache
AutoloadingStorage::write public function Writes configuration data to the storage. Overrides StorageInterface::write
AutoloadingStorage::__construct public function Constructs a new AutoloadingStorage.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
StorageInterface::DEFAULT_COLLECTION constant The default collection name.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.