@@ -28,11 +28,9 @@ public function __construct()
28
28
}
29
29
30
30
/**
31
- * Returns a stored instance or creates a new one and stores it.
32
- *
33
31
* @throws ContainerException
34
32
*/
35
- public function get (string $ name )
33
+ public function get (string $ name ): object
36
34
{
37
35
$ name = $ this ->getDefinitiveName ($ name );
38
36
@@ -54,12 +52,19 @@ public function has(string $name): bool
54
52
return isset ($ this ->instances [$ name ]);
55
53
}
56
54
55
+ public function assertHas (string $ name ): void
56
+ {
57
+ if (!$ this ->has ($ name )) {
58
+ throw NotFoundException::fromName ($ name );
59
+ }
60
+ }
61
+
57
62
/**
58
63
* Build a new instance with stored dependencies.
59
64
*
60
65
* @throws ContainerException
61
66
*/
62
- public function build (string $ name )
67
+ public function build (string $ name ): object
63
68
{
64
69
return $ this ->createInstance ($ name , self ::USE_STORED_DEPENDENCIES );
65
70
}
@@ -69,7 +74,7 @@ public function build(string $name)
69
74
*
70
75
* @throws ContainerException
71
76
*/
72
- public function buildAll (string $ name )
77
+ public function buildAll (string $ name ): object
73
78
{
74
79
return $ this ->createInstance ($ name , self ::USE_NEW_DEPENDENCIES );
75
80
}
@@ -79,7 +84,7 @@ public function buildAll(string $name)
79
84
*
80
85
* @throws ContainerException
81
86
*/
82
- protected function createInstance (string $ name , int $ useStoredDependencies )
87
+ protected function createInstance (string $ name , int $ useStoredDependencies ): object
83
88
{
84
89
$ name = $ this ->getDefinitiveName ($ name );
85
90
@@ -92,8 +97,8 @@ protected function createInstance(string $name, int $useStoredDependencies)
92
97
93
98
try {
94
99
$ dependencies = $ this ->getDependenciesFor ($ name , $ useStoredDependencies );
95
- } catch (Throwable $ e ) {
96
- throw new ContainerException ($ e ->getMessage ());
100
+ } catch (Throwable $ t ) {
101
+ throw new ContainerException ($ t ->getMessage (), $ t -> getCode (), $ t );
97
102
}
98
103
99
104
return new $ name (...$ dependencies );
@@ -120,6 +125,7 @@ protected function getMapIfExists(string $requested): string
120
125
* Get the dependencies for an instance, based on the constructor.
121
126
* Optionally use stored dependencies or always create new ones.
122
127
*
128
+ * @return mixed[]
123
129
* @throws ContainerException
124
130
*/
125
131
public function getDependenciesFor (
@@ -135,10 +141,14 @@ public function getDependenciesFor(
135
141
*/
136
142
$ reflection = @new ReflectionClass ($ name );
137
143
} catch (Throwable $ t ) {
138
- throw new ContainerException (sprintf (
139
- 'Could not create instance for class `%s`. ' ,
140
- $ name
141
- ));
144
+ throw new ContainerException (
145
+ sprintf (
146
+ 'Could not create instance for class `%s`. ' ,
147
+ $ name
148
+ ),
149
+ $ t ->getCode (),
150
+ $ t
151
+ );
142
152
}
143
153
144
154
$ constructor = $ reflection ->getConstructor ();
@@ -153,15 +163,17 @@ public function getDependenciesFor(
153
163
foreach ($ parameters as $ parameter ) {
154
164
$ type = $ parameter ->getType ();
155
165
166
+ $ builtIn = false ;
167
+
156
168
if ($ type instanceof ReflectionNamedType) {
157
169
$ builtIn = $ type ->isBuiltin ();
158
- } else {
159
- $ builtIn = false ;
160
170
}
161
171
162
- $ class = $ parameter ->getType () && $ builtIn === false
163
- ? new ReflectionClass ($ parameter ->getType ()->getName ())
164
- : null ;
172
+ $ class = null ;
173
+
174
+ if ($ parameter ->getType () instanceof ReflectionNamedType && $ builtIn === false ) {
175
+ $ class = new ReflectionClass ($ parameter ->getType ()->getName ());
176
+ }
165
177
166
178
if ($ class === null ) {
167
179
if (!$ parameter ->isOptional ()) {
@@ -173,6 +185,7 @@ public function getDependenciesFor(
173
185
174
186
$ dependencies [] = $ parameter ->getDefaultValue ();
175
187
188
+ // For the foreach loop
176
189
continue ;
177
190
}
178
191
@@ -196,15 +209,11 @@ public function getDependenciesFor(
196
209
}
197
210
198
211
/**
199
- * Store the provided instance with the provided id , or the class name of the object.
212
+ * Store the provided instance with the provided name , or the class name of the object.
200
213
*/
201
- public function store ($ instance , string $ name = null ): void
214
+ public function store (object $ instance , string $ name = null ): void
202
215
{
203
- if ($ name === null ) {
204
- $ name = get_class ($ instance );
205
- }
206
-
207
- $ name = $ this ->getDefinitiveName ($ name );
216
+ $ name = $ this ->getDefinitiveName ($ name ?? $ instance ::class);
208
217
209
218
$ this ->instances [$ name ] = $ instance ;
210
219
}
@@ -218,9 +227,7 @@ public function clear(string $name): void
218
227
{
219
228
$ name = $ this ->getDefinitiveName ($ name );
220
229
221
- if (!$ this ->has ($ name )) {
222
- throw NotFoundException::fromId ($ name );
223
- }
230
+ $ this ->assertHas ($ name );
224
231
225
232
unset($ this ->instances [$ name ]);
226
233
@@ -238,12 +245,11 @@ public function clear(string $name): void
238
245
public function clearExcept (array $ keep ): void
239
246
{
240
247
$ kept = [];
248
+
241
249
foreach ($ keep as $ name ) {
242
250
$ name = $ this ->getDefinitiveName ($ name );
243
251
244
- if (!$ this ->has ($ name )) {
245
- throw NotFoundException::fromId ($ name );
246
- }
252
+ $ this ->assertHas ($ name );
247
253
248
254
$ kept [$ name ] = $ this ->get ($ name );
249
255
}
@@ -269,7 +275,10 @@ protected function storeRelationship(string $class, string $dependency): void
269
275
{
270
276
$ this ->relationships [$ class ][$ dependency ] = true ;
271
277
272
- if (isset ($ this ->relationships [$ class ][$ dependency ]) && isset ($ this ->relationships [$ dependency ][$ class ])) {
278
+ if (isset (
279
+ $ this ->relationships [$ class ][$ dependency ],
280
+ $ this ->relationships [$ dependency ][$ class ]
281
+ )) {
273
282
throw new ContainerException (sprintf (
274
283
'Cyclical dependency found between `%s` and `%s`. ' ,
275
284
$ class ,
0 commit comments