From 9fb1d1618e9d066b92aa3303066e770cbff5336d Mon Sep 17 00:00:00 2001 From: Holger Veltrup Date: Thu, 20 Jun 2024 11:17:13 +0200 Subject: [PATCH] feat: supports the deletion of loader caches --- src/Loader/CachedResourceLoader.php | 6 +++++ src/Loader/SiteKitLoader.php | 5 ++++ src/Loader/SiteKitResourceHierarchyLoader.php | 5 ++++ src/ResourceLoader.php | 6 +++++ test/Loader/CachedResourceLoaderTest.php | 25 +++++++++++++++++++ test/Loader/SiteKitLoaderTest.php | 6 +++++ .../SiteKitResourceHierarchyLoaderTest.php | 14 +++++++++++ 7 files changed, 67 insertions(+) diff --git a/src/Loader/CachedResourceLoader.php b/src/Loader/CachedResourceLoader.php index 1bd83a9..7839877 100644 --- a/src/Loader/CachedResourceLoader.php +++ b/src/Loader/CachedResourceLoader.php @@ -46,4 +46,10 @@ public function exists(ResourceLocation $location): bool } return $this->resourceLoader->exists($location); } + + public function cleanup(): void + { + $this->resourceLoader->cleanup(); + $this->cache = []; + } } diff --git a/src/Loader/SiteKitLoader.php b/src/Loader/SiteKitLoader.php index f44b8b2..b360b76 100644 --- a/src/Loader/SiteKitLoader.php +++ b/src/Loader/SiteKitLoader.php @@ -68,6 +68,11 @@ public function exists(ResourceLocation $location): bool ); } + public function cleanup(): void + { + $this->langLocaleMap = null; + } + private function locationToFile(ResourceLocation $location): string { $file = $this->resourceChannel->resourceDir . '/' . diff --git a/src/Loader/SiteKitResourceHierarchyLoader.php b/src/Loader/SiteKitResourceHierarchyLoader.php index 0965cb9..3a60b9e 100644 --- a/src/Loader/SiteKitResourceHierarchyLoader.php +++ b/src/Loader/SiteKitResourceHierarchyLoader.php @@ -33,6 +33,11 @@ public function exists(ResourceLocation $location): bool return $this->resourceLoader->exists($location); } + public function cleanup(): void + { + $this->resourceLoader->cleanup(); + } + /** * @throws InvalidResourceException * @throws ResourceNotFoundException diff --git a/src/ResourceLoader.php b/src/ResourceLoader.php index 47a2ec5..1de56ca 100644 --- a/src/ResourceLoader.php +++ b/src/ResourceLoader.php @@ -20,4 +20,10 @@ interface ResourceLoader public function load(ResourceLocation $location): Resource; public function exists(ResourceLocation $location): bool; + + /** + * Can be used, for example, to clear the loader's + * cache if the loader uses a cache. + */ + public function cleanup(): void; } diff --git a/test/Loader/CachedResourceLoaderTest.php b/test/Loader/CachedResourceLoaderTest.php index c14234b..5b8c629 100644 --- a/test/Loader/CachedResourceLoaderTest.php +++ b/test/Loader/CachedResourceLoaderTest.php @@ -74,4 +74,29 @@ public function testExistsCached(): void 'Resource should be test from cache' ); } + + public function testCleanup(): void + { + + $location = ResourceLocation::of('test'); + $resource = $this->createStub(Resource::class); + $loader = $this->createMock(ResourceLoader::class); + $loader->expects($this->once()) + ->method('load') + ->with($location) + ->willReturn($resource); + $loader->expects($this->exactly(1)) + ->method('exists') + ->with($location) + ->willReturn(false); + + $cachedLoader = new CachedResourceLoader($loader); + $cachedLoader->load($location); // cache warmup + $cachedLoader->cleanup(); + + $this->assertFalse( + $cachedLoader->exists($location), + 'Resource should be test from cache' + ); + } } diff --git a/test/Loader/SiteKitLoaderTest.php b/test/Loader/SiteKitLoaderTest.php index d6a8042..09dc9c2 100644 --- a/test/Loader/SiteKitLoaderTest.php +++ b/test/Loader/SiteKitLoaderTest.php @@ -81,6 +81,12 @@ public function testLoadValidResourceWithLang(): void ); } + public function testCleanup(): void + { + $this->expectNotToPerformAssertions(); + $this->loader->cleanup(); + } + public function testLoadMissingLocation(): void { $this->expectException(ResourceNotFoundException::class); diff --git a/test/Loader/SiteKitResourceHierarchyLoaderTest.php b/test/Loader/SiteKitResourceHierarchyLoaderTest.php index bcb75af..38dd554 100644 --- a/test/Loader/SiteKitResourceHierarchyLoaderTest.php +++ b/test/Loader/SiteKitResourceHierarchyLoaderTest.php @@ -84,6 +84,20 @@ public function testExists(): void $hierarchyLoader->exists(ResourceLocation::of('/a.php')); } + public function testCleanUp(): void + { + $resourceLoader = $this->createMock(ResourceLoader::class); + $hierarchyLoader = new SiteKitResourceHierarchyLoader( + $resourceLoader, + 'category' + ); + + $resourceLoader->expects($this->once()) + ->method('cleanup'); + + $hierarchyLoader->cleanup(); + } + public function testLoadPrimaryParentResourceWithoutParent(): void {