diff --git a/src/Bootstrappers/BroadcastingConfigBootstrapper.php b/src/Bootstrappers/BroadcastingConfigBootstrapper.php index e9685ef5..bf392728 100644 --- a/src/Bootstrappers/BroadcastingConfigBootstrapper.php +++ b/src/Bootstrappers/BroadcastingConfigBootstrapper.php @@ -29,6 +29,8 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper * [ * 'config.key.name' => 'tenant_property', * ] + * + * $tenant->tenant_property will be mapped to config('config.key.name') when tenancy is initialized. */ public static array $credentialsMap = []; @@ -72,15 +74,18 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper $this->setConfig($tenant); - // Make BroadcastManager resolve to TenancyBroadcastManager which always re-resolves the used broadcasters so that - // the credentials used by broadcasters are always up-to-date with the config when retrieving the broadcasters using - // the manager and gives the channels of the broadcaster from central context to the newly resolved broadcasters in tenant context. + // Make BroadcastManager resolve to TenancyBroadcastManager. The manager: + // - resolves fresh broadcasters so that the updated (tenant) broadcasting config is used while broadcasting + // - makes the tenant broadcasters inherit the channels of the original (central) broadcaster + // (since newly resolved broadcasters don't receive any channels by default, broadcasting on + // channels registered in central context, e.g. in routes/channels.php, would otherwise not + // work with the tenant broadcasters) $this->app->extend(BroadcastManager::class, function (BroadcastManager $broadcastManager) { $originalCustomCreators = invade($broadcastManager)->customCreators; $tenantBroadcastManager = new TenancyBroadcastManager($this->app); - // TenancyBroadcastManager inherits the custom driver creators registered in the central context so that - // custom drivers work in tenant context without having to re-register the creators manually. + // Make TenancyBroadcastManager inherit the custom driver creators registered in the central context + // so that custom drivers work in tenant context without having to re-register the creators manually. foreach ($originalCustomCreators as $driver => $closure) { $tenantBroadcastManager->extend($driver, $closure); } @@ -105,8 +110,8 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper public function revert(): void { - // Change the BroadcastManager and Broadcaster singletons back to what they were before initializing tenancy - $this->app->singleton(BroadcastManager::class, fn (Application $app) => $this->originalBroadcastManager); + // Revert the bound BroadcastManager and Broadcaster singletons back to their original state + $this->app->singleton(BroadcastManager::class, fn (Application $app): ?BroadcastManager => $this->originalBroadcastManager); $this->app->singleton(Broadcaster::class, fn (Application $app) => $this->originalBroadcaster); // Clear the resolved Broadcast facade instance so that it gets re-resolved as the central BroadcastManager diff --git a/src/Overrides/TenancyBroadcastManager.php b/src/Overrides/TenancyBroadcastManager.php index 6454156b..3d77cc4a 100644 --- a/src/Overrides/TenancyBroadcastManager.php +++ b/src/Overrides/TenancyBroadcastManager.php @@ -10,10 +10,11 @@ use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract; /** * BroadcastManager override that always re-resolves the broadcasters in static::$tenantBroadcasters - * when attempting to retrieve them and passes the channels of the original (central) broadcaster + * when attempting to retrieve them so that they use the updated tenant-specific config + * and passes the channels of the original (central) broadcaster * to the newly resolved (tenant) broadcasters. * - * Affects calls that use app(BroadcastManager::class)->get(). + * Affects calls that use BroadcastManager's get() method. * * @see Stancl\Tenancy\Bootstrappers\BroadcastingConfigBootstrapper */ @@ -49,7 +50,7 @@ class TenancyBroadcastManager extends BroadcastManager // Give the channels of the original (central) broadcaster to the newly resolved one. // // Broadcasters only have to implement the Illuminate\Contracts\Broadcasting\Broadcaster contract - // which doesn't require the channels property, so passing the channels is only needed for + // which doesn't require the channels property, so we only pass the channels to // Illuminate\Broadcasting\Broadcasters\Broadcaster instances (= all the default broadcasters, e.g. PusherBroadcaster). if ($originalBroadcaster instanceof Broadcaster && $newBroadcaster instanceof Broadcaster) { $this->passChannelsFromOriginalBroadcaster($originalBroadcaster, $newBroadcaster);