public function testConnection() { $config = ['database' => 'reach_testing', 'host' => 'localhost', 'port' => 27017, 'options' => ['connect' => true, 'socketTimeoutMS' => 60000]]; $adapter = new \Reach\DI\DefaultAdapter(); $adapter->register('reach_mongo_connection', $config, '\\Reach\\Mongo\\Connection'); \Reach\Service\Container::setDI($adapter); $di = \Reach\Service\Container::getDI(); $this->assertInstanceOf('\\Reach\\Mongo\\Connection', $di->get('reach_mongo_connection')); }
public function getNewId() { $collection_name = $this->owner->getCollectionName(); $connection_name = $this->owner->getConnectionName(); $collection = Container::getDI()->get($connection_name)->getCollection($this->collection_name); $result = $collection->findAndModify(['_id' => $collection_name], ['$inc' => ['sequence' => 1]], ['sequence' => 1], ['new' => true, 'upsert' => true]); if (empty($result)) { throw new Exception('Unknown error'); // todo } return $result['sequence']; }
/** * @param array $config * @param string $connection_name * @throws Exception */ public static function registerConnection(array $config, $connection_name = null) { if (!extension_loaded('mongo')) { throw new Exception('The mongo extension is required'); } $connection_name = empty($connection_name) ? self::$default_connection_name : $connection_name; if (!isset($config['option'])) { $config['option'] = []; } /** @var \Reach\DI\DefaultAdapter $di */ $di = Container::getDI(); $di->register($connection_name, $config, '\\Reach\\Mongo\\Connection'); if (array_key_exists($connection_name, self::$_connections)) { unset(self::$_connections[$connection_name]); } }
public static function setUpBeforeClass() { self::$config = ['database' => 'reach_testing', 'host' => 'localhost', 'port' => 27017, 'options' => ['connect' => true, 'socketTimeoutMS' => 60000]]; \Reach\Service\Container::register('mongo', self::$config, '\\Reach\\Mongo\\Connection'); self::$connection = \Reach\Service\Container::getDI()->get('mongo'); //ConnectionManager::registerConnection(self::$config); //self::$connection = ConnectionManager::getConnection(); if (!self::$phactory) { if (!self::$connection->getDb() instanceof \MongoDB) { throw new \Exception('Could not connect to MongoDB'); } self::$phactory = new Phactory(self::$connection->getDb()); self::$phactory->reset(); } //set up Phactory db connection self::$phactory->reset(); }
/** * @return \Reach\Mongo\Connection */ protected function getConnection() { return Container::getDI()->get($this->connection_name); }