Skip to content

Commit

Permalink
Require unique route names
Browse files Browse the repository at this point in the history
  • Loading branch information
SychO9 committed Apr 10, 2021
1 parent e376cf2 commit f5a2fff
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
6 changes: 3 additions & 3 deletions src/Extend/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function route(string $path, string $name, $content = null)

public function removeRoute(string $name)
{
$this->removedRoutes[] = compact('name');
$this->removedRoutes[] = $name;

return $this;
}
Expand Down Expand Up @@ -159,8 +159,8 @@ function (RouteCollection $collection, Container $container) {
/** @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) {
Expand Down
8 changes: 4 additions & 4 deletions src/Extend/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ private function route($httpMethod, $path, $name, $handler)
return $this;
}

public function remove(string $method, string $name)
public function remove(string $name)
{
$this->removedRoutes[] = compact('method', 'name');
$this->removedRoutes[] = $name;

return $this;
}
Expand All @@ -82,8 +82,8 @@ function (RouteCollection $collection, Container $container) {
/** @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) {
Expand Down
24 changes: 11 additions & 13 deletions src/Http/RouteCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,34 +73,32 @@ public function delete($path, $name, $handler)

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 = [];
Expand Down
4 changes: 2 additions & 2 deletions tests/integration/extenders/RoutesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function existing_route_can_be_removed()
{
$this->extend(
(new Extend\Routes('api'))
->remove('GET', 'forum.show')
->remove('forum.show')
);

$response = $this->send(
Expand All @@ -72,7 +72,7 @@ public function custom_route_can_override_existing_route_if_removed()
{
$this->extend(
(new Extend\Routes('api'))
->remove('GET', 'forum.show')
->remove('forum.show')
->get('/', 'forum.show', CustomRoute::class)
);

Expand Down

0 comments on commit f5a2fff

Please sign in to comment.