1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 02:54:03 +00:00

Allow cloning specific routes by name

This commit is contained in:
lukinovec 2025-05-19 08:39:09 +02:00
parent 3b90c91763
commit 645232e53c
2 changed files with 14 additions and 5 deletions

View file

@ -32,7 +32,7 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver;
class CloneRoutesAsTenant class CloneRoutesAsTenant
{ {
protected array $routesToClone = []; protected array $routesToClone = [];
protected Closure|null $cloneUsing = null; protected Closure|null $cloneUsing = null; // The callback should accept Route instance or the route name (string)
protected Closure|null $shouldBeCloned = null; protected Closure|null $shouldBeCloned = null;
protected array $cloneRoutesWithMiddleware = ['clone']; protected array $cloneRoutesWithMiddleware = ['clone'];
@ -59,6 +59,11 @@ class CloneRoutesAsTenant
continue; continue;
} }
if (is_string($route)) {
$this->router->getRoutes()->refreshNameLookups();
$route = $this->router->getRoutes()->getByName($route);
}
$this->copyMiscRouteProperties($route, $this->createNewRoute($route)); $this->copyMiscRouteProperties($route, $this->createNewRoute($route));
} }
@ -86,7 +91,7 @@ class CloneRoutesAsTenant
return $this; return $this;
} }
public function cloneRoute(Route $route): static public function cloneRoute(Route|string $route): static
{ {
$this->routesToClone[] = $route; $this->routesToClone[] = $route;

View file

@ -113,7 +113,7 @@ test('custom callbacks can be used for customizing the creation of the cloned ro
pest()->get(route('cloned.bar'))->assertSee('cloned route'); pest()->get(route('cloned.bar'))->assertSee('cloned route');
}); });
test('the clone action can clone specific routes', function() { test('the clone action can clone specific routes', function(bool $cloneRouteByName) {
RouteFacade::get('/foo', fn () => true)->name('foo'); RouteFacade::get('/foo', fn () => true)->name('foo');
$barRoute = RouteFacade::get('/bar', fn () => true)->name('bar'); $barRoute = RouteFacade::get('/bar', fn () => true)->name('bar');
RouteFacade::get('/baz', fn () => true)->name('baz'); RouteFacade::get('/baz', fn () => true)->name('baz');
@ -124,13 +124,17 @@ test('the clone action can clone specific routes', function() {
/** @var CloneRoutesAsTenant $cloneRoutesAction */ /** @var CloneRoutesAsTenant $cloneRoutesAction */
$cloneRoutesAction = app(CloneRoutesAsTenant::class); $cloneRoutesAction = app(CloneRoutesAsTenant::class);
$cloneRoutesAction->cloneRoute($barRoute)->handle(); // A route instance or a route name can be passed to cloneRoute()
$cloneRoutesAction->cloneRoute($cloneRouteByName ? $barRoute->getName() : $barRoute)->handle();
// Exactly one route should be cloned // Exactly one route should be cloned
expect($currentRouteCount())->toEqual($initialRouteCount + 1); expect($currentRouteCount())->toEqual($initialRouteCount + 1);
expect(RouteFacade::getRoutes()->getByName('tenant.bar'))->not()->toBeNull(); expect(RouteFacade::getRoutes()->getByName('tenant.bar'))->not()->toBeNull();
}); })->with([
true,
false,
]);
test('the clone action prefixes already prefixed routes correctly', function () { test('the clone action prefixes already prefixed routes correctly', function () {
$routes = [ $routes = [