1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-08-07 10:24:03 +00:00

Improve route cloning action (#8)

* Allow cloning routes when only kernel identification is used, explicitly enable specific cloning modes

* Explicitly enable needed clone modes in tests, use "clone" instead of "reregister"

* Fix code style (php-cs-fixer)

* Use  "cloning" instead of "re-registration" in UniversalRouteTest

* Only clone routes using path identification

* Revert clone mode changes

* Fix code style (php-cs-fixer)

* Update comment

* Skip cloning 'stancl.tenancy.asset' by default

* Decide which routes should get cloned in the filtering step, improve method organization

* Return `RouteMode::UNIVERSAL` in getMiddlewareContext if route is universal

* Give universal route the path ID MW so that it gets cloned

* Fix code style (php-cs-fixer)

* Simplify UsableWithEarlyIdentification code

* Handle universal route mode in ForgetTenantParameter

* Fix code style (php-cs-fixer)

* Rename getMiddlewareContext to getRouteMode

* Append '/' to the route prefix

* Rename variable

* Wrap part of condition in parentheses

* Refresh name lookups after cloning routes

* Test giving tenant flag to cloned routes

* Add routeIsUniversal method

* Correct ForgetTenantParameter condition

* Improve tenant flag giving logic

* Improve test name

* Delete leftover testing code

* Put part of condition into `()`

* Improve CloneRoutesAsTenant code + comments

* Extract route mode-related code into methods, refactor and improve code

* Improve ForgetTenantParameter, test tenant parameter removing in universal routes

* Fix code style (php-cs-fixer)

* Fix test

* Simplify adding tenant flag

* Don't skip stancl.tenancy.asset route cloning

* clean up comment

* fix in_array() argument

* Fix code style (php-cs-fixer)

---------

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
lukinovec 2023-08-28 13:17:17 +02:00 committed by GitHub
parent 4d4639450e
commit f7d9f02fd4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 222 additions and 111 deletions

View file

@ -29,21 +29,13 @@ trait UsableWithEarlyIdentification
{
/**
* Skip middleware if the route is universal and uses path identification or if the route is universal and the context should be central.
* Universal routes using path identification should get re-registered using ReregisterRoutesAsTenant.
* Universal routes using path identification should get cloned using CloneRoutesAsTenant.
*
* @see \Stancl\Tenancy\Actions\CloneRoutesAsTenant
*/
protected function shouldBeSkipped(Route $route): bool
{
$routeMiddleware = tenancy()->getRouteMiddleware($route);
$universalFlagUsed = in_array('universal', $routeMiddleware);
$defaultToUniversalRoutes = config('tenancy.default_route_mode') === RouteMode::UNIVERSAL;
// Route is universal only if it doesn't have the central/tenant flag
$routeIsUniversal = ($universalFlagUsed || $defaultToUniversalRoutes) &&
! (in_array('central', $routeMiddleware) || in_array('tenant', $routeMiddleware));
if ($routeIsUniversal && $this instanceof IdentificationMiddleware) {
if (tenancy()->routeIsUniversal($route) && $this instanceof IdentificationMiddleware) {
/** @phpstan-ignore-next-line */
throw_unless($this instanceof UsableWithUniversalRoutes, MiddlewareNotUsableWithUniversalRoutesException::class);
@ -71,10 +63,15 @@ trait UsableWithEarlyIdentification
// Check if this is the identification middleware the route should be using
// Route-level identification middleware is prioritized
$middlewareUsed = tenancy()->routeHasMiddleware($route, static::class) || ! tenancy()->routeHasIdentificationMiddleware($route) && static::inGlobalStack();
$globalIdentificationUsed = ! tenancy()->routeHasIdentificationMiddleware($route) && static::inGlobalStack();
$routeLevelIdentificationUsed = tenancy()->routeHasMiddleware($route, static::class);
/** @var UsableWithUniversalRoutes $this */
return $middlewareUsed && $this->requestHasTenant($request) ? Context::TENANT : Context::CENTRAL;
if (($globalIdentificationUsed || $routeLevelIdentificationUsed) && $this->requestHasTenant($request)) {
return Context::TENANT;
}
return Context::CENTRAL;
}
protected function shouldIdentificationMiddlewareBeSkipped(Route $route): bool
@ -88,10 +85,9 @@ trait UsableWithEarlyIdentification
if (! $request->attributes->get('_tenancy_kernel_identification_skipped')) {
if (
// Skip identification if the current route is central
// The route is central if defaulting is set to central and the route isn't flagged as tenant or it doesn't have identification middleware
tenancy()->getMiddlewareContext($route) === RouteMode::CENTRAL
// Don't skip identification if the central route is considered universal
&& (config('tenancy.default_route_mode') !== RouteMode::UNIVERSAL || ! tenancy()->routeHasMiddleware($route, 'universal'))
// The route is central if it's flagged as central
// Or if it isn't flagged and the default route mode is set to central
tenancy()->getRouteMode($route) === RouteMode::CENTRAL
) {
return true;
}