Skip to content

Commit

Permalink
feat: supports the deletion of loader caches
Browse files Browse the repository at this point in the history
  • Loading branch information
sitepark-veltrup committed Jun 20, 2024
1 parent 2261f1f commit 9fb1d16
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Loader/CachedResourceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,10 @@ public function exists(ResourceLocation $location): bool
}
return $this->resourceLoader->exists($location);
}

public function cleanup(): void
{
$this->resourceLoader->cleanup();
$this->cache = [];
}
}
5 changes: 5 additions & 0 deletions src/Loader/SiteKitLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 . '/' .
Expand Down
5 changes: 5 additions & 0 deletions src/Loader/SiteKitResourceHierarchyLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/ResourceLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
25 changes: 25 additions & 0 deletions test/Loader/CachedResourceLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
}
}
6 changes: 6 additions & 0 deletions test/Loader/SiteKitLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions test/Loader/SiteKitResourceHierarchyLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{

Expand Down

0 comments on commit 9fb1d16

Please sign in to comment.