From fc76cf2fde9a97512e8dcda40ffe5f4c0c2abc9b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20Kne=C5=BEevi=C4=87?= Date: Fri, 21 Jun 2024 17:58:43 +0200 Subject: [PATCH 1/3] NGSTACK-901 implement decorator service for variation path generator, which will replace file extension with webp if image variation is configured for this format --- .../WebpFormatVariationPathGenerator.php | 44 +++++++++++++++++++ ...matVariationPathGeneratorDecoratorPass.php | 30 +++++++++++++ bundle/NetgenSiteBundle.php | 1 + 3 files changed, 75 insertions(+) create mode 100644 bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php create mode 100644 bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php diff --git a/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php b/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php new file mode 100644 index 00000000..73a5b605 --- /dev/null +++ b/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php @@ -0,0 +1,44 @@ +innerVariationPathGenerator = $innerVariationPathGenerator; + $this->filterConfiguration = $filterConfiguration; + } + + public function getVariationPath($originalPath, $filter): string + { + $variationPath = $this->innerVariationPathGenerator->getVariationPath($originalPath, $filter); + $filterConfig = $this->filterConfiguration->get($filter); + + if (!isset($filterConfig['format']) || $filterConfig['format'] !== 'webp') { + return $variationPath; + } + + $info = pathinfo($originalPath); + + if(empty($info['extension'])){ + return $variationPath . '.webp'; + } + + return preg_replace("/\.{$info['extension']}$/", '.webp', $variationPath); + } +} diff --git a/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php b/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php new file mode 100644 index 00000000..bee36f62 --- /dev/null +++ b/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php @@ -0,0 +1,30 @@ +has('ezpublish.image_alias.variation_path_generator')) { + + $container->register( + 'ezpublish.image_alias.webp_variation_path_generator_decorator', + WebpFormatVariationPathGenerator::class) + ->setDecoratedService('ezpublish.image_alias.variation_path_generator') + ->addArgument(new Reference('ezpublish.image_alias.webp_variation_path_generator_decorator.inner')) + ->addArgument(new Reference('liip_imagine.filter.configuration')); + } + } +} diff --git a/bundle/NetgenSiteBundle.php b/bundle/NetgenSiteBundle.php index c0e4415b..87b50185 100644 --- a/bundle/NetgenSiteBundle.php +++ b/bundle/NetgenSiteBundle.php @@ -20,5 +20,6 @@ public function build(ContainerBuilder $container): void $container->addCompilerPass(new Compiler\LocationFactoryPass()); $container->addCompilerPass(new Compiler\AsseticPass()); $container->addCompilerPass(new Compiler\IoStorageAllowListPass()); + $container->addCompilerPass(new Compiler\WebpFormatVariationPathGeneratorDecoratorPass()); } } From 99a67ec65370ef05cc0d411fb31d1baf00cb119f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20Kne=C5=BEevi=C4=87?= Date: Tue, 13 Aug 2024 16:10:47 +0200 Subject: [PATCH 2/3] NGSTACK-901 CS fixes --- .../WebpFormatVariationPathGenerator.php | 5 ++++- ...matVariationPathGeneratorDecoratorPass.php | 19 ++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php b/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php index 73a5b605..bb720760 100644 --- a/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php +++ b/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php @@ -7,6 +7,9 @@ use eZ\Bundle\EzPublishCoreBundle\Imagine\VariationPathGenerator; use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration; +use function pathinfo; +use function preg_replace; + /** * Decorates VariationPathGenerator with .webp extension if image variation is configured for this format. */ @@ -35,7 +38,7 @@ public function getVariationPath($originalPath, $filter): string $info = pathinfo($originalPath); - if(empty($info['extension'])){ + if (!is_string($info['extension']) || strlen($info['extension']) === 0) { return $variationPath . '.webp'; } diff --git a/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php b/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php index bee36f62..9e325251 100644 --- a/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php +++ b/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php @@ -7,7 +7,6 @@ use Netgen\Bundle\SiteBundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Definition; use Symfony\Component\DependencyInjection\Reference; class WebpFormatVariationPathGeneratorDecoratorPass implements CompilerPassInterface @@ -17,14 +16,16 @@ class WebpFormatVariationPathGeneratorDecoratorPass implements CompilerPassInter */ public function process(ContainerBuilder $container): void { - if ($container->has('ezpublish.image_alias.variation_path_generator')) { - - $container->register( - 'ezpublish.image_alias.webp_variation_path_generator_decorator', - WebpFormatVariationPathGenerator::class) - ->setDecoratedService('ezpublish.image_alias.variation_path_generator') - ->addArgument(new Reference('ezpublish.image_alias.webp_variation_path_generator_decorator.inner')) - ->addArgument(new Reference('liip_imagine.filter.configuration')); + if (!$container->has('ezpublish.image_alias.variation_path_generator')) { + return; } + + $container->register( + 'ezpublish.image_alias.webp_variation_path_generator_decorator', + WebpFormatVariationPathGenerator::class, + ) + ->setDecoratedService('ezpublish.image_alias.variation_path_generator') + ->addArgument(new Reference('ezpublish.image_alias.webp_variation_path_generator_decorator.inner')) + ->addArgument(new Reference('liip_imagine.filter.configuration')); } } From ae5072acc769f75a0b2088d62cc04cc7a77ddc51 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20Kne=C5=BEevi=C4=87?= Date: Tue, 10 Sep 2024 08:45:28 +0200 Subject: [PATCH 3/3] NGSTACK-901 fix comment --- .../Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php b/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php index 9e325251..793312e5 100644 --- a/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php +++ b/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php @@ -12,7 +12,7 @@ class WebpFormatVariationPathGeneratorDecoratorPass implements CompilerPassInterface { /** - * Overrides the IO resolver to disable generating absolute URIs to images. + * Decorates default image alias variation path generator to comply with legacy variation URL pattern */ public function process(ContainerBuilder $container): void {