-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
NGSTACK-901 replace original webp image variation path generator with…
… our implementation which generates legacy-compliant paths for ngadminui
- Loading branch information
Showing
3 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\Bundle\SiteBundle\Core\Imagine\VariationPathGenerator; | ||
|
||
use Ibexa\Contracts\Core\Variation\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. | ||
*/ | ||
final class WebpFormatVariationPathGenerator implements VariationPathGenerator | ||
{ | ||
private VariationPathGenerator $innerVariationPathGenerator; | ||
|
||
private FilterConfiguration $filterConfiguration; | ||
|
||
public function __construct( | ||
VariationPathGenerator $innerVariationPathGenerator, | ||
FilterConfiguration $filterConfiguration | ||
) { | ||
$this->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) { | ||
Check failure on line 41 in bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php
|
||
return $variationPath . '.webp'; | ||
} | ||
|
||
return preg_replace("/\.{$info['extension']}$/", '.webp', $variationPath); | ||
Check failure on line 45 in bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php
|
||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Netgen\Bundle\SiteBundle\DependencyInjection\Compiler; | ||
|
||
use Netgen\Bundle\SiteBundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator; | ||
use Ibexa\Bundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator as BaseWebpFormatVariationPathGenerator; | ||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
use Symfony\Component\DependencyInjection\Reference; | ||
|
||
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) | ||
*/ | ||
public function process(ContainerBuilder $container): void | ||
{ | ||
$activatedBundles = array_keys($container->getParameter('kernel.bundles')); | ||
|
||
if (!in_array('NetgenAdminUIBundle', $activatedBundles, true)) { | ||
return; | ||
} | ||
|
||
if (!$container->has(BaseWebpFormatVariationPathGenerator::class)) { | ||
return; | ||
} | ||
|
||
$container | ||
->findDefinition(BaseWebpFormatVariationPathGenerator::class) | ||
->setClass(WebpFormatVariationPathGenerator::class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters