You are here

class Tools in MongoDB 8.2

Class MongoDbCommands provides the Drush commands for the mongodb module.

Hierarchy

  • class \Drupal\mongodb\Install\Tools

Expanded class hierarchy of Tools

4 files declare their use of Tools
FindCommand.php in modules/mongodb/src/Command/FindCommand.php
MongodbCommands.php in modules/mongodb/src/Commands/MongodbCommands.php
SettingsCommand.php in modules/mongodb/src/Command/SettingsCommand.php
ToolsTest.php in modules/mongodb/tests/src/Kernel/ToolsTest.php
1 string reference to 'Tools'
mongodb.services.yml in modules/mongodb/mongodb.services.yml
modules/mongodb/mongodb.services.yml
1 service uses Tools
mongodb.tools in modules/mongodb/mongodb.services.yml
\Drupal\mongodb\Install\Tools

File

modules/mongodb/src/Install/Tools.php, line 16

Namespace

Drupal\mongodb\Install
View source
class Tools {

  /**
   * The mongobb.database_factory service.
   *
   * @var \Drupal\mongodb\DatabaseFactory
   */
  protected $dbFactory;

  /**
   * The settings service.
   *
   * @var \Drupal\Core\Site\Settings
   */
  protected $settings;

  /**
   * The serialization.yaml service.
   *
   * @var \Drupal\Component\Serialization\SerializationInterface
   */
  protected $yaml;

  /**
   * MongoDbCommands constructor.
   *
   * @param \Drupal\mongodb\DatabaseFactory $databaseFactory
   *   The mongobb.database_factory service.
   * @param \Drupal\Core\Site\Settings $settings
   *   The settings service.
   * @param \Drupal\Component\Serialization\SerializationInterface $yaml
   *   The serialization.yaml service.
   */
  public function __construct(DatabaseFactory $databaseFactory, Settings $settings, SerializationInterface $yaml) {
    $this->dbFactory = $databaseFactory;
    $this->settings = $settings;
    $this->yaml = $yaml;
  }

  /**
   * Command callback for mongodb:mdbf.
   *
   * @param string $alias
   *   The alias of the database in which to perform the query.
   * @param string $collection
   *   The name of the collection in which to find.
   * @param string $selector
   *   The selector to apply to the query.
   *
   * @return array
   *   The query results.
   */
  public function find(string $alias, string $collection, string $selector = '{}') : array {

    /** @var \MongoDB\Database $database */
    $database = $this->dbFactory
      ->get($alias);
    $jsonSelector = json_decode($selector);
    if ($jsonSelector === NULL) {
      throw new InvalidArgumentException("Your JSON selector could not be decoded. Here is how PHP received it: " . var_export($selector, TRUE));
    }
    $docs1 = $database
      ->selectCollection($collection)
      ->find($jsonSelector, [
      'typeMap' => [
        'root' => 'array',
        'document' => 'array',
        'array' => 'array',
      ],
    ]);
    $docs2 = [];

    // Convert objects in result set to hashes.
    foreach ($docs1 as $doc1) {
      $docs2[] = json_decode(json_encode($doc1), TRUE);
    }
    return $docs2;
  }

  /**
   * List collections matching $regex in database $alias.
   *
   * @param string $alias
   *   The alias in which to list the collections.
   * @param string $regex
   *   The pattern collection names must match.
   *
   * @return array
   *   An array of collection objects.
   */
  public function listCollections(string $alias, string $regex) : array {

    /** @var \MongoDB\Database $database */
    $database = $this->dbFactory
      ->get($alias);
    $collections = [];
    foreach ($database
      ->listCollections() as $info) {
      $name = $info
        ->getName();
      $collection = $database
        ->selectCollection($name);
      if (preg_match($regex, $name)) {
        $collections[] = $collection;
      }
    }
    return $collections;
  }

  /**
   * Command callback for mongodb:mdbs.
   *
   * @return array
   *   The MongoDB portion of the settings.
   */
  public function settings() : array {
    return $this->settings
      ->get(MongoDb::MODULE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Tools::$dbFactory protected property The mongobb.database_factory service.
Tools::$settings protected property The settings service.
Tools::$yaml protected property The serialization.yaml service.
Tools::find public function Command callback for mongodb:mdbf.
Tools::listCollections public function List collections matching $regex in database $alias.
Tools::settings public function Command callback for mongodb:mdbs.
Tools::__construct public function MongoDbCommands constructor.