get connector
실제 connection 을 만들지 않고 connector 에서 config 에 따라
master, slave 가 어떤 connection 이름을 사용할지 결정 후 connector 를 반환.
public connection ( null | string $name = null ) : Xpressengine\Database\VirtualConnectionInterface | ||
$name | null | string | config/xe.php database connector name |
Результат | Xpressengine\Database\VirtualConnectionInterface |
/** * Execute the console command. * * @return mixed */ public function fire() { $tables = $this->input->getOption('tables'); if ($tables == false) { $this->error('캐시를 위한 테이블 이름이 없습니다.'); } $tables = explode(',', $tables); if (count($tables) == 0) { $this->error('캐시를 위한 테이블 이름이 없습니다.'); } $config = $this->databaseHandler->getConfig(); // 어떤 connection 에 table 이 있는지 알 수 없음 $connectors = []; foreach ($config as $name => $settings) { $connectors[] = $this->databaseHandler->connection($name); } $cacheNames = []; /** @var \Xpressengine\Database\VirtualConnectionInterface $connector */ foreach ($connectors as $connector) { foreach ($tables as $table) { if (in_array($table, $cacheNames) === true) { continue; } if ($connector->setSchemaCache($table, true) === true) { $cacheNames[] = $table; } } } if (count($cacheNames) === 0) { $this->error('캐시된 테이블이 없습니다. 테이블 이름을 확인하세요.'); } else { $this->info(implode(',', array_unique($cacheNames)) . ' 테이블을 캐시 했습니다.'); } }
/** * test database connect * * @return void */ public function testDatabaseConnect() { $defaultConnection = m::mock('Illuminate\\Database\\Connection'); $defaultConnection->shouldReceive('getName')->andReturn('default'); $databaseManager = m::mock('Illuminate\\Database\\DatabaseManager'); $databaseManager->shouldReceive('getDefaultConnection')->andReturn($defaultConnection); $databaseManager->shouldReceive('setDefaultConnection'); $queryBuilder = m::mock('Xpressengine\\Database\\DynamicQuery'); $defaultConnector = m::mock('Xpressengine\\Database\\VirtualConnection'); $defaultConnector->shouldReceive('getName')->andReturn('default'); $defaultConnector->shouldReceive('table')->andReturn($queryBuilder); $someConnector = m::mock('Xpressengine\\Database\\VirtualConnection'); $someConnector->shouldReceive('getName')->andReturn('some'); $coupler = m::mock('Xpressengine\\Database\\DatabaseCoupler'); $coupler->shouldReceive('getConnector')->with(DatabaseHandler::DEFAULT_CONNECTOR_NAME)->andReturn($defaultConnector); $coupler->shouldReceive('getConnector')->with('some')->andReturn($someConnector); $coupler->shouldReceive('addConnector')->with('default', $defaultConnector)->andReturn($defaultConnector); $coupler->shouldReceive('addConnector')->with('some', $someConnector)->andReturn($someConnector); $coupler->shouldReceive('databaseManager')->andReturn($databaseManager); /** @var \Xpressengine\Database\DatabaseCoupler $coupler */ $handler = new DatabaseHandler($coupler, $this->databaseConfig); // make default connector $connector = $handler->connection(); $this->assertInstanceOf('Xpressengine\\Database\\VirtualConnection', $connector); $this->assertEquals('default', $connector->getName()); $connection = $handler->getDefaultConnection(); $this->assertInstanceOf('Illuminate\\Database\\Connection', $connection); $this->assertEquals('default', $connection->getName()); $handler->setDefaultConnection('some1_con'); // test magic method $queryBuilder = $handler->table('table'); $this->assertInstanceOf('Xpressengine\\Database\\DynamicQuery', $queryBuilder); }