Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix path support after unlimited optional placeholders #292

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/GenerateUri/FromProcessedConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use function array_key_exists;
use function array_keys;
use function array_reverse;
use function assert;
use function count;
use function is_string;
Expand All @@ -34,7 +35,7 @@ public function forRoute(string $name, array $substitutions = []): string

$missingParameters = [];

foreach ($this->processedConfiguration[$name] as $parsedRoute) {
foreach (array_reverse($this->processedConfiguration[$name]) as $parsedRoute) {
$missingParameters = $this->missingParameters($parsedRoute, $substitutions);

// Only attempt to generate the path if we have the necessary info
Expand Down
2 changes: 1 addition & 1 deletion src/RouteCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct(
public function addRoute(string|array $httpMethod, string $route, mixed $handler, array $extraParameters = []): void
{
$route = $this->currentGroupPrefix . $route;
$parsedRoutes = $this->routeParser->parse($route);
$parsedRoutes = array_reverse($this->routeParser->parse($route));

$extraParameters = [self::ROUTE_REGEX => $route] + $extraParameters;

Expand Down
12 changes: 12 additions & 0 deletions test/Dispatcher/DispatcherTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,18 @@ public static function provideFoundDispatchCases(): iterable
};

yield 'options method is supported' => ['OPTIONS', '/about', $callback, 'handler0', [], ['_route' => '/about']];

$callback = static function (ConfigureRoutes $r): void {
$r->addRoute('GET', '/about[/{aboutwhat}[/location]]', 'handler0');
};

yield 'Paths can be placed after an optional placeholder' => ['GET', '/about/some/location', $callback, 'handler0', ['aboutwhat' => 'some'], ['_route' => '/about[/{aboutwhat}[/location]]']];

$callback = static function (ConfigureRoutes $r): void {
$r->addRoute('GET', '/about[/{aboutwhat:.*}[/location]]', 'handler0');
};

yield 'Paths can be placed after an unlimited optional placeholder' => ['GET', '/about/the/nested/location', $callback, 'handler0', ['aboutwhat' => 'the/nested'], ['_route' => '/about[/{aboutwhat:.*}[/location]]']];
}

/** @return iterable<string, array{string, string, Closure(ConfigureRoutes):void}> */
Expand Down
3 changes: 1 addition & 2 deletions test/GenerateUri/FromProcessedConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use PHPUnit\Framework\TestCase;

use function array_map;
use function array_reverse;

/** @phpstan-import-type ParsedRoutes from RouteParser */
final class FromProcessedConfigurationTest extends TestCase
Expand Down Expand Up @@ -129,7 +128,7 @@ public function unicodeParametersAreAlsoAccepted(): void
private static function routeGeneratorFor(array $routeMap): GenerateUri
{
$parseRoutes = static function (string $route): array {
return array_reverse((new RouteParser\Std())->parse($route));
return (new RouteParser\Std())->parse($route);
};

return new GenerateUri\FromProcessedConfiguration(array_map($parseRoutes, $routeMap));
Expand Down
51 changes: 51 additions & 0 deletions test/RouteCollectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,26 @@ public function routesCanBeGrouped(): void
self::assertSame($expected, $dataGenerator->routes);
}

#[PHPUnit\Test]
public function optionalRoutesCanBeUsed(): void
{
$dataGenerator = self::dummyNestedDataGenerator();
$r = new RouteCollector(new Std(), $dataGenerator);

$r->any('/any[/{optional}]', 'optional');
$r->get('/any[/{optional}/hello]', 'optional');

$expected = [
['*', ['/any/', ['optional', '[^/]+']], 'optional', ['_route' => '/any[/{optional}]']],
['*', ['/any'], 'optional', ['_route' => '/any[/{optional}]']],
['GET', ['/any/', ['optional', '[^/]+'], '/hello'], 'optional', ['_route' => '/any[/{optional}/hello]']],
['GET', ['/any'], 'optional', ['_route' => '/any[/{optional}/hello]']],
];

self::assertObjectHasProperty('routes', $dataGenerator);
self::assertSame($expected, $dataGenerator->routes);
}

#[PHPUnit\Test]
public function namedRoutesShouldBeRegistered(): void
{
Expand Down Expand Up @@ -180,4 +200,35 @@ public function addRoute(string $httpMethod, array $routeData, mixed $handler, a
}
};
}

private static function dummyNestedDataGenerator(): DataGenerator
{
return new class implements DataGenerator
{
/** @var list<array{string, array<string|array{string, string}>, mixed, array<string, bool|float|int|string>}> */
public array $routes = [];

/** @inheritDoc */
public function getData(): array
{
return [[], []];
}

/** @inheritDoc */
public function addRoute(string $httpMethod, array $routeData, mixed $handler, array $extraParameters = []): void
{
$route = [$httpMethod];

$subroute = [];
foreach ($routeData as $data) {
$subroute[] = $data;
}
$route[] = $subroute;

$route[] = $handler;
$route[] = $extraParameters;
$this->routes[] = $route;
}
};
}
}