/** * Register MongoDbConnector within the application * * @return void */ public function registerConnector() { $connectionString = $this->buildConnectionString(); $connection = new MongoDbConnector(); $connection->getConnection($connectionString); $this->app['MongoLidConnector'] = $this->app->share(function ($app) use($connection) { return $connection; }); }
/** * Returns the connection. If non existent then create it * * @var MongoDB * @return MongoClient|MongoDB */ public function getConnection($connectionString = '') { // If exists in $shared_connection, use it if (MongoDbConnector::$shared_connection) { $connection = MongoDbConnector::$shared_connection; } else { // Else, connect and place connection in $shared_connection try { $connection = new MongoClient($connectionString); } catch (\MongoConnectionException $e) { return trigger_error('Failed to connect with string: "' . $connectionString . '"'); } MongoDbConnector::$shared_connection = $connection; } return $connection; }
/** * Returns the database object (the connection) * * @return MongoDB */ protected function db() { if (!static::$connection) { $connector = new MongoDbConnector(); static::$connection = $connector->getConnection(); } return static::$connection->{$this->database}; }