From be6b2f82486d06db635008b823d1030137942ee0 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Mon, 28 Nov 2022 11:23:30 +0500 Subject: [PATCH] remove `tenant_unaware_features` key and use static function --- assets/config.php | 3 --- src/Contracts/Feature.php | 2 ++ src/Features/CrossDomainRedirect.php | 5 +++++ src/Features/TelescopeTags.php | 5 +++++ src/Features/TenantConfig.php | 5 +++++ src/Features/UniversalRoutes.php | 5 +++++ src/Features/UserImpersonation.php | 5 +++++ src/TenancyServiceProvider.php | 10 +++++++--- tests/Features/RedirectTest.php | 4 +--- 9 files changed, 35 insertions(+), 9 deletions(-) diff --git a/assets/config.php b/assets/config.php index ec0c5642..4c3f0cb2 100644 --- a/assets/config.php +++ b/assets/config.php @@ -283,9 +283,6 @@ return [ // Stancl\Tenancy\Features\TelescopeTags::class, // Stancl\Tenancy\Features\UniversalRoutes::class, // Stancl\Tenancy\Features\TenantConfig::class, // https://tenancyforlaravel.com/docs/v3/features/tenant-config - ], - - 'tenant_unaware_features' => [ Stancl\Tenancy\Features\CrossDomainRedirect::class, // https://tenancyforlaravel.com/docs/v3/features/cross-domain-redirect ], diff --git a/src/Contracts/Feature.php b/src/Contracts/Feature.php index 25363cf5..b3031622 100644 --- a/src/Contracts/Feature.php +++ b/src/Contracts/Feature.php @@ -8,4 +8,6 @@ namespace Stancl\Tenancy\Contracts; interface Feature { public function bootstrap(): void; + + public static function alwaysBootstrap(): bool; } diff --git a/src/Features/CrossDomainRedirect.php b/src/Features/CrossDomainRedirect.php index 57786274..20d906f1 100644 --- a/src/Features/CrossDomainRedirect.php +++ b/src/Features/CrossDomainRedirect.php @@ -27,4 +27,9 @@ class CrossDomainRedirect implements Feature return $this; }); } + + public static function alwaysBootstrap(): bool + { + return true; + } } diff --git a/src/Features/TelescopeTags.php b/src/Features/TelescopeTags.php index 225049df..16a8ee70 100644 --- a/src/Features/TelescopeTags.php +++ b/src/Features/TelescopeTags.php @@ -32,4 +32,9 @@ class TelescopeTags implements Feature return $tags; }); } + + public static function alwaysBootstrap(): bool + { + return false; + } } diff --git a/src/Features/TenantConfig.php b/src/Features/TenantConfig.php index d268680b..deacec68 100644 --- a/src/Features/TenantConfig.php +++ b/src/Features/TenantConfig.php @@ -69,4 +69,9 @@ class TenantConfig implements Feature $this->config->set($key, $value); } } + + public static function alwaysBootstrap(): bool + { + return false; + } } diff --git a/src/Features/UniversalRoutes.php b/src/Features/UniversalRoutes.php index b0f8c757..a667a3fb 100644 --- a/src/Features/UniversalRoutes.php +++ b/src/Features/UniversalRoutes.php @@ -60,4 +60,9 @@ class UniversalRoutes implements Feature return false; } + + public static function alwaysBootstrap(): bool + { + return false; + } } diff --git a/src/Features/UserImpersonation.php b/src/Features/UserImpersonation.php index 5a6bf0d9..00895625 100644 --- a/src/Features/UserImpersonation.php +++ b/src/Features/UserImpersonation.php @@ -50,4 +50,9 @@ class UserImpersonation implements Feature return redirect($token->redirect_url); } + + public static function alwaysBootstrap(): bool + { + return false; + } } diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index 3e124bd5..a8239ed9 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -29,12 +29,16 @@ class TenancyServiceProvider extends ServiceProvider $this->app->resolving(Tenancy::class, function (Tenancy $tenancy, $app) { foreach ($this->app['config']['tenancy.features'] ?? [] as $feature) { - $this->app[$feature]->bootstrap(); + if (! $feature::alwaysBootstrap()) { // avoid bootstrapping already bootstrapped features + $this->app[$feature]->bootstrap(); + } } }); - foreach ($this->app['config']['tenancy.tenant_unaware_features'] ?? [] as $feature) { - $this->app[$feature]->bootstrap(); + foreach ($this->app['config']['tenancy.features'] ?? [] as $feature) { + if ($feature::alwaysBootstrap()) { + $this->app[$feature]->bootstrap(); + } } // Make it possible to inject the current tenant by typehinting the Tenant contract. diff --git a/tests/Features/RedirectTest.php b/tests/Features/RedirectTest.php index 7808961d..847b0cdf 100644 --- a/tests/Features/RedirectTest.php +++ b/tests/Features/RedirectTest.php @@ -7,9 +7,7 @@ use Stancl\Tenancy\Features\CrossDomainRedirect; use Stancl\Tenancy\Tests\Etc\Tenant; test('tenant redirect macro replaces only the hostname', function () { - config([ - 'tenancy.tenant_unaware_features' => [CrossDomainRedirect::class], - ]); + // `CrossDomainRedirect` feature already enabled in config Route::get('/foobar', function () { return 'Foo';