@@ -14,25 +14,29 @@ class Router
1414 * @var array
1515 */
1616 protected static array $ routes = [];
17+
1718 /**
1819 * Define the functions related to a http
1920 * code executed if this code is up
2021 *
2122 * @var array
2223 */
2324 protected array $ error_code = [];
25+
2426 /**
2527 * Define the global middleware
2628 *
2729 * @var array
2830 */
2931 protected array $ middlewares = [];
32+
3033 /**
3134 * Define the routing prefix
3235 *
3336 * @var string
3437 */
3538 protected string $ prefix = '' ;
39+
3640 /**
3741 * @var ?string
3842 */
@@ -59,13 +63,6 @@ class Router
5963 */
6064 private string $ base_route ;
6165
62- /**
63- * Define the request method
64- *
65- * @var string
66- */
67- private string $ method ;
68-
6966 /**
7067 * Define the request _method parse to form
7168 * for helper router define a good method called
@@ -74,26 +71,62 @@ class Router
7471 */
7572 private ?string $ magic_method ;
7673
74+ /**
75+ * Define the instance of router
76+ *
77+ * @var ?Router
78+ */
79+ private static ?Router $ instance = null ;
80+
7781 /**
7882 * Router constructor
7983 *
80- * @param string $method
8184 * @param ?string $magic_method
8285 * @param string $base_route
8386 * @param array $middlewares
8487 */
8588 protected function __construct (
86- string $ method ,
8789 ?string $ magic_method = null ,
8890 string $ base_route = '' ,
8991 array $ middlewares = []
9092 ) {
91- $ this ->method = $ method ;
9293 $ this ->magic_method = $ magic_method ;
9394 $ this ->middlewares = $ middlewares ;
9495 $ this ->base_route = $ base_route ;
9596 }
9697
98+ /**
99+ * Configure route singleton instance
100+ *
101+ * @param string|null $magic_method
102+ * @param string $base_route
103+ * @param array $middlewares
104+ * @return Router
105+ */
106+ public static function configure (
107+ ?string $ magic_method = null ,
108+ string $ base_route = '' ,
109+ array $ middlewares = []
110+ ): Router {
111+ static ::$ instance = new static ($ magic_method , $ base_route , $ middlewares );
112+
113+ return static ::$ instance ;
114+ }
115+
116+ /**
117+ * Get the instance of router
118+ *
119+ * @return ?Router
120+ */
121+ public static function getInstance (): ?Router
122+ {
123+ if (!static ::$ instance ) {
124+ static ::$ instance = new static ();
125+ }
126+
127+ return static ::$ instance ;
128+ }
129+
97130 /**
98131 * Set the base route
99132 *
@@ -178,7 +211,7 @@ public function route(array $definition): void
178211
179212 $ where = $ definition ['where ' ] ?? [];
180213
181- $ cb = (array )$ definition ['handler ' ];
214+ $ cb = (array ) $ definition ['handler ' ];
182215
183216 if (isset ($ cb ['middleware ' ])) {
184217 unset($ cb ['middleware ' ]);
@@ -207,7 +240,7 @@ public function route(array $definition): void
207240 */
208241 private function pushHttpVerb (string |array $ methods , string $ path , callable |string |array $ cb ): Route
209242 {
210- $ methods = (array )$ methods ;
243+ $ methods = (array ) $ methods ;
211244
212245 if (!$ this ->magic_method ) {
213246 return $ this ->routeLoader ($ methods , $ path , $ cb );
@@ -269,15 +302,15 @@ private function routeLoader(string|array $methods, string $path, callable|strin
269302 */
270303 public function middleware (array |string $ middlewares ): Router
271304 {
272- $ middlewares = (array )$ middlewares ;
305+ $ middlewares = (array ) $ middlewares ;
273306
274307 $ collection = [];
275308
276309 foreach ($ middlewares as $ middleware ) {
277310 $ collection [] = class_exists ($ middleware ) ? [new $ middleware (), 'process ' ] : $ middleware ;
278311 }
279312
280- return new Router ($ this ->method , $ this -> magic_method , $ this ->base_route , $ collection );
313+ return new Router ($ this ->magic_method , $ this ->base_route , $ collection );
281314 }
282315
283316 /**
0 commit comments