예제 #1
0
파일: Db.php 프로젝트: rzajac/schema
 /**
  * Database factory.
  *
  * It returns the same instance for the same config.
  *
  * @see \Kicaj\Tools\Itf\DbConnect
  *
  * @param array $dbConfig The database configuration.
  *
  * @throws SchemaException
  *
  * @return Db
  */
 public static function factory(array $dbConfig)
 {
     $key = md5(json_encode($dbConfig));
     if (isset(self::$instances[$key])) {
         return self::$instances[$key];
     }
     switch (DbConnect::getDriver($dbConfig[Schema::CONFIG_KEY_CONNECTION])) {
         case DbConnector::DB_DRIVER_MYSQL:
             $driver = new MySQL();
             break;
         default:
             throw new SchemaException('Unknown database driver name: ' . DbConnect::getDriver($dbConfig[Schema::CONFIG_KEY_CONNECTION]));
     }
     $driver->dbSetup($dbConfig[Schema::CONFIG_KEY_CONNECTION])->dbConnect();
     self::$instances[$key] = new self($driver);
     return self::$instances[$key];
 }
예제 #2
0
 /**
  * Database factory.
  *
  * It returns the same instance for the same config.
  *
  * @param array $dbConfig The database configuration
  *
  * @throws DatabaseException
  *
  * @return DbItf
  */
 public static function factory(array $dbConfig)
 {
     /** @var DbItf[] $instances */
     static $instances = [];
     $key = md5(json_encode($dbConfig));
     if (isset($instances[$key])) {
         return $instances[$key];
     }
     switch (DbConnect::getDriver($dbConfig)) {
         case DbConnector::DB_DRIVER_MYSQL:
             $instances[$key] = new MySQL();
             break;
         default:
             throw new DatabaseException('Unknown database driver name: ' . DbConnect::getDriver($dbConfig));
     }
     $instances[$key]->dbSetup($dbConfig);
     if ($dbConfig[DbConnector::DB_CFG_CONNECT]) {
         $instances[$key]->dbConnect();
     }
     return $instances[$key];
 }
예제 #3
0
 /**
  * @covers ::getDriver
  */
 public function test_getDriver()
 {
     $cfg = DbConnect::getCfg(DbConnector::DB_DRIVER_MYSQL, 'localhost', 'testUser', 'testPass', 'testDb', 1234);
     $this->assertSame(DbConnector::DB_DRIVER_MYSQL, DbConnect::getDriver($cfg));
 }