Skip to content

Commit f2bf113

Browse files
committed
update:
static/cache routes storage structure modify. dynamic route match modify fixed: bug fixed for cache route
1 parent 897ec43 commit f2bf113

File tree

8 files changed

+99
-76
lines changed

8 files changed

+99
-76
lines changed

src/AbstractRouter.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ protected static function filterMatches(array $matches, array $conf)
115115
}
116116

117117
// decode ...
118-
// foreach ($matches as $k => $v) {
119-
// $matches[$k] = urldecode($v);
120-
// }
118+
// foreach ($matches as $k => $v) {
119+
// $matches[$k] = urldecode($v);
120+
// }
121121

122122
return $matches;
123123
}
@@ -149,7 +149,7 @@ public static function parseParamRoute($route, array $params, array $conf)
149149
}
150150

151151
// quote '.','/' to '\.','\/'
152-
// $route = preg_quote($route, '/');
152+
// $route = preg_quote($route, '/');
153153
$route = str_replace('.', '\.', $route);
154154

155155
// 解析参数,替换为对应的 正则
@@ -202,6 +202,19 @@ public static function parseParamRoute($route, array $params, array $conf)
202202

203203
/**
204204
* @param array $routes
205+
* [
206+
* 'GET,POST,' => [
207+
* 'handler' => 'handler',
208+
* 'methods' => 'GET,POST,',
209+
* 'option' => [...],
210+
* ],
211+
* 'PUT,' => [
212+
* 'handler' => 'handler',
213+
* 'methods' => 'PUT,',
214+
* 'option' => [...],
215+
* ],
216+
* ...
217+
* ]
205218
* @param string $path
206219
* @param string $method
207220
* @return array

src/CachedRouter.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
namespace Inhere\Route;
1010

1111
/**
12-
* Class CachedRouter - this is object version.
12+
* Class CachedRouter - this is object version and support cache routes.
1313
*
1414
* - 支持缓存路由信息到文件
15-
* - handler 将不支持设置为 \Closure 无法缓存 \Closure)
15+
* - handler 将不支持设置为 \Closure (无法缓存 \Closure)
1616
* - 路由选项的 选项值 同样不允许 \Closure
1717
*
1818
* @package Inhere\Route
@@ -137,14 +137,17 @@ public function dumpRoutesCache()
137137
$code = <<<EOF
138138
<?php
139139
/*
140-
* This inhere/sroute routes cache file.
140+
* This `inhere/sroute` routes cache file.
141141
* It is auto generate by $class.
142142
* @date $date
143-
* @notice Please don't change it.
143+
* @notice Please don't edit it.
144144
*/
145145
return [
146+
// static routes
146147
'staticRoutes' => $staticRoutes,
148+
// regular routes
147149
'regularRoutes' => $regularRoutes,
150+
// vague routes
148151
'vagueRoutes' => $vagueRoutes,
149152
];
150153
EOF;

src/Dispatcher.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
*/
1515
class Dispatcher implements DispatcherInterface
1616
{
17-
const FAV_ICON = '/favicon.ico';
18-
1917
/**
2018
* event handlers
2119
* @var array
@@ -43,7 +41,7 @@ class Dispatcher implements DispatcherInterface
4341
// $router->any('/demo/{act}', app\controllers\Demo::class);
4442
// you access '/demo/test' will call 'app\controllers\Demo::test()'
4543
'dynamicAction' => false,
46-
// @see ORouter::$globalTokens['act']
44+
// @see ORouter::$globalParams['act']
4745
'dynamicActionVar' => 'act',
4846

4947
// action executor. will auto call controller's executor method to run all action.

src/DispatcherInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
interface DispatcherInterface
1616
{
17+
const FAV_ICON = '/favicon.ico';
18+
1719
// events
1820
const ON_FOUND = 'found';
1921
const ON_NOT_FOUND = 'notFound';

src/ORouter.php

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/

src/RouterInterface.php

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,15 @@
1414
*/
1515
interface RouterInterface
1616
{
17-
const ANY_METHOD = 'ANY';
18-
1917
// match result status
2018
const FOUND = 1;
2119
const NOT_FOUND = 2;
2220
const METHOD_NOT_ALLOWED = 3;
2321

2422
const DEFAULT_REGEX = '[^/]+';
2523

26-
/**
27-
* supported Methods
28-
* @var array
29-
*/
30-
const SUPPORTED_METHODS = [
31-
'ANY',
32-
'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD', 'SEARCH', 'CONNECT', 'TRACE',
33-
];
24+
// supported method list
25+
const ANY = 'ANY';
3426

3527
const GET = 'GET';
3628
const POST = 'POST';
@@ -43,7 +35,14 @@ interface RouterInterface
4335
const CONNECT = 'CONNECT';
4436
const TRACE = 'TRACE';
4537

46-
const ANY = 'ANY';
38+
/**
39+
* supported methods
40+
* @var array
41+
*/
42+
const SUPPORTED_METHODS = [
43+
'ANY',
44+
'GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS', 'HEAD', 'SEARCH', 'CONNECT', 'TRACE',
45+
];
4746

4847
/**
4948
* the matched result index key

src/SRouter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public static function map($methods, $route, $handler, array $opts = [])
188188
self::$routeCounter++;
189189
$opts = array_replace([
190190
'params' => null,
191-
'domains' => null,
191+
// 'domains' => null,
192192
], self::$currentGroupOption, $opts);
193193
$conf = [
194194
'methods' => $methods,

tests/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)