/** * @param string $connection_name * @return mixed */ public static function getConnection($connection_name = null) { if (empty($connection_name)) { $connection_name = self::$default_connection_name; } if (!ServiceContainer::has($connection_name)) { throw new InvalidArgumentException("Reach service \"{$connection_name}\" is not defined."); } return ServiceContainer::get($connection_name); }
public function testConstruct() { $config = ['class' => '\\Reach\\Connection\\Dummy', 'host' => 'localhost']; \Reach\Service\Container::register('connection', $config); $connection = \Reach\Service\Container::get('connection'); $this->assertInstanceOf('\\Reach\\Connection\\Dummy', $connection); $connection = new \Reach\Connection\Dummy($config); \Reach\Service\Container::set('connection', $connection); $this->assertInstanceOf('\\Reach\\Connection\\Dummy', \Reach\Service\Container::get('connection')); $this->assertTrue(\Reach\Service\Container::has('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 function testConnectionOld() { $config = ['database' => 'reach_testing', 'host' => 'localhost', 'port' => 27017, 'options' => ['connect' => true, 'socketTimeoutMS' => 60000]]; ConnectionManager::registerConnection($config); $connection = ConnectionManager::getConnection(); $this->assertInstanceOf('\\Reach\\Mongo\\Connection', $connection); $this->assertTrue(method_exists($connection, 'getDb')); $this->assertInstanceOf('\\MongoDB', $connection->getDb()); $config2 = ['database' => 'reach_testing2']; Container::register('another', $config2, '\\Reach\\Mongo\\Connection'); $connection2 = Container::get('another'); $this->assertInstanceOf('\\Reach\\Mongo\\Connection', $connection2); $this->assertEquals('reach_testing2', $connection2->getDbName()); $connection = ConnectionManager::getConnection(); $this->assertInstanceOf('\\Reach\\Mongo\\Connection', $connection); $this->assertEquals('reach_testing', $connection->getDbName()); }
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(); }
public function testAnotherConnection() { $model = TestSchema::getCollection('another')->findOne(); $this->assertNull($model); $model = new TestSchema(['title' => '123']); $model->setConnectionName('another')->save(); $model = TestSchema::getCollection('another')->findOne($model->_id); $this->assertInstanceOf('\\Model\\Mongo\\TestSchema', $model); $this->assertEquals('123', $model->title); \Reach\Service\Container::get('another')->selectDB('reach_testing2')->drop(); \Reach\Service\Container::get('another')->close(); }
public static function setUpBeforeClass() { self::$config = ['class' => '\\Reach\\Sphinx\\Connection', 'host' => 'localhost', 'port' => 9312]; \Reach\Service\Container::register('sphinx', self::$config); self::$connection = \Reach\Service\Container::get('sphinx'); }
/** * @return \Reach\Mongo\Connection */ protected function getConnection() { return Container::getDI()->get($this->connection_name); }