You are here

public function ClientFactory::get in MongoDB 8.2

Return a Client instance for a given alias.

Parameters

string $alias: The alias defined in settings for a Client.

Return value

\MongoDB\Client A Client instance for the chosen server.

File

modules/mongodb/src/ClientFactory.php, line 48

Class

ClientFactory
Helper class to construct a MongoDB client with Drupal specific config.

Namespace

Drupal\mongodb

Code

public function get($alias) {
  if (!isset($this->clients[$alias]) || !$this->clients[$alias] instanceof Client) {
    $info = $this->settings[$alias] ?? [];
    $info += [
      'uri' => 'mongodb://localhost:27017',
      'uriOptions' => [],
      'driverOptions' => [],
    ];

    // Don't use ...$info: keys can be out of order.
    $this->clients[$alias] = new Client($info['uri'], $info['uriOptions'], $info['driverOptions']);
  }
  return $this->clients[$alias];
}