DatabaseStorage example


  public function __construct(SerializationInterface $serializer, Connection $connection) {
    $this->serializer = $serializer;
    $this->connection = $connection;
  }

  /** * {@inheritdoc} */
  public function get($collection) {
    return new DatabaseStorage($collection$this->serializer, $this->connection);
  }

}

    catch (\Exception $e) {
      $this->assertInstanceOf(ConfigValueException::class$e);
    }
  }

  /** * Tests data type handling. */
  public function testDataTypes() {
    \Drupal::service('module_installer')->install(['config_test']);
    $storage = new DatabaseStorage($this->container->get('database'), 'config');
    $name = 'config_test.types';
    $config = $this->config($name);

    // Verify variable data types are intact.     $data = [
      'array' => [],
      'boolean' => TRUE,
      'exp' => 1.2e+34,
      'float' => 3.14159,
      'float_as_integer' => (float) 1,
      'hex' => 0xC,
      

    // Fallback to the DatabaseStorage.     return $storage_backend ?: self::getDatabaseStorage();
  }

  /** * Returns a Database configuration storage implementation. * * @return \Drupal\Core\Config\DatabaseStorage */
  public static function getDatabaseStorage() {
    return new DatabaseStorage(Database::getConnection(), 'config');
  }

}

class DatabaseStorageTest extends ConfigStorageTestBase {

  /** * {@inheritdoc} */
  protected function setUp(): void {
    parent::setUp();

    $this->storage = new DatabaseStorage($this->container->get('database'), 'config');
    $this->invalidStorage = new DatabaseStorage($this->container->get('database'), 'invalid');
  }

  protected function read($name) {
    $data = Database::getConnection()->select('config', 'c')->fields('c', ['data'])->condition('name', $name)->execute()->fetchField();
    return unserialize($data);
  }

  protected function insert($name$data) {
    Database::getConnection()->insert('config')->fields(['name' => $name, 'data' => $data])->execute();
  }

  
$this->installEntitySchema('user');
    $this->installEntitySchema('file');
    $this->installEntitySchema('menu_link_content');
    $this->installEntitySchema('path_alias');
    $this->installSchema('system', 'sequences');

    // Place some sample config to test for in the export.     $this->data = [
      'foo' => $this->randomMachineName(),
      'bar' => $this->randomMachineName(),
    ];
    $storage = new DatabaseStorage(Database::getConnection(), 'config');
    $storage->write('test_config', $this->data);

    // Create user account with some potential syntax issues.     // cspell:disable-next-line     $account = User::create(['mail' => 'q\'uote$dollar@example.com', 'name' => '$dollar']);
    $account->save();

    // Create a path alias.     $this->createPathAlias('/user/' . $account->id(), '/user/example');

    // Create a cache table (this will create 'cache_discovery').

  public function __construct(StorageInterface $active, Connection $connection, EventDispatcherInterface $event_dispatcher, LockBackendInterface $lock) {
    $this->active = $active;
    $this->eventDispatcher = $event_dispatcher;
    $this->lock = $lock;
    // The point of this service is to provide the storage and dispatch the     // event when needed, so the storage itself can not be a service.     $this->storage = new DatabaseStorage($connection, 'config_export');
  }

  /** * {@inheritdoc} */
  public function getStorage() {
    // Acquire a lock for the request to assert that the storage does not change     // when a concurrent request transforms the storage.     if (!$this->lock->acquire(self::LOCK_NAME)) {
      $this->lock->wait(self::LOCK_NAME);
      if (!$this->lock->acquire(self::LOCK_NAME)) {
        

  public function transform(StorageInterface $storage) {
    // We use a database storage to reduce the memory requirement.     $mutable = new DatabaseStorage($this->connection, 'config_import');

    if (!$this->persistentLock->lockMayBeAvailable(ConfigImporter::LOCK_NAME)) {
      // If the config importer is already importing, the transformation will       // always be the one the config importer is already using. This makes sure       // that even if the storage changes the importer continues importing the       // same configuration.       return $mutable;
    }

    // Acquire a lock to ensure that the storage is not changed when a     // concurrent request tries to transform the storage. The lock will be
Home | Imprint | This part of the site doesn't use cookies.