From 57bcf9f0168b4ca50702bdbad9aa22c4e1c5d68c Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 24 Jun 2024 17:06:55 -0400 Subject: [PATCH 1/2] Adjust so that any store using the file driver will use the custom version --- src/Providers/CacheServiceProvider.php | 12 ++++--- tests/Extensions/FileStoreTest.php | 47 ++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 tests/Extensions/FileStoreTest.php diff --git a/src/Providers/CacheServiceProvider.php b/src/Providers/CacheServiceProvider.php index 21399cdbe79..642dd7d46e2 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 00000000000..cfaa71d396a --- /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', + ]); + } +} From c1e018d1a37ae2f74d9dfb0f816bd8b3ef1be28e Mon Sep 17 00:00:00 2001 From: Jason Varga Date: Mon, 24 Jun 2024 17:07:10 -0400 Subject: [PATCH 2/2] prevent double stache directory --- src/Extensions/FileStore.php | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Extensions/FileStore.php b/src/Extensions/FileStore.php index fe9ae2ac5c9..04bfe853edb 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/'; } }