1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-05-07 06:54:03 +00:00

Improve comments

This commit is contained in:
lukinovec 2026-04-02 15:28:33 +02:00
parent 0fbe1bc846
commit b6c035c912
2 changed files with 25 additions and 21 deletions

View file

@ -66,8 +66,9 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper
$this->setConfig($tenant); $this->setConfig($tenant);
// Make BroadcastManager resolve to a custom BroadcastManager which makes the broadcasters use the tenant credentials. // Make BroadcastManager resolve to TenancyBroadcastManager which always re-resolves the used broadcasters (so that
// TenantBroadcastManager also inherits the custom drivers registered in the original BroadcastManager, so that they can be used in tenant context as well. // the broadcasting credentials are always up-to-date at the point of broadcasting) and gives the channels of
// the broadcaster from the central context to the newly resolved broadcasters in tenant context.
$this->app->extend(BroadcastManager::class, function (BroadcastManager $broadcastManager) { $this->app->extend(BroadcastManager::class, function (BroadcastManager $broadcastManager) {
$originalCustomCreators = invade($broadcastManager)->customCreators; $originalCustomCreators = invade($broadcastManager)->customCreators;
$tenantBroadcastManager = new TenancyBroadcastManager($this->app); $tenantBroadcastManager = new TenancyBroadcastManager($this->app);
@ -79,13 +80,17 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper
return $tenantBroadcastManager; return $tenantBroadcastManager;
}); });
// Make the Broadcaster singleton resolve to the broadcaster of the TenantBroadcastManager so that it uses the tenant credentials // Swap currently bound Broadcaster instance for one that's resolved through the tenant broadcast manager.
// Note that changing tenant's credentials in tenant context doesn't update them in the bound Broadcaster instance.
// If you need to e.g. send a notification in response to changing tenant's broadcasting credentials,
// it's recommended to use the broadcast() helper which always uses fresh broadcasters with the current credentials.
$this->app->extend(Broadcaster::class, function (Broadcaster $broadcaster) { $this->app->extend(Broadcaster::class, function (Broadcaster $broadcaster) {
return $this->app->make(BroadcastManager::class)->connection(); return $this->app->make(BroadcastManager::class)->connection();
}); });
// Clear the resolved Broadcast facade instance so that it gets re-resolved as the tenant broadcast manager // Clear the resolved Broadcast facade's Illuminate\Contracts\Broadcasting\Factory instance
// when used (e.g. in BroadcastController::authenticate) // so that it gets re-resolved as the tenant broadcast manager when used (e.g. the
// Broadcast::auth() call in BroadcastController::authenticate).
Broadcast::clearResolvedInstance(BroadcastingFactory::class); Broadcast::clearResolvedInstance(BroadcastingFactory::class);
} }

View file

@ -11,21 +11,18 @@ use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
class TenancyBroadcastManager extends BroadcastManager class TenancyBroadcastManager extends BroadcastManager
{ {
/** /**
* Names of broadcasters that should always be recreated using $this->resolve() * Names of broadcasters that
* (even when they're cached and available in the $broadcasters property to prevent * - should always be recreated using $this->resolve(), even when they're cached and available
* any potential leaks between contexts) and that should inherit the original broadcaster's channels. * in $this->drivers (so that e.g. when you update tenant's broadcaster credentials in the tenant context,
* * the updated credentials will be used for broadcasting in the same context)
* The main concern is inheriting the channels, since the channels get registered * - should inherit the original broadcaster's channels (= the channels registered in
* (e.g. in routes/channels.php) before this manager overrides the BroadcastManager instance * the central context, e.g. in routes/channels.php, before this manager overrides the bound BroadcastManager)
* and new broadcaster instances don't receive the channels automatically.
*/ */
public static array $tenantBroadcasters = ['pusher', 'ably']; public static array $tenantBroadcasters = ['pusher', 'ably'];
/** /**
* Override the get method so that the broadcasters in $tenantBroadcasters * Override the get method so that the broadcasters in static::$tenantBroadcasters
* receive the original broadcaster's channels and always get freshly resolved * receive the original broadcaster's channels and always get freshly resolved.
* even when they're cached and available in the $broadcasters property,
* and that the resolved broadcaster will override the BroadcasterContract::class singleton.
*/ */
protected function get($name) protected function get($name)
{ {
@ -34,10 +31,10 @@ class TenancyBroadcastManager extends BroadcastManager
$originalBroadcaster = $this->app->make(BroadcasterContract::class); $originalBroadcaster = $this->app->make(BroadcasterContract::class);
$newBroadcaster = $this->resolve($name); $newBroadcaster = $this->resolve($name);
// If there is a current broadcaster, give its channels to the newly resolved one // Give the channels of the original broadcaster (from the central context) to the newly resolved one.
// Broadcasters only have to implement the Illuminate\Contracts\Broadcasting\Broadcaster contract // Broadcasters only have to implement the Illuminate\Contracts\Broadcasting\Broadcaster contract
// Which doesn't require the channels property // which doesn't require the channels property, so passing the channels is only
// So passing the channels is only needed for Illuminate\Broadcasting\Broadcasters\Broadcaster instances // needed for Illuminate\Broadcasting\Broadcasters\Broadcaster instances.
if ($originalBroadcaster instanceof Broadcaster && $newBroadcaster instanceof Broadcaster) { if ($originalBroadcaster instanceof Broadcaster && $newBroadcaster instanceof Broadcaster) {
$this->passChannelsFromOriginalBroadcaster($originalBroadcaster, $newBroadcaster); $this->passChannelsFromOriginalBroadcaster($originalBroadcaster, $newBroadcaster);
} }
@ -48,8 +45,10 @@ class TenancyBroadcastManager extends BroadcastManager
return parent::get($name); return parent::get($name);
} }
// Because, unlike the original broadcaster, the newly resolved broadcaster won't have the channels registered using routes/channels.php // The newly resolved broadcasters don't automatically receive the channels registered
// Using it for broadcasting won't work, unless we make it have the original broadcaster's channels // in central context (e.g. in routes/channels.php), so we have to obtain the channels from the
// broadcaster used in central context and manually pass them to the new broadcasters
// (attempting to broadcast using a broadcaster with no channels results in a 403 error).
protected function passChannelsFromOriginalBroadcaster(Broadcaster $originalBroadcaster, Broadcaster $newBroadcaster): void protected function passChannelsFromOriginalBroadcaster(Broadcaster $originalBroadcaster, Broadcaster $newBroadcaster): void
{ {
// invade() because channels can't be retrieved through any of the broadcaster's public methods // invade() because channels can't be retrieved through any of the broadcaster's public methods