From fa00fc7a2800d619251d32fdd9c899133aed5b43 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 16 Dec 2022 11:12:33 +0100 Subject: [PATCH] Add changes to PR to Laravel --- src/CacheManager.php | 60 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/src/CacheManager.php b/src/CacheManager.php index e316c3ea..21751f60 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -4,8 +4,16 @@ declare(strict_types=1); namespace Stancl\Tenancy; -use Illuminate\Cache\CacheManager as BaseCacheManager; +use Illuminate\Cache\ApcStore; +use Illuminate\Cache\FileStore; +use Illuminate\Cache\NullStore; +use Illuminate\Cache\ApcWrapper; +use Illuminate\Cache\ArrayStore; +use Illuminate\Cache\RedisStore; use Illuminate\Cache\Repository; +use Illuminate\Cache\MemcachedStore; +use Illuminate\Contracts\Cache\Store; +use Illuminate\Cache\CacheManager as BaseCacheManager; // todo move to Cache namespace? @@ -49,8 +57,54 @@ class CacheManager extends BaseCacheManager $this->store = $store; }); - $newStore = $this->resolve($repository ?? $this->getDefaultDriver())->getStore(); + $this->driver($repository)->setStore($this->createStore($repository ?? $this->getDefaultDriver())); + } - $this->driver($repository)->setStore($newStore); + protected function createApcStore(array $config): ApcStore + { + return new ApcStore(new ApcWrapper, $this->getPrefix($config)); + } + + protected function createArrayStore(array $config): ArrayStore + { + return new ArrayStore($config['serialize'] ?? false); + } + + protected function createFileStore(array $config): FileStore + { + return new FileStore($this->app['files'], $config['path'], $config['permission'] ?? null); + } + + protected function createMemcachedStore(array $config): MemcachedStore + { + $memcached = $this->app['memcached.connector']->connect( + $config['servers'], + $config['persistent_id'] ?? null, + $config['options'] ?? [], + array_filter($config['sasl'] ?? []) + ); + + return new MemcachedStore($memcached, $this->getPrefix($config)); + } + + protected function createRedisStore(array $config): RedisStore + { + $connection = $config['connection'] ?? 'default'; + $store = new RedisStore($this->app['redis'], $this->getPrefix($config), $connection); + + return $store->setLockConnection($config['lock_connection'] ?? $connection); + } + + protected function createNullStore(): NullStore + { + return new NullStore; + } + + public function createStore(string|null $name, array|null $config = null): Store + { + $name ??= 'null'; + $storeCreationMethod = 'create' . ucfirst($name) . 'Store'; + + return $this->{$storeCreationMethod}($config ?? $this->getConfig($name)); } }