44
55namespace MaplePHP \Container ;
66
7+ use Exception ;
78use ReflectionClass ;
9+ use ReflectionException ;
810use ReflectionMethod ;
911use MaplePHP \Container \Exceptions \NotFoundException ;
1012
1113class Reflection
1214{
13- private $ method ;
14- private $ reflect ;
15- private $ args ;
16- private $ allowInterfaces = true ;
17- private $ dependMethod = null ;
18- private static $ class = array ();
19- private static $ interfaceFactory ;
15+ private ?string $ method = null ;
16+ private ReflectionClass $ reflect ;
17+ private ?array $ args = null ;
18+ private bool $ allowInterfaces = true ;
19+ private ?string $ dependMethod = null ;
20+ private static array $ class = array ();
21+ private static ?array $ interfaceFactory = null ;
22+ //private static array $attr = [];
2023 //private static $interfaceProtocol;
2124
2225 /**
23- * Start relection of a class or method
26+ * Start reflection of a class or method
2427 * @param class-string|object $classData
28+ * @throws ReflectionException
2529 */
2630 public function __construct (string |object $ classData )
2731 {
@@ -31,7 +35,7 @@ public function __construct(string|object $classData)
3135 $ this ->method = substr ($ classData , $ pos + 2 );
3236 }
3337 if (!class_exists ($ classData )) {
34- throw new NotFoundException ("Could not find the class \"{ $ classData} \". " , 1 );
38+ throw new NotFoundException ("Could not find the class \"$ classData \". " , 1 );
3539 }
3640 }
3741 $ this ->reflect = new ReflectionClass ($ classData );
@@ -64,6 +68,7 @@ public function allowInterfaces(bool $bool): void
6468 /**
6569 * Call dependency injector
6670 * @return object
71+ * @throws ReflectionException|Exception
6772 */
6873 public function dependencyInjector (?object $ class = null , ?string $ method = null ): mixed
6974 {
@@ -88,7 +93,14 @@ public function dependencyInjector(?object $class = null, ?string $method = null
8893 return $ this ->reflect ->newInstanceArgs ($ args );
8994 }
9095
91- function setDependMethod (?string $ method , ReflectionClass $ inst )
96+ /**
97+ * Set dependent method
98+ * @param string|null $method
99+ * @param ReflectionClass $inst
100+ * @return ReflectionMethod|null
101+ * @throws ReflectionException
102+ */
103+ function setDependMethod (?string $ method , ReflectionClass $ inst ): ?ReflectionMethod
92104 {
93105 $ method = ($ method === "constructor " ) ? null : $ method ;
94106 $ this ->dependMethod = $ method ;
@@ -100,29 +112,31 @@ function setDependMethod(?string $method, ReflectionClass $inst)
100112
101113 /**
102114 * This will return reflection if class exist or error pointing to file where error existed,
103- * @param class-string $className
104- * @param class-string $fromClass
115+ * @param class-string $className
116+ * @param class-string $fromClass
105117 * @return ReflectionClass
118+ * @throws Exception
106119 */
107120 private function initReclusiveReflect (string $ className , string $ fromClass ): ReflectionClass
108121 {
109122 try {
110123 return new ReflectionClass ($ className );
111- } catch (\ Exception $ e ) {
124+ } catch (Exception $ e ) {
112125 if (!class_exists ($ className )) {
113126 throw new NotFoundException ('Class " ' . $ className . '" does not exist in the class " ' . $ fromClass . '". ' , 1 );
114127 } else {
115- throw new \ Exception ($ e ->getMessage () . '. You might want to check the file ' . $ fromClass . '. ' , 1 );
128+ throw new Exception ($ e ->getMessage () . '. You might want to check the file ' . $ fromClass . '. ' , 1 );
116129 }
117130 }
118131 }
119132
120133 /**
121- * Recursion inject dependancies
122- * @param array $params
123- * @param class-string $fromClass
124- * @param array $_args
134+ * Recursion inject dependencies
135+ * @param array $params
136+ * @param class-string $fromClass
137+ * @param array $_args
125138 * @return array
139+ * @throws Exception
126140 */
127141 private function injectRecursion (array $ params , string $ fromClass , array $ _args = array ()): array
128142 {
@@ -141,7 +155,7 @@ private function injectRecursion(array $params, string $fromClass, array $_args
141155 if (count ($ reflectParam ) > 0 ) {
142156 $ _args = $ this ->injectRecursion ($ reflectParam , $ inst ->getName (), $ _args );
143157
144- // Will make it posible to set same instance in multiple nested classes
158+ // Will make it possible to set same instance in multiple nested classes
145159 $ _args = $ this ->insertMultipleNestedClasses ($ inst , $ constructor , $ classNameA , $ reflectParam );
146160 } else {
147161 if ($ inst ->isInterface ()) {
@@ -159,7 +173,7 @@ private function injectRecursion(array $params, string $fromClass, array $_args
159173 }
160174
161175 /**
162- * Will insert interface classes (the defualt classes)
176+ * Will insert interface classes (the default classes)
163177 * @param ReflectionClass $inst
164178 * @param string $classNameA
165179 * @return void
@@ -178,12 +192,13 @@ private function insertInterfaceClasses(ReflectionClass $inst, string $className
178192 }
179193
180194 /**
181- * Will make it posible to set same instance in multiple nested classes
182- * @param ReflectionClass $inst
183- * @param ReflectionMethod|null $constructor
184- * @param string $classNameA
185- * @param array $reflectParam
195+ * Will make it possible to set same instance in multiple nested classes
196+ * @param ReflectionClass $inst
197+ * @param ReflectionMethod|null $constructor
198+ * @param string $classNameA
199+ * @param array $reflectParam
186200 * @return array
201+ * @throws ReflectionException
187202 */
188203 private function insertMultipleNestedClasses (
189204 ReflectionClass $ inst ,
@@ -207,13 +222,14 @@ private function insertMultipleNestedClasses(
207222 }
208223
209224 /**
210- * Create a instance from reflection
211- * @param ReflectionClass $inst
212- * @param bool $hasCon
213- * @param array $args
225+ * Create an instance from reflection
226+ * @param ReflectionClass $inst
227+ * @param bool $hasCon
228+ * @param array $args
214229 * @return object
230+ * @throws ReflectionException
215231 */
216- public function newInstance (ReflectionClass $ inst , bool $ hasCon , array $ args )
232+ public function newInstance (ReflectionClass $ inst , bool $ hasCon , array $ args ): object
217233 {
218234 if ($ hasCon ) {
219235 return $ inst ->newInstanceArgs ($ args );
@@ -222,7 +238,7 @@ public function newInstance(ReflectionClass $inst, bool $hasCon, array $args)
222238 }
223239
224240 /**
225- * Set argumnets to constructor or method (depending on how $data in new Reflection($data) is defined).
241+ * Set arguments to constructor or method (depending on how $data in new Reflection($data) is defined).
226242 * IF method is set then method arguments will be passed, (the method will be treated as a static method)
227243 * @param array $array [description]
228244 */
@@ -244,16 +260,18 @@ public function getReflect(): ReflectionClass
244260 /**
245261 * Get the loaded container data
246262 * @return mixed
263+ * @throws ReflectionException
264+ * @throws Exception
247265 */
248- public function get ()
266+ public function get (): mixed
249267 {
250268 if (!is_null ($ this ->method )) {
251269 $ method = $ this ->reflect ->getMethod ($ this ->method );
252270 if ($ method ->isConstructor ()) {
253271 return $ this ->getClass ();
254272 }
255273 if ($ method ->isDestructor ()) {
256- throw new \ Exception ("You can not set a Destructor as a container " , 1 );
274+ throw new Exception ("You can not set a Destructor as a container " , 1 );
257275 }
258276 $ inst = $ this ->reflect ->newInstanceWithoutConstructor ();
259277
@@ -266,14 +284,15 @@ public function get()
266284 return $ this ->getClass ();
267285 }
268286
269- public static function getClassList ()
287+ public static function getClassList (): array
270288 {
271289 return self ::$ class ;
272290 }
273291
274292 /**
275293 * Load dependencyInjector / or just a container
276294 * @return object
295+ * @throws ReflectionException
277296 */
278297 private function getClass (): object
279298 {
@@ -284,4 +303,23 @@ private function getClass(): object
284303 }
285304 return $ inst ;
286305 }
306+
307+
308+ /*
309+ // Possible attribute snippet in working progress
310+ function propagateAttr($reflectionClass, $class) {
311+ foreach ($reflectionClass->getMethods() as $method) {
312+ $attributes = $method->getAttributes(ListensTo::class);
313+
314+ foreach ($attributes as $attribute) {
315+ $listener = $attribute->newInstance();
316+ $name = $method->getName();
317+ self::$attr[$name] = [
318+ $listener,
319+ $name
320+ ];
321+ }
322+ }
323+ }
324+ */
287325}
0 commit comments