ptut-vhost/build/database/core/Repo.php

87 lines
2.0 KiB
PHP
Raw Permalink Normal View History

2018-02-17 17:18:58 +00:00
<?php
/**************************
* Repo *
* 24-11-2017 *
***************************
* Designed & Developed by *
* xdrm-brackets *
***************************
* https://xdrm.io/ *
**************************/
namespace database\core;
use database\core\PDOWrapper\PDOWrapper;
2018-02-17 17:18:58 +00:00
use \error\core\Error;
use \error\core\Err;
class Repo{
/* (1) Driver
---------------------------------------------------------*/
/**
* @var DatabaseDriver
*/
2018-02-17 17:18:58 +00:00
private static $driver = null;
public static function setDriver(DatabaseDriver $driver){ self::$driver = $driver; }
2018-02-27 19:41:36 +00:00
public static function getRepo(String $repo=null) : Repo_i{
2018-02-17 17:18:58 +00:00
/* (1) Check arguments
---------------------------------------------------------*/
/* (1) Check @repo */
if( !is_string($repo) || strlen($repo) < 1 )
throw new \Exception("@repo is not a non-empty string");
2018-02-27 19:41:36 +00:00
/* (1) Check class path */
2018-02-17 17:18:58 +00:00
$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());
2018-02-27 19:41:36 +00:00
return $instance;
2018-02-17 17:18:58 +00:00
}
public static function enableStacking(){
static::$driver->enableStacking();
}
public static function flushStack(){
static::$driver->flushStack();
}
2018-03-08 19:53:40 +00:00
public static function getDebug() : array{
return static::$driver->getDebug();
}
public static function isDebugEnabled() : bool{
return static::$driver->isDebugEnabled();
}
2018-03-13 23:12:18 +00:00
public static function switchDatabase(string $dbName){
return static::$driver->pdo()->prepare("USE $dbName")->execute();
}
2018-02-17 17:18:58 +00:00
}