You are here

public function DatabaseFactory::get in MongoDB 8.2

Return the MongoDB database matching an alias.

Parameters

string $dbAlias: The alias string, like "default".

Return value

\MongoDB\Database|null The selected database, or NULL if an error occurred.

File

modules/mongodb/src/DatabaseFactory.php, line 53

Class

DatabaseFactory
Helper class to construct a MongoDB Database with Drupal specific config.

Namespace

Drupal\mongodb

Code

public function get($dbAlias) {
  if (!isset($this->settings[$dbAlias])) {
    throw new \InvalidArgumentException((new FormattableMarkup('Nonexistent database alias: @alias', [
      '@alias' => $dbAlias,
    ]))
      ->__toString());
  }
  try {
    list($clientAlias, $database) = $this->settings[$dbAlias];
    $client = $this->clientFactory
      ->get($clientAlias);
    $result = $client
      ->selectDatabase($database);
  } catch (\InvalidArgumentException $e) {
    $result = NULL;
  }
  return $result;
}