1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Starlit App. |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2016 Starweb AB |
6
|
|
|
* @license BSD 3-Clause |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Starlit\App\Container; |
10
|
|
|
|
11
|
|
|
use Psr\Container\ContainerInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Dependency injection container. |
15
|
|
|
* |
16
|
|
|
* @author Andreas Nilsson <http://github.com/jandreasn> |
17
|
|
|
*/ |
18
|
|
|
class Container implements ContainerInterface |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $dicValues = []; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $aliases = []; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var array |
32
|
|
|
*/ |
33
|
|
|
private $dicObjects = []; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Set a DIC value. |
37
|
|
|
* |
38
|
|
|
* Wrap objects provided in a closure for lazy loading. |
39
|
|
|
* |
40
|
|
|
* @param string $key |
41
|
|
|
* @param mixed $value |
42
|
|
|
* @return Container |
43
|
|
|
*/ |
44
|
21 |
|
public function set(string $key, $value): self |
45
|
|
|
{ |
46
|
21 |
|
if (!(\is_string($value) || \is_object($value))) { |
47
|
|
|
throw new \InvalidArgumentException('Value must be a class name, an object instance, or a callable'); |
48
|
|
|
} |
49
|
|
|
|
50
|
21 |
|
$this->dicValues[$key] = $value; |
51
|
21 |
|
unset($this->dicObjects[$key]); // In case an object instance was stored for sharing |
52
|
|
|
|
53
|
21 |
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
21 |
|
public function alias(string $alias, string $key): self |
57
|
|
|
{ |
58
|
21 |
|
$this->aliases[$alias] = $key; |
59
|
|
|
|
60
|
21 |
|
return $this; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Check if a DIC value/object exists. |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
2 |
|
public function has($key): bool |
70
|
|
|
{ |
71
|
2 |
|
if (array_key_exists($key, $this->aliases)) { |
72
|
|
|
$key = $this->aliases[$key]; |
73
|
|
|
} |
74
|
|
|
|
75
|
2 |
|
return array_key_exists($key, $this->dicValues); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param string $key |
80
|
|
|
* @return bool |
81
|
|
|
*/ |
82
|
2 |
|
public function hasInstance($key): bool |
83
|
|
|
{ |
84
|
2 |
|
if (array_key_exists($key, $this->aliases)) { |
85
|
1 |
|
$key = $this->aliases[$key]; |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return isset($this->dicObjects[$key]); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get the shared instance of a DIC object |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
21 |
|
public function get($key) |
98
|
|
|
{ |
99
|
21 |
|
if (array_key_exists($key, $this->aliases)) { |
100
|
|
|
$key = $this->aliases[$key]; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
// Get already instantiated object if it exist |
104
|
21 |
|
if (isset($this->dicObjects[$key])) { |
105
|
2 |
|
return $this->dicObjects[$key]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
try { |
109
|
21 |
|
if (array_key_exists($key, $this->dicValues)) { |
110
|
21 |
|
$value = $this->dicValues[$key]; |
111
|
21 |
|
if (\is_object($value)) { |
112
|
|
|
// Is it an invokable? (closure/anonymous function) |
113
|
21 |
|
if (method_exists($value, '__invoke')) { |
114
|
21 |
|
$instance = $value($this); |
115
|
|
|
} else { |
116
|
21 |
|
$instance = $value; |
117
|
|
|
} |
118
|
|
|
} else { |
119
|
21 |
|
$instance = $this->resolveInstance($value); |
120
|
|
|
} |
121
|
|
|
} else { |
122
|
21 |
|
$instance = $this->resolveInstance($key); |
123
|
|
|
} |
124
|
1 |
|
} catch (\ReflectionException $e) { |
125
|
1 |
|
throw new NotFoundException(sprintf('Key "%s" could not be resolved. ', $key)); |
126
|
|
|
} |
127
|
|
|
|
128
|
21 |
|
$this->dicObjects[$key] = $instance; |
129
|
|
|
|
130
|
21 |
|
return $this->dicObjects[$key]; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get a new instance of a DIC object |
135
|
|
|
* |
136
|
|
|
* @param string $key |
137
|
|
|
* @return mixed |
138
|
|
|
*/ |
139
|
3 |
|
public function getNew(string $key) |
140
|
|
|
{ |
141
|
3 |
|
if (array_key_exists($key, $this->aliases)) { |
142
|
|
|
$key = $this->aliases[$key]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
try { |
146
|
3 |
|
if (array_key_exists($key, $this->dicValues)) { |
147
|
2 |
|
if (\is_object($this->dicValues[$key])) { |
148
|
|
|
// Is it an invokable? (closure/anonymous function) |
149
|
1 |
|
if (method_exists($this->dicValues[$key], '__invoke')) { |
150
|
1 |
|
return $this->dicValues[$key]($this); |
151
|
|
|
} |
152
|
|
|
throw new \LogicException('The value for the specified key is a pre-made instance'); |
153
|
|
|
} |
154
|
1 |
|
return $this->resolveInstance($this->dicValues[$key]); |
155
|
|
|
} |
156
|
1 |
|
return $this->resolveInstance($key); |
157
|
2 |
|
} catch (\ReflectionException $e) { |
158
|
2 |
|
throw new NotFoundException(sprintf('Key "%s" could not be resolved.', $key)); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Destroy a DIC object instance. |
164
|
|
|
* |
165
|
|
|
* Will force a new object to be created on next call. |
166
|
|
|
* |
167
|
|
|
* @param string $key |
168
|
|
|
*/ |
169
|
1 |
|
public function destroyInstance($key): void |
170
|
|
|
{ |
171
|
1 |
|
unset($this->dicObjects[$key]); |
172
|
1 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Destroy all DIC object instances. |
176
|
|
|
* |
177
|
|
|
* Will force new objects to be created on next call. |
178
|
|
|
*/ |
179
|
1 |
|
public function destroyAllInstances(): void |
180
|
|
|
{ |
181
|
1 |
|
$this->dicObjects = []; |
182
|
|
|
|
183
|
|
|
// To make sure objects (like database connections) are destructed properly. PHP might not destruct objects |
184
|
|
|
// until the end of execution otherwise. |
185
|
1 |
|
gc_collect_cycles(); |
186
|
1 |
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* Magic method to get or set DIC values. |
190
|
|
|
* |
191
|
|
|
* @param string $name |
192
|
|
|
* @param array $arguments |
193
|
|
|
* @return mixed |
194
|
|
|
*/ |
195
|
3 |
|
public function __call($name, $arguments) |
196
|
|
|
{ |
197
|
|
|
// getNew followed by an upper letter like getNewApple() |
198
|
3 |
|
if (preg_match('/^getNew([A-Z].*)/', $name, $matches)) { |
199
|
|
|
$key = lcfirst($matches[1]); |
200
|
|
|
|
201
|
|
|
return $this->getNew($key); |
202
|
3 |
|
} elseif (strpos($name, 'get') === 0) { |
203
|
1 |
|
$key = lcfirst(substr($name, 3)); |
204
|
|
|
|
205
|
1 |
|
return $this->get($key); |
206
|
3 |
|
} elseif (strpos($name, 'set') === 0) { |
207
|
2 |
|
$argumentCount = \count($arguments); |
208
|
2 |
|
if ($argumentCount !== 1) { |
209
|
1 |
|
throw new \BadMethodCallException("Invalid argument count[{$argumentCount}] for application {$name}()"); |
210
|
|
|
} |
211
|
|
|
|
212
|
1 |
|
$key = lcfirst(substr($name, 3)); |
213
|
|
|
|
214
|
1 |
|
return $this->set($key, $arguments[0]); |
215
|
|
|
} else { |
216
|
1 |
|
throw new \BadMethodCallException("No application method named {$name}()"); |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Instantiate an object of named class, recursively resolving dependencies |
222
|
|
|
* |
223
|
|
|
* @param string $className Fully qualified class name |
224
|
|
|
* @return mixed |
225
|
|
|
* @throws \ReflectionException |
226
|
|
|
*/ |
227
|
3 |
|
protected function resolveInstance(string $className) |
228
|
|
|
{ |
229
|
3 |
|
$class = new \ReflectionClass($className); |
230
|
|
|
|
231
|
|
|
if (!$class->isInstantiable()) { |
232
|
|
|
throw new \ReflectionException(sprintf('Class %s cannot be instantiated', $className)); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
$parameterValues = []; |
236
|
|
|
if (($constructor = $class->getConstructor())) { |
237
|
|
|
$parameterValues = $this->resolveParameters( |
238
|
|
|
$constructor->getParameters() |
239
|
|
|
); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
return $class->newInstanceArgs($parameterValues); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
/** |
246
|
|
|
* Recursively resolve function parameters using type hints |
247
|
|
|
* |
248
|
|
|
* @param \ReflectionParameter[] |
249
|
|
|
* @return mixed |
250
|
|
|
*/ |
251
|
|
|
protected function resolveParameters(array $parameters): array |
252
|
|
|
{ |
253
|
|
|
$values = []; |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* @var \ReflectionParameter $parameter |
257
|
|
|
*/ |
258
|
|
|
foreach ($parameters as $parameter) { |
259
|
|
|
if (($parameterClass = $parameter->getClass())) { |
260
|
|
|
try { |
261
|
|
|
$values[] = $this->get($parameterClass->getName()); |
262
|
|
|
} |
263
|
|
|
catch (NotFoundException $e) { // We're probably dealing with an unmapped interface here |
264
|
|
|
$values[] = $parameter->getDefaultValue(); |
265
|
|
|
} |
266
|
|
|
} else { |
267
|
|
|
$values[] = $parameter->getDefaultValue(); |
268
|
|
|
} |
269
|
|
|
} |
270
|
|
|
|
271
|
|
|
return $values; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|