@@ -331,12 +331,15 @@ public static function map($method, $route, $handler, array $opts = [])
331331
332332 // have dynamic param tokens
333333
334- $ route = $ tmp = self :: replaceTokenToPattern ( $ route, $ opts ) ;
334+ $ tmp = $ route ;
335335
336- list ($ first ,) = explode ('/ ' , trim ($ tmp , '/ ' ), 2 );
336+ // replace token name To pattern regex
337+ $ route = ORouter::parseRoute ($ route , ORouter::getAvailableTokens (self ::$ globalTokens , $ opts ['tokens ' ]));
337338
338- // first node is a normal string '/user/:id', '/a/:post'
339- if (preg_match ('#^(?|[\w-]+)$# ' , $ first )) {
339+ // e.g '/hello[/{name}]' first: 'hello', '/user/{id}' first: 'user', '/a/{post}' first: 'a'
340+ // first node is a normal string
341+ if (preg_match ('#^/([\w-]+)# ' , $ tmp , $ ms )) {
342+ $ first = $ ms [1 ];
340343 $ conf = [
341344 'first ' => '/ ' . $ first ,
342345 'regex ' => '#^ ' . $ route . '$# ' ,
@@ -376,43 +379,6 @@ private static function validateArguments($method, $handler)
376379 }
377380 }
378381
379- /**
380- * @param string $route
381- * @param array $opts
382- * @return string
383- */
384- private static function replaceTokenToPattern ($ route , array $ opts )
385- {
386- /** @var array $tokens */
387- $ tokens = self ::$ globalTokens ;
388-
389- if ($ opts ['tokens ' ]) {
390- foreach ((array )$ opts ['tokens ' ] as $ name => $ pattern ) {
391- $ key = trim ($ name , '{} ' );
392- $ tokens [$ key ] = $ pattern ;
393- }
394- }
395-
396- if (preg_match_all ('#\{([a-zA-Z_][a-zA-Z0-9_-]*)\}# ' , $ route , $ m )) {
397- /** @var array[] $m */
398- $ replacePairs = [];
399-
400- foreach ($ m [1 ] as $ name ) {
401- $ key = '{ ' . $ name . '} ' ;
402- // 匹配定义的 token , 未匹配到的使用默认 self::DEFAULT_REGEX
403- $ regex = isset ($ tokens [$ name ]) ? $ tokens [$ name ] : self ::DEFAULT_REGEX ;
404-
405- // 将匹配结果命名 (?P<arg1>[^/]+)
406- // $replacePairs[$key] = '(?P<' . $name . '>' . $pattern . ')';
407- $ replacePairs [$ key ] = '( ' . $ regex . ') ' ;
408- }
409-
410- $ route = strtr ($ route , $ replacePairs );
411- }
412-
413- return $ route ;
414- }
415-
416382//////////////////////////////////////////////////////////////////////
417383/// route match
418384//////////////////////////////////////////////////////////////////////
@@ -520,7 +486,7 @@ public static function match($path, $method)
520486 }
521487
522488 // handle Auto Route
523- if ($ handler = self ::matchAutoRoute ($ path )) {
489+ if ($ handler = ORouter ::matchAutoRoute ($ path, static :: $ config [ ' autoRoute ' ] )) {
524490 return [$ path , [
525491 'handler ' => $ handler
526492 ]];
@@ -530,80 +496,6 @@ public static function match($path, $method)
530496 return false ;
531497 }
532498
533- /**
534- * handle Auto Route
535- * when config `'autoRoute' => true`
536- * @param string $path The route path
537- * @return bool|callable
538- */
539- private static function matchAutoRoute ($ path )
540- {
541- /**
542- * @var array $opts
543- * contains: [
544- * 'controllerNamespace' => '', // controller namespace. eg: 'app\\controllers'
545- * 'controllerSuffix' => '', // controller suffix. eg: 'Controller'
546- * ]
547- */
548- $ opts = static ::$ config ['autoRoute ' ];
549-
550- // not enabled
551- if (!$ opts || !isset ($ opts ['enable ' ]) || !$ opts ['enable ' ]) {
552- return false ;
553- }
554-
555- $ cnp = $ opts ['controllerNamespace ' ];
556- $ sfx = $ opts ['controllerSuffix ' ];
557- $ tmp = trim ($ path , '/- ' );
558-
559- // one node. eg: 'home'
560- if (!strpos ($ tmp , '/ ' )) {
561- $ tmp = ORouter::convertNodeStr ($ tmp );
562- $ class = "$ cnp \\" . ucfirst ($ tmp ) . $ sfx ;
563-
564- return class_exists ($ class ) ? $ class : false ;
565- }
566-
567- $ ary = array_map ([ORouter::class, 'convertNodeStr ' ], explode ('/ ' , $ tmp ));
568- $ cnt = count ($ ary );
569-
570- // two nodes. eg: 'home/test' 'admin/user'
571- if ($ cnt === 2 ) {
572- list ($ n1 , $ n2 ) = $ ary ;
573-
574- // last node is an controller class name. eg: 'admin/user'
575- $ class = "$ cnp \\$ n1 \\" . ucfirst ($ n2 ) . $ sfx ;
576-
577- if (class_exists ($ class )) {
578- return $ class ;
579- }
580-
581- // first node is an controller class name, second node is a action name,
582- $ class = "$ cnp \\" . ucfirst ($ n1 ) . $ sfx ;
583-
584- return class_exists ($ class ) ? "$ class@ $ n2 " : false ;
585- }
586-
587- // max allow 5 nodes
588- if ($ cnt > 5 ) {
589- return false ;
590- }
591-
592- // last node is an controller class name
593- $ n2 = array_pop ($ ary );
594- $ class = sprintf ('%s \\%s \\%s ' , $ cnp , implode ('\\' , $ ary ), ucfirst ($ n2 ) . $ sfx );
595-
596- if (class_exists ($ class )) {
597- return $ class ;
598- }
599-
600- // last second is an controller class name, last node is a action name,
601- $ n1 = array_pop ($ ary );
602- $ class = sprintf ('%s \\%s \\%s ' , $ cnp , implode ('\\' , $ ary ), ucfirst ($ n1 ) . $ sfx );
603-
604- return class_exists ($ class ) ? "$ class@ $ n2 " : false ;
605- }
606-
607499//////////////////////////////////////////////////////////////////////
608500/// route callback handler dispatch
609501//////////////////////////////////////////////////////////////////////
0 commit comments