Fixed routes with similar regexes not being able to match.

This commit is contained in:
flash 2024-04-02 17:27:06 +00:00
parent 9b57fbb42c
commit 09fc6b3958
3 changed files with 29 additions and 15 deletions

View file

@ -1 +1 @@
0.2403.301624
0.2404.21726

View file

@ -1,7 +1,7 @@
<?php
// HttpRouter.php
// Created: 2024-03-28
// Updated: 2024-03-28
// Updated: 2024-04-02
namespace Index\Http\Routing;
@ -153,25 +153,27 @@ class HttpRouter implements IRouter {
}
$methods = [];
$handler = null;
$args = [];
if(array_key_exists($path, $this->staticRoutes)) {
$methods = $this->staticRoutes[$path];
foreach($this->staticRoutes[$path] as $sMethodName => $sMethodHandler)
$methods[$sMethodName] = [$sMethodHandler, []];
} else {
foreach($this->dynamicRoutes as $rPattern => $rMethods)
if(preg_match($rPattern, $path, $args) === 1) {
$methods = $rMethods;
array_shift($args);
break;
}
if(preg_match($rPattern, $path, $args) === 1)
foreach($rMethods as $rMethodName => $rMethodHandler)
if(!array_key_exists($rMethodName, $methods))
$methods[$rMethodName] = [$rMethodHandler, array_slice($args, 1)];
}
$method = strtoupper($method);
if(array_key_exists($method, $methods))
$handler = $methods[$method];
elseif($method === 'HEAD' && array_key_exists('GET', $methods))
$handler = $methods['GET'];
if(array_key_exists($method, $methods)) {
[$handler, $args] = $methods[$method];
} elseif($method === 'HEAD' && array_key_exists('GET', $methods)) {
[$handler, $args] = $methods['GET'];
} else {
$handler = null;
$args = [];
}
return new ResolvedRouteInfo($middlewares, array_keys($methods), $handler, $args);
}

View file

@ -1,7 +1,7 @@
<?php
// RouterTest.php
// Created: 2022-01-20
// Updated: 2024-03-30
// Updated: 2024-04-02
declare(strict_types=1);
@ -147,4 +147,16 @@ final class RouterTest extends TestCase {
$this->assertEquals('meow', $router->resolve('GET', '/scoped/meow')->dispatch([]));
$this->assertEquals('meow', $scoped->resolve('POST', '/meow')->dispatch([]));
}
public function testEEPROMSituation(): void {
$router = new HttpRouter;
$router->options('/uploads/([A-Za-z0-9\-_]+)(?:\.(t|json))?', function() {});
$router->get('/uploads/([A-Za-z0-9\-_]+)(?:\.(t|json))?', function() {});
$router->delete('/uploads/([A-Za-z0-9\-_]+)', function() {});
$resolved = $router->resolve('DELETE', '/uploads/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA');
$this->assertEquals(['OPTIONS', 'GET', 'DELETE'], $resolved->getSupportedMethods());
}
}