From 1252b7a637d0dfaa9ecbeb1db16b9273ddf2ec15 Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 24 Jun 2024 17:33:42 -0400 Subject: [PATCH] [5.x] Custom file cache store adjustments (#10362) --- src/Extensions/FileStore.php | 15 +++++++- src/Providers/CacheServiceProvider.php | 12 ++++--- tests/Extensions/FileStoreTest.php | 47 ++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 tests/Extensions/FileStoreTest.php diff --git a/src/Extensions/FileStore.php b/src/Extensions/FileStore.php index fe9ae2ac5c..04bfe853ed 100644 --- a/src/Extensions/FileStore.php +++ b/src/Extensions/FileStore.php @@ -8,6 +8,8 @@ class FileStore extends LaravelFileStore implements Store { + private ?string $dir = null; + public function path($key) { if (! Str::startsWith($key, 'stache::')) { @@ -16,6 +18,17 @@ public function path($key) $key = Str::after($key, 'stache::'); - return $this->directory.'/stache/'.str_replace('::', '/', $key); + return $this->dir().str_replace('::', '/', $key); + } + + private function dir() + { + if ($this->dir) { + return $this->dir; + } + + return $this->dir = Str::endsWith($this->directory, '/stache') + ? $this->directory.'/' + : $this->directory.'/stache/'; } } diff --git a/src/Providers/CacheServiceProvider.php b/src/Providers/CacheServiceProvider.php index 21399cdbe7..642dd7d46e 100644 --- a/src/Providers/CacheServiceProvider.php +++ b/src/Providers/CacheServiceProvider.php @@ -37,6 +37,7 @@ public function boot() private function extendFileStore() { $this->app->booting(function () { + /** @deprecated */ Cache::extend('statamic', function () { return Cache::repository(new FileStore( $this->app['files'], @@ -45,10 +46,13 @@ private function extendFileStore() ), $this->app['config']['cache.stores.file']); }); - if (config('cache.default') === 'file') { - config(['cache.stores.statamic' => ['driver' => 'statamic']]); - config(['cache.default' => 'statamic']); - } + Cache::extend('file', function ($app, $config) { + return Cache::repository( + (new FileStore($app['files'], $config['path'], $config['permission'] ?? null)) + ->setLockDirectory($config['lock_path'] ?? null), + $config + ); + }); }); } diff --git a/tests/Extensions/FileStoreTest.php b/tests/Extensions/FileStoreTest.php new file mode 100644 index 0000000000..cfaa71d396 --- /dev/null +++ b/tests/Extensions/FileStoreTest.php @@ -0,0 +1,47 @@ +getStore(); + $this->assertInstanceOf(FileStore::class, $alfa); + $this->assertEquals(storage_path('framework/cache/alfa'), $alfa->getDirectory()); + + $bravo = Cache::store('bravo')->getStore(); + $this->assertInstanceOf(FileStore::class, $bravo); + $this->assertEquals(storage_path('framework/cache/bravo'), $bravo->getDirectory()); + + // Non-file stores shouldn't be modified. + $charlie = Cache::store('charlie')->getStore(); + $this->assertInstanceOf(ArrayStore::class, $charlie); + } + + public function cache($app) + { + $app['config']->set('cache.stores.alfa', [ + 'driver' => 'file', + 'path' => storage_path('framework/cache/alfa'), + ]); + + $app['config']->set('cache.stores.bravo', [ + 'driver' => 'file', + 'path' => storage_path('framework/cache/bravo'), + ]); + + $app['config']->set('cache.stores.charlie', [ + 'driver' => 'array', + ]); + } +}