/** * Get instance of connection * * @param string $name connection identifier * @return \Sokil\Mongo\Client * @throws \Sokil\Mongo\Exception */ public function get($name) { // get from cache if (isset($this->pool[$name])) { return $this->pool[$name]; } // check if connection exists if (!isset($this->configuration[$name])) { throw new Exception('Connection with name ' . $name . ' not found'); } // check if dsn exists if (!isset($this->configuration[$name]['dsn'])) { $this->configuration[$name]['dsn'] = null; } // check if connect options exists if (empty($this->configuration[$name]['connectOptions'])) { $this->configuration[$name]['connectOptions'] = null; } // init client $client = new Client($this->configuration[$name]['dsn'], $this->configuration[$name]['connectOptions']); if (isset($this->configuration[$name]['mapping'])) { $client->map($this->configuration[$name]['mapping']); } if (isset($this->configuration[$name]['defaultDatabase'])) { $client->useDatabase($this->configuration[$name]['defaultDatabase']); } $this->pool[$name] = $client; return $client; }
public function testGetDatabase_NameNotSpecified_DefaultNameSpecified() { $client = new Client('mongodb://127.0.0.1/'); $client->useDatabase('some_name'); $this->assertEquals('some_name', $client->getDatabase()->getName()); }
public function testGetDatabase_NameNotSpecified_DefaultNameSpecified() { $client = new Client(getenv('PHPMONGO_DSN') ? getenv('PHPMONGO_DSN') : null); $client->useDatabase('some_name'); $this->assertEquals('some_name', $client->getDatabase()->getName()); }