mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 12:54:05 +00:00
Add broadcasting channel prefixing bootstrapper (#12)
* Rename old broadcast bootstrapper, add new one * Add broadcast tenancy bootstrapper + tests * Fix code style (php-cs-fixer) * Fix prefixing * Work on th bootstrapper's tests (wip – problem with events) * Fix bootstrapper * Test that auth closures of channels work correctly * Fix bootstrapper * Fix code style (php-cs-fixer) * Delete channel cloning bootstrapper * Add bootstrapper that prefixes broadcastOn channels under the hood * Add broadcast channel registering helpers * Update prefixing tests (WIP) * Fix code style (php-cs-fixer) * Improve comment * Fix code style (php-cs-fixer) * Allow customization of Pusher/Ably broadcaster extension * Fix code style (php-cs-fixer) * Implement prefix bootstrapper logic, test channel prefixing using a closure * Work on the prefixing bootstrapper and tests * Fix code style (php-cs-fixer) * Add optional $options param to broadcasting helpers * Test broadcasting helpers * Fix code style (php-cs-fixer) * Broadcasting channel prefixing + testing progress * Improve helper methods * Fix and improve test * Fix extending in bootstrap() * Fix code style (php-cs-fixer) * Add docblocks, name things more accurately * Fix code style (php-cs-fixer) * Delete redundant method from testing broadcaster * Test Pusher channel prefixing (probabaly redundant?) * Test if channels get prefixed correctly when switching tenants * Work with the current broadcast manager instead of overriding it * Give the original channels to the overriden broadcasters * Fix code style (php-cs-fixer) * Simplify channel prefix bootstrapper * Fix code style (php-cs-fixer) * Fix comment * Fix test * Delete annotation * Delete unused classes from test * Delete outdated test * Move broadcasting bootstrapper test to BootstrapperTest * Improve bootstrapper test, delete unused event * Add annotations to the bootstrapper * Fix code style (php-cs-fixer) * Improve wording * Improve comment * Update src/Bootstrappers/BroadcastChannelPrefixBootstrapper.php * Apply suggestions from code review * Optionally skip prefixing of specific channels * Add and test central channel helper, update formatChannels overrides and tests * Fix code style (php-cs-fixer) * minor fixes * Improve annotation * Use "global__" prefix instead of "central__", add comments * Correct tests --------- Co-authored-by: PHP CS Fixer <phpcsfixer@example.com> Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
parent
b503dbf33d
commit
c34952f328
7 changed files with 450 additions and 24 deletions
95
src/Bootstrappers/BroadcastingConfigBootstrapper.php
Normal file
95
src/Bootstrappers/BroadcastingConfigBootstrapper.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\Overrides\TenancyBroadcastManager;
|
||||
|
||||
class BroadcastingConfigBootstrapper 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