@@ -25,6 +25,7 @@ class ORouter extends AbstractRouter
2525{
2626 /** @var int */
2727 private $ routeCounter = 0 ;
28+ private $ cacheCounter = 0 ;
2829
2930 /** @var array global Options */
3031 private $ globalOptions = [
@@ -48,15 +49,16 @@ class ORouter extends AbstractRouter
4849 * @var array[]
4950 * [
5051 * '/user/login' => [
51- * [
52+ * // METHODS => [...] // 这里 key 和 value里的 'methods' 是一样的。仅是为了防止重复添加
53+ * 'GET,POST,' => [
5254 * 'handler' => 'handler',
5355 * 'methods' => 'GET,POST,',
54- * 'option' => null ,
56+ * 'option' => [...] ,
5557 * ],
56- * [
58+ * 'PUT,' => [
5759 * 'handler' => 'handler',
58- * 'methods' => 'GET,POST ,',
59- * 'option' => null ,
60+ * 'methods' => 'PUT ,',
61+ * 'option' => [...] ,
6062 * ],
6163 * ...
6264 * ]
@@ -77,7 +79,7 @@ class ORouter extends AbstractRouter
7779 * 'regex' => '/a/(\w+)',
7880 * 'methods' => 'GET,POST,',
7981 * 'handler' => 'handler',
80- * 'option' => null ,
82+ * 'option' => [...] ,
8183 * ],
8284 * ... ...
8385 * ],
@@ -87,17 +89,17 @@ class ORouter extends AbstractRouter
8789 * 'regex' => '/add/(\w+)',
8890 * 'methods' => 'GET,',
8991 * 'handler' => 'handler',
90- * 'option' => null ,
92+ * 'option' => [...] ,
9193 * ],
9294 * ... ...
9395 * ],
9496 * 'blog' => [
9597 * [
96- * 'start' => '/blog/',
97- * 'regex' => '/blog/(\w+)',
98+ * 'start' => '/blog/post- ',
99+ * 'regex' => '/blog/post- (\w+)',
98100 * 'methods' => 'GET,',
99101 * 'handler' => 'handler',
100- * 'option' => null ,
102+ * 'option' => [...] ,
101103 * ],
102104 * ... ...
103105 * ],
@@ -117,14 +119,14 @@ class ORouter extends AbstractRouter
117119 * 'regex' => '/(\w+)/profile',
118120 * 'methods' => 'GET,',
119121 * 'handler' => 'handler',
120- * 'option' => null ,
122+ * 'option' => [...] ,
121123 * ],
122124 * [
123125 * 'include' => null,
124126 * 'regex' => '/(\w+)/(\w+)',
125127 * 'methods' => 'GET,POST,',
126128 * 'handler' => 'handler',
127- * 'option' => null ,
129+ * 'option' => [...] ,
128130 * ],
129131 * ... ...
130132 * ]
@@ -192,6 +194,11 @@ public function __construct(array $config = [])
192194
193195 $ this ->currentGroupPrefix = '' ;
194196 $ this ->currentGroupOption = [];
197+
198+ // load routes
199+ if (($ file = $ this ->config ['routesFile ' ]) && is_file ($ file )) {
200+ require $ file ;
201+ }
195202 }
196203
197204 /**
@@ -209,11 +216,6 @@ public function setConfig(array $config)
209216 $ this ->config [$ name ] = $ value ;
210217 }
211218 }
212-
213- // load routes
214- if (($ file = $ this ->config ['routesFile ' ]) && is_file ($ file )) {
215- require $ file ;
216- }
217219 }
218220
219221 /*******************************************************************************
@@ -304,7 +306,7 @@ public function map($methods, $route, $handler, array $opts = [])
304306 $ this ->routeCounter ++;
305307 $ opts = array_replace ([
306308 'params ' => null ,
307- 'domains ' => null ,
309+ // 'domains' => null,
308310 ], $ this ->currentGroupOption , $ opts );
309311 $ conf = [
310312 'methods ' => $ methods ,
@@ -314,7 +316,7 @@ public function map($methods, $route, $handler, array $opts = [])
314316
315317 // no dynamic param params
316318 if (self ::isNoDynamicParam ($ route )) {
317- $ this ->staticRoutes [$ route ][] = $ conf ;
319+ $ this ->staticRoutes [$ route ][$ methods ] = $ conf ;
318320
319321 return $ this ;
320322 }
@@ -362,7 +364,6 @@ public function match($path, $method = self::GET)
362364 // clear '//', '///' => '/'
363365 $ path = rawurldecode (preg_replace ('#\/\/+# ' , '/ ' , $ path ));
364366 $ method = strtoupper ($ method );
365- $ number = $ this ->config ['tmpCacheNumber ' ];
366367
367368 // setting 'ignoreLastSep'
368369 if ($ path !== '/ ' && $ this ->config ['ignoreLastSep ' ]) {
@@ -380,27 +381,14 @@ public function match($path, $method = self::GET)
380381 }
381382
382383 $ first = self ::getFirstFromPath ($ path );
383- // is a regular dynamic route(the first char is 1th level index key).
384+
385+ // is a regular dynamic route(the first node is 1th level index key).
384386 if (isset ($ this ->regularRoutes [$ first ])) {
385387 foreach ($ this ->regularRoutes [$ first ] as $ conf ) {
386388 if (0 === strpos ($ path , $ conf ['start ' ]) && preg_match ($ conf ['regex ' ], $ path , $ matches )) {
387- // method not allowed
388- if (false === strpos ($ conf ['methods ' ], $ method . ', ' )) {
389- return [self ::METHOD_NOT_ALLOWED , $ path , explode (', ' , trim ($ conf ['methods ' ], ', ' ))];
390- }
391-
392- $ conf ['matches ' ] = self ::filterMatches ($ matches , $ conf );
393-
394- // cache latest $number routes.
395- if ($ number > 0 ) {
396- if (count ($ this ->routeCaches ) === $ number ) {
397- array_shift ($ this ->routeCaches );
398- }
389+ $ conf ['matches ' ] = $ matches ;
399390
400- $ this ->routeCaches [$ path ][] = $ conf ;
401- }
402-
403- return [self ::FOUND , $ path , $ conf ];
391+ return $ this ->checkMatched ($ path , $ method , $ conf );
404392 }
405393 }
406394 }
@@ -412,23 +400,9 @@ public function match($path, $method = self::GET)
412400 }
413401
414402 if (preg_match ($ conf ['regex ' ], $ path , $ matches )) {
415- // method not allowed
416- if (false === strpos ($ conf ['methods ' ], $ method . ', ' )) {
417- return [self ::METHOD_NOT_ALLOWED , $ path , explode (', ' , trim ($ conf ['methods ' ], ', ' ))];
418- }
419-
420- $ conf ['matches ' ] = self ::filterMatches ($ matches , $ conf );
421-
422- // cache last $number routes.
423- if ($ number > 0 ) {
424- if (count ($ this ->routeCaches ) === $ number ) {
425- array_shift ($ this ->routeCaches );
426- }
403+ $ conf ['matches ' ] = $ matches ;
427404
428- $ this ->routeCaches [$ path ][] = $ conf ;
429- }
430-
431- return [self ::FOUND , $ path , $ conf ];
405+ return $ this ->checkMatched ($ path , $ method , $ conf );
432406 }
433407 }
434408
@@ -481,6 +455,40 @@ public function dispatch($dispatcher = null, $path = null, $method = null)
481455 * helper methods
482456 ******************************************************************************/
483457
458+ /**
459+ * checkMatched
460+ * @param string $path
461+ * @param string $method
462+ * @param array $conf
463+ * @return array
464+ */
465+ protected function checkMatched ($ path , $ method , array $ conf )
466+ {
467+ $ methods = $ conf ['methods ' ];
468+ $ cacheNumber = (int )$ this ->config ['tmpCacheNumber ' ];
469+
470+ // method not allowed
471+ if (false === strpos ($ methods , $ method . ', ' )) {
472+ return [self ::METHOD_NOT_ALLOWED , $ path , explode (', ' , trim ($ methods , ', ' ))];
473+ }
474+
475+ $ conf ['matches ' ] = self ::filterMatches ($ conf ['matches ' ], $ conf );
476+
477+ // cache last $cacheNumber routes.
478+ if ($ cacheNumber > 0 ) {
479+ if ($ this ->cacheCounter === $ cacheNumber ) {
480+ array_shift ($ this ->routeCaches );
481+ }
482+
483+ if (!isset ($ this ->routeCaches [$ path ][$ methods ])) {
484+ $ this ->cacheCounter ++;
485+ $ this ->routeCaches [$ path ][$ methods ] = $ conf ;
486+ }
487+ }
488+
489+ return [self ::FOUND , $ path , $ conf ];
490+ }
491+
484492 /**
485493 * @return int
486494 */
0 commit comments