Di.php
5.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
namespace Codeception\Lib;
use Codeception\Exception\InjectionException;
class Di
{
const DEFAULT_INJECT_METHOD_NAME = '_inject';
protected $container = [];
/**
* @var Di
*/
protected $fallback;
public function __construct($fallback = null)
{
$this->fallback = $fallback;
}
public function get($className)
{
// normalize namespace
$className = ltrim($className, '\\');
return isset($this->container[$className]) ? $this->container[$className] : null;
}
public function set($class)
{
$this->container[get_class($class)] = $class;
}
/**
* @param string $className
* @param array $constructorArgs
* @param string $injectMethodName Method which will be invoked after object creation;
* Resolved dependencies will be passed to it as arguments
* @throws InjectionException
* @return null|object
*/
public function instantiate(
$className,
$constructorArgs = null,
$injectMethodName = self::DEFAULT_INJECT_METHOD_NAME
) {
// normalize namespace
$className = ltrim($className, '\\');
// get class from container
if (isset($this->container[$className])) {
if ($this->container[$className] instanceof $className) {
return $this->container[$className];
} else {
throw new InjectionException("Failed to resolve cyclic dependencies for class '$className'");
}
}
// get class from parent container
if ($this->fallback) {
if ($class = $this->fallback->get($className)) {
return $class;
}
}
$this->container[$className] = false; // flag that object is being instantiated
$reflectedClass = new \ReflectionClass($className);
if (!$reflectedClass->isInstantiable()) {
return null;
}
$reflectedConstructor = $reflectedClass->getConstructor();
if (is_null($reflectedConstructor)) {
$object = new $className;
} else {
try {
if (!$constructorArgs) {
$constructorArgs = $this->prepareArgs($reflectedConstructor);
}
} catch (\Exception $e) {
throw new InjectionException("Failed to create instance of '$className'. " . $e->getMessage());
}
$object = $reflectedClass->newInstanceArgs($constructorArgs);
}
if ($injectMethodName) {
$this->injectDependencies($object, $injectMethodName);
}
$this->container[$className] = $object;
return $object;
}
/**
* @param $object
* @param string $injectMethodName Method which will be invoked with resolved dependencies as its arguments
* @throws InjectionException
*/
public function injectDependencies($object, $injectMethodName = self::DEFAULT_INJECT_METHOD_NAME, $defaults = [])
{
if (!is_object($object)) {
return;
}
$reflectedObject = new \ReflectionObject($object);
if (!$reflectedObject->hasMethod($injectMethodName)) {
return;
}
$reflectedMethod = $reflectedObject->getMethod($injectMethodName);
try {
$args = $this->prepareArgs($reflectedMethod, $defaults);
} catch (\Exception $e) {
$msg = $e->getMessage();
if ($e->getPrevious()) { // injection failed because PHP code is invalid. See #3869
$msg .= '; '. $e->getPrevious();
}
throw new InjectionException(
"Failed to inject dependencies in instance of '{$reflectedObject->name}'. $msg"
);
}
if (!$reflectedMethod->isPublic()) {
$reflectedMethod->setAccessible(true);
}
$reflectedMethod->invokeArgs($object, $args);
}
/**
* @param \ReflectionMethod $method
* @param $defaults
* @throws InjectionException
* @return array
*/
protected function prepareArgs(\ReflectionMethod $method, $defaults = [])
{
$args = [];
$parameters = $method->getParameters();
foreach ($parameters as $k => $parameter) {
$dependency = $parameter->getClass();
if (is_null($dependency)) {
if (!$parameter->isOptional()) {
if (!isset($defaults[$k])) {
throw new InjectionException("Parameter '$parameter->name' must have default value.");
}
$args[] = $defaults[$k];
continue;
}
$args[] = $parameter->getDefaultValue();
} else {
$arg = $this->instantiate($dependency->name);
if (is_null($arg)) {
throw new InjectionException("Failed to resolve dependency '{$dependency->name}'.");
}
$args[] = $arg;
}
}
return $args;
}
}