From 598bb94657e644b313aea898c6150d6ead867f54 Mon Sep 17 00:00:00 2001 From: Sami Mazouz Date: Sat, 10 Apr 2021 20:38:25 +0100 Subject: [PATCH] Require unique route names (#2771) --- framework/core/src/Extend/Frontend.php | 6 ++--- framework/core/src/Extend/Routes.php | 8 +++---- framework/core/src/Http/RouteCollection.php | 24 +++++++++---------- .../integration/extenders/RoutesTest.php | 4 ++-- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/framework/core/src/Extend/Frontend.php b/framework/core/src/Extend/Frontend.php index ba515cf6f..1726307d9 100644 --- a/framework/core/src/Extend/Frontend.php +++ b/framework/core/src/Extend/Frontend.php @@ -62,7 +62,7 @@ class Frontend implements ExtenderInterface public function removeRoute(string $name) { - $this->removedRoutes[] = compact('name'); + $this->removedRoutes[] = $name; return $this; } @@ -159,8 +159,8 @@ class Frontend implements ExtenderInterface /** @var RouteHandlerFactory $factory */ $factory = $container->make(RouteHandlerFactory::class); - foreach ($this->removedRoutes as $route) { - $collection->removeRoute('GET', $route['name']); + foreach ($this->removedRoutes as $routeName) { + $collection->removeRoute($routeName); } foreach ($this->routes as $route) { diff --git a/framework/core/src/Extend/Routes.php b/framework/core/src/Extend/Routes.php index 5cc0d9f90..e78099823 100644 --- a/framework/core/src/Extend/Routes.php +++ b/framework/core/src/Extend/Routes.php @@ -63,9 +63,9 @@ class Routes implements ExtenderInterface return $this; } - public function remove(string $method, string $name) + public function remove(string $name) { - $this->removedRoutes[] = compact('method', 'name'); + $this->removedRoutes[] = $name; return $this; } @@ -82,8 +82,8 @@ class Routes implements ExtenderInterface /** @var RouteHandlerFactory $factory */ $factory = $container->make(RouteHandlerFactory::class); - foreach ($this->removedRoutes as $route) { - $collection->removeRoute($route['method'], $route['name']); + foreach ($this->removedRoutes as $routeName) { + $collection->removeRoute($routeName); } foreach ($this->routes as $route) { diff --git a/framework/core/src/Http/RouteCollection.php b/framework/core/src/Http/RouteCollection.php index f7da73aa2..57f7c9449 100644 --- a/framework/core/src/Http/RouteCollection.php +++ b/framework/core/src/Http/RouteCollection.php @@ -73,34 +73,32 @@ class RouteCollection public function addRoute($method, $path, $name, $handler) { - if (isset($this->routes[$method][$name])) { - throw new \RuntimeException("Route $name on method $method already exists"); + if (isset($this->routes[$name])) { + throw new \RuntimeException("Route $name already exists"); } - $this->routes[$method][$name] = $this->pendingRoutes[$method][$name] = compact('path', 'handler'); + $this->routes[$name] = $this->pendingRoutes[$name] = compact('method', 'path', 'handler'); return $this; } - public function removeRoute(string $method, string $name): self + public function removeRoute(string $name): self { - unset($this->routes[$method][$name], $this->pendingRoutes[$method][$name]); + unset($this->routes[$name], $this->pendingRoutes[$name]); return $this; } protected function applyRoutes(): void { - foreach ($this->pendingRoutes as $method => $routes) { - foreach ($routes as $name => $route) { - $routeDatas = $this->routeParser->parse($route['path']); + foreach ($this->pendingRoutes as $name => $route) { + $routeDatas = $this->routeParser->parse($route['path']); - foreach ($routeDatas as $routeData) { - $this->dataGenerator->addRoute($method, $routeData, ['name' => $name, 'handler' => $route['handler']]); - } - - $this->reverse[$name] = $routeDatas; + foreach ($routeDatas as $routeData) { + $this->dataGenerator->addRoute($route['method'], $routeData, ['name' => $name, 'handler' => $route['handler']]); } + + $this->reverse[$name] = $routeDatas; } $this->pendingRoutes = []; diff --git a/framework/core/tests/integration/extenders/RoutesTest.php b/framework/core/tests/integration/extenders/RoutesTest.php index f451d611d..3458219f4 100644 --- a/framework/core/tests/integration/extenders/RoutesTest.php +++ b/framework/core/tests/integration/extenders/RoutesTest.php @@ -55,7 +55,7 @@ class RoutesTest extends TestCase { $this->extend( (new Extend\Routes('api')) - ->remove('GET', 'forum.show') + ->remove('forum.show') ); $response = $this->send( @@ -72,7 +72,7 @@ class RoutesTest extends TestCase { $this->extend( (new Extend\Routes('api')) - ->remove('GET', 'forum.show') + ->remove('forum.show') ->get('/', 'forum.show', CustomRoute::class) );