1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 11:14:04 +00:00

[4.x] Route cloning refactor (#1353)

* Refactor cloning action, update tests

* Delete redundant "should not be cloned" part from shouldBeCloned()

* Use 'clone' instead of a universal route in tenant parameter removal test

* Improve comment

* Add test for cloneRoutesWithMiddleware(), correct existing tests

* Allow cloning specific routes by name

* Fix typo in CloneActionTest

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* clean up CloneRoutesAsTenant, add a todo

* phpstan

* Add test for handling 'clone'  in MW groups

* Improve regression test

* Improve regression test

* Handle nested cloning flags in CloneRoutesAsTenant

* Ignore routes that are already considered tenant routes from cloning, update test accordingly

* Clarify cloning logic

* CloneRoutesAsTenant cleanup

* Rewrite clone action annotation, fix fluent usage bug

* Improve tests (comments, use $tenant->id instead of $tenant->getTenantKey())

* Test that the clone action can be used fluently without issues now (could serve as a regression test for the routesToClone change in previous commit)

* Minor annotation improvements

* Improve route cloning action docblock

* Add note about clearing the $routesToClone property

* improve docblock

* clean up tests

* fix typo

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Samuel Štancl <samuel@archte.ch>
This commit is contained in:
lukinovec 2025-07-01 00:32:42 +02:00 committed by GitHub
parent 7e1fe075f4
commit 1e926a1dde
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 362 additions and 351 deletions

View file

@ -7,40 +7,72 @@ namespace Stancl\Tenancy\Actions;
use Closure;
use Illuminate\Routing\Route;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Stancl\Tenancy\Enums\RouteMode;
use Stancl\Tenancy\Resolvers\PathTenantResolver;
/**
* The CloneRoutesAsTenant action clones
* routes flagged with the 'universal' middleware,
* all routes without a flag if the default route mode is universal,
* and routes that directly use the InitializeTenancyByPath middleware.
* Clones either all existing routes for which shouldBeCloned() returns true
* (by default, all routes with any middleware present in $cloneRoutesWithMiddleware),
* or if any routes were manually added to $routesToClone using $action->cloneRoute($route),
* clone just the routes in $routesToClone. This means that only the routes specified
* by cloneRoute() (which can be chained infinitely -- you can specify as many routes as you want)
* will be cloned.
*
* The main purpose of this action is to make the integration
* of packages (e.g., Jetstream or Livewire) easier with path-based tenant identification.
* The main purpose of this action is to make the integration of packages
* (e.g., Jetstream or Livewire) easier with path-based tenant identification.
*
* By default, universal routes are cloned as tenant routes (= they get flagged with the 'tenant' middleware)
* and prefixed with the '/{tenant}' path prefix. Their name also gets prefixed with the tenant name prefix.
* The default for $cloneRoutesWithMiddleware is ['clone'].
* If $routesToClone is empty, all routes with any middleware specified in $cloneRoutesWithMiddleware will be cloned.
* The middleware can be in a group, nested as deep as you want
* (e.g. if a route has a 'foo' middleware which is a group containing the 'clone' middleware, the route will be cloned).
*
* Routes with the path identification middleware get cloned similarly, but only if they're not universal at the same time.
* Unlike universal routes, these routes don't get the tenant flag,
* because they don't need it (they're not universal, and they have the identification MW, so they're already considered tenant).
* You may customize $cloneRoutesWithMiddleware using cloneRoutesWithMiddleware() to make any middleware of your choice trigger cloning.
* By providing a callback to shouldClone(), you can change how it's determined if a route should be cloned if you don't want to use middleware flags.
*
* You can use the `cloneUsing()` hook to customize the route definitions,
* and the `skipRoute()` method to skip cloning of specific routes.
* You can also use the $tenantParameterName and $tenantRouteNamePrefix
* static properties to customize the tenant parameter name or the route name prefix.
* Cloned routes are prefixed with '/{tenant}', flagged with 'tenant' middleware, and have their names prefixed with 'tenant.'.
* The parameter name and prefix can be changed e.g. to `/{team}` and `team.` by configuring the path resolver (tenantParameterName and tenantRouteNamePrefix).
* Routes with names that are already prefixed won't be cloned - but that's just the default behavior.
* The cloneUsing() method allows you to completely override the default behavior and customize how the cloned routes will be defined.
*
* Note that routes already containing the tenant parameter or prefix won't be cloned.
* After cloning, only top-level middleware in $cloneRoutesWithMiddleware will be removed
* from the new route (so by default, 'clone' will be omitted from the new route's MW).
* Middleware groups are preserved as-is, even if they contain cloning middleware.
*
* Routes that already contain the tenant parameter or have names with the tenant prefix
* will not be cloned.
*
* Example usage:
* ```
* Route::get('/foo', fn () => true)->name('foo')->middleware('clone');
* Route::get('/bar', fn () => true)->name('bar')->middleware('universal');
*
* $cloneAction = app(CloneRoutesAsTenant::class);
*
* // Clone foo route as /{tenant}/foo/ and name it tenant.foo ('clone' middleware won't be present in the cloned route)
* $cloneAction->handle();
*
* // Clone bar route as /{tenant}/bar and name it tenant.bar ('universal' middleware won't be present in the cloned route)
* $cloneAction->cloneRoutesWithMiddleware(['universal'])->handle();
*
* Route::get('/baz', fn () => true)->name('baz');
*
* // Clone baz route as /{tenant}/bar and name it tenant.baz ('universal' middleware won't be present in the cloned route)
* $cloneAction->cloneRoute('baz')->handle();
* ```
*
* Calling handle() will also clear the $routesToClone array.
* This means that $action->cloneRoute('foo')->handle() will clone the 'foo' route, but subsequent calls to handle() will behave
* as if cloneRoute() wasn't called at all ($routesToClone will be empty).
* Note that calling handle() does not reset the other properties.
*
* @see Stancl\Tenancy\Resolvers\PathTenantResolver
*/
class CloneRoutesAsTenant
{
protected array $cloneRouteUsing = [];
protected array $skippedRoutes = [
'stancl.tenancy.asset',
];
protected array $routesToClone = [];
protected Closure|null $cloneUsing = null; // The callback should accept Route instance or the route name (string)
protected Closure|null $shouldClone = null;
protected array $cloneRoutesWithMiddleware = ['clone'];
public function __construct(
protected Router $router,
@ -48,100 +80,77 @@ class CloneRoutesAsTenant
public function handle(): void
{
$this->getRoutesToClone()->each(fn (Route $route) => $this->cloneRoute($route));
// If no routes were specified using cloneRoute(), get all routes
// and for each, determine if it should be cloned
if (! $this->routesToClone) {
$this->routesToClone = collect($this->router->getRoutes()->get())
->filter(fn (Route $route) => $this->shouldBeCloned($route))
->all();
}
foreach ($this->routesToClone as $route) {
// If the cloneUsing callback is set,
// use the callback to clone the route instead of the default
if ($this->cloneUsing) {
($this->cloneUsing)($route);
continue;
}
if (is_string($route)) {
$this->router->getRoutes()->refreshNameLookups();
$route = $this->router->getRoutes()->getByName($route);
}
$this->copyMiscRouteProperties($route, $this->createNewRoute($route));
}
// Clean up the routesToClone array after cloning so that subsequent calls aren't affected
$this->routesToClone = [];
$this->router->getRoutes()->refreshNameLookups();
}
/**
* Make the action clone a specific route using the provided callback instead of the default one.
*/
public function cloneUsing(string $routeName, Closure $callback): static
public function cloneUsing(Closure|null $cloneUsing): static
{
$this->cloneRouteUsing[$routeName] = $callback;
$this->cloneUsing = $cloneUsing;
return $this;
}
/**
* Skip a route's cloning.
*/
public function skipRoute(string $routeName): static
public function cloneRoutesWithMiddleware(array $middleware): static
{
$this->skippedRoutes[] = $routeName;
$this->cloneRoutesWithMiddleware = $middleware;
return $this;
}
/**
* @return Collection<int, Route>
*/
protected function getRoutesToClone(): Collection
public function shouldClone(Closure|null $shouldClone): static
{
$tenantParameterName = PathTenantResolver::tenantParameterName();
$this->shouldClone = $shouldClone;
/**
* Clone all routes that:
* - don't have the tenant parameter
* - aren't in the $skippedRoutes array
* - are using path identification (kernel or route-level).
*
* Non-universal cloned routes will only be available in the tenant context,
* universal routes will be available in both contexts.
*/
return collect($this->router->getRoutes()->get())->filter(function (Route $route) use ($tenantParameterName) {
if (
tenancy()->routeHasMiddleware($route, 'tenant') ||
in_array($route->getName(), $this->skippedRoutes, true) ||
in_array($tenantParameterName, $route->parameterNames(), true)
) {
return false;
}
$pathIdentificationMiddleware = config('tenancy.identification.path_identification_middleware');
$routeHasPathIdentificationMiddleware = tenancy()->routeHasMiddleware($route, $pathIdentificationMiddleware);
$routeHasNonPathIdentificationMiddleware = tenancy()->routeHasIdentificationMiddleware($route) && ! $routeHasPathIdentificationMiddleware;
$pathIdentificationMiddlewareInGlobalStack = tenancy()->globalStackHasMiddleware($pathIdentificationMiddleware);
/**
* The route should get cloned if:
* - it has route-level path identification middleware, OR
* - it uses kernel path identification (it doesn't have any route-level identification middleware) and the route is tenant or universal.
*
* The route is considered tenant if:
* - it's flagged as tenant, OR
* - it's not flagged as tenant or universal, but it has the identification middleware
*
* The route is considered universal if it's flagged as universal, and it doesn't have the tenant flag
* (it's still considered universal if it has route-level path identification middleware + the universal flag).
*
* If the route isn't flagged, the context is determined using the default route mode.
*/
$pathIdentificationUsed = (! $routeHasNonPathIdentificationMiddleware) &&
($routeHasPathIdentificationMiddleware || $pathIdentificationMiddlewareInGlobalStack);
return $pathIdentificationUsed &&
(tenancy()->getRouteMode($route) === RouteMode::UNIVERSAL || tenancy()->routeHasMiddleware($route, 'clone'));
});
return $this;
}
/**
* Clone a route using a callback specified in the $cloneRouteUsing property (using the cloneUsing method).
* If there's no callback specified for the route, use the default way of cloning routes.
*/
protected function cloneRoute(Route $route): void
public function cloneRoute(Route|string $route): static
{
$routeName = $route->getName();
$this->routesToClone[] = $route;
// If the route's cloning callback exists
// Use the callback to clone the route instead of the default way of cloning routes
if ($routeName && $customRouteCallback = data_get($this->cloneRouteUsing, $routeName)) {
$customRouteCallback($route);
return $this;
}
return;
protected function shouldBeCloned(Route $route): bool
{
// Don't clone routes that already have tenant parameter or prefix
if ($this->routeIsTenant($route)) {
return false;
}
$this->copyMiscRouteProperties($route, $this->createNewRoute($route));
if ($this->shouldClone) {
return ($this->shouldClone)($route);
}
return tenancy()->routeHasMiddleware($route, $this->cloneRoutesWithMiddleware);
}
protected function createNewRoute(Route $route): Route
@ -150,33 +159,24 @@ class CloneRoutesAsTenant
$prefix = trim($route->getPrefix() ?? '', '/');
$uri = $route->getPrefix() ? Str::after($route->uri(), $prefix) : $route->uri();
$newRouteAction = collect($route->action)->tap(function (Collection $action) use ($route, $prefix) {
/** @var array $routeMiddleware */
$routeMiddleware = $action->get('middleware') ?? [];
$action = collect($route->action);
// Make the new route have the same middleware as the original route
// Add the 'tenant' middleware to the new route
// Exclude `universal` and `clone` middleware from the new route (it should only be flagged as tenant)
$newRouteMiddleware = collect($routeMiddleware)
->merge(['tenant']) // Add 'tenant' flag
->filter(fn (string $middleware) => ! in_array($middleware, ['universal', 'clone']))
->toArray();
// Make the new route have the same middleware as the original route
// Add the 'tenant' middleware to the new route
// Exclude $this->cloneRoutesWithMiddleware MW from the new route (it should only be flagged as tenant)
$tenantRouteNamePrefix = PathTenantResolver::tenantRouteNamePrefix();
$middleware = $this->processMiddlewareForCloning($action->get('middleware') ?? []);
// Make sure the route name has the tenant route name prefix
$newRouteNamePrefix = $route->getName()
? $tenantRouteNamePrefix . Str::after($route->getName(), $tenantRouteNamePrefix)
: null;
if ($name = $route->getName()) {
$action->put('as', PathTenantResolver::tenantRouteNamePrefix() . $name);
}
return $action
->put('as', $newRouteNamePrefix)
->put('middleware', $newRouteMiddleware)
->put('prefix', $prefix . '/{' . PathTenantResolver::tenantParameterName() . '}');
})->toArray();
$action
->put('middleware', $middleware)
->put('prefix', $prefix . '/{' . PathTenantResolver::tenantParameterName() . '}');
/** @var Route $newRoute */
$newRoute = $this->router->$method($uri, $newRouteAction);
$newRoute = $this->router->$method($uri, $action->toArray());
return $newRoute;
}
@ -194,4 +194,26 @@ class CloneRoutesAsTenant
->withTrashed($originalRoute->allowsTrashedBindings())
->setDefaults($originalRoute->defaults);
}
/** Removes top-level cloneRoutesWithMiddleware and adds 'tenant' middleware. */
protected function processMiddlewareForCloning(array $middleware): array
{
$processedMiddleware = array_filter(
$middleware,
fn ($mw) => ! in_array($mw, $this->cloneRoutesWithMiddleware)
);
$processedMiddleware[] = 'tenant';
return array_unique($processedMiddleware);
}
/** Check if route already has tenant parameter or name prefix. */
protected function routeIsTenant(Route $route): bool
{
$routeHasTenantParameter = in_array(PathTenantResolver::tenantParameterName(), $route->parameterNames());
$routeHasTenantPrefix = $route->getName() && str_starts_with($route->getName(), PathTenantResolver::tenantRouteNamePrefix());
return $routeHasTenantParameter || $routeHasTenantPrefix;
}
}