20
20
/**
21
21
* Class Router
22
22
*
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)
35
35
*
36
36
* @package Buki
37
37
*/
@@ -66,12 +66,6 @@ class Router
66
66
* @var array $patterns Pattern definitions for parameters of Route
67
67
*/
68
68
protected $ patterns = [
69
- '{a} ' => '([^/]+) ' ,
70
- '{d} ' => '(\d+) ' ,
71
- '{i} ' => '(\d+) ' ,
72
- '{s} ' => '(\w+) ' ,
73
- '{u} ' => '([\w\-_]+) ' ,
74
- '{*} ' => '(.*) ' ,
75
69
':id ' => '(\d+) ' ,
76
70
':number ' => '(\d+) ' ,
77
71
':any ' => '([^/]+) ' ,
@@ -102,7 +96,7 @@ class Router
102
96
protected $ mainMethod = 'main ' ;
103
97
104
98
/**
105
- * @var string $mainMethod Cache file
99
+ * @var string $cacheFile Cache file
106
100
*/
107
101
protected $ cacheFile = null ;
108
102
@@ -116,14 +110,29 @@ class Router
116
110
*/
117
111
protected $ errorCallback ;
118
112
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
+
119
128
/**
120
129
* Router constructor method.
121
130
*
122
131
* @param array $params
123
132
*
124
133
* @return void
125
134
*/
126
- function __construct (array $ params = [])
135
+ public function __construct (array $ params = [])
127
136
{
128
137
$ this ->documentRoot = realpath ($ _SERVER ['DOCUMENT_ROOT ' ]);
129
138
$ this ->runningPath = realpath (getcwd ());
@@ -137,6 +146,123 @@ function __construct(array $params = [])
137
146
$ this ->loadCache ();
138
147
}
139
148
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
+
140
266
/**
141
267
* Add route method;
142
268
* Get, Post, Put, Delete, Patch, Any, Ajax...
@@ -170,7 +296,7 @@ public function __call($method, $params)
170
296
$ callback = $ params [2 ];
171
297
}
172
298
173
- if (strstr ($ route , ': ' ) || strstr ( $ route , ' { ' ) ) {
299
+ if (strstr ($ route , ': ' )) {
174
300
$ route1 = $ route2 = '' ;
175
301
foreach (explode ('/ ' , $ route ) as $ key => $ value ) {
176
302
if ($ value != '' ) {
@@ -195,7 +321,7 @@ public function __call($method, $params)
195
321
$ this ->addRoute ($ route , $ method , $ callback , $ settings );
196
322
}
197
323
198
- return true ;
324
+ return $ this ;
199
325
}
200
326
201
327
/**
@@ -276,10 +402,7 @@ public function run()
276
402
$ uri = substr ($ uri , 0 , (strlen ($ uri ) - 1 ));
277
403
}
278
404
279
- if ($ uri === '' ) {
280
- $ uri = '/ ' ;
281
- }
282
-
405
+ $ uri = $ this ->clearRouteName ($ uri );
283
406
$ method = RouterRequest::getRequestMethod ();
284
407
$ searches = array_keys ($ this ->patterns );
285
408
$ replaces = array_values ($ this ->patterns );
@@ -302,7 +425,7 @@ public function run()
302
425
} else {
303
426
foreach ($ this ->routes as $ data ) {
304
427
$ route = $ data ['route ' ];
305
- if (strstr ($ route , ': ' ) !== false || strpos ( $ route , ' { ' ) !== false ) {
428
+ if (strstr ($ route , ': ' ) !== false ) {
306
429
$ route = str_replace ($ searches , $ replaces , $ route );
307
430
}
308
431
@@ -356,16 +479,15 @@ public function group($name, $settings = null, $callback = null)
356
479
return true ;
357
480
}
358
481
359
- $ groupName = trim ($ name , '/ ' );
360
482
$ group = [];
361
- $ group ['route ' ] = ' / ' . $ groupName ;
483
+ $ group ['route ' ] = $ this -> clearRouteName ( $ name ) ;
362
484
$ group ['before ' ] = $ group ['after ' ] = null ;
363
485
364
486
if (is_null ($ callback )) {
365
487
$ callback = $ settings ;
366
488
} 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 ' ];
369
491
}
370
492
371
493
$ groupCount = count ($ this ->groups );
@@ -434,7 +556,7 @@ public function controller($route, $settings, $controller = null)
434
556
if ($ classMethods ) {
435
557
foreach ($ classMethods as $ methodName ) {
436
558
if (!strstr ($ methodName , '__ ' )) {
437
- $ method = " any " ;
559
+ $ method = ' any ' ;
438
560
foreach (explode ('| ' , RouterRequest::$ validMethods ) as $ m ) {
439
561
if (stripos ($ methodName , strtolower ($ m ), 0 ) === 0 ) {
440
562
$ method = strtolower ($ m );
@@ -517,7 +639,7 @@ public function runRouteMiddleware($middleware, $type)
517
639
*/
518
640
public function getList ()
519
641
{
520
- echo '<pre style="font-size:15px;" > ' ;
642
+ echo '<pre> ' ;
521
643
var_dump ($ this ->getRoutes ());
522
644
echo '</pre> ' ;
523
645
die;
@@ -708,22 +830,13 @@ private function addRoute($uri, $method, $callback, $settings)
708
830
))
709
831
: null ;
710
832
$ data = [
711
- 'route ' => str_replace ( ' // ' , ' / ' , $ route ),
833
+ 'route ' => $ this -> clearRouteName ( $ route ),
712
834
'method ' => strtoupper ($ method ),
713
835
'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 ],
727
840
];
728
841
array_push ($ this ->routes , $ data );
729
842
}
@@ -750,4 +863,15 @@ private function endGroup()
750
863
{
751
864
array_pop ($ this ->groups );
752
865
}
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
+ }
753
877
}
0 commit comments