Skip to content

Commit a0d9036

Browse files
committed
Added some new pattern and made some optimization.
1 parent 674fe55 commit a0d9036

File tree

1 file changed

+16
-22
lines changed

1 file changed

+16
-22
lines changed

src/Router.php

+16-22
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010

1111
namespace Buki\Router;
1212

13-
use Buki\Router\RouterCommand;
14-
use Buki\Router\RouterException;
15-
use Buki\Router\RouterRequest;
1613
use Closure;
1714
use Exception;
1815
use ReflectionMethod;
@@ -43,7 +40,7 @@ class Router
4340
/**
4441
* Router Version
4542
*/
46-
const VERSION = '2.2.1';
43+
const VERSION = '2.3.0';
4744

4845
/**
4946
* @var string $baseFolder Pattern definitions for parameters of Route
@@ -64,11 +61,14 @@ class Router
6461
* @var array $patterns Pattern definitions for parameters of Route
6562
*/
6663
protected $patterns = [
67-
':id' => '(\d+)',
68-
':number' => '(\d+)',
69-
':any' => '([^/]+)',
7064
':all' => '(.*)',
71-
':string' => '(\w+)',
65+
':any' => '([^/]+)',
66+
':id' => '(\d+)',
67+
':int' => '(\d+)',
68+
':number' => '([+-]?([0-9]*[.])?[0-9]+)',
69+
':float' => '([+-]?([0-9]*[.])?[0-9]+)',
70+
':bool' => '(true|false|1|0)',
71+
':string' => '([\w\-_]+)',
7272
':slug' => '([\w\-_]+)',
7373
':uuid' => '([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})',
7474
':date' => '([0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1]))',
@@ -390,30 +390,24 @@ public function controller(string $route, string $controller, array $options = [
390390
}
391391
}
392392

393-
$methodVar = lcfirst(preg_replace('/' . $method . '/i', '', $methodName, 1));
393+
$methodVar = lcfirst(
394+
preg_replace('/' . $method . '_?/i', '', $methodName, 1)
395+
);
394396
$methodVar = strtolower(preg_replace('%([a-z]|[0-9])([A-Z])%', '\1-\2', $methodVar));
395397

396-
if (!empty($only) && !in_array($methodVar, $only)) {
397-
continue;
398-
}
399-
400-
if (!empty($except) && in_array($methodVar, $except)) {
398+
if ((!empty($only) && !in_array($methodVar, $only))
399+
|| (!empty($except) && in_array($methodVar, $except))) {
401400
continue;
402401
}
403402

404403
$ref = new ReflectionMethod($controller, $methodName);
405404
$endpoints = [];
406405
foreach ($ref->getParameters() as $param) {
407406
$typeHint = $param->hasType() ? $param->getType()->getName() : null;
408-
if (in_array($typeHint, ['int'])) {
409-
$pattern = ':id';
410-
} elseif (in_array($typeHint, ['string', 'float', 'bool'])) {
411-
$pattern = ':slug';
412-
} elseif ($typeHint === null) {
413-
$pattern = ':any';
414-
} else {
407+
if (!in_array($typeHint, ['int', 'float', 'string', 'bool']) && $typeHint !== null) {
415408
continue;
416409
}
410+
$pattern = $this->patterns[":{$typeHint}"] ? ":{$typeHint}" : ":any";
417411
$endpoints[] = $param->isOptional() ? "{$pattern}?" : $pattern;
418412
}
419413

@@ -785,7 +779,7 @@ protected function getRequestUri(): string
785779
$dirname = dirname($script);
786780
$dirname = $dirname === '/' ? '' : $dirname;
787781
$basename = basename($script);
788-
$uri = str_replace([$dirname, $basename],null, $this->request()->server->get('REQUEST_URI'));
782+
$uri = str_replace([$dirname, $basename], null, $this->request()->server->get('REQUEST_URI'));
789783
return $this->clearRouteName(explode('?', $uri)[0]);
790784
}
791785
}

0 commit comments

Comments
 (0)