mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 15:34:03 +00:00
Cloning: improve type annotations, add cloneRoutes() for convenience
This commit is contained in:
parent
197513dd84
commit
6ef4b91744
2 changed files with 60 additions and 6 deletions
|
|
@ -86,13 +86,27 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
||||||
*/
|
*/
|
||||||
class CloneRoutesAsTenant
|
class CloneRoutesAsTenant
|
||||||
{
|
{
|
||||||
|
/** @var list<Route|string> */
|
||||||
protected array $routesToClone = [];
|
protected array $routesToClone = [];
|
||||||
|
|
||||||
protected bool $addTenantParameter = true;
|
protected bool $addTenantParameter = true;
|
||||||
protected bool $tenantParameterBeforePrefix = true;
|
protected bool $tenantParameterBeforePrefix = true;
|
||||||
protected string|null $domain = null;
|
protected string|null $domain = null;
|
||||||
protected Closure|null $cloneUsing = null; // The callback should accept Route instance or the route name (string)
|
|
||||||
|
/**
|
||||||
|
* The callback should accept a Route instance or the route name (string).
|
||||||
|
*
|
||||||
|
* @var ?Closure(Route|string): void
|
||||||
|
*/
|
||||||
|
protected Closure|null $cloneUsing = null;
|
||||||
|
|
||||||
|
/** @var ?Closure(Route): bool */
|
||||||
protected Closure|null $shouldClone = null;
|
protected Closure|null $shouldClone = null;
|
||||||
|
|
||||||
|
/** @var list<string> */
|
||||||
protected array $cloneRoutesWithMiddleware = ['clone'];
|
protected array $cloneRoutesWithMiddleware = ['clone'];
|
||||||
|
|
||||||
|
/** @var list<string> */
|
||||||
protected array $addTenantMiddleware = ['tenant'];
|
protected array $addTenantMiddleware = ['tenant'];
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
|
@ -110,9 +124,12 @@ class CloneRoutesAsTenant
|
||||||
// If no routes were specified using cloneRoute(), get all routes
|
// If no routes were specified using cloneRoute(), get all routes
|
||||||
// and for each, determine if it should be cloned
|
// and for each, determine if it should be cloned
|
||||||
if (! $this->routesToClone) {
|
if (! $this->routesToClone) {
|
||||||
$this->routesToClone = collect($this->router->getRoutes()->get())
|
/** @var list<Route> */
|
||||||
|
$routesToClone = collect($this->router->getRoutes()->get())
|
||||||
->filter(fn (Route $route) => $this->shouldBeCloned($route))
|
->filter(fn (Route $route) => $this->shouldBeCloned($route))
|
||||||
->all();
|
->all();
|
||||||
|
|
||||||
|
$this->routesToClone = $routesToClone;
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->routesToClone as $route) {
|
foreach ($this->routesToClone as $route) {
|
||||||
|
|
@ -126,7 +143,9 @@ class CloneRoutesAsTenant
|
||||||
|
|
||||||
if (is_string($route)) {
|
if (is_string($route)) {
|
||||||
$this->router->getRoutes()->refreshNameLookups();
|
$this->router->getRoutes()->refreshNameLookups();
|
||||||
$route = $this->router->getRoutes()->getByName($route);
|
$routeName = $route;
|
||||||
|
$route = $this->router->getRoutes()->getByName($routeName);
|
||||||
|
assert(! is_null($route), "Route [{$routeName}] was meant to be cloned but does not exist.");
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->createNewRoute($route);
|
$this->createNewRoute($route);
|
||||||
|
|
@ -155,6 +174,8 @@ class CloneRoutesAsTenant
|
||||||
* The tenant middleware to be added to the cloned route.
|
* The tenant middleware to be added to the cloned route.
|
||||||
*
|
*
|
||||||
* If used with early identification, make sure to include 'tenant' in this array.
|
* If used with early identification, make sure to include 'tenant' in this array.
|
||||||
|
*
|
||||||
|
* @param list<string> $middleware
|
||||||
*/
|
*/
|
||||||
public function addTenantMiddleware(array $middleware): static
|
public function addTenantMiddleware(array $middleware): static
|
||||||
{
|
{
|
||||||
|
|
@ -171,7 +192,11 @@ class CloneRoutesAsTenant
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Provide a custom callback for cloning routes, instead of the default behavior. */
|
/**
|
||||||
|
* Provide a custom callback for cloning routes, instead of the default behavior.
|
||||||
|
*
|
||||||
|
* @param ?Closure(Route|string): void $cloneUsing
|
||||||
|
*/
|
||||||
public function cloneUsing(Closure|null $cloneUsing): static
|
public function cloneUsing(Closure|null $cloneUsing): static
|
||||||
{
|
{
|
||||||
$this->cloneUsing = $cloneUsing;
|
$this->cloneUsing = $cloneUsing;
|
||||||
|
|
@ -179,7 +204,11 @@ class CloneRoutesAsTenant
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Specify which middleware should serve as "flags" telling this action to clone those routes. */
|
/**
|
||||||
|
* Specify which middleware should serve as "flags" telling this action to clone those routes.
|
||||||
|
*
|
||||||
|
* @param list<string> $middleware
|
||||||
|
*/
|
||||||
public function cloneRoutesWithMiddleware(array $middleware): static
|
public function cloneRoutesWithMiddleware(array $middleware): static
|
||||||
{
|
{
|
||||||
$this->cloneRoutesWithMiddleware = $middleware;
|
$this->cloneRoutesWithMiddleware = $middleware;
|
||||||
|
|
@ -190,7 +219,9 @@ class CloneRoutesAsTenant
|
||||||
/**
|
/**
|
||||||
* Provide a custom callback for determining whether a route should be cloned.
|
* Provide a custom callback for determining whether a route should be cloned.
|
||||||
* Overrides the default middleware-based detection.
|
* Overrides the default middleware-based detection.
|
||||||
* */
|
*
|
||||||
|
* @param Closure(Route): bool $shouldClone
|
||||||
|
*/
|
||||||
public function shouldClone(Closure|null $shouldClone): static
|
public function shouldClone(Closure|null $shouldClone): static
|
||||||
{
|
{
|
||||||
$this->shouldClone = $shouldClone;
|
$this->shouldClone = $shouldClone;
|
||||||
|
|
@ -213,6 +244,18 @@ class CloneRoutesAsTenant
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Clone individual routes.
|
||||||
|
*
|
||||||
|
* @param list<Route|string> $routes
|
||||||
|
*/
|
||||||
|
public function cloneRoutes(array $routes): static
|
||||||
|
{
|
||||||
|
$this->routesToClone = array_merge($this->routesToClone, $routes);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
protected function shouldBeCloned(Route $route): bool
|
protected function shouldBeCloned(Route $route): bool
|
||||||
{
|
{
|
||||||
// Don't clone routes that already have tenant parameter or prefix
|
// Don't clone routes that already have tenant parameter or prefix
|
||||||
|
|
|
||||||
|
|
@ -464,3 +464,14 @@ test('addTenantMiddleware can be used to specify the tenant middleware for the c
|
||||||
expect($cloned->getDomain())->toBe('foo.localhost');
|
expect($cloned->getDomain())->toBe('foo.localhost');
|
||||||
expect(tenancy()->getRouteMiddleware($cloned))->toBe([InitializeTenancyByDomain::class]);
|
expect(tenancy()->getRouteMiddleware($cloned))->toBe([InitializeTenancyByDomain::class]);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('cloneRoutes can be used to clone multiple routes', function () {
|
||||||
|
RouteFacade::get('/foo', fn () => true)->name('foo');
|
||||||
|
$bar = RouteFacade::get('/bar', fn () => true)->name('bar');
|
||||||
|
RouteFacade::get('/baz', fn () => true)->name('baz');
|
||||||
|
|
||||||
|
CloneRoutesAsTenant::make()->cloneRoutes(['foo', $bar])->handle();
|
||||||
|
expect(collect(RouteFacade::getRoutes()->get())->map->getName())->toContain('tenant.foo');
|
||||||
|
expect(collect(RouteFacade::getRoutes()->get())->map->getName())->toContain('tenant.bar');
|
||||||
|
expect(collect(RouteFacade::getRoutes()->get())->map->getName())->not()->toContain('tenant.baz');
|
||||||
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue