mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 00:54:05 +00:00
55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Stancl\Tenancy;
|
|
|
|
use Illuminate\Support\Facades\Cache;
|
|
use Illuminate\Cache\CacheManager as BaseCacheManager;
|
|
|
|
// todo move to Cache namespace?
|
|
|
|
class CacheManager extends BaseCacheManager
|
|
{
|
|
/**
|
|
* Add tags and forward the call to the inner cache store.
|
|
*
|
|
* @param string $method
|
|
* @param array $parameters
|
|
*/
|
|
public function __call($method, $parameters)
|
|
{
|
|
// todo0 Make PrefixCacheTenancyBootstrapper work with tags
|
|
if (! tenancy()->initialized) {
|
|
return parent::__call($method, $parameters);
|
|
}
|
|
|
|
$tags = [config('tenancy.cache.tag_base') . tenant()?->getTenantKey()];
|
|
|
|
if ($method === 'tags') {
|
|
$count = count($parameters);
|
|
|
|
if ($count !== 1) {
|
|
throw new \Exception("Method tags() takes exactly 1 argument. $count passed.");
|
|
}
|
|
|
|
$names = $parameters[0];
|
|
$names = (array) $names; // cache()->tags('foo') https://laravel.com/docs/9.x/cache#removing-tagged-cache-items
|
|
|
|
return $this->store()->tags(array_merge($tags, $names));
|
|
}
|
|
|
|
return $this->store()->tags($tags)->$method(...$parameters);
|
|
}
|
|
|
|
public function refreshStore(string|null $repository = null): void
|
|
{
|
|
Cache::macro('setStore', function ($store) {
|
|
$this->store = $store;
|
|
});
|
|
|
|
$newStore = $this->resolve($repository ?? $this->getDefaultDriver())->getStore();
|
|
|
|
$this->driver($repository)->setStore($newStore);
|
|
}
|
|
}
|