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 */
0 commit comments