You are here

class ContentDatabaseStorage in Content Synchronization 8.2

Same name and namespace in other branches
  1. 3.0.x src/Content/ContentDatabaseStorage.php \Drupal\content_sync\Content\ContentDatabaseStorage

Defines the Database storage.

Hierarchy

Expanded class hierarchy of ContentDatabaseStorage

3 files declare their use of ContentDatabaseStorage
ContentExportTrait.php in src/Form/ContentExportTrait.php
content_sync.module in ./content_sync.module
Allows site administrators to modify content.
ExportQueueResolver.php in src/DependencyResolver/ExportQueueResolver.php

File

src/Content/ContentDatabaseStorage.php, line 11

Namespace

Drupal\content_sync\Content
View source
class ContentDatabaseStorage extends DatabaseStorage {

  /**
   * {@inheritdoc}
   */
  public function cs_write($name, array $data, $collection) {
    $data = $this
      ->encode($data);
    try {
      return $this
        ->cs_doWrite($name, $data, $collection);
    } catch (\Exception $e) {

      // If there was an exception, try to create the table.
      if ($this
        ->ensureTableExists()) {
        return $this
          ->cs_doWrite($name, $data, $collection);
      }

      // Some other failure that we can not recover from.
      throw $e;
    }
  }

  /**
   * Helper method so we can re-try a write.
   *
   * @param string $name
   *   The content name.
   * @param string $data
   *   The content data, already dumped to a string.
   * @param string $collection
   *   The content collection name, entity type + bundle.
   *
   * @return bool
   */
  protected function cs_doWrite($name, $data, $collection) {
    $options = [
      'return' => Database::RETURN_AFFECTED,
    ] + $this->options;
    $this->connection
      ->delete($this->table, $options)
      ->condition('name', $name)
      ->execute();
    return (bool) $this->connection
      ->merge($this->table, $options)
      ->keys([
      'collection',
      'name',
    ], [
      $collection,
      $name,
    ])
      ->fields([
      'data' => $data,
    ])
      ->execute();
  }

  /**
   * {@inheritdoc}
   */
  public function cs_read($name) {
    $data = FALSE;
    try {
      $raw = $this->connection
        ->query('SELECT data FROM {' . $this->connection
        ->escapeTable($this->table) . '} WHERE name = :name', [
        ':name' => $name,
      ], $this->options)
        ->fetchField();
      if ($raw !== FALSE) {
        $data = $this
          ->decode($raw);
      }
    } catch (\Exception $e) {

      // If we attempt a read without actually having the database or the table
      // available, just return FALSE so the caller can handle it.
    }
    return $data;
  }

  /**
   * Implements Drupal\Core\Config\StorageInterface::delete().
   *
   * @throws PDOException
   *
   * @todo Ignore replica targets for data manipulation operations.
   */
  public function cs_delete($name) {
    $options = [
      'return' => Database::RETURN_AFFECTED,
    ] + $this->options;
    return (bool) $this->connection
      ->delete($this->table, $options)
      ->condition('name', $name)
      ->execute();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ContentDatabaseStorage::cs_delete public function Implements Drupal\Core\Config\StorageInterface::delete().
ContentDatabaseStorage::cs_doWrite protected function Helper method so we can re-try a write.
ContentDatabaseStorage::cs_read public function
ContentDatabaseStorage::cs_write public function
DatabaseStorage::$collection protected property The storage collection.
DatabaseStorage::$connection protected property The database connection.
DatabaseStorage::$options protected property Additional database connection options to use in queries.
DatabaseStorage::$table protected property The database table name.
DatabaseStorage::createCollection public function Creates a collection on the storage. Overrides StorageInterface::createCollection
DatabaseStorage::decode public function Implements Drupal\Core\Config\StorageInterface::decode(). Overrides StorageInterface::decode
DatabaseStorage::delete public function Implements Drupal\Core\Config\StorageInterface::delete(). Overrides StorageInterface::delete
DatabaseStorage::deleteAll public function Deletes configuration objects whose names start with a given prefix. Overrides StorageInterface::deleteAll
DatabaseStorage::doWrite protected function Helper method so we can re-try a write.
DatabaseStorage::encode public function Encodes configuration data into the storage-specific format. Overrides StorageInterface::encode
DatabaseStorage::ensureTableExists protected function Check if the config table exists and create it if not.
DatabaseStorage::exists public function Returns whether a configuration object exists. Overrides StorageInterface::exists
DatabaseStorage::getAllCollectionNames public function Gets the existing collections. Overrides StorageInterface::getAllCollectionNames
DatabaseStorage::getCollectionName public function Gets the name of the current collection the storage is using. Overrides StorageInterface::getCollectionName
DatabaseStorage::listAll public function Gets configuration object names starting with a given prefix. Overrides StorageInterface::listAll
DatabaseStorage::read public function Reads configuration data from the storage. Overrides StorageInterface::read
DatabaseStorage::readMultiple public function Reads configuration data from the storage. Overrides StorageInterface::readMultiple
DatabaseStorage::rename public function Implements Drupal\Core\Config\StorageInterface::rename(). Overrides StorageInterface::rename
DatabaseStorage::schemaDefinition protected static function Defines the schema for the configuration table.
DatabaseStorage::write public function Writes configuration data to the storage. Overrides StorageInterface::write
DatabaseStorage::__construct public function Constructs a new DatabaseStorage.
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.