class AllowListConfigStorage

A read-only storage wrapper that only allows access to certain config names.

@internal This API is experimental.

Hierarchy

Expanded class hierarchy of AllowListConfigStorage

File

core/lib/Drupal/Core/Recipe/AllowListConfigStorage.php, line 15

Namespace

Drupal\Core\Recipe
View source
final class AllowListConfigStorage implements StorageInterface {
  
  /**
   * @param \Drupal\Core\Config\StorageInterface $decorated
   *   A config storage backend to wrap around.
   * @param string[] $allowList
   *   A list of config names. Only these names will be visible, or readable,
   *   by this storage. Cannot be empty.
   */
  public function __construct(private readonly StorageInterface $decorated, private readonly array $allowList) {
    if (empty($allowList)) {
      throw new \LogicException('AllowListConfigStorage cannot be constructed with an empty allow list.');
    }
  }
  
  /**
   * {@inheritdoc}
   */
  public function exists($name) : bool {
    if (in_array($name, $this->allowList, TRUE)) {
      return $this->decorated
        ->exists($name);
    }
    return FALSE;
  }
  
  /**
   * {@inheritdoc}
   */
  public function read($name) : array|false {
    return $this->exists($name) ? $this->decorated
      ->read($name) : FALSE;
  }
  
  /**
   * {@inheritdoc}
   */
  public function readMultiple(array $names) : array {
    $names = array_intersect($names, $this->allowList);
    return $this->decorated
      ->readMultiple($names);
  }
  
  /**
   * {@inheritdoc}
   */
  public function write($name, array $data) : never {
    throw new \BadMethodCallException('This storage is read-only.');
  }
  
  /**
   * {@inheritdoc}
   */
  public function delete($name) : never {
    throw new \BadMethodCallException('This storage is read-only.');
  }
  
  /**
   * {@inheritdoc}
   */
  public function rename($name, $new_name) : never {
    throw new \BadMethodCallException('This storage is read-only.');
  }
  
  /**
   * {@inheritdoc}
   */
  public function encode($data) : string {
    return $this->decorated
      ->encode($data);
  }
  
  /**
   * {@inheritdoc}
   */
  public function decode($raw) : array {
    return $this->decorated
      ->decode($raw);
  }
  
  /**
   * {@inheritdoc}
   */
  public function listAll($prefix = '') : array {
    return array_intersect($this->decorated
      ->listAll($prefix), $this->allowList);
  }
  
  /**
   * {@inheritdoc}
   */
  public function deleteAll($prefix = '') : never {
    throw new \BadMethodCallException('This storage is read-only.');
  }
  
  /**
   * {@inheritdoc}
   */
  public function createCollection($collection) : static {
    return new static($this->decorated
      ->createCollection($collection), $this->allowList);
  }
  
  /**
   * {@inheritdoc}
   */
  public function getAllCollectionNames() : array {
    return $this->decorated
      ->getAllCollectionNames();
  }
  
  /**
   * {@inheritdoc}
   */
  public function getCollectionName() : string {
    return $this->decorated
      ->getCollectionName();
  }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
AllowListConfigStorage::createCollection public function Creates a collection on the storage. Overrides StorageInterface::createCollection
AllowListConfigStorage::decode public function Decodes configuration data from the storage-specific format. Overrides StorageInterface::decode
AllowListConfigStorage::delete public function Deletes a configuration object from the storage. Overrides StorageInterface::delete
AllowListConfigStorage::deleteAll public function Deletes configuration objects whose names start with a given prefix. Overrides StorageInterface::deleteAll
AllowListConfigStorage::encode public function Encodes configuration data into the storage-specific format. Overrides StorageInterface::encode
AllowListConfigStorage::exists public function Returns whether a configuration object exists. Overrides StorageInterface::exists
AllowListConfigStorage::getAllCollectionNames public function Gets the existing collections. Overrides StorageInterface::getAllCollectionNames
AllowListConfigStorage::getCollectionName public function Gets the name of the current collection the storage is using. Overrides StorageInterface::getCollectionName
AllowListConfigStorage::listAll public function Gets configuration object names starting with a given prefix. Overrides StorageInterface::listAll
AllowListConfigStorage::read public function Reads configuration data from the storage. Overrides StorageInterface::read
AllowListConfigStorage::readMultiple public function Reads configuration data from the storage. Overrides StorageInterface::readMultiple
AllowListConfigStorage::rename public function Renames a configuration object in the storage. Overrides StorageInterface::rename
AllowListConfigStorage::write public function Writes configuration data to the storage. Overrides StorageInterface::write
AllowListConfigStorage::__construct public function
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.