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

Improve comments

This commit is contained in:
lukinovec 2026-04-21 13:25:05 +02:00
parent e592b3700e
commit ddd8c68fbd
2 changed files with 16 additions and 10 deletions

View file

@ -29,6 +29,8 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper
* [ * [
* 'config.key.name' => 'tenant_property', * 'config.key.name' => 'tenant_property',
* ] * ]
*
* $tenant->tenant_property will be mapped to config('config.key.name') when tenancy is initialized.
*/ */
public static array $credentialsMap = []; public static array $credentialsMap = [];
@ -72,15 +74,18 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper
$this->setConfig($tenant); $this->setConfig($tenant);
// Make BroadcastManager resolve to TenancyBroadcastManager which always re-resolves the used broadcasters so that // Make BroadcastManager resolve to TenancyBroadcastManager. The manager:
// the credentials used by broadcasters are always up-to-date with the config when retrieving the broadcasters using // - resolves fresh broadcasters so that the updated (tenant) broadcasting config is used while broadcasting
// the manager and gives the channels of the broadcaster from central context to the newly resolved broadcasters in tenant context. // - 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) { $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);
// TenancyBroadcastManager inherits the custom driver creators registered in the central context so that // Make TenancyBroadcastManager inherit the custom driver creators registered in the central context
// custom drivers work in tenant context without having to re-register the creators manually. // so that custom drivers work in tenant context without having to re-register the creators manually.
foreach ($originalCustomCreators as $driver => $closure) { foreach ($originalCustomCreators as $driver => $closure) {
$tenantBroadcastManager->extend($driver, $closure); $tenantBroadcastManager->extend($driver, $closure);
} }
@ -105,8 +110,8 @@ class BroadcastingConfigBootstrapper implements TenancyBootstrapper
public function revert(): void public function revert(): void
{ {
// Change the BroadcastManager and Broadcaster singletons back to what they were before initializing tenancy // Revert the bound BroadcastManager and Broadcaster singletons back to their original state
$this->app->singleton(BroadcastManager::class, fn (Application $app) => $this->originalBroadcastManager); $this->app->singleton(BroadcastManager::class, fn (Application $app): ?BroadcastManager => $this->originalBroadcastManager);
$this->app->singleton(Broadcaster::class, fn (Application $app) => $this->originalBroadcaster); $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 // Clear the resolved Broadcast facade instance so that it gets re-resolved as the central BroadcastManager

View file

@ -10,10 +10,11 @@ use Illuminate\Contracts\Broadcasting\Broadcaster as BroadcasterContract;
/** /**
* BroadcastManager override that always re-resolves the broadcasters in static::$tenantBroadcasters * 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. * 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 * @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. // Give the channels of the original (central) broadcaster 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, 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). // Illuminate\Broadcasting\Broadcasters\Broadcaster instances (= all the default broadcasters, e.g. PusherBroadcaster).
if ($originalBroadcaster instanceof Broadcaster && $newBroadcaster instanceof Broadcaster) { if ($originalBroadcaster instanceof Broadcaster && $newBroadcaster instanceof Broadcaster) {
$this->passChannelsFromOriginalBroadcaster($originalBroadcaster, $newBroadcaster); $this->passChannelsFromOriginalBroadcaster($originalBroadcaster, $newBroadcaster);