From 0d5410dc2e68cbd9bbd159c84f28dbb543ddd133 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Thu, 29 May 2025 13:13:34 +0100 Subject: [PATCH 1/2] Allow stache stores to be removed --- src/Stache/Stache.php | 7 +++++++ tests/Stache/StacheTest.php | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Stache/Stache.php b/src/Stache/Stache.php index 1eb76aec62d..517b3a86d3f 100644 --- a/src/Stache/Stache.php +++ b/src/Stache/Stache.php @@ -60,6 +60,13 @@ public function registerStores($stores) return $this; } + public function removeStore(string $store) + { + $this->stores->forget($store); + + return $this; + } + public function stores() { return $this->stores; diff --git a/tests/Stache/StacheTest.php b/tests/Stache/StacheTest.php index f8be258d5e9..450bf07d550 100644 --- a/tests/Stache/StacheTest.php +++ b/tests/Stache/StacheTest.php @@ -61,6 +61,24 @@ public function stores_can_be_registered() }); } + #[Test] + public function stores_can_be_removed() + { + $this->stache->sites(['en']); // store expects the stache to have site(s) + $this->assertTrue($this->stache->stores()->isEmpty()); + + $this->stache->registerStore( + new CollectionsStore($this->stache, \Mockery::mock(Filesystem::class)) + ); + + $return = $this->stache->removeStore('collections'); + + $this->assertEquals($this->stache, $return); + tap($this->stache->stores(), function ($stores) { + $this->assertEquals(0, $stores->count()); + }); + } + #[Test] public function multiple_stores_can_be_registered_at_once() { From b34e9b725a835f43ccb05add2320cc351715b288 Mon Sep 17 00:00:00 2001 From: Ryan Mitchell Date: Mon, 2 Jun 2025 08:21:33 +0100 Subject: [PATCH 2/2] Change approach to exclude rather than remove --- src/Stache/Stache.php | 9 +++++---- tests/Stache/StacheTest.php | 23 +++++++++++++++-------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/src/Stache/Stache.php b/src/Stache/Stache.php index 517b3a86d3f..e130ae8d7a6 100644 --- a/src/Stache/Stache.php +++ b/src/Stache/Stache.php @@ -22,6 +22,7 @@ class Stache protected $lockFactory; protected $locks = []; protected $duplicates; + protected $exclude = []; public function __construct() { @@ -60,9 +61,9 @@ public function registerStores($stores) return $this; } - public function removeStore(string $store) + public function exclude(string $store) { - $this->stores->forget($store); + $this->exclude[] = $store; return $this; } @@ -95,7 +96,7 @@ public function generateId() public function clear() { - $this->stores()->reverse()->each->clear(); + $this->stores()->except($this->exclude)->reverse()->each->clear(); $this->duplicates()->clear(); @@ -117,7 +118,7 @@ public function warm() $this->startTimer(); - $this->stores()->each->warm(); + $this->stores()->except($this->exclude)->each->warm(); $this->stopTimer(); diff --git a/tests/Stache/StacheTest.php b/tests/Stache/StacheTest.php index 450bf07d550..721b51208be 100644 --- a/tests/Stache/StacheTest.php +++ b/tests/Stache/StacheTest.php @@ -6,10 +6,12 @@ use Illuminate\Support\Collection; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\Attributes\Test; +use Statamic\Stache\NullLockStore; use Statamic\Stache\Stache; use Statamic\Stache\Stores\ChildStore; use Statamic\Stache\Stores\CollectionsStore; use Statamic\Stache\Stores\EntriesStore; +use Symfony\Component\Lock\LockFactory; use Tests\TestCase; class StacheTest extends TestCase @@ -62,21 +64,26 @@ public function stores_can_be_registered() } #[Test] - public function stores_can_be_removed() + public function stores_can_be_excluded_from_warming_and_clearing() { $this->stache->sites(['en']); // store expects the stache to have site(s) $this->assertTrue($this->stache->stores()->isEmpty()); - $this->stache->registerStore( - new CollectionsStore($this->stache, \Mockery::mock(Filesystem::class)) - ); + $mockStore = $this->mock(CollectionsStore::class, function ($mock) { + $mock->shouldReceive('warm')->never(); + $mock->shouldReceive('clear')->never(); + $mock->shouldReceive('key')->andReturn('collections'); + }); + + $this->stache->registerStore($mockStore); - $return = $this->stache->removeStore('collections'); + $return = $this->stache->exclude('collections'); $this->assertEquals($this->stache, $return); - tap($this->stache->stores(), function ($stores) { - $this->assertEquals(0, $stores->count()); - }); + + $this->stache->setLockFactory(new LockFactory(new NullLockStore())); + $this->stache->warm(); + $this->stache->clear(); } #[Test]