From 0e4448becf138690c6cbdc521175b07c2412c238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Cobucci?= Date: Tue, 6 Feb 2024 22:20:13 +0100 Subject: [PATCH] Adopt extract interface for route configuration MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Luís Cobucci --- README.md | 10 ++-- benchmark/DispatcherForBenchmark.php | 6 +-- src/FastRoute.php | 20 +++---- src/functions.php | 8 +-- test/Dispatcher/CachingTest.php | 4 +- test/Dispatcher/DispatcherTestCase.php | 74 +++++++++++++------------- test/FastRouteTest.php | 4 +- test/RouteCollectorTest.php | 9 ++-- 8 files changed, 69 insertions(+), 66 deletions(-) diff --git a/README.md b/README.md index 37ec4ae..9141d8a 100644 --- a/README.md +++ b/README.md @@ -27,7 +27,7 @@ Here's a basic usage example: require '/path/to/vendor/autoload.php'; -$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) { +$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\ConfigureRoutes $r) { $r->addRoute('GET', '/users', 'get_all_users_handler'); // {id} must be a number (\d+) $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler'); @@ -65,7 +65,7 @@ switch ($routeInfo[0]) { ### Defining routes The routes are defined by calling the `FastRoute\simpleDispatcher()` function, which accepts -a callable taking a `FastRoute\RouteCollector` instance. The routes are added by calling +a callable taking a `FastRoute\ConfigureRoutes` instance. The routes are added by calling `addRoute()` on the collector instance: ```php @@ -147,7 +147,7 @@ Additionally, you can specify routes inside a group. All routes defined inside a For example, defining your routes as: ```php -$r->addGroup('/admin', function (FastRoute\RouteCollector $r) { +$r->addGroup('/admin', function (FastRoute\ConfigureRoutes $r) { $r->addRoute('GET', '/do-something', 'handler'); $r->addRoute('GET', '/do-another-thing', 'handler'); $r->addRoute('GET', '/do-something-else', 'handler'); @@ -173,7 +173,7 @@ routing data and construct the dispatcher from the cached information: ```php addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/user/{name}', 'handler2'); @@ -275,7 +275,7 @@ through the options array: ```php 'FastRoute\\RouteParser\\Std', diff --git a/benchmark/DispatcherForBenchmark.php b/benchmark/DispatcherForBenchmark.php index 141153f..15065b2 100644 --- a/benchmark/DispatcherForBenchmark.php +++ b/benchmark/DispatcherForBenchmark.php @@ -3,9 +3,9 @@ namespace FastRoute\Benchmark; +use FastRoute\ConfigureRoutes; use FastRoute\DataGenerator; use FastRoute\Dispatcher; -use FastRoute\RouteCollector; use RuntimeException; use function FastRoute\simpleDispatcher; @@ -17,7 +17,7 @@ final class DispatcherForBenchmark public static function realLifeExample(string $dispatcher): Dispatcher { return simpleDispatcher( - static function (RouteCollector $routes): void { + static function (ConfigureRoutes $routes): void { $routes->addRoute('GET', '/', ['name' => 'home']); $routes->addRoute('GET', '/page/{page_slug:[a-zA-Z0-9\-]+}', ['name' => 'page.show']); $routes->addRoute('GET', '/about-us', ['name' => 'about-us']); @@ -68,7 +68,7 @@ static function (RouteCollector $routes): void { public static function manyRoutes(string $dispatcher, int $routeCount = 400): Dispatcher { return simpleDispatcher( - static function (RouteCollector $routes) use ($routeCount): void { + static function (ConfigureRoutes $routes) use ($routeCount): void { for ($i = 0; $i < $routeCount; ++$i) { $routes->addRoute('GET', '/abc' . $i, ['name' => 'static-' . $i]); $routes->addRoute('GET', '/abc{foo}/' . $i, ['name' => 'not-static-' . $i]); diff --git a/src/FastRoute.php b/src/FastRoute.php index 6048e17..9bc9cde 100644 --- a/src/FastRoute.php +++ b/src/FastRoute.php @@ -11,11 +11,11 @@ final class FastRoute { /** - * @param Closure(RouteCollector):void $routeDefinitionCallback + * @param Closure(ConfigureRoutes):void $routeDefinitionCallback * @param class-string $routeParser * @param class-string $dataGenerator * @param class-string $dispatcher - * @param class-string $routeCollector + * @param class-string $routesConfiguration * @param Cache|class-string|null $cacheDriver */ private function __construct( @@ -23,12 +23,12 @@ private function __construct( private readonly string $routeParser, private readonly string $dataGenerator, private readonly string $dispatcher, - private readonly string $routeCollector, + private readonly string $routesConfiguration, private readonly Cache|string|null $cacheDriver, ) { } - /** @param Closure(RouteCollector):void $routeDefinitionCallback */ + /** @param Closure(ConfigureRoutes):void $routeDefinitionCallback */ public static function recommendedSettings(Closure $routeDefinitionCallback): self { return new self( @@ -48,7 +48,7 @@ public function disableCache(): self $this->routeParser, $this->dataGenerator, $this->dispatcher, - $this->routeCollector, + $this->routesConfiguration, null, ); } @@ -61,7 +61,7 @@ public function withCache(Cache|string $driver): self $this->routeParser, $this->dataGenerator, $this->dispatcher, - $this->routeCollector, + $this->routesConfiguration, $driver, ); } @@ -97,7 +97,7 @@ public function useCustomDispatcher(string $dataGenerator, string $dispatcher): $this->routeParser, $dataGenerator, $dispatcher, - $this->routeCollector, + $this->routesConfiguration, $this->cacheDriver, ); } @@ -105,14 +105,14 @@ public function useCustomDispatcher(string $dataGenerator, string $dispatcher): public function dispatcher(string $cacheKey): Dispatcher { $loader = function (): array { - $collector = new $this->routeCollector( + $configuredRoutes = new $this->routesConfiguration( new $this->routeParser(), new $this->dataGenerator(), ); - ($this->routeDefinitionCallback)($collector); + ($this->routeDefinitionCallback)($configuredRoutes); - return $collector->getData(); + return $configuredRoutes->processedRoutes(); }; if ($this->cacheDriver === null) { diff --git a/src/functions.php b/src/functions.php index a619ed5..6e45d4e 100644 --- a/src/functions.php +++ b/src/functions.php @@ -16,7 +16,8 @@ * @see FastRoute::recommendedSettings() * @see FastRoute::disableCache() * - * @param array{routeParser?: class-string, dataGenerator?: class-string, dispatcher?: class-string, routeCollector?: class-string, cacheDisabled?: bool, cacheKey?: string, cacheFile?: string, cacheDriver?: class-string|Cache} $options + * @param callable(ConfigureRoutes):void $routeDefinitionCallback + * @param array{routeParser?: class-string, dataGenerator?: class-string, dispatcher?: class-string, routeCollector?: class-string, cacheDisabled?: bool, cacheKey?: string, cacheFile?: string, cacheDriver?: class-string|Cache} $options */ function simpleDispatcher(callable $routeDefinitionCallback, array $options = []): Dispatcher { @@ -31,7 +32,8 @@ function simpleDispatcher(callable $routeDefinitionCallback, array $options = [] * * @see FastRoute::recommendedSettings() * - * @param array{routeParser?: class-string, dataGenerator?: class-string, dispatcher?: class-string, routeCollector?: class-string, cacheDisabled?: bool, cacheKey?: string, cacheFile?: string, cacheDriver?: class-string|Cache} $options + * @param callable(ConfigureRoutes):void $routeDefinitionCallback + * @param array{routeParser?: class-string, dataGenerator?: class-string, dispatcher?: class-string, routeCollector?: class-string, cacheDisabled?: bool, cacheKey?: string, cacheFile?: string, cacheDriver?: class-string|Cache} $options */ function cachedDispatcher(callable $routeDefinitionCallback, array $options = []): Dispatcher { @@ -52,7 +54,7 @@ function cachedDispatcher(callable $routeDefinitionCallback, array $options = [] $routeDefinitionCallback($routeCollector); - return $routeCollector->getData(); + return $routeCollector->processedRoutes(); }; if ($options['cacheDisabled'] === true) { diff --git a/test/Dispatcher/CachingTest.php b/test/Dispatcher/CachingTest.php index e91b7da..02496b1 100644 --- a/test/Dispatcher/CachingTest.php +++ b/test/Dispatcher/CachingTest.php @@ -3,8 +3,8 @@ namespace FastRoute\Test\Dispatcher; +use FastRoute\ConfigureRoutes; use FastRoute\Dispatcher; -use FastRoute\RouteCollector; use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; @@ -30,7 +30,7 @@ public function warmUpCache(): void public function createDispatcher(string $optionName = 'cacheKey'): Dispatcher { return cachedDispatcher( - static function (RouteCollector $collector): void { + static function (ConfigureRoutes $collector): void { $collector->get('/testing', ['test']); $collector->get('/admin/{page}', ['admin-page']); }, diff --git a/test/Dispatcher/DispatcherTestCase.php b/test/Dispatcher/DispatcherTestCase.php index eb66c7d..ba45688 100644 --- a/test/Dispatcher/DispatcherTestCase.php +++ b/test/Dispatcher/DispatcherTestCase.php @@ -5,12 +5,12 @@ use Closure; use FastRoute\BadRouteException; +use FastRoute\ConfigureRoutes; use FastRoute\DataGenerator; use FastRoute\Dispatcher; use FastRoute\Dispatcher\Result\Matched; use FastRoute\Dispatcher\Result\MethodNotAllowed; use FastRoute\Dispatcher\Result\NotMatched; -use FastRoute\RouteCollector; use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; @@ -125,7 +125,7 @@ public function duplicateVariableNameError(): void $this->expectException(BadRouteException::class); $this->expectExceptionMessage('Cannot use the same placeholder "test" twice'); - simpleDispatcher(static function (RouteCollector $r): void { + simpleDispatcher(static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/foo/{test}/{test:\d+}', 'handler0'); }, $this->generateDispatcherOptions()); } @@ -136,7 +136,7 @@ public function duplicateVariableRoute(): void $this->expectException(BadRouteException::class); $this->expectExceptionMessage('Cannot register two routes matching "/user/([^/]+)" for method "GET"'); - simpleDispatcher(static function (RouteCollector $r): void { + simpleDispatcher(static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user/{id}', 'handler0'); // oops, forgot \d+ restriction ;) $r->addRoute('GET', '/user/{name}', 'handler1'); }, $this->generateDispatcherOptions()); @@ -148,7 +148,7 @@ public function duplicateStaticRoute(): void $this->expectException(BadRouteException::class); $this->expectExceptionMessage('Cannot register two routes matching "/user" for method "GET"'); - simpleDispatcher(static function (RouteCollector $r): void { + simpleDispatcher(static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user', 'handler0'); $r->addRoute('GET', '/user', 'handler1'); }, $this->generateDispatcherOptions()); @@ -160,7 +160,7 @@ public function shadowedStaticRoute(): void $this->expectException(BadRouteException::class); $this->expectExceptionMessage('Static route "/user/nikic" is shadowed by previously defined variable route "/user/([^/]+)" for method "GET"'); - simpleDispatcher(static function (RouteCollector $r): void { + simpleDispatcher(static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('GET', '/user/nikic', 'handler1'); }, $this->generateDispatcherOptions()); @@ -172,21 +172,21 @@ public function capturing(): void $this->expectException(BadRouteException::class); $this->expectExceptionMessage('Regex "(en|de)" for parameter "lang" contains a capturing group'); - simpleDispatcher(static function (RouteCollector $r): void { + simpleDispatcher(static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/{lang:(en|de)}', 'handler0'); }, $this->generateDispatcherOptions()); } - /** @return iterable}> */ + /** @return iterable}> */ public static function provideFoundDispatchCases(): iterable { - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/resource/123/456', 'handler0'); }; yield 'single static route' => ['GET', '/resource/123/456', $callback, 'handler0', [], ['_route' => '/resource/123/456']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/handler0', 'handler0'); $r->addRoute('GET', '/handler1', 'handler1'); $r->addRoute('GET', '/handler2', 'handler2'); @@ -194,7 +194,7 @@ public static function provideFoundDispatchCases(): iterable yield 'multiple static routes' => ['GET', '/handler2', $callback, 'handler2', [], ['_route' => '/handler2']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/user/{name}', 'handler2'); @@ -205,7 +205,7 @@ public static function provideFoundDispatchCases(): iterable yield 'parameter matching precedence {/user/rdlowrey}' => ['GET', '/user/rdlowrey', $callback, 'handler2', ['name' => 'rdlowrey'], ['_route' => '/user/{name}']]; yield 'parameter matching precedence {/user/NaN}' => ['GET', '/user/NaN', $callback, 'handler2', ['name' => 'NaN'], ['_route' => '/user/{name}']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/12345/extension', 'handler1'); $r->addRoute('GET', '/user/{id:[0-9]+}.{extension}', 'handler2'); @@ -213,7 +213,7 @@ public static function provideFoundDispatchCases(): iterable yield 'dynamic file extensions' => ['GET', '/user/12345.svg', $callback, 'handler2', ['id' => '12345', 'extension' => 'svg'], ['_route' => '/user/{id:[0-9]+}.{extension}']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/static0', 'handler2'); @@ -226,14 +226,14 @@ public static function provideFoundDispatchCases(): iterable yield 'fallback to GET on HEAD route miss {/static0}' => ['HEAD', '/static0', $callback, 'handler2', [], ['_route' => '/static0']]; yield 'registered HEAD route is used' => ['HEAD', '/static1', $callback, 'handler4', [], ['_route' => '/static1']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('POST', '/user/{name:[a-z]+}', 'handler1'); }; yield 'more specific routes are not shadowed by less specific of another method' => ['POST', '/user/rdlowrey', $callback, 'handler1', ['name' => 'rdlowrey'], ['_route' => '/user/{name:[a-z]+}']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('POST', '/user/{name:[a-z]+}', 'handler1'); $r->addRoute('POST', '/user/{name}', 'handler2'); @@ -242,14 +242,14 @@ public static function provideFoundDispatchCases(): iterable yield 'more specific routes are used, according to the registration order {/user/rdlowrey}' => ['POST', '/user/rdlowrey', $callback, 'handler1', ['name' => 'rdlowrey'], ['_route' => '/user/{name:[a-z]+}']]; yield 'more specific routes are used, according to the registration order {/user/rdlowrey1}' => ['POST', '/user/rdlowrey1', $callback, 'handler2', ['name' => 'rdlowrey1'], ['_route' => '/user/{name}']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user/{name}', 'handler0'); $r->addRoute('GET', '/user/{name}/edit', 'handler1'); }; yield 'route with constant suffix' => ['GET', '/user/rdlowrey/edit', $callback, 'handler1', ['name' => 'rdlowrey'], ['_route' => '/user/{name}/edit']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute(['GET', 'POST'], '/user', 'handlerGetPost'); $r->addRoute(['DELETE'], '/user', 'handlerDelete'); $r->addRoute([], '/user', 'handlerNone'); @@ -259,41 +259,41 @@ public static function provideFoundDispatchCases(): iterable yield 'multiple methods with the same handler {' . $method . '}' => [$method, '/user', $callback, $handler, [], ['_route' => '/user']]; } - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('POST', '/user.json', 'handler0'); $r->addRoute('GET', '/{entity}.json', 'handler1'); }; yield 'fallback to dynamic routes when method does not match' => ['GET', '/user.json', $callback, 'handler1', ['entity' => 'user'], ['_route' => '/{entity}.json']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '', 'handler0'); }; yield 'match empty route' => ['GET', '', $callback, 'handler0', [], ['_route' => '']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('HEAD', '/a/{foo}', 'handler0'); $r->addRoute('GET', '/b/{foo}', 'handler1'); }; yield 'fallback to GET route on HEAD miss {dynamic routes}' => ['HEAD', '/b/bar', $callback, 'handler1', ['foo' => 'bar'], ['_route' => '/b/{foo}']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('HEAD', '/a', 'handler0'); $r->addRoute('GET', '/b', 'handler1'); }; yield 'fallback to GET route on HEAD miss {static routes}' => ['HEAD', '/b', $callback, 'handler1', [], ['_route' => '/b']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/foo', 'handler0'); $r->addRoute('HEAD', '/{bar}', 'handler1'); }; yield 'fallback to GET route on HEAD miss {dynamic/static routes}' => ['HEAD', '/foo', $callback, 'handler1', ['bar' => 'foo'], ['_route' => '/{bar}']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('*', '/user', 'handler0'); $r->addRoute('*', '/{user}', 'handler1'); $r->addRoute('GET', '/user', 'handler2'); @@ -310,33 +310,33 @@ public static function provideFoundDispatchCases(): iterable yield 'fallback method is used when needed {' . $method . ',dynamic}' => [$method, '/foo', $callback, 'handler1', ['user' => 'foo'], ['_route' => '/{user}']]; } - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/{bar}', 'handler0'); $r->addRoute('*', '/foo', 'handler1'); }; yield 'fallback method is used as last resource' => ['GET', '/foo', $callback, 'handler0', ['bar' => 'foo'], ['_route' => '/{bar}']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user', 'handler0'); $r->addRoute('*', '/{foo:.*}', 'handler1'); }; yield 'fallback method can capture arguments' => ['POST', '/bar', $callback, 'handler1', ['foo' => 'bar'], ['_route' => '/{foo:.*}']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('OPTIONS', '/about', 'handler0'); }; yield 'options method is supported' => ['OPTIONS', '/about', $callback, 'handler0', [], ['_route' => '/about']]; } - /** @return iterable */ + /** @return iterable */ public static function provideNotFoundDispatchCases(): iterable { $methods = ['GET', 'POST', 'DELETE', 'PUT', 'HEAD', 'OPTIONS']; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/resource/123/456', 'handler0'); }; @@ -344,7 +344,7 @@ public static function provideNotFoundDispatchCases(): iterable yield 'single static route {' . $method . '}' => [$method, '/not-found', $callback]; } - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/handler0', 'handler0'); $r->addRoute('GET', '/handler1', 'handler1'); $r->addRoute('GET', '/handler2', 'handler2'); @@ -354,7 +354,7 @@ public static function provideNotFoundDispatchCases(): iterable yield 'multiple static routes {' . $method . '}' => [$method, '/not-found', $callback]; } - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1'); $r->addRoute('GET', '/user/{name}', 'handler2'); @@ -367,16 +367,16 @@ public static function provideNotFoundDispatchCases(): iterable } } - /** @return iterable}> */ + /** @return iterable}> */ public static function provideMethodNotAllowedDispatchCases(): iterable { - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/resource/123/456', 'handler0'); }; yield 'match static routes' => ['POST', '/resource/123/456', $callback, ['GET']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/resource/123/456', 'handler0'); $r->addRoute('POST', '/resource/123/456', 'handler1'); $r->addRoute('PUT', '/resource/123/456', 'handler2'); @@ -385,7 +385,7 @@ public static function provideMethodNotAllowedDispatchCases(): iterable yield 'ignore fallbacks for unmatched routes ' => ['DELETE', '/resource/123/456', $callback, ['GET', 'POST', 'PUT']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0'); $r->addRoute('POST', '/user/{name}/{id:[0-9]+}', 'handler1'); $r->addRoute('PUT', '/user/{name}/{id:[0-9]+}', 'handler2'); @@ -394,7 +394,7 @@ public static function provideMethodNotAllowedDispatchCases(): iterable yield 'match dynamic routes' => ['DELETE', '/user/rdlowrey/42', $callback, ['GET', 'POST', 'PUT', 'PATCH']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('POST', '/user/{name}', 'handler1'); $r->addRoute('PUT', '/user/{name:[a-z]+}', 'handler2'); $r->addRoute('PATCH', '/user/{name:[a-z]+}', 'handler3'); @@ -402,7 +402,7 @@ public static function provideMethodNotAllowedDispatchCases(): iterable yield 'match with and without validations' => ['GET', '/user/rdlowrey', $callback, ['POST', 'PUT', 'PATCH']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('POST', '/user/{name}', 'handler1'); $r->addRoute('PUT', '/user/{name:[a-z]+}', 'handler2'); $r->addRoute('PATCH', '/user/{name:[a-z]+}', 'handler3'); @@ -411,7 +411,7 @@ public static function provideMethodNotAllowedDispatchCases(): iterable yield 'match respects validations' => ['GET', '/user/rdlowrey42', $callback, ['POST', 'DELETE']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute(['GET', 'POST'], '/user', 'handlerGetPost'); $r->addRoute(['DELETE'], '/user', 'handlerDelete'); $r->addRoute([], '/user', 'handlerNone'); @@ -419,7 +419,7 @@ public static function provideMethodNotAllowedDispatchCases(): iterable yield 'match all valid methods' => ['PUT', '/user', $callback, ['GET', 'POST', 'DELETE']]; - $callback = static function (RouteCollector $r): void { + $callback = static function (ConfigureRoutes $r): void { $r->addRoute('POST', '/user.json', 'handler0'); $r->addRoute('GET', '/{entity}.json', 'handler1'); }; diff --git a/test/FastRouteTest.php b/test/FastRouteTest.php index f2de1a9..2362481 100644 --- a/test/FastRouteTest.php +++ b/test/FastRouteTest.php @@ -4,9 +4,9 @@ namespace FastRoute\Test; use FastRoute\Cache; +use FastRoute\ConfigureRoutes; use FastRoute\Dispatcher; use FastRoute\FastRoute; -use FastRoute\RouteCollector; use PHPUnit\Framework\Attributes as PHPUnit; use PHPUnit\Framework\TestCase; use RuntimeException; @@ -94,7 +94,7 @@ public function get(string $key, callable $loader): array self::assertSame(['test' => true], $result->extraParameters); // should use data from cache, not from loader } - private static function routes(RouteCollector $collector): void + private static function routes(ConfigureRoutes $collector): void { $collector->get('/', 'test'); } diff --git a/test/RouteCollectorTest.php b/test/RouteCollectorTest.php index 9930b26..99ff33f 100644 --- a/test/RouteCollectorTest.php +++ b/test/RouteCollectorTest.php @@ -3,6 +3,7 @@ namespace FastRoute\Test; +use FastRoute\ConfigureRoutes; use FastRoute\DataGenerator; use FastRoute\RouteCollector; use FastRoute\RouteParser\Std; @@ -59,7 +60,7 @@ public function routesCanBeGrouped(): void $r->put('/put', 'put'); $r->options('/options', 'options'); - $r->addGroup('/group-one', static function (RouteCollector $r): void { + $r->addGroup('/group-one', static function (ConfigureRoutes $r): void { $r->delete('/delete', 'delete'); $r->get('/get', 'get'); $r->head('/head', 'head'); @@ -68,7 +69,7 @@ public function routesCanBeGrouped(): void $r->put('/put', 'put'); $r->options('/options', 'options'); - $r->addGroup('/group-two', static function (RouteCollector $r): void { + $r->addGroup('/group-two', static function (ConfigureRoutes $r): void { $r->delete('/delete', 'delete'); $r->get('/get', 'get'); $r->head('/head', 'head'); @@ -79,10 +80,10 @@ public function routesCanBeGrouped(): void }); }); - $r->addGroup('/admin', static function (RouteCollector $r): void { + $r->addGroup('/admin', static function (ConfigureRoutes $r): void { $r->get('-some-info', 'admin-some-info'); }); - $r->addGroup('/admin-', static function (RouteCollector $r): void { + $r->addGroup('/admin-', static function (ConfigureRoutes $r): void { $r->get('more-info', 'admin-more-info'); });