diff --git a/composer.json b/composer.json index 63196393..3ca36b70 100644 --- a/composer.json +++ b/composer.json @@ -16,6 +16,7 @@ }, "require-dev": { "vlucas/phpdotenv": "^3.3", + "laravel/telescope": "^2.0", "laravel/framework": "5.8.*", "orchestra/testbench-browser-kit": "~3.8", "league/flysystem-aws-s3-v3": "~1.0", diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index 411873f5..2ee59ab2 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -2,6 +2,7 @@ namespace Stancl\Tenancy; +use Laravel\Telescope\Telescope; use Stancl\Tenancy\Commands\Run; use Stancl\Tenancy\Commands\Seed; use Illuminate\Cache\CacheManager; @@ -49,10 +50,30 @@ class TenancyServiceProvider extends ServiceProvider $this->app->register(TenantRouteServiceProvider::class); + if (\class_exists(Telescope::class)) { + $this->setTelescopeTags(); + } + $this->registerTenantRedirectMacro(); $this->makeQueuesTenantAware(); } + public function setTelescopeTags() + { + Telescope::tag(function (\Laravel\Telescope\IncomingEntry $entry) { + $tags = $this->app->make(TenantManager::class)->integration('telescope', $entry); + + if (\in_array('tenancy', request()->route()->middleware())) { + $tags = \array_merge($tags, [ + 'tenant:' . tenant('uuid'), + 'domain:' . tenant('domain'), + ]); + } + + return $tags; + }); + } + public function registerTenantRedirectMacro() { RedirectResponse::macro('tenant', function (string $domain) { diff --git a/src/Traits/TenantManagerEvents.php b/src/Traits/TenantManagerEvents.php index 38e303a0..986388cb 100644 --- a/src/Traits/TenantManagerEvents.php +++ b/src/Traits/TenantManagerEvents.php @@ -18,6 +18,13 @@ trait TenantManagerEvents 'ended' => [], ]; + /** + * Integration listeners. + * + * @var callable[][] + */ + protected $integrationListeners = []; + /** * Register a listener that will be executed before tenancy is bootstrapped. * @@ -82,4 +89,39 @@ trait TenantManagerEvents return $prevents->merge($listener($this) ?? []); }, collect([])); } + + /** + * Register a callback for an integration event. + * + * @param string $name + * @param callable $callback + * @return void + */ + public function integrationEvent(string $name, callable $callback) + { + if (\array_key_exists($name, $this->integrationListeners)) { + $this->integrationListeners[$name][] = $callback; + } else { + $this->integrationListeners[$name] = [$callback]; + } + } + + /** + * Return callbacks for an integration event. + * + * @param string $name + * @param mixed $arguments,... + * @return callable[] + */ + public function integration(string $name, ...$arguments) + { + if ($arguments) { + // If $arguments are supplied, execute all listeners with arguments. + return \array_reduce($this->integrationListeners[$name] ?? [], function ($tags, $listener) use ($arguments) { + return \array_merge($tags, $listener(...$arguments)); + }, []); + } + + return $this->integrationListeners[$name]; + } }