mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 09:54:05 +00:00
cleanup
This commit is contained in:
parent
b7471c4381
commit
53ea46afe8
3 changed files with 48 additions and 51 deletions
|
|
@ -25,15 +25,14 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
||||||
class UrlGeneratorBootstrapper implements TenancyBootstrapper
|
class UrlGeneratorBootstrapper implements TenancyBootstrapper
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Determine if the tenant route parameter should get added to the defaults of the TenancyUrlGenerator.
|
* Should the tenant route parameter get added to TenancyUrlGenerator::defaults().
|
||||||
*
|
*
|
||||||
* This is preferrable with path identification since the tenant parameter is passed to the tenant routes automatically,
|
* This is recommended when using path identification since defaults() generally has better support in integrations,
|
||||||
* even with integrations like the Ziggy route() helper.
|
* namely Ziggy, compared to TenancyUrlGenerator::$passTenantParameterToRoutes.
|
||||||
*
|
*
|
||||||
* With query strig identification, this essentialy has no effect because URL::defaults() works only for route paramaters,
|
* With query string identification, this has no effect since URL::defaults() only works for route paramaters.
|
||||||
* not for query strings.
|
|
||||||
*/
|
*/
|
||||||
public static bool $addTenantParameterToDefaults = false;
|
public static bool $addTenantParameterToDefaults = true;
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
protected Application $app,
|
protected Application $app,
|
||||||
|
|
|
||||||
|
|
@ -13,75 +13,77 @@ use Stancl\Tenancy\Resolvers\PathTenantResolver;
|
||||||
/**
|
/**
|
||||||
* This class is used in place of the default UrlGenerator when UrlGeneratorBootstrapper is enabled.
|
* This class is used in place of the default UrlGenerator when UrlGeneratorBootstrapper is enabled.
|
||||||
*
|
*
|
||||||
* TenancyUrlGenerator does two extra things:
|
* TenancyUrlGenerator does a few extra things:
|
||||||
|
* - Autofills the tenant parameter in the tenant context with the current tenant.
|
||||||
|
* This is done either by:
|
||||||
|
* - URL::defaults() -- if UrlGeneratorBootstrapper::$addTenantParameterToDefaults is enabled.
|
||||||
|
* This generally has the best support since tools like e.g. Ziggy read defaults().
|
||||||
|
* - Automatically passing ['tenant' => ...] to each route() call -- if TenancyUrlGenerator::$passTenantParameterToRoutes is enabled
|
||||||
|
* This is a more universal solution since it supports both path identification and query parameter identification.
|
||||||
*
|
*
|
||||||
* - Autofills the tenant parameter in the tenant context with the current tenant if $passTenantParameterToRoutes is enabled.
|
* - Prepends route names passed to route() and URL::temporarySignedRoute()
|
||||||
* With path identification, this is required to be done by URL::defaults() -- setting
|
* with `tenant.` (or the configured prefix) if $prefixRouteNames is enabled.
|
||||||
* UrlGeneratorBootstrapper::$addTenantParameterToDefaults (which is false by default) to true handles that.
|
* This is primarily useful when using route cloning with path identification.
|
||||||
* Enabling $passTenantParameterToRoutes is preferable with query string identification.
|
|
||||||
*
|
*
|
||||||
* - Prepends the route name passed to route() and URL::temporarySignedRoute()
|
* To bypass this behavior on any single route() call, pass the $bypassParameter as true (['central' => true] by default).
|
||||||
* with `tenant.` (or the configured prefix) if $prefixRouteNames is enabled (disabled by default).
|
|
||||||
* Primarily intended to be used with path identification.
|
|
||||||
*
|
|
||||||
* This behavior can be bypassed by passing the $bypassParameter to the mentioned methods (`['central' => true]` by default).
|
|
||||||
*/
|
*/
|
||||||
class TenancyUrlGenerator extends UrlGenerator
|
class TenancyUrlGenerator extends UrlGenerator
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* Parameter which bypasses the behavior modification of route() and temporarySignedRoute().
|
* Parameter which works as a flag for bypassing the behavior modification of route() and temporarySignedRoute().
|
||||||
*
|
*
|
||||||
* For example, in tenant context:
|
* For example, in tenant context:
|
||||||
* Route::get('/', ...)->name('home');
|
* Route::get('/', ...)->name('home');
|
||||||
|
* // query string identification
|
||||||
* Route::get('/tenant', ...)->middleware(InitializeTenancyByRequestData::class)->name('tenant.home');
|
* Route::get('/tenant', ...)->middleware(InitializeTenancyByRequestData::class)->name('tenant.home');
|
||||||
* - route('home') => app.test/tenant?tenant=tenantKey
|
* - route('home') => app.test/tenant?tenant=tenantKey
|
||||||
* - route('home', [$bypassParameter => true]) => app.test/
|
* - route('home', [$bypassParameter => true]) => app.test/
|
||||||
* - route('tenant.home', [$bypassParameter => true]) => app.test/tenant -- query string identification (no query string passed)
|
* - route('tenant.home', [$bypassParameter => true]) => app.test/tenant -- no query string added
|
||||||
*
|
*
|
||||||
* With path identification, it is recommended to pass the tenant parameter automatically by setting
|
* Note: UrlGeneratorBootstrapper::$addTenantParameterToDefaults is not affected by this, though
|
||||||
* UrlGeneratorBootstrapper::$addTenantParameterToDefaults to true,
|
* it doesn't matter since it doesn't pass any extra parameters when not needed.
|
||||||
* and in that case, this affects only the automatic route name prefixing,
|
|
||||||
* and the forceful route name overrides set in the $override property.
|
|
||||||
*
|
*
|
||||||
* @see UrlGeneratorBootstrapper
|
* @see UrlGeneratorBootstrapper
|
||||||
*/
|
*/
|
||||||
public static string $bypassParameter = 'central';
|
public static string $bypassParameter = 'central';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the route names passed to `route()` or `temporarySignedRoute()`
|
* Should route names passed to route() or temporarySignedRoute()
|
||||||
* should get prefixed with the tenant route name prefix.
|
* get prefixed with the tenant route name prefix.
|
||||||
*
|
*
|
||||||
* This is useful when using path identification with packages that generate URLs,
|
* This is useful when using e.g. path identification with third-party packages
|
||||||
* like Jetstream, so that you don't have to manually prefix route names passed to each route() call.
|
* where you don't have control over all route() calls or don't want to change
|
||||||
|
* too many files. Often this will be when using route cloning.
|
||||||
*/
|
*/
|
||||||
public static bool $prefixRouteNames = false;
|
public static bool $prefixRouteNames = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine if the tenant parameter should get passed
|
* Should the tenant parameter be passed to route() or temporarySignedRoute() calls.
|
||||||
* to the links generated by `route()` or `temporarySignedRoute()` whenever available.
|
|
||||||
* This is primarily intended to be used with query string identification.
|
|
||||||
*
|
*
|
||||||
* With path identification, the tenant parameter is passed by URL::defaults()
|
* This is useful with path or query parameter identification. The former can be handled
|
||||||
* when UrlGeneratorBootstrapper::$addTenantParameterToDefaults is true (which is the default).
|
* more elegantly using UrlGeneratorBootstrapper::$addTenantParameterToDefaults.
|
||||||
*
|
*
|
||||||
* @see UrlGeneratorBootstrapper
|
* @see UrlGeneratorBootstrapper
|
||||||
*/
|
*/
|
||||||
public static bool $passTenantParameterToRoutes = false;
|
public static bool $passTenantParameterToRoutes = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Route names that should always be overridden.
|
* Route name overrides.
|
||||||
* This behavior can still be bypassed by passing the bypass parameter.
|
|
||||||
*
|
*
|
||||||
* For example, Jetstream integration:
|
* Note: This behavior can be bypassed using $bypassParameter just like
|
||||||
|
* $prefixRouteNames and $passTenantParameterToRoutes.
|
||||||
|
*
|
||||||
|
* Example from a Jetstream integration:
|
||||||
* [
|
* [
|
||||||
* 'profile.show' => 'tenant.profile.show',
|
* 'profile.show' => 'tenant.profile.show',
|
||||||
* 'two-factor.login' => 'tenant.two-factor.login',
|
* 'two-factor.login' => 'tenant.two-factor.login',
|
||||||
* ]
|
* ]
|
||||||
*
|
*
|
||||||
* `route('profile.show')` will return an URL as if you called `route('tenant.profile.show')`.
|
* In the tenant context:
|
||||||
* `route('profile.show', ['central' => true])` will return an URL as if you called `route('profile.show')`.
|
* - `route('profile.show')` will return a URL as if you called `route('tenant.profile.show')`.
|
||||||
|
* - `route('profile.show', ['central' => true])` will return a URL as if you called `route('profile.show')`.
|
||||||
*/
|
*/
|
||||||
public static array $override = [];
|
public static array $overrides = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Override the route() method so that the route name gets prefixed
|
* Override the route() method so that the route name gets prefixed
|
||||||
|
|
@ -160,7 +162,7 @@ class TenancyUrlGenerator extends UrlGenerator
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* If `tenant()` isn't null, add tenant parameter to the passed parameters.
|
* If `tenant()` isn't null, add the tenant parameter to the passed parameters.
|
||||||
*/
|
*/
|
||||||
protected function addTenantParameter(array $parameters): array
|
protected function addTenantParameter(array $parameters): array
|
||||||
{
|
{
|
||||||
|
|
@ -169,6 +171,6 @@ class TenancyUrlGenerator extends UrlGenerator
|
||||||
|
|
||||||
protected function routeNameOverride(string $name): string|null
|
protected function routeNameOverride(string $name): string|null
|
||||||
{
|
{
|
||||||
return static::$override[$name] ?? null;
|
return static::$overrides[$name] ?? null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -62,10 +62,8 @@ test('tenancy url generator can prefix route names passed to the route helper',
|
||||||
// When $prefixRouteNames is true, the route name passed to the route() helper ('home') gets prefixed with 'tenant.' automatically.
|
// When $prefixRouteNames is true, the route name passed to the route() helper ('home') gets prefixed with 'tenant.' automatically.
|
||||||
TenancyUrlGenerator::$prefixRouteNames = true;
|
TenancyUrlGenerator::$prefixRouteNames = true;
|
||||||
|
|
||||||
// Reinitialize tenancy to apply the URL generator changes
|
|
||||||
tenancy()->initialize($tenant);
|
|
||||||
|
|
||||||
expect(route('home'))->toBe($tenantRouteUrl);
|
expect(route('home'))->toBe($tenantRouteUrl);
|
||||||
|
|
||||||
// The 'tenant.home' route name doesn't get prefixed -- it is already prefixed with 'tenant.'
|
// The 'tenant.home' route name doesn't get prefixed -- it is already prefixed with 'tenant.'
|
||||||
expect(route('tenant.home'))->toBe($tenantRouteUrl);
|
expect(route('tenant.home'))->toBe($tenantRouteUrl);
|
||||||
|
|
||||||
|
|
@ -86,8 +84,8 @@ test('the route helper can receive the tenant parameter automatically', function
|
||||||
|
|
||||||
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = $addTenantParameterToDefaults;
|
UrlGeneratorBootstrapper::$addTenantParameterToDefaults = $addTenantParameterToDefaults;
|
||||||
|
|
||||||
// When the tenant parameter isn't added to the defaults, the tenant parameter has to be passed "manually"
|
// When the tenant parameter isn't added to defaults, the tenant parameter has to be passed "manually"
|
||||||
// by setting $passTenantParameterToRoutes to true. This is only preferrable with query string identification.
|
// by setting $passTenantParameterToRoutes to true. This is only preferable with query string identification.
|
||||||
// With path identification, this ultimately doesn't have any effect
|
// With path identification, this ultimately doesn't have any effect
|
||||||
// if UrlGeneratorBootstrapper::$addTenantParameterToDefaults is true,
|
// if UrlGeneratorBootstrapper::$addTenantParameterToDefaults is true,
|
||||||
// but TenancyUrlGenerator::$passTenantParameterToRoutes can still be used instead.
|
// but TenancyUrlGenerator::$passTenantParameterToRoutes can still be used instead.
|
||||||
|
|
@ -122,14 +120,14 @@ test('the route helper can receive the tenant parameter automatically', function
|
||||||
->with([true, false]) // UrlGeneratorBootstrapper::$addTenantParameterToDefaults
|
->with([true, false]) // UrlGeneratorBootstrapper::$addTenantParameterToDefaults
|
||||||
->with([true, false]); // TenancyUrlGenerator::$passTenantParameterToRoutes
|
->with([true, false]); // TenancyUrlGenerator::$passTenantParameterToRoutes
|
||||||
|
|
||||||
test('url generator can override specific route names while all other functionality is disabled', function() {
|
test('url generator can override specific route names', function() {
|
||||||
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
|
config(['tenancy.bootstrappers' => [UrlGeneratorBootstrapper::class]]);
|
||||||
|
|
||||||
Route::get('/foo', fn () => 'foo')->name('foo');
|
Route::get('/foo', fn () => 'foo')->name('foo');
|
||||||
Route::get('/bar', fn () => 'bar')->name('bar');
|
Route::get('/bar', fn () => 'bar')->name('bar');
|
||||||
Route::get('/baz', fn () => 'baz')->name('baz'); // Not overridden
|
Route::get('/baz', fn () => 'baz')->name('baz'); // Not overridden
|
||||||
|
|
||||||
TenancyUrlGenerator::$override = ['foo' => 'bar'];
|
TenancyUrlGenerator::$overrides = ['foo' => 'bar'];
|
||||||
|
|
||||||
expect(route('foo'))->toBe(url('/foo'));
|
expect(route('foo'))->toBe(url('/foo'));
|
||||||
expect(route('bar'))->toBe(url('/bar'));
|
expect(route('bar'))->toBe(url('/bar'));
|
||||||
|
|
@ -138,13 +136,11 @@ test('url generator can override specific route names while all other functional
|
||||||
tenancy()->initialize(Tenant::create());
|
tenancy()->initialize(Tenant::create());
|
||||||
|
|
||||||
expect(route('foo'))->toBe(url('/bar'));
|
expect(route('foo'))->toBe(url('/bar'));
|
||||||
|
expect(route('bar'))->toBe(url('/bar')); // not overridden
|
||||||
|
expect(route('baz'))->toBe(url('/baz')); // not overridden
|
||||||
|
|
||||||
// Pass the bypass parameter bypasses the override
|
// Bypass the override
|
||||||
expect(route('foo', ['central' => true]))->toBe(url('/foo'));
|
expect(route('foo', ['central' => true]))->toBe(url('/foo'));
|
||||||
|
|
||||||
// Not overridden
|
|
||||||
expect(route('bar'))->toBe(url('/bar'));
|
|
||||||
expect(route('baz'))->toBe(url('/baz'));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
test('both the name prefixing and the tenant parameter logic gets skipped when bypass parameter is used', function () {
|
test('both the name prefixing and the tenant parameter logic gets skipped when bypass parameter is used', function () {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue