1
0
mirror of https://github.com/flarum/core.git synced 2025-08-16 21:34:08 +02:00

Allow overriding routes (#2577)

This commit is contained in:
Sami Mazouz
2021-02-28 20:01:30 +01:00
committed by GitHub
parent ea291508ab
commit ea840ba594
6 changed files with 160 additions and 9 deletions

View File

@@ -31,6 +31,7 @@ class Frontend implements ExtenderInterface
private $css = [];
private $js;
private $routes = [];
private $removedRoutes = [];
private $content = [];
public function __construct(string $frontend)
@@ -59,6 +60,13 @@ class Frontend implements ExtenderInterface
return $this;
}
public function removeRoute(string $name)
{
$this->removedRoutes[] = compact('name');
return $this;
}
/**
* @param callable|string $callback
* @return $this
@@ -141,7 +149,7 @@ class Frontend implements ExtenderInterface
private function registerRoutes(Container $container)
{
if (empty($this->routes)) {
if (empty($this->routes) && empty($this->removedRoutes)) {
return;
}
@@ -151,6 +159,10 @@ class Frontend implements ExtenderInterface
/** @var RouteHandlerFactory $factory */
$factory = $container->make(RouteHandlerFactory::class);
foreach ($this->removedRoutes as $route) {
$collection->removeRoute('GET', $route['name']);
}
foreach ($this->routes as $route) {
$collection->get(
$route['path'],

View File

@@ -19,6 +19,7 @@ class Routes implements ExtenderInterface
private $appName;
private $routes = [];
private $removedRoutes = [];
public function __construct($appName)
{
@@ -62,9 +63,16 @@ class Routes implements ExtenderInterface
return $this;
}
public function remove(string $method, string $name)
{
$this->removedRoutes[] = compact('method', 'name');
return $this;
}
public function extend(Container $container, Extension $extension = null)
{
if (empty($this->routes)) {
if (empty($this->routes) && empty($this->removedRoutes)) {
return;
}
@@ -74,6 +82,10 @@ 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->routes as $route) {
$collection->addRoute(
$route['method'],