1414use Stancl \Tenancy \Contracts \Tenant ;
1515use Stancl \Tenancy \Overrides \TenancyBroadcastManager ;
1616
17+ /**
18+ * Maps tenant properties to broadcasting config and overrides
19+ * the BroadcastManager binding with TenancyBroadcastManager in tenant context.
20+ *
21+ * @see TenancyBroadcastManager
22+ */
1723class BroadcastingConfigBootstrapper implements TenancyBootstrapper
1824{
1925 /**
@@ -66,13 +72,15 @@ public function bootstrap(Tenant $tenant): void
6672
6773 $ this ->setConfig ($ tenant );
6874
69- // Make BroadcastManager resolve to TenancyBroadcastManager which always re-resolves the used broadcasters ( so that
70- // the broadcasting credentials are always up-to-date at the point of broadcasting) and gives the channels of
71- // the broadcaster from the central context to the newly resolved broadcasters in tenant context.
75+ // Make BroadcastManager resolve to TenancyBroadcastManager which always re-resolves the used broadcasters so that
76+ // the credentials used by broadcasters are always up-to-date with the config when retrieving the broadcasters using
77+ // the manager and gives the channels of the broadcaster from central context to the newly resolved broadcasters in tenant context.
7278 $ this ->app ->extend (BroadcastManager::class, function (BroadcastManager $ broadcastManager ) {
7379 $ originalCustomCreators = invade ($ broadcastManager )->customCreators ;
7480 $ tenantBroadcastManager = new TenancyBroadcastManager ($ this ->app );
7581
82+ // TenancyBroadcastManager inherits the custom driver creators registered in the central context so that
83+ // custom drivers work in tenant context without having to re-register the creators manually.
7684 foreach ($ originalCustomCreators as $ driver => $ closure ) {
7785 $ tenantBroadcastManager ->extend ($ driver , $ closure );
7886 }
@@ -81,16 +89,17 @@ public function bootstrap(Tenant $tenant): void
8189 });
8290
8391 // Swap currently bound Broadcaster instance for one that's resolved through the tenant broadcast manager.
84- // Note that changing tenant's credentials in tenant context doesn't update them in the bound Broadcaster instance.
85- // If you need to e.g. send a notification in response to updating tenant's broadcasting credentials in tenant context,
86- // it's recommended to use the broadcast() helper which always uses fresh broadcasters with the current credentials.
92+ // Note that updating broadcasting config (credentials) in tenant context doesn't update the credentials
93+ // used by the bound Broadcaster instance. If you need to e.g. send a notification in response to
94+ // updating tenant's broadcasting credentials in tenant context, it's recommended to
95+ // reinitialize tenancy after updating the credentials.
8796 $ this ->app ->extend (Broadcaster::class, function (Broadcaster $ broadcaster ) {
8897 return $ this ->app ->make (BroadcastManager::class)->connection ();
8998 });
9099
91100 // Clear the resolved Broadcast facade's Illuminate\Contracts\Broadcasting\Factory instance
92- // so that it gets re-resolved as the tenant broadcast manager when used (e.g. the
93- // Broadcast::auth() call in BroadcastController::authenticate).
101+ // so that it gets re-resolved as TenancyBroadcastManager instead of the central BroadcastManager
102+ // when used e.g. in the Broadcast::auth() call in BroadcastController::authenticate (/broadcasting/auth ).
94103 Broadcast::clearResolvedInstance (BroadcastingFactory::class);
95104 }
96105
0 commit comments