diff --git a/src/RouteCollector.php b/src/RouteCollector.php index c1c1762..0897924 100644 --- a/src/RouteCollector.php +++ b/src/RouteCollector.php @@ -38,7 +38,7 @@ public function __construct(RouteParser $routeParser, DataGenerator $dataGenerat public function addRoute($httpMethod, $route, $handler) { $route = $this->currentGroupPrefix . $route; - $routeDatas = $this->routeParser->parse($route); + $routeDatas = array_reverse($this->routeParser->parse($route)); foreach ((array) $httpMethod as $method) { foreach ($routeDatas as $routeData) { $this->dataGenerator->addRoute($method, $routeData, $handler); diff --git a/test/Dispatcher/DispatcherTest.php b/test/Dispatcher/DispatcherTest.php index c2edf8c..075196f 100644 --- a/test/Dispatcher/DispatcherTest.php +++ b/test/Dispatcher/DispatcherTest.php @@ -413,6 +413,30 @@ public function provideFoundDispatchCases() $cases[] = ['POST', '/bar', $callback, 'handler1', ['foo' => 'bar']]; + // 27 --------------------------------------------------------------------------------------> + + $callback = function (RouteCollector $r) { + $r->addRoute('GET', '/about[/{aboutwhat}[/location]]', 'handler0'); + }; + + $cases[] = ['GET', '/about/some/location', $callback, 'handler0', [ + 'aboutwhat' => 'some', + ], [ + '_route' => '/about[/{aboutwhat}[/location]]', + ]]; + + // 28 --------------------------------------------------------------------------------------> + + $callback = function (RouteCollector $r) { + $r->addRoute('GET', '/about[/{aboutwhat:.*}[/location]]', 'handler0'); + }; + + $cases[] = ['GET', '/about/the/nested/location', $callback, 'handler0', [ + 'aboutwhat' => 'the/nested', + ], [ + '_route' => '/about[/{aboutwhat:.*}[/location]]', + ]]; + // x --------------------------------------------------------------------------------------> return $cases; diff --git a/test/RouteCollectorTest.php b/test/RouteCollectorTest.php index cc54407..7196e79 100644 --- a/test/RouteCollectorTest.php +++ b/test/RouteCollectorTest.php @@ -90,6 +90,25 @@ public function testGroups() $this->assertSame($expected, $r->routes); } + + public function testOptionalRoutesCanBeUsed(): void + { + $r = new DummyRouteCollector(); + + $r->head('/head[/{optional}]', 'headHandler'); + $r->get('/get[/{optional}/hello]', 'getHandler'); + $r->post('/post[/{optional:.*}]', 'postHandler'); + $r->delete('/delete[/{optional:.*}/hello]', 'deleteHandler'); + + $expected = [ + ['HEAD', '/head[/{optional}]', 'headHandler'], + ['GET', '/get[/{optional}/hello]', 'getHandler'], + ['POST', '/post[/{optional:.*}]', 'postHandler'], + ['DELETE', '/delete[/{optional:.*}/hello]', 'deleteHandler'], + ]; + + self::assertSame($expected, $r->routes); + } } class DummyRouteCollector extends RouteCollector