1
|
|
|
<?php |
2
|
|
|
namespace DD; |
3
|
|
|
|
4
|
|
|
use DD\DiMaria\Exception\ContainerException; |
5
|
|
|
use DD\DiMaria\Exception\NotFoundException; |
6
|
|
|
use Interop\Container\ContainerInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* DiMaria Dependency injector |
10
|
|
|
*/ |
11
|
|
|
class DiMaria implements ContainerInterface |
12
|
|
|
{ |
13
|
|
|
protected $aliases = []; |
14
|
|
|
protected $factory = []; |
15
|
|
|
protected $injections = []; |
16
|
|
|
protected $params = []; |
17
|
|
|
protected $preferences = []; |
18
|
|
|
protected $shared = []; |
19
|
|
|
protected $sharedInstance = []; |
20
|
|
|
|
21
|
77 |
|
public function __construct() |
22
|
|
|
{ |
23
|
77 |
|
$this->sharedInstance[__CLASS__] = $this; |
24
|
77 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Alias a class/interface/alias to a string |
28
|
|
|
* @param string $alias the name of the alias |
29
|
|
|
* @param string $class the name of the class |
30
|
|
|
* @param array $params a key/value array of parameter names and values |
31
|
|
|
* @return self |
32
|
|
|
*/ |
33
|
24 |
|
public function setAlias(string $alias, string $class, array $params = []): self |
34
|
|
|
{ |
35
|
24 |
|
$this->aliases[$alias] = [ |
36
|
24 |
|
'class' => $class, |
37
|
24 |
|
'params' => $params |
38
|
|
|
]; |
39
|
24 |
|
return $this; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Set rule to call a method after constructing a class |
44
|
|
|
* @param string $class the name of the class |
45
|
|
|
* @param string $method the name of the method |
46
|
|
|
* @param array $params a key/value array of parameter names and values |
47
|
|
|
* @return self |
48
|
|
|
*/ |
49
|
18 |
|
public function setInjection(string $class, string $method, array $params = []): self |
50
|
|
|
{ |
51
|
18 |
|
$this->injections[$class][$method][] = $params; |
52
|
18 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set parameters of a class |
57
|
|
|
* @param string $class the name of the class |
58
|
|
|
* @param array $params a key/value array of parameter names and values |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
20 |
|
public function setParams(string $class, array $params): self |
62
|
|
|
{ |
63
|
20 |
|
$this->params[$class] = $params + ($this->params[$class] ?? []); |
64
|
20 |
|
return $this; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Set a preferred implementation of a class/interface |
69
|
|
|
* @param string $alias the name of the alias |
70
|
|
|
* @param string $class the name of the class/interface |
71
|
|
|
* @return self |
72
|
|
|
*/ |
73
|
3 |
|
public function setPreference(string $alias, string $class): self |
74
|
|
|
{ |
75
|
3 |
|
$this->preferences[$alias] = $class; |
76
|
3 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set an instance to always return a shared or new instance, regardless of method used |
81
|
|
|
* @param string $class the name of class/alias |
82
|
|
|
* @param bool $isShared true will always return a shared instance. false will always return a new instance |
83
|
|
|
* @return self |
84
|
|
|
*/ |
85
|
6 |
|
public function setShared(string $class, bool $isShared = true): self |
86
|
|
|
{ |
87
|
6 |
|
$this->shared[$class] = $isShared; |
88
|
6 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Set a value. Retrievable with the get method |
93
|
|
|
* @param string $key a name to retrieve the value by |
94
|
|
|
* @param mixed $value the content to store against the key |
95
|
|
|
* @return self |
96
|
|
|
*/ |
97
|
4 |
|
public function set(string $key, $value): self |
98
|
|
|
{ |
99
|
4 |
|
$this->sharedInstance[$key] = $value; |
100
|
4 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Set a factory. Retrievable with the create method. The get method will fetch but also cache the response |
105
|
|
|
* @param string $key a name to retrieve the content by |
106
|
|
|
* @param callable $factory a callable to be invoked when fetched |
107
|
|
|
* @return self |
108
|
|
|
*/ |
109
|
6 |
|
public function setFactory(string $key, callable $factory): self |
110
|
|
|
{ |
111
|
6 |
|
$this->factory[$key] = $factory; |
112
|
6 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Returns true if DiMaria can return an entry for the given string. Returns false otherwise. |
117
|
|
|
* @param string $class identifier of the entry to look for |
118
|
|
|
* @return boolean |
119
|
|
|
*/ |
120
|
71 |
|
public function has($class): bool |
121
|
|
|
{ |
122
|
71 |
|
return class_exists($class) ?: isset($this->aliases[$class]) ?: isset($this->preferences[$class]) ?: isset($this->sharedInstance[$class]) ?: isset($this->factory[$class]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Get an instance of a class |
127
|
|
|
* @param string $class the name of class/alias to create |
128
|
|
|
* @param array $params a key/value array of parameter names and values |
129
|
|
|
* @throws NotFoundException when no entry was found |
130
|
|
|
* @throws ContainerException if class could not be constructed |
131
|
|
|
* @return mixed an instance of the class requested |
132
|
|
|
*/ |
133
|
68 |
|
public function get($class, array $params = []) |
134
|
|
|
{ |
135
|
68 |
View Code Duplication |
if (isset($this->shared[$class]) && !$this->shared[$class]) { |
|
|
|
|
136
|
1 |
|
return $this->create($class, $params); |
137
|
|
|
} |
138
|
67 |
|
if (isset($this->sharedInstance[$class])) { |
139
|
7 |
|
return $this->sharedInstance[$class]; |
140
|
|
|
} |
141
|
63 |
|
$class = $this->getClassName($class); |
142
|
|
|
|
143
|
61 |
|
$object = $this->getObject($class, $params); |
144
|
57 |
|
$this->sharedInstance[$class] = $object; |
145
|
57 |
|
return $object; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Get a new instance of a class |
150
|
|
|
* @param string $class the name of class/alias to create |
151
|
|
|
* @param array $params a key/value array of parameter names and values |
152
|
|
|
* @throws NotFoundException if no entry was found |
153
|
|
|
* @throws ContainerException if class could not be constructed |
154
|
|
|
* @return mixed an instance of the class requested |
155
|
|
|
*/ |
156
|
30 |
|
public function create(string $class, array $params = []) |
157
|
|
|
{ |
158
|
30 |
View Code Duplication |
if (isset($this->shared[$class]) && $this->shared[$class]) { |
|
|
|
|
159
|
4 |
|
return $this->get($class, $params); |
160
|
|
|
} |
161
|
26 |
|
return $this->getObject($this->getClassName($class), $params); |
162
|
|
|
} |
163
|
|
|
|
164
|
68 |
|
protected function getClassName(string $class): string |
165
|
|
|
{ |
166
|
68 |
|
if (!$this->has($class)) { |
167
|
2 |
|
throw new NotFoundException('Class or alias ' . $class . ' does not exist'); |
168
|
|
|
} |
169
|
66 |
|
while ($preference = $this->preferences[$class] ?? false) { |
170
|
3 |
|
$class = $preference; |
171
|
|
|
} |
172
|
66 |
|
return $class; |
173
|
|
|
} |
174
|
|
|
|
175
|
66 |
|
protected function getObject(string $class, array $params) |
176
|
|
|
{ |
177
|
66 |
|
$originalClass = $class; |
178
|
66 |
|
while ($alias = $this->aliases[$class] ?? false) { |
179
|
22 |
|
$params = $params + $alias['params']; |
180
|
22 |
|
$class = $alias['class']; |
181
|
|
|
} |
182
|
|
|
try { |
183
|
66 |
|
$callback = $this->factory[$originalClass] ?? $this->factory[$originalClass] = $this->getCallback($class, $originalClass); |
184
|
65 |
|
return $callback($params); |
185
|
5 |
|
} catch (\Exception $e) { |
186
|
4 |
|
throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
187
|
1 |
|
} catch (\Error $e) { |
188
|
1 |
|
throw new ContainerException($e->getMessage(), $e->getCode(), $e); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
63 |
|
protected function getCallback(string $class, string $originalClass): callable |
193
|
|
|
{ |
194
|
63 |
|
$callback = $this->generateCallback($class); |
195
|
62 |
|
if (isset($this->injections[$originalClass])) { |
196
|
17 |
|
foreach ($this->injections[$originalClass] as $method => $instance) { |
197
|
17 |
|
$methodInfo = $this->getMethodInfo(new \ReflectionMethod($class, $method)); |
198
|
17 |
|
foreach ($instance as $methodParameters) { |
199
|
17 |
|
$methodParams = $this->getParameters($methodInfo, $methodParameters); |
200
|
|
|
$callback = function ($params) use ($callback, $method, $methodParams) { |
201
|
17 |
|
$object = $callback($params); |
202
|
17 |
|
$object->$method(...$methodParams); |
203
|
17 |
|
return $object; |
204
|
17 |
|
}; |
205
|
|
|
}; |
206
|
|
|
} |
207
|
|
|
} |
208
|
62 |
|
return $callback; |
209
|
|
|
} |
210
|
|
|
|
211
|
63 |
|
protected function generateCallback(string $class): callable |
212
|
|
|
{ |
213
|
63 |
|
$constructor = (new \ReflectionClass($class))->getConstructor(); |
214
|
62 |
|
if (!$constructor || !$constructor->getNumberOfParameters()) { |
215
|
|
|
return function () use ($class) { |
216
|
30 |
|
return new $class; |
217
|
30 |
|
}; |
218
|
|
|
} |
219
|
42 |
|
$constructorInfo = $this->getMethodInfo($constructor); |
220
|
42 |
|
$predefinedParams = $this->params[$class] ?? []; |
221
|
|
|
|
222
|
42 |
|
return function ($params) use ($class, $constructorInfo, $predefinedParams) { |
223
|
42 |
|
return new $class(...$this->getParameters($constructorInfo, $params + $predefinedParams)); |
224
|
42 |
|
}; |
225
|
|
|
} |
226
|
|
|
|
227
|
57 |
|
protected function getMethodInfo(\ReflectionMethod $method): array |
228
|
|
|
{ |
229
|
57 |
|
$paramInfo = []; |
230
|
57 |
|
foreach ($method->getParameters() as $param) { |
231
|
54 |
|
$paramType = $param->hasType() ? $param->getType() : null; |
232
|
54 |
|
$paramType = $paramType ? $paramType->isBuiltin() ? null : $paramType->__toString() : null; |
233
|
54 |
|
$paramInfo[$param->name] = [ |
234
|
54 |
|
'name' => $param->name, |
235
|
54 |
|
'optional' => $param->isOptional(), |
236
|
54 |
|
'default' => $param->isOptional() ? ($param->isVariadic() ? null : $param->getDefaultValue()) : null, |
237
|
54 |
|
'variadic' => $param->isVariadic(), |
238
|
54 |
|
'type' => $paramType, |
239
|
|
|
]; |
240
|
|
|
} |
241
|
57 |
|
return $paramInfo; |
242
|
|
|
} |
243
|
|
|
|
244
|
57 |
|
protected function getParameters(array $methodInfo, array $params): array |
245
|
|
|
{ |
246
|
57 |
|
$parameters = []; |
247
|
57 |
|
foreach ($methodInfo as $param) { |
248
|
54 |
|
if (isset($params[$param['name']])) { |
249
|
40 |
|
array_push($parameters, ...$this->determineParameter($params[$param['name']], $param['variadic'])); |
250
|
30 |
|
} elseif ($param['optional']) { |
251
|
26 |
|
if ($param['variadic']) { |
252
|
4 |
|
break; |
253
|
|
|
} |
254
|
22 |
|
$parameters[] = $param['default']; |
255
|
15 |
|
} elseif ($param['type']) { |
256
|
12 |
|
$parameters[] = $this->create($param['type']); |
257
|
|
|
} else { |
258
|
52 |
|
throw new ContainerException('Required parameter $' . $param['name'] . ' is missing'); |
259
|
|
|
} |
260
|
|
|
} |
261
|
54 |
|
return $parameters; |
262
|
|
|
} |
263
|
|
|
|
264
|
40 |
|
protected function determineParameter($param, bool $isVariadic): array |
265
|
|
|
{ |
266
|
40 |
|
if (is_array($param)) { |
267
|
16 |
|
if ($isVariadic) { |
268
|
7 |
|
$params = []; |
269
|
7 |
|
foreach ($param as $val) { |
270
|
7 |
|
$params[] = isset($val['instanceOf']) ? $this->create($val['instanceOf'], $val['params'] ?? []) : $val; |
271
|
|
|
} |
272
|
7 |
|
return $params; |
273
|
|
|
} |
274
|
9 |
|
return isset($param['instanceOf']) ? [$this->create($param['instanceOf'], $param['params'] ?? [])] : [$param]; |
275
|
|
|
} |
276
|
34 |
|
return [$param]; |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.