@@ -21,55 +21,18 @@ class Definition implements ArgumentResolverInterface, DefinitionInterface
21
21
use ArgumentResolverTrait;
22
22
use ContainerAwareTrait;
23
23
24
- /**
25
- * @var string
26
- */
27
- protected $ alias ;
28
-
29
- /**
30
- * @var mixed
31
- */
32
- protected $ concrete ;
33
-
34
- /**
35
- * @var boolean
36
- */
37
- protected $ shared = false ;
38
-
39
- /**
40
- * @var array
41
- */
42
- protected $ tags = [];
43
-
44
- /**
45
- * @var array
46
- */
47
- protected $ arguments = [];
48
-
49
- /**
50
- * @var array
51
- */
52
- protected $ methods = [];
53
-
54
- /**
55
- * @var mixed
56
- */
57
- protected $ resolved ;
58
-
59
- /**
60
- * @var bool
61
- */
62
- protected $ isAlreadySearched = false ;
63
-
64
- /**
65
- * @param string $id
66
- * @param mixed|null $concrete
67
- */
68
- public function __construct (string $ id , $ concrete = null )
69
- {
70
- $ concrete ??= $ id ;
71
- $ this ->alias = $ id ;
72
- $ this ->concrete = $ concrete ;
24
+ protected mixed $ resolved = null ;
25
+ protected array $ recursiveCheck = [];
26
+
27
+ public function __construct (
28
+ protected string $ id ,
29
+ protected mixed $ concrete = null ,
30
+ protected bool $ shared = false ,
31
+ protected array $ arguments = [],
32
+ protected array $ methods = [],
33
+ protected array $ tags = [],
34
+ ) {
35
+ $ this ->concrete ??= $ this ->id ;
73
36
}
74
37
75
38
public function addTag (string $ tag ): DefinitionInterface
@@ -83,15 +46,25 @@ public function hasTag(string $tag): bool
83
46
return isset ($ this ->tags [$ tag ]);
84
47
}
85
48
86
- public function setAlias (string $ id ): DefinitionInterface
49
+ public function setId (string $ id ): DefinitionInterface
87
50
{
88
- $ this ->alias = $ id ;
51
+ $ this ->id = $ id ;
89
52
return $ this ;
90
53
}
91
54
55
+ public function getId (): string
56
+ {
57
+ return $ this ->id ;
58
+ }
59
+
60
+ public function setAlias (string $ id ): DefinitionInterface
61
+ {
62
+ return $ this ->setId ($ id );
63
+ }
64
+
92
65
public function getAlias (): string
93
66
{
94
- return $ this ->alias ;
67
+ return $ this ->getId () ;
95
68
}
96
69
97
70
public function setShared (bool $ shared = true ): DefinitionInterface
@@ -135,7 +108,7 @@ public function addArguments(array $args): DefinitionInterface
135
108
public function addMethodCall (string $ method , array $ args = []): DefinitionInterface
136
109
{
137
110
$ this ->methods [] = [
138
- 'method ' => $ method ,
111
+ 'method ' => $ method ,
139
112
'arguments ' => $ args
140
113
];
141
114
@@ -151,7 +124,7 @@ public function addMethodCalls(array $methods = []): DefinitionInterface
151
124
return $ this ;
152
125
}
153
126
154
- public function resolve ()
127
+ public function resolve (): mixed
155
128
{
156
129
if (null !== $ this ->resolved && $ this ->isShared ()) {
157
130
return $ this ->resolved ;
@@ -160,7 +133,7 @@ public function resolve()
160
133
return $ this ->resolveNew ();
161
134
}
162
135
163
- public function resolveNew ()
136
+ public function resolveNew (): mixed
164
137
{
165
138
$ concrete = $ this ->concrete ;
166
139
@@ -192,28 +165,23 @@ public function resolveNew()
192
165
}
193
166
194
167
// stop recursive resolving
195
- if ($ this ->isAlreadySearched ) {
196
- throw new NotFoundException (
197
- sprintf ('Alias or class "%s" did not found. ' , $ concrete )
198
- );
168
+ if (is_string ($ concrete ) && in_array ($ concrete , $ this ->recursiveCheck )) {
169
+ $ this ->resolved = $ concrete ;
170
+ return $ concrete ;
199
171
}
200
172
201
173
// if we still have a string, try to pull it from the container
202
174
// this allows for `alias -> alias -> ... -> concrete
203
175
if (is_string ($ concrete ) && $ container instanceof ContainerInterface && $ container ->has ($ concrete )) {
204
- $ this ->isAlreadySearched = true ;
176
+ $ this ->recursiveCheck [] = $ concrete ;
205
177
$ concrete = $ container ->get ($ concrete );
206
178
}
207
179
208
180
$ this ->resolved = $ concrete ;
209
181
return $ concrete ;
210
182
}
211
183
212
- /**
213
- * @param callable $concrete
214
- * @return mixed
215
- */
216
- protected function resolveCallable (callable $ concrete )
184
+ protected function resolveCallable (callable $ concrete ): mixed
217
185
{
218
186
$ resolved = $ this ->resolveArguments ($ this ->arguments );
219
187
return call_user_func_array ($ concrete , $ resolved );
0 commit comments