mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-06 00:04:03 +00:00
Merge branch 'master' of github.com:archtechx/tenancy into add-postgres-rls-support
This commit is contained in:
commit
7f1f594809
13 changed files with 546 additions and 34 deletions
|
|
@ -7,13 +7,20 @@ namespace Stancl\Tenancy\Bootstrappers;
|
|||
use Illuminate\Cache\CacheManager;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Stancl\Tenancy\CacheManager as TenantCacheManager;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class CacheTenancyBootstrapper implements TenancyBootstrapper
|
||||
/**
|
||||
* todo name.
|
||||
*
|
||||
* Separate tenant cache using tagging.
|
||||
* This is the legacy approach. Some things, like dependency injection, won't work properly with this bootstrapper.
|
||||
* PrefixCacheTenancyBootstrapper is the recommended bootstrapper for cache separation.
|
||||
*/
|
||||
class CacheTagsBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
protected ?CacheManager $originalCache = null;
|
||||
public static string $cacheManagerWithTags = \Stancl\Tenancy\CacheManager::class;
|
||||
|
||||
public function __construct(
|
||||
protected Application $app
|
||||
|
|
@ -24,9 +31,9 @@ class CacheTenancyBootstrapper implements TenancyBootstrapper
|
|||
{
|
||||
$this->resetFacadeCache();
|
||||
|
||||
$this->originalCache = $this->originalCache ?? $this->app['cache'];
|
||||
$this->originalCache ??= $this->app['cache'];
|
||||
$this->app->extend('cache', function () {
|
||||
return new TenantCacheManager($this->app);
|
||||
return new static::$cacheManagerWithTags($this->app);
|
||||
});
|
||||
}
|
||||
|
||||
84
src/Bootstrappers/PrefixCacheTenancyBootstrapper.php
Normal file
84
src/Bootstrappers/PrefixCacheTenancyBootstrapper.php
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Stancl\Tenancy\Bootstrappers;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Cache\CacheManager;
|
||||
use Illuminate\Cache\Repository;
|
||||
use Illuminate\Contracts\Config\Repository as ConfigRepository;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||
use Stancl\Tenancy\Contracts\Tenant;
|
||||
|
||||
class PrefixCacheTenancyBootstrapper implements TenancyBootstrapper
|
||||
{
|
||||
protected string|null $originalPrefix = null;
|
||||
public static array $tenantCacheStores = []; // E.g. ['redis']
|
||||
public static Closure|null $prefixGenerator = null;
|
||||
|
||||
public function __construct(
|
||||
protected ConfigRepository $config,
|
||||
protected CacheManager $cacheManager,
|
||||
) {
|
||||
}
|
||||
|
||||
public function bootstrap(Tenant $tenant): void
|
||||
{
|
||||
$this->originalPrefix = $this->config->get('cache.prefix');
|
||||
|
||||
$prefix = $this->generatePrefix($tenant);
|
||||
|
||||
foreach (static::$tenantCacheStores as $store) {
|
||||
$this->setCachePrefix($store, $prefix);
|
||||
|
||||
// Now that the store uses the passed prefix
|
||||
// Set the configured prefix back to the default one
|
||||
$this->config->set('cache.prefix', $this->originalPrefix);
|
||||
}
|
||||
}
|
||||
|
||||
public function revert(): void
|
||||
{
|
||||
foreach (static::$tenantCacheStores as $store) {
|
||||
$this->setCachePrefix($store, $this->originalPrefix);
|
||||
}
|
||||
}
|
||||
|
||||
protected function setCachePrefix(string $driver, string|null $prefix): void
|
||||
{
|
||||
$this->config->set('cache.prefix', $prefix);
|
||||
|
||||
// Refresh driver's store to make the driver use the current prefix
|
||||
$this->refreshStore($driver);
|
||||
|
||||
// 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();
|
||||
}
|
||||
|
||||
public function generatePrefix(Tenant $tenant): string
|
||||
{
|
||||
$defaultPrefix = $this->originalPrefix . $this->config->get('tenancy.cache.prefix_base') . $tenant->getTenantKey();
|
||||
|
||||
return static::$prefixGenerator ? (static::$prefixGenerator)($tenant) : $defaultPrefix;
|
||||
}
|
||||
|
||||
public static function generatePrefixUsing(Closure $prefixGenerator): void
|
||||
{
|
||||
static::$prefixGenerator = $prefixGenerator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh cache driver's store.
|
||||
*/
|
||||
protected function refreshStore(string $driver): void
|
||||
{
|
||||
$newStore = $this->cacheManager->resolve($driver)->getStore();
|
||||
/** @var Repository $repository */
|
||||
$repository = $this->cacheManager->driver($driver);
|
||||
|
||||
$repository->setStore($newStore);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue