Skip to content

Commit 15bc904

Browse files
committed
update: router config modify
1 parent 0cdd2f3 commit 15bc904

File tree

6 files changed

+39
-60
lines changed

6 files changed

+39
-60
lines changed

examples/cached/index.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@
3434

3535
// enable autoRoute
3636
// you can access '/demo' '/admin/user/info', Don't need to configure any route
37-
'autoRoute' => [
38-
'enable' => 1,
39-
'controllerNamespace' => 'inhere\sroute\examples\controllers',
40-
'controllerSuffix' => 'Controller',
41-
],
37+
'autoRoute' => 1,
38+
'controllerNamespace' => 'inhere\sroute\examples\controllers',
39+
'controllerSuffix' => 'Controller',
4240
]);
4341

4442
require __DIR__ . '/routes.php';

examples/cached/routes-cache.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/*
33
* This inhere/sroute routes cache file. is auto generate by inhere\sroute\ORouter.
4-
* @date 2017-08-08 14:03:49
4+
* @date 2017-08-08 15:07:34
55
*/
66
return [
77
'staticRoutes' => array (

examples/object/index.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,9 @@
3333

3434
// enable autoRoute
3535
// you can access '/demo' '/admin/user/info', Don't need to configure any route
36-
'autoRoute' => [
37-
'enable' => 1,
38-
'controllerNamespace' => 'inhere\sroute\examples\controllers',
39-
'controllerSuffix' => 'Controller',
40-
],
36+
'autoRoute' => 1,
37+
'controllerNamespace' => 'inhere\sroute\examples\controllers',
38+
'controllerSuffix' => 'Controller',
4139
]);
4240

4341
require __DIR__ . '/routes.php';

examples/static/index.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,9 @@
3030

3131
// enable autoRoute
3232
// you can access '/demo' '/admin/user/info', Don't need to configure any route
33-
'autoRoute' => [
34-
'enable' => 1,
35-
'controllerNamespace' => 'inhere\sroute\examples\controllers',
36-
'controllerSuffix' => 'Controller',
37-
],
33+
'autoRoute' => 1,
34+
'controllerNamespace' => 'inhere\sroute\examples\controllers',
35+
'controllerSuffix' => 'Controller',
3836
]);
3937

4038
require __DIR__ . '/routes.php';

src/ORouter.php

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,12 @@ class ORouter implements RouterInterface
170170
'matchAll' => '',
171171

172172
// auto route match @like yii framework
173-
'autoRoute' => [
174-
// If is True, will auto find the handler controller file.
175-
'enable' => false,
176-
// The default controllers namespace, is valid when `'enable' = true`
177-
'controllerNamespace' => '', // eg: 'app\\controllers'
178-
// controller suffix, is valid when `'enable' = true`
179-
'controllerSuffix' => '', // eg: 'Controller'
180-
],
173+
// If is True, will auto find the handler controller file.
174+
'autoRoute' => false,
175+
// The default controllers namespace, is valid when `'enable' = true`
176+
'controllerNamespace' => '', // eg: 'app\\controllers'
177+
// controller suffix, is valid when `'enable' = true`
178+
'controllerSuffix' => '', // eg: 'Controller'
181179
];
182180

183181
/** @var DispatcherInterface */
@@ -218,11 +216,7 @@ public function setConfig(array $config)
218216
}
219217

220218
foreach ($config as $name => $value) {
221-
if ($name === 'autoRoute') {
222-
$this->config['autoRoute'] = array_merge($this->config['autoRoute'], (array)$value);
223-
} else {
224-
$this->config[$name] = $value;
225-
}
219+
$this->config[$name] = $value;
226220
}
227221
}
228222

@@ -494,7 +488,7 @@ public function match($path, $method)
494488
// clear '//', '///' => '/'
495489
$path = rawurldecode(preg_replace('#\/\/+#', '/', $path));
496490
$method = strtoupper($method);
497-
$number = $this->config['tmpCacheNumber'];
491+
$number = (int)$this->config['tmpCacheNumber'];
498492

