/** * 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]; }
/** * 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]; }
/** * Returns database configuration. * * @param string $testDbName The name of database connection details form phpunit.xml. * @param bool $connect Set to true to connect to database right away. * @param bool $debug Set to true to put database driver in debug mode. * * @return array */ public static function dbGetConfig($testDbName, $connect = true, $debug = true) { $timezone = isset($GLOBALS['TEST_DB_' . $testDbName . '_TIMEZONE']) ? $GLOBALS['TEST_DB_' . $testDbName . '_TIMEZONE'] : ''; return DbConnect::getCfg($GLOBALS['TEST_DB_' . $testDbName . '_DRIVER'], $GLOBALS['TEST_DB_' . $testDbName . '_HOST'], $GLOBALS['TEST_DB_' . $testDbName . '_USERNAME'], $GLOBALS['TEST_DB_' . $testDbName . '_PASSWORD'], $GLOBALS['TEST_DB_' . $testDbName . '_DATABASE'], $GLOBALS['TEST_DB_' . $testDbName . '_PORT'], $connect, $timezone, $debug); }
<?php use Kicaj\Schema\Schema; use Kicaj\DbKit\DbConnect; use Kicaj\DbKit\DbConnector; $config = [Schema::CONFIG_KEY_CONNECTION => DbConnect::getCfg(DbConnector::DB_DRIVER_MYSQL, '127.0.0.1', 'testUser', 'unitTestPass', 'test', '3306'), Schema::CONFIG_KEY_EXPORT_FORMAT => Schema::FORMAT_PHP_ARRAY, Schema::CONFIG_KEY_AINE => true, Schema::CONFIG_KEY_OUTPUT_FILE => 'schema.php']; return $config;
/** * @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)); }