1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-12 22:14:03 +00:00
tenancy/src/Bootstrappers/PrefixCacheTenancyBootstrapper.php

51 lines
1.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Bootstrappers;
use Illuminate\Cache\CacheManager;
use Illuminate\Contracts\Config\Repository;
use Illuminate\Support\Facades\Cache;
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
use Stancl\Tenancy\Contracts\Tenant;
class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
{
protected string|null $originalPrefix = null;
protected string $storeName;
public static array $tenantCacheStores = [];
public function __construct(
protected Repository $config,
protected CacheManager $cacheManager,
) {
}
public function bootstrap(Tenant $tenant): void
{
$this->originalPrefix = $this->config->get('cache.prefix');
$this->storeName = $this->config->get('cache.default');
if (in_array($this->storeName, static::$tenantCacheStores)) {
$this->setCachePrefix($this->originalPrefix . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey());
}
}
public function revert(): void
{
$this->setCachePrefix($this->originalPrefix);
$this->originalPrefix = null;
}
protected function setCachePrefix(string|null $prefix): void
{
$this->config->set('cache.prefix', $prefix);
$this->cacheManager->refreshStore();
// It is needed when a call to the facade has been made before bootstrapping tenancy
// The facade has its own cache, separate from the container
Cache::clearResolvedInstances();
}
}