diff --git a/src/Contracts/Feature.php b/src/Contracts/Feature.php new file mode 100644 index 00000000..b9958f3d --- /dev/null +++ b/src/Contracts/Feature.php @@ -0,0 +1,12 @@ +getTags($entry); + + if (in_array('tenancy', optional(request()->route())->middleware() ?? [])) { + $tags = array_merge($tags, [ + 'tenant:' . tenant('uuid'), + 'domain:' . tenant('domain'), + ]); + } + + return $tags; + }); + } + + public function getTags(IncomingEntry $entry): array + { + return array_reduce($this->callbacks, function ($tags, $listener) use($entry) { + return array_merge($tags, $listener($entry)); + }, []); + } +} \ No newline at end of file diff --git a/src/Features/TenantRedirect.php b/src/Features/TenantRedirect.php new file mode 100644 index 00000000..8a223149 --- /dev/null +++ b/src/Features/TenantRedirect.php @@ -0,0 +1,20 @@ +getTargetUrl(); + $hostname = parse_url($url, PHP_URL_HOST); + $position = strpos($url, $hostname); + $this->setTargetUrl(substr_replace($url, $domain, $position, strlen($hostname))); + + return $this; + }); + } +} \ No newline at end of file diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index 9e82f6df..d7c14d5b 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -4,7 +4,6 @@ declare(strict_types=1); namespace Stancl\Tenancy; -use Laravel\Telescope\Telescope; use Stancl\Tenancy\Commands\Run; use Stancl\Tenancy\Commands\Seed; use Illuminate\Cache\CacheManager; @@ -50,43 +49,7 @@ class TenancyServiceProvider extends ServiceProvider ]); $this->app->register(TenantRouteServiceProvider::class); - - if (\class_exists(Telescope::class)) { - $this->setTelescopeTags(); - } - - $this->registerTenantRedirectMacro(); } - - public function setTelescopeTags() - { - Telescope::tag(function (\Laravel\Telescope\IncomingEntry $entry) { - $tags = $this->app->make(TenantManager::class)->integration('telescope', $entry); - - if (\in_array('tenancy', optional(request()->route())->middleware() ?? [])) { - $tags = \array_merge($tags, [ - 'tenant:' . tenant('uuid'), - 'domain:' . tenant('domain'), - ]); - } - - return $tags; - }); - } - - public function registerTenantRedirectMacro() - { - RedirectResponse::macro('tenant', function (string $domain) { - // replace first occurance of hostname fragment with $domain - $url = $this->getTargetUrl(); - $hostname = \parse_url($url, PHP_URL_HOST); - $position = \strpos($url, $hostname); - $this->setTargetUrl(\substr_replace($url, $domain, $position, \strlen($hostname))); - - return $this; - }); - } - /** * Register services. * diff --git a/src/TenantManagerv2.php b/src/TenantManagerv2.php index 0498e662..11b6c797 100644 --- a/src/TenantManagerv2.php +++ b/src/TenantManagerv2.php @@ -6,8 +6,6 @@ namespace Stancl\Tenancy; use Illuminate\Foundation\Application; -// todo rethink integration events - /** * @internal Class is subject to breaking changes in minor and patch versions. */ @@ -34,6 +32,8 @@ class TenantManagerv2 { $this->app = $app; $this->storage = $storage; + + $this->bootstrapFeatures(); } public function createTenant(Tenant $tenant): self @@ -128,6 +128,15 @@ class TenantManagerv2 return $this; } + protected function bootstrapFeatures(): self + { + foreach ($this->app['config']['tenancy.features'] as $feature) { + $this->app[$feature]->bootstrap(); + } + + return $this; + } + /** * Return a list of TenancyBoostrappers. * @@ -136,7 +145,7 @@ class TenantManagerv2 */ public function tenancyBootstrappers($except = []): array { - return array_key_diff(config('tenancy.bootstrappers'), $except); + return array_key_diff($this->app['config']['tenancy.bootstrappers'], $except); } /**