Skip to content

Commit 4937a74

Browse files
committed
BroadcastingConfigBootstrapper and TenancyBroadcastManager: comments
1 parent 29dd23d commit 4937a74

2 files changed

Lines changed: 35 additions & 13 deletions

File tree

src/Bootstrappers/BroadcastingConfigBootstrapper.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@
1414
use Stancl\Tenancy\Contracts\Tenant;
1515
use 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+
*/
1723
class 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

src/Overrides/TenancyBroadcastManager.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,26 @@
88
use Illuminate\Broadcasting\BroadcastManager;
99
use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
1010

11+
/**
12+
* BroadcastManager override that always re-resolves the broadcasters in static::$tenantBroadcasters
13+
* when attempting to retrieve them and passes the channels of the original (central) broadcaster
14+
* to the broadcasters newly resolved in tenant context.
15+
*
16+
* Affects calls that use app(BroadcastManager::class)->get().
17+
*
18+
* @see Stancl\Tenancy\Bootstrappers\BroadcastingConfigBootstrapper
19+
*/
1120
class TenancyBroadcastManager extends BroadcastManager
1221
{
1322
/**
1423
* Names of broadcasters that
1524
* - should always be recreated using $this->resolve(), even when they're cached and available
16-
* in $this->drivers (so that e.g. when you update broadcasting credentials in the tenant context,
17-
* the updated credentials will be used for broadcasting in the same context)
25+
* in $this->drivers so that when you update broadcasting config in the tenant context,
26+
* the updated config/credentials will be used for broadcasting in the same context.
27+
* Note that in cases like this, only direct config changes are reflected immediately.
28+
* For the broadcasters to reflect tenant property changes made in tenant context,
29+
* you still have to reinitialize tenancy after updating the tenant properties intended
30+
* for broadcasting config mapping, since the properties are only mapped to config on BroadcastingConfigBootstrapper::bootstrap().
1831
* - should inherit the original broadcaster's channels (= the channels registered in
1932
* the central context, e.g. in routes/channels.php, before this manager overrides the bound BroadcastManager).
2033
*/
@@ -46,9 +59,9 @@ protected function get($name)
4659
}
4760

4861
// The newly resolved broadcasters don't automatically receive the channels registered
49-
// in central context (e.g. in routes/channels.php), so the channels have to be obtained from the
50-
// broadcaster used in central context and manually passed to the new broadcasters
51-
// (attempting to broadcast using a broadcaster with no channels results in a 403 error).
62+
// in central context (e.g. Broadcast::channel() in routes/channels.php), so the channels
63+
// have to be obtained from the original (central) broadcaster and manually passed to the new broadcasters
64+
// (broadcasting using a broadcaster with no channels results in a 403 error on Broadcast::auth()).
5265
protected function passChannelsFromOriginalBroadcaster(Broadcaster $originalBroadcaster, Broadcaster $newBroadcaster): void
5366
{
5467
// invade() because channels can't be retrieved through any of the broadcaster's public methods

0 commit comments

Comments
 (0)