Skip to content

Commit f07e123

Browse files
committed
some logic update for find param route
1 parent ed456b8 commit f07e123

File tree

7 files changed

+94
-81
lines changed

7 files changed

+94
-81
lines changed

example/tests/compare_exec.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,30 @@
44

55
$str = '/50be3774f6/{arg1}/arg2/arg3/{int}/arg5/arg6/{arg7}/arg8/arg9[/850726135a]';
66

7-
$sample1 = function () {
8-
$path = '/50be3774f6/{arg1}/{arg2}/{arg3}/{arg4}/{arg5}/{arg6}/{arg7}/{arg8}/{arg9}/850726135a';
7+
$sample1 = function ($path) {
8+
$first = null;
99

10-
preg_match_all('#\{([a-zA-Z_][\w-]*)\}#', $path, $m);
10+
// eg '/article/12'
11+
if ($pos = \strpos($path, '/', 1)) {
12+
$first = \substr($path, 1, $pos - 1);
13+
}
1114

12-
return $m;
15+
return $first;
1316
};
1417

15-
$sample2 = function () {
16-
$path = '/50be3774f6/{arg1}/{arg2}/{arg3}/{arg4}/{arg5}/{arg6}/{arg7}/{arg8}/{arg9}/850726135a';
18+
$sample2 = function ($path) {
19+
$first = null;
20+
$path = ltrim($path, '/');
1721

18-
preg_match_all('#\{([a-zA-Z_][\w-]*)\}#', $path, $m, PREG_SET_ORDER);
22+
// eg '/article/12'
23+
if ($pos = \strpos($path, '/')) {
24+
$first = \substr($path, 0, $pos);
25+
}
1926

20-
return $m;
27+
return $first;
2128
};
2229

23-
compare_speed($sample1, $sample2, $times);
30+
compare_speed($sample1, $sample2, $times, [$str]);
2431

2532
function compare_speed(callable $sample1, callable $sample2, int $times = 1000, array $args = [])
2633
{

src/Base/AbstractRouter.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ abstract class AbstractRouter implements RouterInterface
109109
* vague Routes - have dynamic arguments,but the first node is exists regex.
110110
* 第一节就包含了正则匹配,称之为无规律/模糊的动态路由
111111
* e.g '/{name}/profile' '/{some}/{some2}'
112-
* @var array
112+
* @var array[]
113113
* [
114114
* // 使用 HTTP METHOD 作为 key进行分组
115115
* 'GET' => [
@@ -465,9 +465,6 @@ public function parseParamRoute(string $route, array $params, array $conf): arra
465465

466466
foreach ($m[1] as $name) {
467467
$regex = $params[$name] ?? self::DEFAULT_REGEX;
468-
469-
// Name the match (?P<arg1>[^/]+)
470-
// $pairs[$key] = '(?P<' . $name . '>' . $regex . ')';
471468
$pairs['{' . $name . '}'] = '(' . $regex . ')';
472469
}
473470

@@ -521,20 +518,19 @@ protected function mergeMatches(array $matches, array $conf): array
521518
}
522519

523520
/**
524-
* @param array $routesData
521+
* @param string $first
525522
* @param string $path
526523
* @param string $method
527524
* @return array
528525
*/
529-
abstract protected function findInRegularRoutes(array $routesData, string $path, string $method): array;
526+
abstract protected function findInRegularRoutes(string $first, string $path, string $method): array;
530527

531528
/**
532-
* @param array $routesData
533529
* @param string $path
534530
* @param string $method
535-
* @return array
531+
* @return array|false
536532
*/
537-
abstract protected function findInVagueRoutes(array $routesData, string $path, string $method): array;
533+
abstract protected function findInVagueRoutes(string $path, string $method);
538534

539535
/**
540536
* handle auto route match, when config `'autoRoute' => true`

src/Dispatcher/SimpleDispatcher.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ protected function callRouteHandler(string $path, string $method, $handler, arra
258258
* True: The `$path` is matched success, but action not exist on route parser
259259
* False: The `$path` is matched fail
260260
* @return bool|mixed
261+
* @throws \RuntimeException
261262
* @throws \InvalidArgumentException
262263
* @throws \Throwable
263264
*/
@@ -290,6 +291,7 @@ protected function handleNotFound(string $path, string $method, $actionNotExist
290291
* @param string $method
291292
* @param array $methods The allowed methods
292293
* @return mixed
294+
* @throws \RuntimeException
293295
* @throws \InvalidArgumentException
294296
* @throws \Throwable
295297
*/

src/ORouter.php

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public function match(string $path, string $method = 'GET'): array
194194

195195
// is a regular dynamic route(the first node is 1th level index key).
196196
if ($first && isset($this->regularRoutes[$first])) {
197-
$result = $this->findInRegularRoutes($this->regularRoutes[$first], $path, $method);
197+
$result = $this->findInRegularRoutes($first, $path, $method);
198198

199199
if ($result[0] === self::FOUND) {
200200
return $result;
@@ -204,12 +204,8 @@ public function match(string $path, string $method = 'GET'): array
204204
}
205205

206206
// is a irregular dynamic route
207-
if (isset($this->vagueRoutes[$method])) {
208-
$result = $this->findInVagueRoutes($this->vagueRoutes[$method], $path, $method);
209-
210-
if ($result[0] === self::FOUND) {
211-
return $result;
212-
}
207+
if ($result = $this->findInVagueRoutes($path, $method)) {
208+
return $result;
213209
}
214210

215211
// handle Auto Route
@@ -226,19 +222,15 @@ public function match(string $path, string $method = 'GET'): array
226222
}
227223

228224
if ($first && isset($this->regularRoutes[$first])) {
229-
$result = $this->findInRegularRoutes($this->regularRoutes[$first], $path, 'GET');
225+
$result = $this->findInRegularRoutes($first, $path, 'GET');
230226

231227
if ($result[0] === self::FOUND) {
232228
return $result;
233229
}
234230
}
235231

236-
if (isset($this->vagueRoutes['GET'])) {
237-
$result = $this->findInVagueRoutes($this->vagueRoutes['GET'], $path, 'GET');
238-
239-
if ($result[0] === self::FOUND) {
240-
return $result;
241-
}
232+
if ($result = $this->findInVagueRoutes($path, 'GET')) {
233+
return $result;
242234
}
243235
}
244236

@@ -276,9 +268,7 @@ protected function findAllowedMethods(string $path, string $method, array $allow
276268
continue;
277269
}
278270

279-
$result = $this->findInVagueRoutes($this->vagueRoutes['GET'], $path, $m);
280-
281-
if ($result[0] === self::FOUND) {
271+
if ($this->findInVagueRoutes($path, $m)) {
282272
$allowedMethods[] = $method;
283273
}
284274
}
@@ -292,16 +282,18 @@ protected function findAllowedMethods(string $path, string $method, array $allow
292282
}
293283

294284
/**
295-
* @param array $routesData
285+
* @param string $first
296286
* @param string $path
297287
* @param string $method
298288
* @return array
299289
*/
300-
protected function findInRegularRoutes(array $routesData, string $path, string $method): array
290+
protected function findInRegularRoutes(string $first, string $path, string $method): array
301291
{
302292
$allowedMethods = '';
293+
/** @var array $routesInfo */
294+
$routesInfo = $this->regularRoutes[$first];
303295

304-
foreach ($routesData as $conf) {
296+
foreach ($routesInfo as $conf) {
305297
if (0 === \strpos($path, $conf['start']) && \preg_match($conf['regex'], $path, $matches)) {
306298
$allowedMethods .= $conf['methods'];
307299

@@ -320,14 +312,20 @@ protected function findInRegularRoutes(array $routesData, string $path, string $
320312
}
321313

322314
/**
323-
* @param array $routesData
324315
* @param string $path
325316
* @param string $method
326-
* @return array
317+
* @return array|false
327318
*/
328-
protected function findInVagueRoutes(array $routesData, string $path, string $method): array
319+
protected function findInVagueRoutes(string $path, string $method)
329320
{
330-
foreach ($routesData as $conf) {
321+
if (!isset($this->vagueRoutes[$method])) {
322+
return false;
323+
}
324+
325+
/** @var array $routeList */
326+
$routeList = $this->vagueRoutes[$method];
327+
328+
foreach ($routeList as $conf) {
331329
if ($conf['start'] && 0 !== \strpos($path, $conf['start'])) {
332330
continue;
333331
}
@@ -339,7 +337,7 @@ protected function findInVagueRoutes(array $routesData, string $path, string $me
339337
}
340338
}
341339

342-
return [self::NOT_FOUND];
340+
return false;
343341
}
344342

345343
/*******************************************************************************

src/PreMatchRouter.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function map($methods, string $route, $handler, array $opts = []): Abstra
8585

8686
// it is param route
8787
if (!self::isStaticRoute($route)) {
88-
$this->collectParamRoute($route, $methods, $conf);
88+
$this->collectParamRoute($route, $methods, $conf, $opts['params'] ?? []);
8989

9090
return $this;
9191
}
@@ -146,7 +146,7 @@ public function match(string $path, string $method = self::GET): array
146146
}
147147

148148
if ($first && isset($this->regularRoutes[$first])) {
149-
$result = $this->findInRegularRoutes($this->regularRoutes[$first], $path, $method);
149+
$result = $this->findInRegularRoutes($first, $path, $method);
150150

151151
if ($result[0] === self::FOUND) {
152152
return $result;
@@ -155,12 +155,8 @@ public function match(string $path, string $method = self::GET): array
155155
$allowedMethods = $result[1];
156156
}
157157

158-
if (isset($this->vagueRoutes[$method])) {
159-
$result = $this->findInVagueRoutes($this->vagueRoutes[$method], $path, $method);
160-
161-
if ($result[0] === self::FOUND) {
162-
return $result;
163-
}
158+
if ($result = $this->findInVagueRoutes($path, $method)) {
159+
return $result;
164160
}
165161

166162
// For HEAD requests, attempt fallback to GET
@@ -170,19 +166,15 @@ public function match(string $path, string $method = self::GET): array
170166
}
171167

172168
if ($first && isset($this->regularRoutes[$first])) {
173-
$result = $this->findInRegularRoutes($this->regularRoutes[$first], $path, 'GET');
169+
$result = $this->findInRegularRoutes($first, $path, 'GET');
174170

175171
if ($result[0] === self::FOUND) {
176172
return $result;
177173
}
178174
}
179175

180-
if (isset($this->vagueRoutes['GET'])) {
181-
$result = $this->findInVagueRoutes($this->vagueRoutes['GET'], $path, 'GET');
182-
183-
if ($result[0] === self::FOUND) {
184-
return $result;
185-
}
176+
if ($result = $this->findInVagueRoutes($path, 'GET')) {
177+
return $result;
186178
}
187179
}
188180

src/Route.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,23 @@
1414
*/
1515
class Route
1616
{
17+
/**
18+
* @var string route pattern
19+
*/
20+
public $pattern;
1721

22+
/**
23+
* @var mixed route handler
24+
*/
25+
public $handler;
26+
27+
/**
28+
* @var string[] map where parameter name => regular expression pattern (or symbol name)
29+
*/
30+
public $params;
31+
32+
/**
33+
* @var array
34+
*/
35+
public $options = [];
1836
}

0 commit comments

Comments
 (0)