mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 17:24:03 +00:00
[4.x] Make broadcasting work with Tenancy (#1027)
* Add BroadcastTenancyBootstrapper and TenancyBroadcastManager * Fix code style (php-cs-fixer) * Bind original BroadcastManager again on `revert()` * Fix code style (php-cs-fixer) * Move manager to correct directory * Fix property type * Make BroadcastTenancyBootstrapper a singleton in tests * Fix code style (php-cs-fixer) * Bind the original broadcaster instance on `revert()` * Instead of just forgetting the old broadcaster instance, bind the new one * Add BroadcastTenancyBootstrapper tests * Separate the test * Fix code style (php-cs-fixer) * Add bootstrapper test * Add broadcaster channels test * Clean up BootstrapperTest * Fix BroadcastingTest * Add comments to TenancyBroadcastManager * Add BroadcastTenancyBootstrapper comments * Simplify BroadcastManager extension, remove setDriver method * Add comment * Fix PHPStan errors * Fix PHPStan errors * Remove duplicate import * Fix test * Delete `::class` from test name Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com> * Create databases for newly created tenants in BroadcastingTest * move spatie/invade to require --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
parent
fbdb13f392
commit
d7a4982cd3
7 changed files with 336 additions and 2 deletions
95
src/Bootstrappers/BroadcastTenancyBootstrapper.php
Normal file
95
src/Bootstrappers/BroadcastTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Illuminate\Broadcasting\BroadcastManager;
|
||||
use Illuminate\Config\Repository;
|
||||
use Illuminate\Contracts\Broadcasting\Broadcaster;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
use Stancl\Tenancy\TenancyBroadcastManager;
|
||||
|
||||
class BroadcastTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/**
|
||||
* Tenant properties to be mapped to config (similarly to the TenantConfig feature).
|
||||
*
|
||||
* For example:
|
||||
* [
|
||||
* 'config.key.name' => 'tenant_property',
|
||||
* ]
|
||||
*/
|
||||
public static array $credentialsMap = [];
|
||||
|
||||
public static string|null $broadcaster = null;
|
||||
|
||||
protected array $originalConfig = [];
|
||||
protected BroadcastManager|null $originalBroadcastManager = null;
|
||||
protected Broadcaster|null $originalBroadcaster = null;
|
||||
|
||||
public static array $mapPresets = [
|
||||
'pusher' => [
|
||||
'broadcasting.connections.pusher.key' => 'pusher_key',
|
||||
'broadcasting.connections.pusher.secret' => 'pusher_secret',
|
||||
'broadcasting.connections.pusher.app_id' => 'pusher_app_id',
|
||||
'broadcasting.connections.pusher.options.cluster' => 'pusher_cluster',
|
||||
],
|
||||
'ably' => [
|
||||
'broadcasting.connections.ably.key' => 'ably_key',
|
||||
'broadcasting.connections.ably.public' => 'ably_public',
|
||||
],
|
||||
];
|
||||
|
||||
public function __construct(
|
||||
protected Repository $config,
|
||||
protected Application $app
|
||||
) {
|
||||
static::$broadcaster ??= $config->get('broadcasting.default');
|
||||
static::$credentialsMap = array_merge(static::$credentialsMap, static::$mapPresets[static::$broadcaster] ?? []);
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant): void
|
||||
{
|
||||
$this->originalBroadcastManager = $this->app->make(BroadcastManager::class);
|
||||
$this->originalBroadcaster = $this->app->make(Broadcaster::class);
|
||||
|
||||
$this->setConfig($tenant);
|
||||
|
||||
// Make BroadcastManager resolve to a custom BroadcastManager which makes the broadcasters use the tenant credentials
|
||||
$this->app->extend(BroadcastManager::class, function (BroadcastManager $broadcastManager) {
|
||||
return new TenancyBroadcastManager($this->app);
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
$this->app->singleton(Broadcaster::class, fn (Application $app) => $this->originalBroadcaster);
|
||||
|
||||
$this->unsetConfig();
|
||||
}
|
||||
|
||||
protected function setConfig(Tenant $tenant): void
|
||||
{
|
||||
foreach (static::$credentialsMap as $configKey => $storageKey) {
|
||||
$override = $tenant->$storageKey;
|
||||
|
||||
if (array_key_exists($storageKey, $tenant->getAttributes())) {
|
||||
$this->originalConfig[$configKey] ??= $this->config->get($configKey);
|
||||
|
||||
$this->config->set($configKey, $override);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function unsetConfig(): void
|
||||
{
|
||||
foreach ($this->originalConfig as $key => $value) {
|
||||
$this->config->set($key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue