2017-11-24 15:00:12 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**************************
|
|
|
|
* Repo *
|
|
|
|
* 24-11-2017 *
|
|
|
|
***************************
|
|
|
|
* Designed & Developed by *
|
|
|
|
* xdrm-brackets *
|
|
|
|
***************************
|
|
|
|
* https://xdrm.io/ *
|
|
|
|
**************************/
|
|
|
|
|
|
|
|
namespace database\core;
|
|
|
|
|
|
|
|
use \error\core\Error;
|
|
|
|
use \error\core\Err;
|
|
|
|
|
|
|
|
class Repo{
|
|
|
|
|
|
|
|
|
|
|
|
/* (1) Driver
|
|
|
|
---------------------------------------------------------*/
|
|
|
|
private static $driver = null;
|
|
|
|
|
|
|
|
public static function setDriver(DatabaseDriver $driver){ self::$driver = $driver; }
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-11-26 10:41:41 +00:00
|
|
|
public static function request(String $repo=null, String $method=null){
|
2017-11-24 15:00:12 +00:00
|
|
|
|
|
|
|
/* (1) Check arguments
|
|
|
|
---------------------------------------------------------*/
|
2017-11-26 10:41:41 +00:00
|
|
|
/* (1) Check @repo */
|
|
|
|
if( !is_string($repo) || strlen($repo) < 1 )
|
|
|
|
throw new \Exception("@repo is not a non-empty string");
|
2017-11-24 15:00:12 +00:00
|
|
|
|
2017-11-26 10:41:41 +00:00
|
|
|
/* (2) Check @method */
|
|
|
|
if( !is_string($method) || strlen($method) < 1 )
|
|
|
|
throw new \Exception("@method is not a non-empty string");
|
2017-11-24 15:00:12 +00:00
|
|
|
|
|
|
|
/* (3) Check class path */
|
2017-11-26 10:41:41 +00:00
|
|
|
$class_path = "\\database\\repo\\$repo";
|
2017-11-24 15:00:12 +00:00
|
|
|
|
|
|
|
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 */
|
2017-11-26 10:41:41 +00:00
|
|
|
if( !( $instance instanceof Repo_i ) )
|
2017-11-24 15:00:12 +00:00
|
|
|
throw new \Exception("Repo class '$class_path' must extends Repo_i");
|
|
|
|
|
|
|
|
/* (3) Bind pdo instance */
|
|
|
|
\call_user_func([$instance, 'setPDO'], self::$driver->pdo());
|
|
|
|
|
|
|
|
|
|
|
|
/* (3) Check if the method exists */
|
2017-11-26 10:41:41 +00:00
|
|
|
if( !\method_exists($instance, $method) )
|
|
|
|
throw new \Exception("Repo '$repo' has no public method '$method'");
|
2017-11-24 15:00:12 +00:00
|
|
|
|
|
|
|
/* (4) Fetch response (send arguments as well) */
|
2017-11-26 10:41:41 +00:00
|
|
|
$response = call_user_func_array([$instance, $method], array_slice(func_get_args(), 2));
|
2017-11-24 15:00:12 +00:00
|
|
|
|
|
|
|
/* (5) Call post-script */
|
|
|
|
$instance = null;
|
|
|
|
|
|
|
|
/* (6) Dispatch response */
|
|
|
|
return $response;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|