55namespace ezsql ;
66
77use Psr \Container \ContainerInterface ;
8+ use ezsql \Exception \ContainerException ;
9+ use ezsql \Exception \NotFoundException ;
810
911/**
1012 * Dependency Injection Container
@@ -19,8 +21,10 @@ class DInjector implements ContainerInterface
1921 protected $ instances = [];
2022
2123 /**
22- * @param $abstract
23- * @param null $concrete
24+ * Register a service with the container.
25+ *
26+ * @param string $abstract - className
27+ * @param string $concrete - friendlyName
2428 */
2529 public function set ($ abstract , $ concrete = NULL )
2630 {
@@ -41,44 +45,43 @@ public function register($abstract, $concrete = NULL)
4145 /**
4246 * Execute with any dependencies
4347 *
44- * @param $abstract
48+ * @param $abstract
4549 * @param array $values
4650 *
4751 * @return mixed
48- * @throws Exception
52+ * @throws ContainerException
4953 */
5054 public function call ($ abstract , $ values = [])
5155 {
5256 $ concrete = $ this ->instances [$ abstract ];
5357 if ($ concrete instanceof \Closure) {
5458 return $ concrete ($ this , $ values );
5559 } else
56- throw new \ Exception ("{$ concrete } is not callable " );
60+ throw new ContainerException ("{$ concrete } is not callable " );
5761 }
5862
5963 /**
60- * @param $abstract
61- * @param array $values
64+ * @param $abstract
6265 *
6366 * @return null|object
64- * @throws Exception
67+ * @throws NotFoundException
6568 */
6669 public function get ($ abstract )
6770 {
6871 if (!$ this ->has ($ abstract )) {
69- throw new \ Exception ("{$ abstract } does not exists " );
72+ throw new NotFoundException ("{$ abstract } does not exists " );
7073 }
7174 return $ this ->instances [$ abstract ];
7275 }
7376
7477 /**
7578 * Auto setup, execute, or resolve any dependencies.
7679 *
77- * @param $abstract
80+ * @param $abstract
7881 * @param array $values
7982 *
8083 * @return mixed|null|object
81- * @throws Exception
84+ * @throws ContainerException
8285 */
8386 public function autoWire ($ abstract , $ values = [])
8487 {
@@ -92,12 +95,12 @@ public function autoWire($abstract, $values = [])
9295
9396 /**
9497 * Do we have dependence
95- * @param $abstract
96- * @return bool
97- */
98- public function has ($ abstract )
99- {
100- return isset ($ this ->instances [$ abstract ]);
98+ * @param $abstract
99+ * @return bool
100+ */
101+ public function has ($ abstract ): bool
102+ {
103+ return isset ($ this ->instances [$ abstract ]);
101104 }
102105
103106 /**
@@ -107,18 +110,18 @@ public function has($abstract)
107110 * @param $values
108111 *
109112 * @return mixed|object
110- * @throws Exception
113+ * @throws ContainerException
111114 */
112- public function resolve ($ concrete , $ values = [])
115+ protected function resolve ($ concrete , $ values = [])
113116 {
114117 if ($ concrete instanceof \Closure) {
115118 return $ concrete ($ this , $ values );
116119 }
117120
118- $ reflector = new ReflectionClass ($ concrete );
121+ $ reflector = new \ ReflectionClass ($ concrete );
119122 // check if class is instantiable
120123 if (!$ reflector ->isInstantiable ()) {
121- throw new \ Exception ("Class {$ concrete } is not instantiable " );
124+ throw new ContainerException ("Class {$ concrete } is not instantiable " );
122125 }
123126
124127 // get class constructor
@@ -129,7 +132,7 @@ public function resolve($concrete, $values = [])
129132 }
130133
131134 // get constructor params
132- $ parameters = $ constructor ->getParameters ();
135+ $ parameters = $ constructor ->getParameters ();
133136 $ dependencies = $ this ->getDependencies ($ parameters , $ values );
134137
135138 // get new instance with dependencies resolved
@@ -142,7 +145,7 @@ public function resolve($concrete, $values = [])
142145 * @param $parameters
143146 *
144147 * @return array
145- * @throws Exception
148+ * @throws ContainerException
146149 */
147150 public function getDependencies ($ parameters , $ values )
148151 {
@@ -152,21 +155,21 @@ public function getDependencies($parameters, $values)
152155 $ dependency = $ parameter ->getClass ();
153156 if ($ dependency === NULL ) {
154157 // check if the constructor parameter name exists as a key in the values array
155- if (array_key_exists ($ parameter ->getName (), $ values )) {
158+ if (\ array_key_exists ($ parameter ->getName (), $ values )) {
156159 // get default value of parameter
157160 $ dependencies [] = $ values [$ parameter ->getName ()];
158161 } else {
159162 // check if default value for a parameter is available
160163 if ($ parameter ->isDefaultValueAvailable ()) {
161- // get default value of parameter
162- $ dependencies [] = $ parameter ->getDefaultValue ();
164+ // get default value of parameter
165+ $ dependencies [] = $ parameter ->getDefaultValue ();
163166 } else {
164- throw new \ Exception ("Can not resolve class dependency {$ parameter ->name }" );
167+ throw new ContainerException ("Can not resolve class dependency {$ parameter ->name }" );
165168 }
166169 }
167170 } else {
168171 // get dependency resolved
169- $ dependencies [] = $ this ->get ($ dependency ->name );
172+ $ dependencies [] = $ this ->autoWire ($ dependency ->name , $ values );
170173 }
171174 }
172175
0 commit comments