499493
// setting 'ignoreLastSep'
500494
if ($path !== '/' && $this->config['ignoreLastSep']) {
@@ -582,7 +576,10 @@ public function match($path, $method)
582576
}
583577

584578
// handle Auto Route
585-
if ($handler = self::matchAutoRoute($path, $this->config['autoRoute'])) {
579+
if (
580+
$this->config['autoRoute'] &&
581+
($handler = self::matchAutoRoute($path, $this->config['controllerNamespace'], $this->config['controllerSuffix']))
582+
) {
586583
return [$path, [
587584
'path' => $path,
588585
'handler' => $handler,
@@ -594,25 +591,16 @@ public function match($path, $method)
594591
}
595592

596593
/**
597-
* handle auto route match
598-
* when config `'autoRoute' => true`
594+
* handle auto route match, when config `'autoRoute' => true`
599595
* @param string $path The route path
600-
* @param array $opts The some options
601-
* contains: [
602-
* 'controllerNamespace' => '', // controller namespace. eg: 'app\\controllers'
603-
* 'controllerSuffix' => '', // controller suffix. eg: 'Controller'
604-
* ]
596+
* @param string $controllerNamespace controller namespace. eg: 'app\\controllers'
597+
* @param string $controllerSuffix controller suffix. eg: 'Controller'
605598
* @return bool|callable
606599
*/
607-
public static function matchAutoRoute($path, array $opts)
600+
public static function matchAutoRoute($path, $controllerNamespace, $controllerSuffix = '')
608601
{
609-
// not enabled
610-
if (!$opts || !isset($opts['enable']) || !$opts['enable']) {
611-
return false;
612-
}
613-
614-
$cnp = $opts['controllerNamespace'];
615-
$sfx = $opts['controllerSuffix'];
602+
$cnp = $controllerNamespace;
603+
$sfx = $controllerSuffix;
616604
$tmp = trim($path, '/- ');
617605

618606
// one node. eg: 'home'

src/SRouter.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,14 +110,12 @@ class SRouter implements RouterInterface
110110
'matchAll' => '',
111111

112112
// auto route match @like yii framework
113-
'autoRoute' => [
114-
// If is True, will auto find the handler controller file.
115-
'enable' => false,
116-
// The default controllers namespace, is valid when `'enable' = true`
117-
'controllerNamespace' => '', // eg: 'app\\controllers'
118-
// controller suffix, is valid when `'enable' = true`
119-
'controllerSuffix' => '', // eg: 'Controller'
120-
],
113+
// If is True, will auto find the handler controller file.
114+
'autoRoute' => false,
115+
// The default controllers namespace, is valid when `'enable' = true`
116+
'controllerNamespace' => '', // eg: 'app\\controllers'
117+
// controller suffix, is valid when `'enable' = true`
118+
'controllerSuffix' => '', // eg: 'Controller'
121119
];
122120

123121
/** @var DispatcherInterface */
@@ -134,11 +132,7 @@ public static function setConfig(array $config)
134132
}
135133

136134
foreach ($config as $name => $value) {
137-
if ($name === 'autoRoute') {
138-
static::$config['autoRoute'] = array_merge(static::$config['autoRoute'], (array)$value);
139-
} elseif (isset(static::$config[$name])) {
140-
static::$config[$name] = $value;
141-
}
135+
static::$config[$name] = $value;
142136
}
143137
}
144138

@@ -387,7 +381,10 @@ public static function match($path, $method)
387381
}
388382

389383
// handle Auto Route
390-
if ($handler = ORouter::matchAutoRoute($path, static::$config['autoRoute'])) {
384+
if (
385+
self::$config['autoRoute'] &&
386+
($handler = ORouter::matchAutoRoute($path, self::$config['controllerNamespace'], self::$config['controllerSuffix']))
387+
) {
391388
return [$path, [
392389
'handler' => $handler
393390
]];

0 commit comments

Comments
 (0)