@@ -35,6 +35,15 @@ public function instantiate(string $type): object
35
35
{
36
36
return $ this ->track ($ type , function () use ($ type ) {
37
37
$ reflection = new ReflectionClass ($ type );
38
+
39
+ if ($ reflection ->isInterface () or $ reflection ->isAbstract ()) {
40
+ if ($ this ->container ->has ($ type )) {
41
+ return $ this ->container ->get ($ type );
42
+ }
43
+
44
+ throw new ContainerException ('Type [ ' . $ type . '] is not instantiable. ' );
45
+ }
46
+
38
47
return ($ constructor = $ reflection ->getConstructor ())
39
48
? $ reflection ->newInstanceArgs ($ this ->getArguments ($ constructor ))
40
49
: $ reflection ->newInstance ();
@@ -67,7 +76,7 @@ private static function getCallableReflectionFunctionAbstract(callable $callable
67
76
return new ReflectionFunction ($ callable );
68
77
}
69
78
70
- throw new ContainerException ('ReflectionFunctionAbstract for given callable type not implemented ' );
79
+ throw new ContainerException ('ReflectionFunctionAbstract for given callable type not implemented. ' );
71
80
}
72
81
73
82
private static function getFunctionIdentifier (ReflectionFunctionAbstract $ reflection ): string
@@ -82,7 +91,7 @@ private static function getFunctionIdentifier(ReflectionFunctionAbstract $reflec
82
91
return $ reflection ->getDeclaringClass ()->getName () . ':: ' . $ reflection ->getName ();
83
92
}
84
93
85
- throw new ContainerException ('Function identifier not implemented for reflection of type ' . get_class ($ reflection ));
94
+ throw new ContainerException ('Function identifier not implemented for reflection of type ' . get_class ($ reflection ) . ' . ' );
86
95
}
87
96
88
97
private function getArguments (ReflectionFunctionAbstract $ reflection ): array
@@ -115,6 +124,6 @@ private function getArgument(ReflectionParameter $parameter)
115
124
return $ this ->instantiate ($ typeName );
116
125
}
117
126
118
- throw new ContainerException ('Cannot resolve value for parameter ' . $ parameter ->getName ());
127
+ throw new ContainerException ('Cannot resolve value for parameter [ ' . $ parameter ->getName () . ' ] with type [ ' . $ typeName . ' ]. ' );
119
128
}
120
129
}
0 commit comments