main/build/database/core/Repo.php

79 lines
2.0 KiB
PHP
Raw Normal View History

<?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; }
public static function request(Array $path=null){
/* (1) Check arguments
---------------------------------------------------------*/
/* (1) Check @path */
if( !is_array($path) || count($path) != 2 )
throw new \Exception("@path is not an array with 2 elements");
/* (2) Deep check @path */
if( !is_string($path[0]) || !is_string($path[1]) )
throw new \Exception("@path elements must be 2 strings");
/* (3) Check class path */
$class_path = "\\database\\repo\\".$path[0];
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());
/* (3) Check if the method exists */
if( !\method_exists($instance, $path[1]) )
throw new \Exception("Repo '${path[0]}' has no public method '{$path[1]}'");
/* (4) Fetch response (send arguments as well) */
$response = call_user_func_array([$instance, $path[1]], array_slice(func_get_args(), 1));
/* (5) Call post-script */
$instance = null;
/* (6) Dispatch response */
return $response;
}
}