91 lines
2.1 KiB
PHP
91 lines
2.1 KiB
PHP
<?php
|
|
|
|
/**************************
|
|
* Repo *
|
|
* 24-11-2017 *
|
|
***************************
|
|
* Designed & Developed by *
|
|
* xdrm-brackets *
|
|
***************************
|
|
* https://xdrm.io/ *
|
|
**************************/
|
|
|
|
namespace database\core;
|
|
|
|
use database\core\PDOWrapper\PDOWrapper;
|
|
use \error\core\Error;
|
|
use \error\core\Err;
|
|
|
|
class Repo{
|
|
|
|
|
|
/* (1) Driver
|
|
---------------------------------------------------------*/
|
|
/**
|
|
* @var DatabaseDriver
|
|
*/
|
|
private static $driver = null;
|
|
|
|
public static function setDriver(DatabaseDriver $driver){ self::$driver = $driver; }
|
|
|
|
|
|
|
|
public static function getRepo(String $repo=null) : Repo_i{
|
|
|
|
/* (1) Check arguments
|
|
---------------------------------------------------------*/
|
|
/* (1) Check @repo */
|
|
if( !is_string($repo) || strlen($repo) < 1 )
|
|
throw new \Exception("@repo is not a non-empty string");
|
|
|
|
/* (1) Check class path */
|
|
$class_path = "\\database\\repo\\$repo";
|
|
|
|
if( !\class_exists($class_path) )
|
|
throw new \Exception("Repo class '$class_path' cannot be found");
|
|
|
|
|
|
/* (2) Call the method
|
|
---------------------------------------------------------*/
|
|
/* (1) Create the instance (pre-script) */
|
|
$instance = new $class_path();
|
|
|
|
/* (2) Check extends Repo_i */
|
|
if( !( $instance instanceof Repo_i ) )
|
|
throw new \Exception("Repo class '$class_path' must extends Repo_i");
|
|
|
|
/* (3) Bind pdo instance */
|
|
\call_user_func([$instance, 'setPDO'], self::$driver->pdo());
|
|
|
|
|
|
return $instance;
|
|
}
|
|
|
|
public static function enableStacking(){
|
|
static::$driver->enableStacking();
|
|
}
|
|
|
|
public static function flushStack(){
|
|
static::$driver->flushStack();
|
|
}
|
|
|
|
public static function getDebug() : array{
|
|
return static::$driver->getDebug();
|
|
}
|
|
|
|
public static function isDebugEnabled() : bool{
|
|
return static::$driver->isDebugEnabled();
|
|
}
|
|
|
|
public static function switchDatabase(string $dbName){
|
|
return static::$driver->pdo()->exec("USE $dbName;SET autocommit=1;");
|
|
}
|
|
|
|
public static function getDBConfig() : array{
|
|
return static::$driver->getConfig();
|
|
}
|
|
|
|
|
|
|
|
|
|
} |