Skip to content

Commit 9262f00

Browse files
committed
some update
1 parent 675f8be commit 9262f00

File tree

3 files changed

+27
-147
lines changed

3 files changed

+27
-147
lines changed

examples/object/index.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
echo "the page $path not found!";
5454
});
5555

56-
//
5756
// $dispatcher->dispatch();
5857

5958
// var_dump($router->getConfig(),$router);die;

src/ORouter.php

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -357,17 +357,14 @@ public function map($method, $route, $handler, array $opts = [])
357357
// have dynamic param tokens
358358

359359
$tmp = $route;
360-
// '/user/{id}' first: 'user', '/a/{post}' first: 'a'
361-
list($first,) = explode('/', trim($tmp, '/'), 2);
362-
363-
// like '/hello[/{name}]' parse fisrt to: 'hello'
364-
$first = trim($first, '[');
365360

366361
// replace token name To pattern regex
367-
$route = $this->parseRoute($route, $opts);
362+
$route = self::parseRoute($route, $this->getAvailableTokens(self::$globalTokens, $opts['tokens']));
368363

364+
// e.g '/hello[/{name}]' first: 'hello', '/user/{id}' first: 'user', '/a/{post}' first: 'a'
369365
// first node is a normal string
370-
if (preg_match('#^(?:[\w-]+)$#', $first, $m)) {
366+
if (preg_match('#^/([\w-]+)#', $tmp, $ms)) {
367+
$first = $ms[1];
371368
$conf = [
372369
'first' => '/' . $first,
373370
'regex' => '#^' . $route . '$#',
@@ -409,13 +406,11 @@ private function validateArguments($method, $handler)
409406

410407
/**
411408
* @param string $route
412-
* @param array $opts
409+
* @param array $tokens
413410
* @return string
414411
*/
415-
private function parseRoute($route, array $opts)
412+
public static function parseRoute($route, array $tokens)
416413
{
417-
$tokens = $this->getAvailableTokens($opts);
418-
419414
// 解析可选参数位
420415
// '/hello[/{name}]' match: /hello/tom /hello
421416
// '/my[/{name}[/{age}]]' match: /my/tom/78 /my/tom
@@ -452,13 +447,10 @@ private function parseRoute($route, array $opts)
452447
return $route;
453448
}
454449

455-
private function getAvailableTokens(array $opts)
450+
public static function getAvailableTokens(array $tokens, $tmpTokens)
456451
{
457-
/** @var array $tokens */
458-
$tokens = self::$globalTokens;
459-
460-
if ($opts['tokens']) {
461-
foreach ((array)$opts['tokens'] as $name => $pattern) {
452+
if ($tmpTokens) {
453+
foreach ($tmpTokens as $name => $pattern) {
462454
$key = trim($name, '{}');
463455
$tokens[$key] = $pattern;
464456
}
@@ -579,9 +571,10 @@ public function match($path, $method)
579571
}
580572

581573
// handle Auto Route
582-
if ($handler = $this->matchAutoRoute($path)) {
574+
if ($handler = self::matchAutoRoute($path, $this->config['autoRoute'])) {
583575
return [$path, [
584-
'handler' => $handler
576+
'path' => $path,
577+
'handler' => $handler,
585578
]];
586579
}
587580

@@ -590,22 +583,18 @@ public function match($path, $method)
590583
}
591584

592585
/**
593-
* handle Auto Route
586+
* handle auto route match
594587
* when config `'autoRoute' => true`
595588
* @param string $path The route path
589+
* @param array $opts The some options
590+
* contains: [
591+
* 'controllerNamespace' => '', // controller namespace. eg: 'app\\controllers'
592+
* 'controllerSuffix' => '', // controller suffix. eg: 'Controller'
593+
* ]
596594
* @return bool|callable
597595
*/
598-
private function matchAutoRoute($path)
596+
public static function matchAutoRoute($path, array $opts)
599597
{
600-
/**
601-
* @var array $opts
602-
* contains: [
603-
* 'controllerNamespace' => '', // controller namespace. eg: 'app\\controllers'
604-
* 'controllerSuffix' => '', // controller suffix. eg: 'Controller'
605-
* ]
606-
*/
607-
$opts = $this->config['autoRoute'];
608-
609598
// not enabled
610599
if (!$opts || !isset($opts['enable']) || !$opts['enable']) {
611600
return false;

src/SRouter.php

Lines changed: 8 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)