You are here

class ConfigReadonlyStorage in Configuration Read-only mode 8

Same name and namespace in other branches
  1. 7 lib/Drupal/config_readonly/Config/ConfigReadonlyStorage.php \Drupal\config_readonly\Config\ConfigReadonlyStorage

Defines a config read-only storage controller.

This fails on write operations.

Hierarchy

Expanded class hierarchy of ConfigReadonlyStorage

File

src/Config/ConfigReadonlyStorage.php, line 23

Namespace

Drupal\config_readonly\Config
View source
class ConfigReadonlyStorage extends CachedStorage {
  use ConfigReadonlyWhitelistTrait;

  /**
   * The used lock backend instance.
   *
   * @var \Drupal\Core\Lock\LockBackendInterface
   */
  protected $lock;

  /**
   * The request stack.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  protected $requestStack;

  /**
   * Constructs a new ConfigReadonlyStorage.
   *
   * @param \Drupal\Core\Config\StorageInterface $storage
   *   A configuration storage to be cached.
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   A cache backend used to store configuration.
   * @param \Drupal\Core\Lock\LockBackendInterface $lock
   *   The lock backend to check if config imports are in progress.
   * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
   *   The request stack.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler to invoke hooks.
   */
  public function __construct(StorageInterface $storage, CacheBackendInterface $cache, LockBackendInterface $lock, RequestStack $request_stack, ModuleHandlerInterface $module_handler) {
    parent::__construct($storage, $cache);
    $this->lock = $lock;
    $this->requestStack = $request_stack;
    $this
      ->setModuleHandler($module_handler);
  }

  /**
   * {@inheritdoc}
   */
  public function createCollection($collection) {
    return new static($this->storage
      ->createCollection($collection), $this->cache, $this->lock, $this->requestStack, $this->moduleHandler);
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\config_readonly\Exception\ConfigReadonlyStorageException
   */
  public function write($name, array $data) {
    $this
      ->checkLock($name);
    return parent::write($name, $data);
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\config_readonly\Exception\ConfigReadonlyStorageException
   */
  public function delete($name) {
    $this
      ->checkLock($name);
    return parent::delete($name);
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\config_readonly\Exception\ConfigReadonlyStorageException
   */
  public function rename($name, $new_name) {
    $this
      ->checkLock($name);
    $this
      ->checkLock($new_name);
    return parent::rename($name, $new_name);
  }

  /**
   * {@inheritdoc}
   *
   * @throws \Drupal\config_readonly\Exception\ConfigReadonlyStorageException
   */
  public function deleteAll($prefix = '') {
    $this
      ->checkLock();
    return parent::deleteAll($prefix);
  }

  /**
   * Check whether config is currently locked.
   *
   * @param string $name
   *   Check for a specific lock config.
   *
   * @throws \Drupal\config_readonly\Exception\ConfigReadonlyStorageException
   */
  protected function checkLock($name = '') {

    // If settings.php says to lock config changes and if the config importer
    // isn't running (we do not want to lock config imports), then throw an
    // exception.
    // @see \Drupal\Core\Config\ConfigImporter::alreadyImporting()
    if (Settings::get('config_readonly') && $this->lock
      ->lockMayBeAvailable(ConfigImporter::LOCK_NAME)) {
      $request = $this->requestStack
        ->getCurrentRequest();
      if ($request && $request->attributes
        ->get(RouteObjectInterface::ROUTE_NAME) === 'system.db_update') {

        // We seem to be in the middle of running update.php.
        // @see \Drupal\Core\Update\UpdateKernel::setupRequestMatch()
        // @todo - always allow or support a flag for blocking it?
        return;
      }

      // Don't block particular patterns.
      if ($name && $this
        ->matchesWhitelistPattern($name)) {
        return;
      }
      throw new ConfigReadonlyStorageException('Your site configuration active store is currently locked.');
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CachedStorage::$cache protected property The instantiated Cache backend.
CachedStorage::$findByPrefixCache protected property List of listAll() prefixes with their results.
CachedStorage::$storage protected property The configuration storage to be cached.
CachedStorage::decode public function Decodes configuration data from the storage-specific format. Overrides StorageInterface::decode
CachedStorage::encode public function Encodes configuration data into the storage-specific format. Overrides StorageInterface::encode
CachedStorage::exists public function Returns whether a configuration object exists. Overrides StorageInterface::exists
CachedStorage::findByPrefix protected function Finds configuration object names starting with a given prefix.
CachedStorage::getAllCollectionNames public function Gets the existing collections. Overrides StorageInterface::getAllCollectionNames
CachedStorage::getCacheKey protected function Returns a cache key for a configuration name using the collection.
CachedStorage::getCacheKeys protected function Returns a cache key map for an array of configuration names.
CachedStorage::getCollectionName public function Gets the name of the current collection the storage is using. Overrides StorageInterface::getCollectionName
CachedStorage::getCollectionPrefix protected function Returns a cache ID prefix to use for the collection.
CachedStorage::listAll public function Gets configuration object names starting with a given prefix. Overrides StorageInterface::listAll
CachedStorage::read public function Reads configuration data from the storage. Overrides StorageInterface::read
CachedStorage::readMultiple public function Reads configuration data from the storage. Overrides StorageInterface::readMultiple
CachedStorage::resetListCache public function Clears the static list cache. Overrides StorageCacheInterface::resetListCache
ConfigReadonlyStorage::$lock protected property The used lock backend instance.
ConfigReadonlyStorage::$requestStack protected property The request stack.
ConfigReadonlyStorage::checkLock protected function Check whether config is currently locked.
ConfigReadonlyStorage::createCollection public function Creates a collection on the storage. Overrides CachedStorage::createCollection
ConfigReadonlyStorage::delete public function Overrides CachedStorage::delete
ConfigReadonlyStorage::deleteAll public function Overrides CachedStorage::deleteAll
ConfigReadonlyStorage::rename public function Overrides CachedStorage::rename
ConfigReadonlyStorage::write public function Overrides CachedStorage::write
ConfigReadonlyStorage::__construct public function Constructs a new ConfigReadonlyStorage. Overrides CachedStorage::__construct
ConfigReadonlyWhitelistTrait::$moduleHandler protected property The module handler.
ConfigReadonlyWhitelistTrait::$patterns protected property An array to store the whitelist ignore patterns.
ConfigReadonlyWhitelistTrait::getWhitelistPatterns protected function Get whitelist patterns.
ConfigReadonlyWhitelistTrait::matchesWhitelistPattern protected function Check if the given name matches any whitelist pattern.
ConfigReadonlyWhitelistTrait::setModuleHandler protected function Set the module handler.
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 1
DependencySerializationTrait::__wakeup public function 2
StorageInterface::DEFAULT_COLLECTION constant The default collection name.