Skip to content

Commit f938509

Browse files
committed
router config modify, change to property
1 parent 1ce41a1 commit f938509

File tree

8 files changed

+115
-96
lines changed

8 files changed

+115
-96
lines changed

docs/router.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,9 @@ $router->setConfig([
345345

346346
```php
347347
// 所有的默认的配置
348-
[
348+
349349
// 是否忽略最后的 '/' 分隔符. 如果是 true,将清除最后一个 '/', 此时请求 '/home' 和 '/home/' 效果相同
350-
'ignoreLastSep' => false,
350+
'ignoreLastSep' = false,
351351

352352
// 匹配所有请求
353353
// 1. 如果是一个有效的URI路径,将匹配所有请求到此URI路径。
@@ -360,7 +360,7 @@ $router->setConfig([
360360
'controllerNamespace' => '', // eg: 'app\\controllers'
361361
// 控制器类后缀
362362
'controllerSuffix' => '', // eg: 'Controller'
363-
]
363+
364364
```
365365

366366
> NOTICE: 必须在添加路由之前调用 `$router->setConfig()`

examples/cached.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
require __DIR__ . '/simple-loader.php';
1919

2020
$router = new CachedRouter([
21-
// 'ignoreLastSep' => true,
21+
// 'ignoreLastSlash' => true,
2222
// 'tmpCacheNumber' => 100,
2323

2424
// 'cacheFile' => '',

examples/object.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
// set config
2323
$router->setConfig([
24-
// 'ignoreLastSep' => true,
24+
// 'ignoreLastSlash' => true,
2525
// 'tmpCacheNumber' => 100,
2626

2727
// 'matchAll' => '/', // a route path

examples/static.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
// set config
2020
SRouter::setConfig([
21-
'ignoreLastSep' => true,
21+
'ignoreLastSlash' => true,
2222

2323
// 'matchAll' => '/', // a route path
2424
// 'matchAll' => function () {

examples/swoole_svr.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
// set config
2222
$router->setConfig([
23-
'ignoreLastSep' => true,
23+
'ignoreLastSlash' => true,
2424
'dynamicAction' => true,
2525

2626
'tmpCacheNumber' => 100,

src/AbstractRouter.php

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
/**
1212
* Class AbstractRouter
1313
* @package Inhere\Route
14-
*
1514
* @method get(string $route, mixed $handler, array $opts = [])
1615
* @method post(string $route, mixed $handler, array $opts = [])
1716
* @method put(string $route, mixed $handler, array $opts = [])
@@ -35,7 +34,7 @@ abstract class AbstractRouter implements RouterInterface
3534
'any' => '[^/]+', // match any except '/'
3635
'num' => '[0-9]+', // match a number
3736
'int' => '\d+', // match a number
38-
'id' => '[1-9][0-9]*', // match a ID number
37+
'id' => '[1-9][0-9]*', // match a ID number
3938
'act' => '[a-zA-Z][\w-]+', // match a action name
4039
];
4140

@@ -48,45 +47,13 @@ abstract class AbstractRouter implements RouterInterface
4847
/** @var array */
4948
protected $currentGroupOption;
5049

51-
/**
52-
* some setting for self
53-
* @var array
54-
*/
55-
protected $config = [
56-
// the routes php file.
57-
'routesFile' => '',
58-
59-
// ignore last '/' char. If is True, will clear last '/'.
60-
'ignoreLastSep' => false,
61-
62-
// 'tmpCacheNumber' => 100,
63-
'tmpCacheNumber' => 0,
64-
65-
// notAllowed As NotFound. Now, only two status value will be return(FOUND, NOT_FOUND).
66-
'notAllowedAsNotFound' => false,
67-
68-
// match all request.
69-
// 1. If is a valid URI path, will matchAll all request uri to the path.
70-
// 2. If is a closure, will matchAll all request then call it
71-
// eg: '/site/maintenance' or `function () { echo 'System Maintaining ... ...'; }`
72-
'matchAll' => false,
73-
74-
// auto route match @like yii framework
75-
// If is True, will auto find the handler controller file.
76-
'autoRoute' => false,
77-
// The default controllers namespace, is valid when `'enable' = true`
78-
'controllerNamespace' => '', // eg: 'app\\controllers'
79-
// controller suffix, is valid when `'enable' = true`
80-
'controllerSuffix' => '', // eg: 'Controller'
81-
];
82-
8350
/**
8451
* static Routes - no dynamic argument match
8552
* 整个路由 path 都是静态字符串 e.g. '/user/login'
8653
* @var array[]
8754
* [
8855
* '/user/login' => [
89-
* // METHOD => [...] // 这里 key 和 value里的 'methods' 是一样的
56+
* // METHOD => [...]
9057
* 'GET' => [
9158
* 'handler' => 'handler',
9259
* 'option' => [...],
@@ -109,16 +76,6 @@ abstract class AbstractRouter implements RouterInterface
10976
* @var array[]
11077
* [
11178
* // 使用完整的第一节作为key进行分组
112-
* 'a' => [
113-
* [
114-
* 'start' => '/a/',
115-
* 'regex' => '/a/(\w+)',
116-
* 'methods' => 'GET,POST',
117-
* 'handler' => 'handler',
118-
* 'option' => [...],
119-
* ],
120-
* ...
121-
* ],
12279
* 'add' => [
12380
* [
12481
* 'start' => '/add/',
@@ -182,6 +139,61 @@ abstract class AbstractRouter implements RouterInterface
182139
*/
183140
protected $routeCaches = [];
184141

142+
/*******************************************************************************
143+
* router config
144+
******************************************************************************/
145+
146+
/**
147+
* Setting a routes file.
148+
* @var string
149+
*/
150+
public $routesFile;
151+
152+
/**
153+
* Ignore last slash char('/'). If is True, will clear last '/'.
154+
* @var bool
155+
*/
156+
public $ignoreLastSlash = false;
157+
158+
/**
159+
* The param route cache number.
160+
* @var int
161+
*/
162+
public $tmpCacheNumber = 0;
163+
164+
/**
165+
* Match all request.
166+
* 1. If is a valid URI path, will matchAll all request uri to the path.
167+
* 2. If is a closure, will matchAll all request then call it
168+
* eg: '/site/maintenance' or `function () { echo 'System Maintaining ... ...'; }`
169+
* @var mixed
170+
*/
171+
public $matchAll;
172+
173+
/**
174+
* @var bool NotAllowed As NotFound. If True, only two status value will be return(FOUND, NOT_FOUND).
175+
*/
176+
public $notAllowedAsNotFound = false;
177+
178+
/**
179+
* Auto route match @like yii framework
180+
* If is True, will auto find the handler controller file.
181+
* @var bool
182+
*/
183+
public $autoRoute = false;
184+
185+
/**
186+
* The default controllers namespace. eg: 'App\\Controllers'
187+
* @var string
188+
*/
189+
public $controllerNamespace;
190+
191+
/**
192+
* Controller suffix, is valid when '$autoRoute' = true. eg: 'Controller'
193+
* @var string
194+
*/
195+
public $controllerSuffix;
196+
185197
/**
186198
* object creator.
187199
* @param array $config
@@ -206,7 +218,7 @@ public function __construct(array $config = [])
206218
$this->currentGroupOption = [];
207219

208220
// load routes
209-
if (($file = $this->config['routesFile']) && is_file($file)) {
221+
if (($file = $this->routesFile) && is_file($file)) {
210222
require $file;
211223
}
212224
}
@@ -221,9 +233,20 @@ public function setConfig(array $config)
221233
throw new \LogicException('Routing has been added, and configuration is not allowed!');
222234
}
223235

236+
$props = [
237+
'routesFile' => 1,
238+
'ignoreLastSlash' => 1,
239+
'tmpCacheNumber' => 1,
240+
'notAllowedAsNotFound' => 1,
241+
'matchAll' => 1,
242+
'autoRoute' => 1,
243+
'controllerNamespace' => 1,
244+
'controllerSuffix' => 1,
245+
];
246+
224247
foreach ($config as $name => $value) {
225-
if (isset($this->config[$name])) {
226-
$this->config[$name] = $value;
248+
if (isset($props[$name])) {
249+
$this->$name = $value;
227250
}
228251
}
229252
}
@@ -289,7 +312,7 @@ public static function validateArguments($methods, $handler)
289312
}
290313

291314
$allow = implode(',', self::SUPPORTED_METHODS) . ',';
292-
$methods = array_map(function ($m) use($allow) {
315+
$methods = array_map(function ($m) use ($allow) {
293316
$m = strtoupper(trim($m));
294317

295318
if (!$m || false === strpos($allow, $m . ',')) {
@@ -349,16 +372,16 @@ protected function getFirstFromPath($path)
349372

350373
/**
351374
* @param string $path
352-
* @param bool $ignoreLastSep
375+
* @param bool $ignoreLastSlash
353376
* @return string
354377
*/
355-
protected function formatUriPath($path, $ignoreLastSep)
378+
protected function formatUriPath($path, $ignoreLastSlash)
356379
{
357380
// clear '//', '///' => '/'
358381
$path = rawurldecode(preg_replace('#\/\/+#', '/', $path));
359382

360-
// setting 'ignoreLastSep'
361-
if ($path !== '/' && $ignoreLastSep) {
383+
// setting 'ignoreLastSlash'
384+
if ($path !== '/' && $ignoreLastSlash) {
362385
$path = rtrim($path, '/');
363386
}
364387

@@ -370,7 +393,7 @@ protected function formatUriPath($path, $ignoreLastSep)
370393
* @param array $conf
371394
* @return array
372395
*/
373-
protected static function filterMatches(array $matches, array $conf)
396+
protected function filterMatches(array $matches, array $conf)
374397
{
375398
// clear all int key
376399
$matches = array_filter($matches, '\is_string', ARRAY_FILTER_USE_KEY);
@@ -444,7 +467,7 @@ public function parseParamRoute($route, array $params, array $conf)
444467
$first = null;
445468
$regex = '#^' . $route . '$#';
446469
$info = [
447-
'regex' => $regex,
470+
'regex' => $regex,
448471
'original' => $bak,
449472
];
450473

@@ -507,8 +530,11 @@ abstract protected function cacheMatchedParamRoute($path, $method, array $conf);
507530
*/
508531
public function matchAutoRoute($path)
509532
{
510-
$cnp = trim($this->config['controllerNamespace']);
511-
$sfx = trim($this->config['controllerSuffix']);
533+
if (!$cnp = trim($this->controllerNamespace)) {
534+
return false;
535+
}
536+
537+
$sfx = trim($this->controllerSuffix);
512538
$tmp = trim($path, '/- ');
513539

514540
// one node. eg: 'home'
@@ -616,20 +642,6 @@ public function addGlobalParam($name, $pattern)
616642
self::$globalParams[$name] = $pattern;
617643
}
618644

619-
/**
620-
* @param null|string $name
621-
* @param null|mixed $default
622-
* @return array|string
623-
*/
624-
public function getConfig($name = null, $default = null)
625-
{
626-
if ($name) {
627-
return isset($this->config[$name]) ? $this->config[$name] : $default;
628-
}
629-
630-
return $this->config;
631-
}
632-
633645
/**
634646
* @return array
635647
*/

src/CachedRouter.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,25 @@ class CachedRouter extends ORouter
2222
/** @var bool */
2323
private $cacheLoaded = false;
2424

25+
/**
26+
* The routes cache file.
27+
* @var string
28+
*/
29+
public $cacheFile;
30+
31+
/**
32+
* Enable routes cache
33+
* @var bool
34+
*/
35+
public $cacheEnable = true;
36+
2537
/**
2638
* object constructor.
2739
* @param array $config
2840
* @throws \LogicException
2941
*/
3042
public function __construct(array $config = [])
3143
{
32-
$this->config = array_merge($this->config,[
33-
'cacheFile' => '',
34-
'cacheEnable' => true,
35-
]);
36-
3744
parent::__construct($config);
3845

3946
// read route caches from cache file
@@ -89,7 +96,7 @@ public function loadRoutesCache()
8996
return false;
9097
}
9198

92-
$file = $this->config['cacheFile'];
99+
$file = $this->cacheFile;
93100

94101
if (!$file || !file_exists($file)) {
95102
return false;
@@ -112,7 +119,7 @@ public function loadRoutesCache()
112119
*/
113120
public function dumpRoutesCache()
114121
{
115-
if (!$file = $this->config['cacheFile']) {
122+
if (!$file = $this->cacheFile) {
116123
return false;
117124
}
118125

@@ -153,15 +160,15 @@ public function dumpRoutesCache()
153160
*/
154161
public function isCacheEnabled()
155162
{
156-
return (bool)$this->getConfig('cacheEnable');
163+
return (bool)$this->cacheEnable;
157164
}
158165

159166
/**
160167
* @return bool
161168
*/
162169
public function isCacheExists()
163170
{
164-
return ($file = $this->config['cacheFile']) && file_exists($file);
171+
return ($file = $this->cacheFile) && file_exists($file);
165172
}
166173

167174
/**

0 commit comments

Comments
 (0)