|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ntentan\panie; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Description of Container |
|
7
|
|
|
* |
|
8
|
|
|
* @author ekow |
|
9
|
|
|
*/ |
|
10
|
|
|
class InjectionContainer |
|
11
|
|
|
{ |
|
12
|
|
|
private static $bindings; |
|
13
|
|
|
private static $singletons = []; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* |
|
17
|
|
|
* @param string|\ReflectionClass $class |
|
18
|
|
|
* @return \ReflectionClass |
|
19
|
|
|
*/ |
|
20
|
7 |
|
public static function getResolvedClassName($class) |
|
21
|
|
|
{ |
|
22
|
7 |
|
$bound = null; |
|
23
|
7 |
|
if(self::getBindings()->has($class)) { |
|
24
|
5 |
|
$bound = self::$bindings->get($class); |
|
25
|
|
|
} |
|
26
|
5 |
|
else if(is_string($class) && class_exists($class)) { |
|
27
|
4 |
|
$bound = $class; |
|
28
|
|
|
} |
|
29
|
7 |
|
return $bound; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
7 |
|
private static function getBindings() |
|
33
|
|
|
{ |
|
34
|
7 |
|
if(!self::$bindings) self::$bindings = new Bindings (); |
|
35
|
7 |
|
return self::$bindings; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
5 |
|
public static function bind($lose) |
|
39
|
|
|
{ |
|
40
|
5 |
|
return self::getBindings()->setActiveKey($lose); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
public static function singleton($type, $constructorArguments = []) |
|
44
|
|
|
{ |
|
45
|
1 |
|
if(!isset(self::$singletons[$type])) { |
|
46
|
1 |
|
self::$singletons[$type] = self::resolve($type, $constructorArguments); |
|
47
|
|
|
} |
|
48
|
1 |
|
return self::$singletons[$type]; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
7 |
|
public static function resolve($type, $constructorArguments = []) |
|
52
|
|
|
{ |
|
53
|
7 |
|
$resolvedClass = self::getResolvedClassName($type); |
|
54
|
7 |
|
if($resolvedClass=== null) { |
|
55
|
1 |
|
throw new exceptions\ResolutionException("Could not resolve dependency $type"); |
|
56
|
|
|
} |
|
57
|
6 |
|
$reflection = new \ReflectionClass($resolvedClass); |
|
58
|
6 |
|
if($reflection->isAbstract()) { |
|
59
|
1 |
|
throw new exceptions\ResolutionException( |
|
60
|
1 |
|
"Abstract class {$reflection->getName()} cannot be instantiated. " |
|
61
|
1 |
|
. "Please provide a binding to an implementation." |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
5 |
|
$constructor = $reflection->getConstructor(); |
|
65
|
5 |
|
$instanceParameters = []; |
|
66
|
|
|
|
|
67
|
5 |
|
if($constructor != null) { |
|
68
|
3 |
|
$parameters = $constructor->getParameters(); |
|
69
|
3 |
|
foreach($parameters as $parameter) { |
|
70
|
3 |
|
$class = $parameter->getClass(); |
|
71
|
3 |
|
if(isset($constructorArguments[$parameter->getName()])) { |
|
72
|
1 |
|
$instanceParameters[] = $constructorArguments[$parameter->getName()]; |
|
73
|
|
|
} else { |
|
74
|
3 |
|
$instanceParameters[] = $class ? self::resolve($class->getName()) : null; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
5 |
|
return $reflection->newInstanceArgs($instanceParameters); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
7 |
|
public static function reset() |
|
82
|
|
|
{ |
|
83
|
7 |
|
self::resetBindings(); |
|
84
|
7 |
|
self::resetSingletons(); |
|
85
|
7 |
|
} |
|
86
|
|
|
|
|
87
|
7 |
|
public static function resetBindings() |
|
88
|
|
|
{ |
|
89
|
7 |
|
self::$bindings = null; |
|
90
|
7 |
|
} |
|
91
|
|
|
|
|
92
|
7 |
|
public static function resetSingletons() |
|
93
|
|
|
{ |
|
94
|
7 |
|
self::$singletons = []; |
|
95
|
7 |
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|