You are here

class MongodbConfigStorage in MongoDB 8

Hierarchy

Expanded class hierarchy of MongodbConfigStorage

3 files declare their use of MongodbConfigStorage
ConfigStorageTest.php in src/Tests/ConfigStorageTest.php
Definition of Drupal\mongodb\Tests\ConfigStorageTest.
mongodb.drush.inc in ./mongodb.drush.inc
Contains
mongodb.install in ./mongodb.install
1 string reference to 'MongodbConfigStorage'
mongodb.services.yml in ./mongodb.services.yml
mongodb.services.yml
2 services use MongodbConfigStorage
mongodb.config.storage in ./mongodb.services.yml
Drupal\mongodb\MongodbConfigStorage
mongodb.config.storage.snapshot in ./mongodb.services.yml
Drupal\mongodb\MongodbConfigStorage

File

src/MongodbConfigStorage.php, line 12
Definition of Drupal\mongodb\Config\MongoStorage.

Namespace

Drupal\mongodb
View source
class MongodbConfigStorage implements StorageInterface {

  /**
   * The object wrapping the MongoDB database object.
   *
   * @var MongoCollectionFactory
   */
  protected $mongo;

  /**
   * The mongo collection name.
   *
   * @var string
   */
  protected $mongoCollectionName;

  /**
   * The prefix, typically 'config' or 'config.staging'.
   *
   * @var string
   */
  protected $prefix;

  /**
   * Constructs a new ConfigStorage controller.
   *
   * @param MongoCollectionFactory $mongo
   *   The object wrapping the MongoDB database object.
   * @param string $collection
   *   Name of the config collection.
   */
  public function __construct(MongoCollectionFactory $mongo, $prefix = 'config', $collection = StorageInterface::DEFAULT_COLLECTION) {
    $this->mongo = $mongo;
    $this->prefix = $prefix;
    $this->mongoCollectionName = $prefix . ($collection ?: '');
  }

  /**
   * {@inheritdoc}
   */
  public function exists($name) {
    return $this
      ->mongoCollection()
      ->count(array(
      '_id' => $name,
    )) ? TRUE : FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function read($name) {
    $result = $this
      ->mongoCollection()
      ->findOne(array(
      '_id' => $name,
    ));
    if (empty($result)) {
      return FALSE;
    }
    unset($result['_id']);
    return $result;
  }

  /**
   * {@inheritdoc}
   */
  public function readMultiple(array $names) {
    $data = $this
      ->mongoCollection()
      ->find(array(
      '_id' => array(
        '$in' => array_values($names),
      ),
    ));
    $list = array();
    foreach ($data as $item) {
      $list[$item['_id']] = $item;
      unset($list[$item['_id']]['_id']);
    }
    return $list;
  }

  /**
   * {@inheritdoc}
   */
  public function write($name, array $data) {
    try {
      $this
        ->mongoCollection()
        ->update(array(
        '_id' => $name,
      ), array(
        '$set' => $data,
      ), array(
        'upsert' => TRUE,
      ));
    } catch (\Exception $e) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function delete($name) {
    try {
      $result = $this
        ->mongoCollection()
        ->remove(array(
        '_id' => $name,
      ));
    } catch (\Exception $e) {
      return FALSE;
    }
    return $result['n'] == 0 ? FALSE : TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function rename($name, $new_name) {
    try {
      $collection = $this
        ->mongoCollection();
      $item = $collection
        ->findOne(array(
        '_id' => $name,
      ));
      if (empty($item)) {
        return FALSE;
      }
      $item['_id'] = $new_name;
      $result = $collection
        ->insert($item);
      if (!empty($result['err'])) {
        return FALSE;
      }
      $collection
        ->remove(array(
        '_id' => $name,
      ));
    } catch (\Exception $e) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function encode($data) {

    // WTF is this part of general StorageInterface if it is only needed for
    // file-based backends?
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function decode($data) {

    // WTF is this part of general StorageInterface if it is only needed for
    // file-based backends?
    return $data;
  }

  /**
   * {@inheritdoc}
   */
  public function listAll($prefix = '') {
    $condition = array();
    if (!empty($prefix)) {
      $condition = array(
        '_id' => new \MongoRegex('/^' . str_replace('.', '\\.', $prefix) . '/'),
      );
    }
    $names = array();
    $result = $this
      ->mongoCollection()
      ->find($condition, array(
      '_id' => TRUE,
    ));
    foreach ($result as $item) {
      $names[] = $item['_id'];
    }
    return $names;
  }

  /**
   * {@inheritdoc}
   */
  public function deleteAll($prefix = '') {
    $condition = array();
    if (!empty($prefix)) {
      $condition = array(
        '_id' => new \MongoRegex('/^' . str_replace('.', '\\.', $prefix) . '/'),
      );
    }
    try {
      $this
        ->mongoCollection()
        ->remove($condition);
    } catch (\Exception $e) {
      return FALSE;
    }
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  public function createCollection($collection) {
    return new static($this->mongo, $this->prefix, $collection);
  }

  /**
   * {@inheritdoc}
   */
  public function getAllCollectionNames() {
    return preg_grep('/^config\\./', $this->mongo
      ->get('config')->db
      ->getCollectionNames());
  }

  /**
   * {@inheritdoc}
   */
  public function getCollectionName() {
    return $this->mongoCollectionName;
  }

  /**
   * @return \MongoCollection
   */
  protected function mongoCollection() {
    return $this->mongo
      ->get($this->mongoCollectionName);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MongodbConfigStorage::$mongo protected property The object wrapping the MongoDB database object.
MongodbConfigStorage::$mongoCollectionName protected property The mongo collection name.
MongodbConfigStorage::$prefix protected property The prefix, typically 'config' or 'config.staging'.
MongodbConfigStorage::createCollection public function Creates a collection on the storage. Overrides StorageInterface::createCollection
MongodbConfigStorage::decode public function Decodes configuration data from the storage-specific format. Overrides StorageInterface::decode
MongodbConfigStorage::delete public function Deletes a configuration object from the storage. Overrides StorageInterface::delete
MongodbConfigStorage::deleteAll public function Deletes configuration objects whose names start with a given prefix. Overrides StorageInterface::deleteAll
MongodbConfigStorage::encode public function Encodes configuration data into the storage-specific format. Overrides StorageInterface::encode
MongodbConfigStorage::exists public function Returns whether a configuration object exists. Overrides StorageInterface::exists
MongodbConfigStorage::getAllCollectionNames public function Gets the existing collections. Overrides StorageInterface::getAllCollectionNames
MongodbConfigStorage::getCollectionName public function Gets the name of the current collection the storage is using. Overrides StorageInterface::getCollectionName
MongodbConfigStorage::listAll public function Gets configuration object names starting with a given prefix. Overrides StorageInterface::listAll
MongodbConfigStorage::mongoCollection protected function
MongodbConfigStorage::read public function Reads configuration data from the storage. Overrides StorageInterface::read 1
MongodbConfigStorage::readMultiple public function Reads configuration data from the storage. Overrides StorageInterface::readMultiple
MongodbConfigStorage::rename public function Renames a configuration object in the storage. Overrides StorageInterface::rename
MongodbConfigStorage::write public function Writes configuration data to the storage. Overrides StorageInterface::write
MongodbConfigStorage::__construct public function Constructs a new ConfigStorage controller. 1
StorageInterface::DEFAULT_COLLECTION constant The default collection name.