1515
1616class RouterCommand
1717{
18+ /**
19+ * Class instance variable
20+ */
21+ protected static $ instance = null ;
1822
19- public static function exception ($ message = '' )
23+ /**
24+ * Get class instance
25+ *
26+ * @return PdoxObject
27+ */
28+ public static function getInstance ()
29+ {
30+ if (null === self ::$ instance )
31+ self ::$ instance = new static ();
32+
33+ return self ::$ instance ;
34+ }
35+
36+ /**
37+ * Throw new Exception for Router Error
38+ *
39+ * @return RouterException
40+ */
41+ public function exception ($ message = '' )
2042 {
2143 return new RouterException ($ message );
2244 }
@@ -26,13 +48,13 @@ public static function exception($message = '')
2648 *
2749 * @return true | false
2850 */
29- public static function beforeAfter ($ command , $ middleware , $ path = '' , $ namespace = '' )
51+ public function beforeAfter ($ command , $ middleware , $ path = '' , $ namespace = '' )
3052 {
3153 if (!is_null ($ command ))
3254 {
3355 if (is_array ($ command ))
3456 foreach ($ command as $ key => $ value )
35- self :: beforeAfter ($ value , $ middleware );
57+ $ this -> beforeAfter ($ value , $ middleware );
3658
3759 elseif (is_object ($ command ))
3860 return call_user_func ($ command );
@@ -48,20 +70,20 @@ public static function beforeAfter($command, $middleware, $path = '', $namespace
4870 $ middlewareFile = realpath ($ path . $ parts [0 ] . '/ ' . (str_replace ($ namespace , '' , $ segments [0 ])) .'.php ' );
4971
5072 if (!file_exists ($ middlewareFile ))
51- return self :: exception ($ segments [0 ] . ' middleware file is not found. Please, check file. ' );
73+ return $ this -> exception ($ segments [0 ] . ' middleware file is not found. Please, check file. ' );
5274
5375 require_once ($ middlewareFile );
5476 $ controller = new $ segments [0 ]();
5577
5678 if (in_array ($ segments [1 ], get_class_methods ($ controller )))
5779 return call_user_func ([$ controller , $ segments [1 ]]);
5880 else
59- return self :: exception ($ segments [1 ] . ' method is not found in <b> ' .$ segments [0 ].'</b> middleware. Please, check file. ' );
81+ return $ this -> exception ($ segments [1 ] . ' method is not found in <b> ' .$ segments [0 ].'</b> middleware. Please, check file. ' );
6082 }
6183 else
6284 {
6385 if (!is_null ($ middleware [$ command ]) && isset ($ middleware [$ command ]))
64- self :: beforeAfter ($ middleware [$ command ], $ middleware );
86+ $ this -> beforeAfter ($ middleware [$ command ], $ middleware );
6587 else
6688 return false ;
6789 }
@@ -75,7 +97,7 @@ public static function beforeAfter($command, $middleware, $path = '', $namespace
7597 *
7698 * @return null
7799 */
78- public static function runRoute ($ command , $ params = null , $ path = '' , $ namespace = '' )
100+ public function runRoute ($ command , $ params = null , $ path = '' , $ namespace = '' )
79101 {
80102 if (!is_object ($ command ))
81103 {
@@ -88,7 +110,7 @@ public static function runRoute($command, $params = null, $path = '', $namespace
88110 $ controllerFile = realpath ($ path . $ parts [0 ] . '/ ' . ($ segments [0 ]).'.php ' );
89111
90112 if (!file_exists ($ controllerFile ))
91- return self :: exception ($ segments [0 ] . ' Controller File is not found. Please, check file. ' );
113+ return $ this -> exception ($ segments [0 ] . ' Controller File is not found. Please, check file. ' );
92114
93115 require_once ($ controllerFile );
94116 $ controller = new $ segments [0 ]();
@@ -98,7 +120,7 @@ public static function runRoute($command, $params = null, $path = '', $namespace
98120 elseif (is_null ($ params ) && in_array ($ segments [1 ], get_class_methods ($ controller )))
99121 echo call_user_func ([$ controller , $ segments [1 ]]);
100122 else
101- return self :: exception ($ segments [1 ] . ' method is not found in ' .$ segments [0 ].' controller. Please, check file. ' );
123+ return $ this -> exception ($ segments [1 ] . ' method is not found in ' .$ segments [0 ].' controller. Please, check file. ' );
102124 }
103125 else
104126 if (!is_null ($ params ))
0 commit comments