mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-14 12:44:03 +00:00
Change bootstrappers namespace
This commit is contained in:
parent
b772590479
commit
1a8d150f2c
18 changed files with 26 additions and 26 deletions
51
src/Bootstrappers/RedisTenancyBootstrapper.php
Normal file
51
src/Bootstrappers/RedisTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Illuminate\Support\Facades\Redis;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class RedisTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
/** @var array<string, string> Original prefixes of connections */
|
||||
public $originalPrefixes = [];
|
||||
|
||||
/** @var Repository */
|
||||
protected $config;
|
||||
|
||||
public function __construct(Repository $config)
|
||||
{
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant)
|
||||
{
|
||||
foreach ($this->prefixedConnections() as $connection) {
|
||||
$prefix = $this->config['tenancy.redis.prefix_base'] . $tenant->getTenantKey();
|
||||
$client = Redis::connection($connection)->client();
|
||||
|
||||
$this->originalPrefixes[$connection] = $client->getOption($client::OPT_PREFIX);
|
||||
$client->setOption($client::OPT_PREFIX, $prefix);
|
||||
}
|
||||
}
|
||||
|
||||
public function revert()
|
||||
{
|
||||
foreach ($this->prefixedConnections() as $connection) {
|
||||
$client = Redis::connection($connection)->client();
|
||||
|
||||
$client->setOption($client::OPT_PREFIX, $this->originalPrefixes[$connection]);
|
||||
}
|
||||
|
||||
$this->originalPrefixes = [];
|
||||
}
|
||||
|
||||
protected function prefixedConnections()
|
||||
{
|
||||
return $this->config['tenancy.redis.prefixed_connections'];
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue