From 3f91586b62894bed619fd44ce003b6667137e5b8 Mon Sep 17 00:00:00 2001 From: Philipp Lang Date: Mon, 6 Sep 2021 14:11:10 +0200 Subject: [PATCH] Add cache --- Plugin.php | 3 ++- tests/ImageTagTest.php | 30 ++++++++++++++++++------------ 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/Plugin.php b/Plugin.php index 07031da..f3f6eb2 100644 --- a/Plugin.php +++ b/Plugin.php @@ -100,7 +100,8 @@ class Plugin extends PluginBase return [ 'filters' => [ 'resize' => function($media, $size = 'original') { - return Cache::rememberForever("resize.{$size}.{$media}", fn () => app(TagGenerator::class)->generate($media, $size)); + $normalPath = app(FileObserver::class)->normalizePath($media); + return Cache::rememberForever("resize.{$size}.{$normalPath}", fn () => app(TagGenerator::class)->generate($media, $size)); }, ] ]; diff --git a/tests/ImageTagTest.php b/tests/ImageTagTest.php index c3b1efb..b5f45f8 100644 --- a/tests/ImageTagTest.php +++ b/tests/ImageTagTest.php @@ -105,19 +105,25 @@ class ImageTagTest extends TestCase Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]); Setting::set('breakpoints', [640]); $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); + + $output = app(Twig::class)->parse('{{"pages/test.svg" | resize}}'); - $this->assertFalse(str_contains( - app(Twig::class)->parse('{{"pages/test.svg" | resize}}'), - 'srcset', - )); - $this->assertFalse(str_contains( - app(Twig::class)->parse('{{"pages/test.svg" | resize}}'), - 'sizes', - )); - $this->assertTrue(str_contains( - app(Twig::class)->parse('{{"pages/test.svg" | resize}}'), - 'src="'.$this->media->findFiles('/pages/test.svg')[0]->publicUrl.'"', - )); + $this->assertFalse(str_contains($output, 'srcset')); + $this->assertFalse(str_contains($output, 'sizes')); + $this->assertTrue(str_contains($output, 'src="'.$this->media->findFiles('/pages/test.svg')[0]->publicUrl.'"')); + $this->assertEquals($output, Cache::get('resize.original.pages/test.svg')); + } + + public function testNormalizeFilePathForCache() + { + Setting::set('folders', ['pages']); + Setting::set('sizes', [['name' => 'size', 'aspect_ratio' => '1200x280']]); + Setting::set('breakpoints', [640]); + $this->media->put('/pages/test.svg', file_get_contents(__DIR__.'/stub/close.svg')); + + $output = app(Twig::class)->parse('{{"/pages/test.svg" | resize}}'); + + $this->assertEquals($output, Cache::get('resize.original.pages/test.svg')); } }