1
0
mirror of https://github.com/flarum/core.git synced 2025-06-08 15:44:57 +02:00

Require unique route names (#2771)

This commit is contained in:
Sami Mazouz 2021-04-10 20:38:25 +01:00 committed by GitHub
parent e376cf2079
commit 548f1321f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 20 additions and 22 deletions

View File

@ -62,7 +62,7 @@ class Frontend implements ExtenderInterface
public function removeRoute(string $name) public function removeRoute(string $name)
{ {
$this->removedRoutes[] = compact('name'); $this->removedRoutes[] = $name;
return $this; return $this;
} }
@ -159,8 +159,8 @@ class Frontend implements ExtenderInterface
/** @var RouteHandlerFactory $factory */ /** @var RouteHandlerFactory $factory */
$factory = $container->make(RouteHandlerFactory::class); $factory = $container->make(RouteHandlerFactory::class);
foreach ($this->removedRoutes as $route) { foreach ($this->removedRoutes as $routeName) {
$collection->removeRoute('GET', $route['name']); $collection->removeRoute($routeName);
} }
foreach ($this->routes as $route) { foreach ($this->routes as $route) {

View File

@ -63,9 +63,9 @@ class Routes implements ExtenderInterface
return $this; return $this;
} }
public function remove(string $method, string $name) public function remove(string $name)
{ {
$this->removedRoutes[] = compact('method', 'name'); $this->removedRoutes[] = $name;
return $this; return $this;
} }
@ -82,8 +82,8 @@ class Routes implements ExtenderInterface
/** @var RouteHandlerFactory $factory */ /** @var RouteHandlerFactory $factory */
$factory = $container->make(RouteHandlerFactory::class); $factory = $container->make(RouteHandlerFactory::class);
foreach ($this->removedRoutes as $route) { foreach ($this->removedRoutes as $routeName) {
$collection->removeRoute($route['method'], $route['name']); $collection->removeRoute($routeName);
} }
foreach ($this->routes as $route) { foreach ($this->routes as $route) {

View File

@ -73,35 +73,33 @@ class RouteCollection
public function addRoute($method, $path, $name, $handler) public function addRoute($method, $path, $name, $handler)
{ {
if (isset($this->routes[$method][$name])) { if (isset($this->routes[$name])) {
throw new \RuntimeException("Route $name on method $method already exists"); 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; 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; return $this;
} }
protected function applyRoutes(): void protected function applyRoutes(): void
{ {
foreach ($this->pendingRoutes as $method => $routes) { foreach ($this->pendingRoutes as $name => $route) {
foreach ($routes as $name => $route) {
$routeDatas = $this->routeParser->parse($route['path']); $routeDatas = $this->routeParser->parse($route['path']);
foreach ($routeDatas as $routeData) { foreach ($routeDatas as $routeData) {
$this->dataGenerator->addRoute($method, $routeData, ['name' => $name, 'handler' => $route['handler']]); $this->dataGenerator->addRoute($route['method'], $routeData, ['name' => $name, 'handler' => $route['handler']]);
} }
$this->reverse[$name] = $routeDatas; $this->reverse[$name] = $routeDatas;
} }
}
$this->pendingRoutes = []; $this->pendingRoutes = [];
} }

View File

@ -55,7 +55,7 @@ class RoutesTest extends TestCase
{ {
$this->extend( $this->extend(
(new Extend\Routes('api')) (new Extend\Routes('api'))
->remove('GET', 'forum.show') ->remove('forum.show')
); );
$response = $this->send( $response = $this->send(
@ -72,7 +72,7 @@ class RoutesTest extends TestCase
{ {
$this->extend( $this->extend(
(new Extend\Routes('api')) (new Extend\Routes('api'))
->remove('GET', 'forum.show') ->remove('forum.show')
->get('/', 'forum.show', CustomRoute::class) ->get('/', 'forum.show', CustomRoute::class)
); );