@@ -281,21 +281,36 @@ public function isShared($abstract)
281
281
return false ;
282
282
}
283
283
284
- $ reflection = new ReflectionClass ($ abstract );
285
-
286
- if (! empty ($ reflection ->getAttributes (Singleton::class))) {
287
- return true ;
284
+ if (($ scopedType = $ this ->getScopedTyped (new ReflectionClass ($ abstract ))) === null ) {
285
+ return false ;
288
286
}
289
287
290
- if (! empty ( $ reflection -> getAttributes (Scoped::class)) ) {
288
+ if ($ scopedType === ' scoped ' ) {
291
289
if (! in_array ($ abstract , $ this ->scopedInstances , true )) {
292
290
$ this ->scopedInstances [] = $ abstract ;
293
291
}
292
+ }
294
293
295
- return true ;
294
+ return true ;
295
+ }
296
+
297
+ /**
298
+ * Determine if a ReflectionClass has scoping attributes applied.
299
+ *
300
+ * @param ReflectionClass<object> $reflection
301
+ * @return "singleton"|"scoped"|null
302
+ */
303
+ protected function getScopedTyped (ReflectionClass $ reflection ): ?string
304
+ {
305
+ if (! empty ($ reflection ->getAttributes (Singleton::class))) {
306
+ return 'singleton ' ;
296
307
}
297
308
298
- return false ;
309
+ if (! empty ($ reflection ->getAttributes (Scoped::class))) {
310
+ return 'scoped ' ;
311
+ }
312
+
313
+ return null ;
299
314
}
300
315
301
316
/**
@@ -983,34 +998,34 @@ protected function getConcrete($abstract)
983
998
return $ abstract ;
984
999
}
985
1000
986
- $ attributes = [];
987
-
988
- try {
989
- $ attributes = (new ReflectionClass ($ abstract ))->getAttributes (Bind::class);
990
- } catch (ReflectionException ) {
991
- }
992
-
993
- $ this ->checkedForAttributeBindings [$ abstract ] = true ;
994
-
995
- if ($ attributes === []) {
996
- return $ abstract ;
997
- }
998
-
999
- return $ this ->getConcreteBindingFromAttributes ($ abstract , $ attributes );
1001
+ return $ this ->getConcreteBindingFromAttributes ($ abstract );
1000
1002
}
1001
1003
1002
1004
/**
1003
1005
* Get the concrete binding for an abstract from the Bind attribute.
1004
1006
*
1005
1007
* @param string $abstract
1006
- * @param array<int, \ReflectionAttribute<Bind>> $reflectedAttributes
1007
1008
* @return mixed
1008
1009
*/
1009
- protected function getConcreteBindingFromAttributes ($ abstract, $ reflectedAttributes )
1010
+ protected function getConcreteBindingFromAttributes ($ abstract )
1010
1011
{
1012
+ $ this ->checkedForAttributeBindings [$ abstract ] = true ;
1013
+
1014
+ try {
1015
+ $ reflected = new ReflectionClass ($ abstract );
1016
+ } catch (ReflectionException ) {
1017
+ return $ abstract ;
1018
+ }
1019
+
1020
+ $ bindAttributes = $ reflected ->getAttributes (Bind::class);
1021
+
1022
+ if ($ bindAttributes === []) {
1023
+ return $ abstract ;
1024
+ }
1025
+
1011
1026
$ concrete = $ maybeConcrete = null ;
1012
1027
1013
- foreach ($ reflectedAttributes as $ reflectedAttribute ) {
1028
+ foreach ($ bindAttributes as $ reflectedAttribute ) {
1014
1029
$ instance = $ reflectedAttribute ->newInstance ();
1015
1030
1016
1031
if ($ instance ->environments === ['* ' ]) {
@@ -1034,7 +1049,11 @@ protected function getConcreteBindingFromAttributes($abstract, $reflectedAttribu
1034
1049
return $ abstract ;
1035
1050
}
1036
1051
1037
- $ this ->bind ($ abstract , $ concrete );
1052
+ match ($ this ->getScopedTyped ($ reflected )) {
1053
+ 'scoped ' => $ this ->scoped ($ abstract , $ concrete ),
1054
+ 'singleton ' => $ this ->singleton ($ abstract , $ concrete ),
1055
+ null => $ this ->bind ($ abstract , $ concrete ),
1056
+ };
1038
1057
1039
1058
return $ this ->bindings [$ abstract ]['concrete ' ];
1040
1059
}
0 commit comments