22
33namespace Technically \DependencyResolver ;
44
5- use LogicException ;
5+ use Psr \ Container \ ContainerExceptionInterface ;
66use Psr \Container \ContainerInterface ;
77use ReflectionClass ;
88use ReflectionMethod ;
1111use ReflectionUnionType ;
1212use Technically \DependencyResolver \Arguments \Argument ;
1313use Technically \DependencyResolver \Arguments \Type ;
14+ use Technically \DependencyResolver \Exceptions \CannotAutowireDependencyArgument ;
1415use Technically \DependencyResolver \Exceptions \ClassCannotBeInstantiated ;
16+ use Technically \DependencyResolver \Exceptions \DependencyResolutionException ;
1517
1618final class DependencyResolver
1719{
@@ -48,7 +50,7 @@ public function resolve(string $className, array $bindings = [])
4850
4951 if ($ constructor = $ reflection ->getConstructor ()) {
5052 $ arguments = $ this ->parseArguments ($ constructor );
51- $ values = $ this ->resolveArguments ($ arguments , $ bindings );
53+ $ values = $ this ->resolveArguments ($ className , $ arguments , $ bindings );
5254 }
5355
5456 return $ reflection ->newInstanceArgs ($ values );
@@ -77,28 +79,34 @@ private function parseArguments(ReflectionMethod $function): array
7779 }
7880
7981 /**
82+ * @param string $className
8083 * @param Argument[] $arguments
8184 * @param array $bindings
8285 * @return array
86+ *
87+ * @throws CannotAutowireDependencyArgument
8388 * @throws \Psr\Container\ContainerExceptionInterface
8489 * @throws \Psr\Container\NotFoundExceptionInterface
8590 */
86- private function resolveArguments (array $ arguments , array $ bindings = []): array
91+ private function resolveArguments (string $ className , array $ arguments , array $ bindings = []): array
8792 {
8893 $ values = [];
8994 foreach ($ arguments as $ argument ) {
90- $ values [] = $ this ->resolveArgument ($ argument , $ bindings );
95+ $ values [] = $ this ->resolveArgument ($ className , $ argument , $ bindings );
9196 }
9297
9398 return $ values ;
9499 }
95100
96101 /**
102+ * @param string $className
97103 * @param Argument $argument
98104 * @param array $bindings
99105 * @return mixed|null
106+ *
107+ * @throws CannotAutowireDependencyArgument
100108 */
101- private function resolveArgument (Argument $ argument , array $ bindings )
109+ private function resolveArgument (string $ className , Argument $ argument , array $ bindings )
102110 {
103111 if (array_key_exists ($ argument ->getName (), $ bindings )) {
104112 return $ bindings [$ argument ->getName ()];
@@ -107,7 +115,11 @@ private function resolveArgument(Argument $argument, array $bindings)
107115 foreach ($ argument ->getTypes () as $ type ) {
108116 $ class = $ type ->getClassName ();
109117 if (! empty ($ class ) && $ this ->container ->has ($ class )) {
110- return $ this ->container ->get ($ class );
118+ try {
119+ return $ this ->container ->get ($ class );
120+ } catch (ContainerExceptionInterface $ exception ) {
121+ throw new CannotAutowireDependencyArgument ($ className , $ argument ->getName ());
122+ }
111123 }
112124 }
113125
@@ -121,11 +133,15 @@ private function resolveArgument(Argument $argument, array $bindings)
121133
122134 foreach ($ argument ->getTypes () as $ type ) {
123135 if ($ class = $ type ->getClassName ()) {
124- return $ this ->resolve ($ class );
136+ try {
137+ return $ this ->resolve ($ class );
138+ } catch (DependencyResolutionException $ exception ) {
139+ // try another one
140+ }
125141 }
126142 }
127143
128- throw new LogicException ( " Cannot resolve argument: ` { $ argument ->getName ()} `. " );
144+ throw new CannotAutowireDependencyArgument ( $ className , $ argument ->getName ());
129145 }
130146
131147 private static function reflectClass (string $ class ): ReflectionClass
0 commit comments