2020/**
2121 * Class Router
2222 *
23- * @method mixed any($route, $settings, $callback = null)
24- * @method mixed get($route, $settings, $callback = null)
25- * @method mixed post($route, $settings, $callback = null)
26- * @method mixed put($route, $settings, $callback = null)
27- * @method mixed delete($route, $settings, $callback = null)
28- * @method mixed patch($route, $settings, $callback = null)
29- * @method mixed head($route, $settings, $callback = null)
30- * @method mixed options($route, $settings, $callback = null)
31- * @method mixed xpost($route, $settings, $callback = null)
32- * @method mixed xput($route, $settings, $callback = null)
33- * @method mixed xdelete($route, $settings, $callback = null)
34- * @method mixed xpatch($route, $settings, $callback = null)
23+ * @method $this any($route, $settings, $callback = null)
24+ * @method $this get($route, $settings, $callback = null)
25+ * @method $this post($route, $settings, $callback = null)
26+ * @method $this put($route, $settings, $callback = null)
27+ * @method $this delete($route, $settings, $callback = null)
28+ * @method $this patch($route, $settings, $callback = null)
29+ * @method $this head($route, $settings, $callback = null)
30+ * @method $this options($route, $settings, $callback = null)
31+ * @method $this xpost($route, $settings, $callback = null)
32+ * @method $this xput($route, $settings, $callback = null)
33+ * @method $this xdelete($route, $settings, $callback = null)
34+ * @method $this xpatch($route, $settings, $callback = null)
3535 *
3636 * @package Buki
3737 */
@@ -66,12 +66,6 @@ class Router
6666 * @var array $patterns Pattern definitions for parameters of Route
6767 */
6868 protected $ patterns = [
69- '{a} ' => '([^/]+) ' ,
70- '{d} ' => '(\d+) ' ,
71- '{i} ' => '(\d+) ' ,
72- '{s} ' => '(\w+) ' ,
73- '{u} ' => '([\w\-_]+) ' ,
74- '{*} ' => '(.*) ' ,
7569 ':id ' => '(\d+) ' ,
7670 ':number ' => '(\d+) ' ,
7771 ':any ' => '([^/]+) ' ,
@@ -102,7 +96,7 @@ class Router
10296 protected $ mainMethod = 'main ' ;
10397
10498 /**
105- * @var string $mainMethod Cache file
99+ * @var string $cacheFile Cache file
106100 */
107101 protected $ cacheFile = null ;
108102
@@ -116,14 +110,29 @@ class Router
116110 */
117111 protected $ errorCallback ;
118112
113+ /**
114+ * @var array $middlewares General middlewares for per request
115+ */
116+ protected $ middlewares = [];
117+
118+ /**
119+ * @var array $routeMiddlewares Route middlewares
120+ */
121+ protected $ routeMiddlewares = [];
122+
123+ /**
124+ * @var array $middlewareGroups Middleware Groups
125+ */
126+ protected $ middlewareGroups = [];
127+
119128 /**
120129 * Router constructor method.
121130 *
122131 * @param array $params
123132 *
124133 * @return void
125134 */
126- function __construct (array $ params = [])
135+ public function __construct (array $ params = [])
127136 {
128137 $ this ->documentRoot = realpath ($ _SERVER ['DOCUMENT_ROOT ' ]);
129138 $ this ->runningPath = realpath (getcwd ());
@@ -137,6 +146,123 @@ function __construct(array $params = [])
137146 $ this ->loadCache ();
138147 }
139148
149+ /**
150+ * [TODO] This method implementation not completed yet.
151+ *
152+ * Set route middleware
153+ *
154+ * @param string|array $middleware
155+ * @param string $type
156+ *
157+ * @return $this
158+ */
159+ public function middleware ($ middleware , $ type = 'before ' )
160+ {
161+ if (!is_array ($ middleware ) && !is_string ($ middleware )) {
162+ return $ this ;
163+ }
164+
165+ $ currentRoute = end ($ this ->routes );
166+ $ currentRoute [$ type ] = $ middleware ;
167+ array_pop ($ this ->routes );
168+ array_push ($ this ->routes , $ currentRoute );
169+
170+ return $ this ;
171+ }
172+
173+ /**
174+ * [TODO] This method implementation not completed yet.
175+ *
176+ * @param string|array $middleware
177+ *
178+ * @return $this
179+ */
180+ public function middlewareBefore ($ middleware )
181+ {
182+ $ this ->middleware ($ middleware , 'before ' );
183+
184+ return $ this ;
185+ }
186+
187+ /**
188+ * [TODO] This method implementation not completed yet.
189+ *
190+ * @param string|array $middleware
191+ *
192+ * @return $this
193+ */
194+ public function middlewareAfter ($ middleware )
195+ {
196+ $ this ->middleware ($ middleware , 'after ' );
197+
198+ return $ this ;
199+ }
200+
201+ /**
202+ * [TODO] This method implementation not completed yet.
203+ *
204+ * Set route name
205+ *
206+ * @param string $name
207+ *
208+ * @return $this
209+ */
210+ public function name ($ name )
211+ {
212+ if (!is_string ($ name )) {
213+ return $ this ;
214+ }
215+
216+ $ currentRoute = end ($ this ->routes );
217+ $ currentRoute ['name ' ] = $ name ;
218+ array_pop ($ this ->routes );
219+ array_push ($ this ->routes , $ currentRoute );
220+
221+ return $ this ;
222+ }
223+
224+ /**
225+ * [TODO] This method implementation not completed yet.
226+ *
227+ * Set general middlewares
228+ *
229+ * @param array $middlewares
230+ *
231+ * @return void
232+ */
233+ public function setMiddleware (array $ middlewares )
234+ {
235+ $ this ->middlewares = $ middlewares ;
236+ }
237+
238+ /**
239+ * [TODO] This method implementation not completed yet.
240+ *
241+ * Set Route middlewares
242+ *
243+ * @param array $middlewares
244+ *
245+ * @return void
246+ */
247+ public function setRouteMiddleware (array $ middlewares )
248+ {
249+ $ this ->routeMiddlewares = $ middlewares ;
250+ }
251+
252+ /**
253+ * [TODO] This method implementation not completed yet.
254+ *
255+ * Set middleware groups
256+ *
257+ * @param array $middlewareGroup
258+ *
259+ * @return void
260+ */
261+ public function setMiddlewareGroup (array $ middlewareGroup )
262+ {
263+ $ this ->middlewareGroups = $ middlewareGroup ;
264+ }
265+
140266 /**
141267 * Add route method;
142268 * Get, Post, Put, Delete, Patch, Any, Ajax...
@@ -170,7 +296,7 @@ public function __call($method, $params)
170296 $ callback = $ params [2 ];
171297 }
172298
173- if (strstr ($ route , ': ' ) || strstr ( $ route , ' { ' ) ) {
299+ if (strstr ($ route , ': ' )) {
174300 $ route1 = $ route2 = '' ;
175301 foreach (explode ('/ ' , $ route ) as $ key => $ value ) {
176302 if ($ value != '' ) {
@@ -195,7 +321,7 @@ public function __call($method, $params)
195321 $ this ->addRoute ($ route , $ method , $ callback , $ settings );
196322 }
197323
198- return true ;
324+ return $ this ;
199325 }
200326
201327 /**
@@ -276,10 +402,7 @@ public function run()
276402 $ uri = substr ($ uri , 0 , (strlen ($ uri ) - 1 ));
277403 }
278404
279- if ($ uri === '' ) {
280- $ uri = '/ ' ;
281- }
282-
405+ $ uri = $ this ->clearRouteName ($ uri );
283406 $ method = RouterRequest::getRequestMethod ();
284407 $ searches = array_keys ($ this ->patterns );
285408 $ replaces = array_values ($ this ->patterns );
@@ -302,7 +425,7 @@ public function run()
302425 } else {
303426 foreach ($ this ->routes as $ data ) {
304427 $ route = $ data ['route ' ];
305- if (strstr ($ route , ': ' ) !== false || strpos ( $ route , ' { ' ) !== false ) {
428+ if (strstr ($ route , ': ' ) !== false ) {
306429 $ route = str_replace ($ searches , $ replaces , $ route );
307430 }
308431
@@ -356,16 +479,15 @@ public function group($name, $settings = null, $callback = null)
356479 return true ;
357480 }
358481
359- $ groupName = trim ($ name , '/ ' );
360482 $ group = [];
361- $ group ['route ' ] = ' / ' . $ groupName ;
483+ $ group ['route ' ] = $ this -> clearRouteName ( $ name ) ;
362484 $ group ['before ' ] = $ group ['after ' ] = null ;
363485
364486 if (is_null ($ callback )) {
365487 $ callback = $ settings ;
366488 } else {
367- $ group ['before ' ][] = ( !isset ($ settings ['before ' ]) ? null : $ settings ['before ' ]) ;
368- $ group ['after ' ][] = ( !isset ($ settings ['after ' ]) ? null : $ settings ['after ' ]) ;
489+ $ group ['before ' ][] = !isset ($ settings ['before ' ]) ? null : $ settings ['before ' ];
490+ $ group ['after ' ][] = !isset ($ settings ['after ' ]) ? null : $ settings ['after ' ];
369491 }
370492
371493 $ groupCount = count ($ this ->groups );
@@ -434,7 +556,7 @@ public function controller($route, $settings, $controller = null)
434556 if ($ classMethods ) {
435557 foreach ($ classMethods as $ methodName ) {
436558 if (!strstr ($ methodName , '__ ' )) {
437- $ method = " any " ;
559+ $ method = ' any ' ;
438560 foreach (explode ('| ' , RouterRequest::$ validMethods ) as $ m ) {
439561 if (stripos ($ methodName , strtolower ($ m ), 0 ) === 0 ) {
440562 $ method = strtolower ($ m );
@@ -517,7 +639,7 @@ public function runRouteMiddleware($middleware, $type)
517639 */
518640 public function getList ()
519641 {
520- echo '<pre style="font-size:15px;" > ' ;
642+ echo '<pre> ' ;
521643 var_dump ($ this ->getRoutes ());
522644 echo '</pre> ' ;
523645 die;
@@ -708,22 +830,13 @@ private function addRoute($uri, $method, $callback, $settings)
708830 ))
709831 : null ;
710832 $ data = [
711- 'route ' => str_replace ( ' // ' , ' / ' , $ route ),
833+ 'route ' => $ this -> clearRouteName ( $ route ),
712834 'method ' => strtoupper ($ method ),
713835 'callback ' => $ callback ,
714- 'name ' => (isset ($ settings ['name ' ])
715- ? $ settings ['name ' ]
716- : $ routeName
717- ),
718- 'before ' => (isset ($ settings ['before ' ])
719- ? $ settings ['before ' ]
720- : null
721- ),
722- 'after ' => (isset ($ settings ['after ' ])
723- ? $ settings ['after ' ]
724- : null
725- ),
726- 'group ' => ($ groupItem === -1 ) ? null : $ this ->groups [$ groupItem ],
836+ 'name ' => isset ($ settings ['name ' ]) ? $ settings ['name ' ] : $ routeName ,
837+ 'before ' => isset ($ settings ['before ' ]) ? $ settings ['before ' ] : null ,
838+ 'after ' => isset ($ settings ['after ' ]) ? $ settings ['after ' ] : null ,
839+ 'group ' => $ groupItem === -1 ? null : $ this ->groups [$ groupItem ],
727840 ];
728841 array_push ($ this ->routes , $ data );
729842 }
@@ -750,4 +863,15 @@ private function endGroup()
750863 {
751864 array_pop ($ this ->groups );
752865 }
866+
867+ /**
868+ * @param string $route
869+ *
870+ * @return string
871+ */
872+ private function clearRouteName ($ route = '' )
873+ {
874+ $ route = trim (str_replace ('// ' , '/ ' , $ route ), '/ ' );
875+ return $ route === '' ? '/ ' : "/ {$ route }" ;
876+ }
753877}
0 commit comments