|
|
@ -40,12 +40,12 @@ class Loader {
|
|
|
|
* Registers a class.
|
|
|
|
* Registers a class.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param string $name Registry name
|
|
|
|
* @param string $name Registry name
|
|
|
|
* @param string $class Class name
|
|
|
|
* @param string|callable $class Class name or function to instantiate class
|
|
|
|
* @param array $params Class initialization parameters
|
|
|
|
* @param array $params Class initialization parameters
|
|
|
|
* @param callback $callback Function to call after object instantiation
|
|
|
|
* @param callback $callback Function to call after object instantiation
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function register($name, $class, array $params = array(), $callback = null) {
|
|
|
|
public function register($name, $class, array $params = array(), $callback = null) {
|
|
|
|
unset($this->instances[$class]);
|
|
|
|
unset($this->instances[$name]);
|
|
|
|
|
|
|
|
|
|
|
|
$this->classes[$name] = array($class, $params, $callback);
|
|
|
|
$this->classes[$name] = array($class, $params, $callback);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -67,50 +67,57 @@ class Loader {
|
|
|
|
* @return object Class instance
|
|
|
|
* @return object Class instance
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function load($name, $shared = true) {
|
|
|
|
public function load($name, $shared = true) {
|
|
|
|
|
|
|
|
$obj = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (isset($this->classes[$name])) {
|
|
|
|
if (isset($this->classes[$name])) {
|
|
|
|
list($class, $params, $callback) = $this->classes[$name];
|
|
|
|
list($class, $params, $callback) = $this->classes[$name];
|
|
|
|
|
|
|
|
|
|
|
|
$do_callback = ($callback && (!$shared || !isset($this->instances[$class])));
|
|
|
|
$exists = isset($this->instances[$name]);
|
|
|
|
|
|
|
|
|
|
|
|
$obj = ($shared) ?
|
|
|
|
if ($shared) {
|
|
|
|
$this->getInstance($class, $params) :
|
|
|
|
$obj = ($exists) ?
|
|
|
|
$this->newInstance($class, $params);
|
|
|
|
$this->getInstance($name) :
|
|
|
|
|
|
|
|
$this->newInstance($class, $params);
|
|
|
|
|
|
|
|
|
|
|
|
if ($do_callback) {
|
|
|
|
if (!$exists) {
|
|
|
|
|
|
|
|
$this->instances[$name] = $obj;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
|
|
|
|
$obj = $this->newInstance($class, $params);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ($callback && (!$shared || !$exists)) {
|
|
|
|
$ref = array(&$obj);
|
|
|
|
$ref = array(&$obj);
|
|
|
|
call_user_func_array($callback, $ref);
|
|
|
|
call_user_func_array($callback, $ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $obj;
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return ($shared) ?
|
|
|
|
return $obj;
|
|
|
|
$this->getInstance($name) :
|
|
|
|
|
|
|
|
$this->newInstance($name);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Gets a single instance of a class.
|
|
|
|
* Gets a single instance of a class.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param string $class Class name
|
|
|
|
* @param string $name Instance name
|
|
|
|
* @param array $params Class initialization parameters
|
|
|
|
* @return object Class instance
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function getInstance($class, array $params = array()) {
|
|
|
|
public function getInstance($name) {
|
|
|
|
if (!isset($this->instances[$class])) {
|
|
|
|
return isset($this->instances[$name]) ? $this->instances[$name] : null;
|
|
|
|
$this->instances[$class] = $this->newInstance($class, $params);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $this->instances[$class];
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Gets a new instance of a class.
|
|
|
|
* Gets a new instance of a class.
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param string $class Class name
|
|
|
|
* @param string|callable $class Class name or callback function to instantiate class
|
|
|
|
* @param array $params Class initialization parameters
|
|
|
|
* @param array $params Class initialization parameters
|
|
|
|
* @return object Class instance
|
|
|
|
* @return object Class instance
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function newInstance($class, array $params = array()) {
|
|
|
|
public function newInstance($class, array $params = array()) {
|
|
|
|
|
|
|
|
if (is_callable($class)) {
|
|
|
|
|
|
|
|
return call_user_func_array($class, $params);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
switch (count($params)) {
|
|
|
|
switch (count($params)) {
|
|
|
|
case 0:
|
|
|
|
case 0:
|
|
|
|
return new $class();
|
|
|
|
return new $class();
|
|
|
|