From 61b1928ff75dd4d8378c0559242011ffc57474e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20Kne=C5=BEevi=C4=87?= Date: Tue, 10 Sep 2024 10:21:29 +0200 Subject: [PATCH 1/2] NGSTACK-901 replace original webp image variation path generator with our implementation which generates legacy-compliant paths for ngadminui --- .../WebpFormatVariationPathGenerator.php | 47 +++++++++++++++++++ ...matVariationPathGeneratorDecoratorPass.php | 35 ++++++++++++++ bundle/NetgenSiteBundle.php | 1 + 3 files changed, 83 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..7425c9de --- /dev/null +++ b/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php @@ -0,0 +1,47 @@ +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 (!is_string($info['extension']) || strlen($info['extension']) === 0) { + 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..cad6636a --- /dev/null +++ b/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php @@ -0,0 +1,35 @@ +getParameter('kernel.bundles')); + + if (!in_array('NetgenAdminUIBundle', $activatedBundles, true)) { + return; + } + + if (!$container->has(BaseWebpFormatVariationPathGenerator::class)) { + return; + } + + $container + ->findDefinition(BaseWebpFormatVariationPathGenerator::class) + ->setClass(WebpFormatVariationPathGenerator::class); + } +} diff --git a/bundle/NetgenSiteBundle.php b/bundle/NetgenSiteBundle.php index 14b1a38c..c8b1a86d 100644 --- a/bundle/NetgenSiteBundle.php +++ b/bundle/NetgenSiteBundle.php @@ -20,5 +20,6 @@ public function build(ContainerBuilder $container): void $container->addCompilerPass(new Compiler\IoStorageAllowListPass()); $container->addCompilerPass(new Compiler\PHPStormPass(), PassConfig::TYPE_OPTIMIZE); $container->addCompilerPass(new Compiler\DirectDownloadPass()); + $container->addCompilerPass(new Compiler\WebpFormatVariationPathGeneratorDecoratorPass()); } } From 001af7d3d5e3e49bf9dc01c55e403b61d92720e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hrvoje=20Kne=C5=BEevi=C4=87?= Date: Tue, 10 Sep 2024 15:09:48 +0200 Subject: [PATCH 2/2] NGSTACK-901 CS fixes --- .../WebpFormatVariationPathGenerator.php | 7 ++++--- ...ebpFormatVariationPathGeneratorDecoratorPass.php | 13 ++++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php b/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php index 7425c9de..c56e8451 100644 --- a/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php +++ b/bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php @@ -7,6 +7,7 @@ use Ibexa\Contracts\Core\Variation\VariationPathGenerator; use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration; +use function array_key_exists; use function pathinfo; use function preg_replace; @@ -21,7 +22,7 @@ final class WebpFormatVariationPathGenerator implements VariationPathGenerator public function __construct( VariationPathGenerator $innerVariationPathGenerator, - FilterConfiguration $filterConfiguration + FilterConfiguration $filterConfiguration, ) { $this->innerVariationPathGenerator = $innerVariationPathGenerator; $this->filterConfiguration = $filterConfiguration; @@ -38,10 +39,10 @@ public function getVariationPath($originalPath, $filter): string $info = pathinfo($originalPath); - if (!is_string($info['extension']) || strlen($info['extension']) === 0) { + if (!array_key_exists('extension', $info) || $info['extension'] === '') { return $variationPath . '.webp'; } - return preg_replace("/\.{$info['extension']}$/", '.webp', $variationPath); + return preg_replace("/\\.{$info['extension']}$/", '.webp', $variationPath) ?? $variationPath; } } diff --git a/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php b/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php index cad6636a..2588c14f 100644 --- a/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php +++ b/bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php @@ -4,20 +4,27 @@ namespace Netgen\Bundle\SiteBundle\DependencyInjection\Compiler; -use Netgen\Bundle\SiteBundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator; use Ibexa\Bundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator as BaseWebpFormatVariationPathGenerator; +use Netgen\Bundle\SiteBundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator; use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Reference; + +use function array_keys; +use function in_array; +use function is_array; class WebpFormatVariationPathGeneratorDecoratorPass implements CompilerPassInterface { /** * Overrides default Webp image alias variation path generator decorator to comply with legacy variation URL pattern - * We do this only if we have Netgen AdminUI installed (legacy-based administration) + * We do this only if we have Netgen AdminUI installed (legacy-based administration). */ public function process(ContainerBuilder $container): void { + if (!$container->hasParameter('kernel.bundles') || !is_array($container->getParameter('kernel.bundles'))) { + return; + } + $activatedBundles = array_keys($container->getParameter('kernel.bundles')); if (!in_array('NetgenAdminUIBundle', $activatedBundles, true)) {