From 654a988af133ccd8857a13b793f157fbcdffd616 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Mon, 20 Feb 2023 15:39:23 +0100 Subject: [PATCH] Add bootstrapFeatures static property and method --- src/TenancyServiceProvider.php | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index a8239ed9..b10fd4ec 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -17,6 +17,8 @@ use Stancl\Tenancy\Resolvers\DomainTenantResolver; class TenancyServiceProvider extends ServiceProvider { + public static $bootstrapFeatures = true; + /* Register services. */ public function register(): void { @@ -27,18 +29,8 @@ class TenancyServiceProvider extends ServiceProvider // Make sure Tenancy is stateful. $this->app->singleton(Tenancy::class); - $this->app->resolving(Tenancy::class, function (Tenancy $tenancy, $app) { - foreach ($this->app['config']['tenancy.features'] ?? [] as $feature) { - if (! $feature::alwaysBootstrap()) { // avoid bootstrapping already bootstrapped features - $this->app[$feature]->bootstrap(); - } - } - }); - - foreach ($this->app['config']['tenancy.features'] ?? [] as $feature) { - if ($feature::alwaysBootstrap()) { - $this->app[$feature]->bootstrap(); - } + if (static::bootstrapFeatures()) { + $this->bootstrapFeatures(); } // Make it possible to inject the current tenant by typehinting the Tenant contract. @@ -149,4 +141,21 @@ class TenancyServiceProvider extends ServiceProvider return $instance; }); } + + public static function bootstrapFeatures(): void + { + foreach (config('tenancy.features') ?? [] as $feature) { + if ($feature::alwaysBootstrap()) { + app($feature)->bootstrap(); + } + } + + app()->resolving(Tenancy::class, function (Tenancy $tenancy, $app) { + foreach (config('tenancy.features') ?? [] as $feature) { + if (! $feature::alwaysBootstrap()) { // Avoid bootstrapping already bootstrapped features + $app[$feature]->bootstrap(); + } + } + }); + } }