From 3bce83c073bfe65946c18d6d8a2d7fa5ad6103e7 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Sun, 18 Feb 2024 14:43:55 +0700 Subject: [PATCH 1/2] PoC array like routes --- tests/ArrayLikeRouteTest.php | 130 +++++++++++++++++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 tests/ArrayLikeRouteTest.php diff --git a/tests/ArrayLikeRouteTest.php b/tests/ArrayLikeRouteTest.php new file mode 100644 index 0000000..a4e0d0b --- /dev/null +++ b/tests/ArrayLikeRouteTest.php @@ -0,0 +1,130 @@ +fromArray($definition); + + $keys = [ + 'name', + 'pattern', + 'host', + 'hosts', + 'methods', + 'defaults', + 'override', + 'hasMiddlewares', + 'enabledMiddlewares', + ]; + foreach ($keys as $key) { + $this->assertSame($collection[0]->getData($key), $route->getData($key)); + } + } + + public static function dataArrayDefinition(): iterable + { + yield 'GET' => [ + [ + '/' => [], + ], + Route::get('/'), + ]; + yield 'POST' => [ + [ + '/' => [ + 'method' => Method::POST, + ], + ], + Route::post('/'), + ]; + yield 'GET, POST, DELETE' => [ + [ + '/' => [ + 'methods' => [Method::GET, Method::POST, Method::DELETE], + ], + ], + Route::methods([Method::GET, Method::POST, Method::DELETE], '/'), + ]; + + yield 'name' => [ + [ + '/' => [ + 'name' => 'test.route', + ], + ], + Route::get('/')->name('test.route'), + ]; + yield 'action' => [ + [ + '/' => [ + 'action' => [TestController::class, 'action'], + ], + ], + Route::get('/') + ->action([TestController::class, 'action']), + ]; + yield 'middlewares' => [ + [ + '/' => [ + 'action' => [TestController::class, 'action'], + 'middlewares' => [TestMiddleware1::class, TestMiddleware2::class], + ], + ], + Route::get('/') + ->middleware(TestMiddleware1::class, TestMiddleware2::class) + ->action([TestController::class, 'action']), + ]; + } + + /** + * @param array $routes + * @return Route[] + */ + private function fromArray(array $routes): array + { + $collection = []; + foreach ($routes as $pattern => $data) { + if (isset($data['method'])) { + $route = Route::methods([$data['method']], $pattern); + } elseif (isset($data['methods'])) { + $route = Route::methods($data['methods'], $pattern); + } else { + $route = Route::get($pattern); + } + if (isset($data['name'])) { + $route = $route->name($data['name']); + } + if (isset($data['host'])) { + $route = $route->host($data['host']); + } + if (isset($data['defaults'])) { + $route = $route->defaults($data['defaults']); + } + if (isset($data['middlewares'])) { + $route = $route->middleware(...$data['middlewares']); + } + if (isset($data['action'])) { + $route = $route->action($data['action']); + } + + $collection[] = $route; + } + + return $collection; + } +} From 8c2a29efd51638d4eec36c881a0570fdca825c39 Mon Sep 17 00:00:00 2001 From: xepozz Date: Sun, 18 Feb 2024 07:44:52 +0000 Subject: [PATCH 2/2] Apply Rector changes (CI) --- tests/ArrayLikeRouteTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/ArrayLikeRouteTest.php b/tests/ArrayLikeRouteTest.php index a4e0d0b..5b52795 100644 --- a/tests/ArrayLikeRouteTest.php +++ b/tests/ArrayLikeRouteTest.php @@ -92,7 +92,6 @@ public static function dataArrayDefinition(): iterable } /** - * @param array $routes * @return Route[] */ private function fromArray(array $routes): array