Newer
Older
namespace DependencyInjector;
/**
* Class DependencyInjector
*
* This is a Dependency Injector. It gives the opportunity to define how to get a dependency from outside of the code
* that needs the dependency. This is especially helpful for testing proposes when you need to mock a dependency.
*
* You can never get an instance of this class. For usage you have only two static methods.
*
* Example usage:
* DependencyInjector::set('hello', function() { return 'hello'; });
* DependencyInjector::set('world', function() { return 'world'; });
* echo DependencyInjector:get('hello') . " " . DependencyInjector:.get('world') . "!\n";
*/
protected static $_instances = [];
protected static $_dependencies = [];
/**
* Get a previously defined dependency identified by $name.
*
* @param string $name
*/
public static function get($name) {
if (isset(self::$_instances[$name])) {
return self::$_instances[$name];
} elseif (isset(self::$_dependencies[$name])) {
if (self::$_dependencies[$name]['singleton']) {
self::$_instances[$name] = call_user_func(self::$_dependencies[$name]['getter']);
return self::$_instances[$name];
}
return call_user_func(self::$_dependencies[$name]['getter']);
} elseif (class_exists($name)) {
return $name;
}
throw new Exception("Unknown dependency '" . $name . "'");
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
* Alias for DI::get($name). Example:
* DI::get('same') === DI::same()
*
* @param string $name
* @param array $arguments
* @return mixed
*/
public static function __callStatic($name, $arguments) {
return self::get($name);
}
/**
* Define a dependency.
*
* @param string $name
* @param mixed $getter
* @param bool $singleton
* @return void
*/
public static function set($name, $getter, $singleton = true) {
if (is_object($getter) && $getter instanceof \Closure && is_callable($getter)) {
if (isset(self::$_instances[$name])) {
unset(self::$_instances[$name]);
}
self::$_dependencies[$name] = [
'singleton' => $singleton,
'getter' => $getter
];
} else {
self::$_instances[$name] = $getter;
}
}
/**
* Resets the DependencyInjector
*
* @return void
*/
public static function reset() {
self::$_instances = [];
self::$_dependencies = [];
}
private function __construct() {}