Skip to content

Commit

Permalink
NGSTACK-901 replace original webp image variation path generator with…
Browse files Browse the repository at this point in the history
… our implementation which generates legacy-compliant paths for ngadminui
  • Loading branch information
hknezevic committed Sep 10, 2024
1 parent 71ee8b3 commit 61b1928
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 0 deletions.
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

View workflow job for this annotation

GitHub Actions / phpstan

Call to function is_string() with string will always evaluate to true.

Check failure on line 41 in bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php

View workflow job for this annotation

GitHub Actions / phpstan

Offset 'extension' does not exist on array{dirname?: string, basename: string, extension?: string, filename: string}.
return $variationPath . '.webp';
}

return preg_replace("/\.{$info['extension']}$/", '.webp', $variationPath);

Check failure on line 45 in bundle/Core/Imagine/VariationPathGenerator/WebpFormatVariationPathGenerator.php

View workflow job for this annotation

GitHub Actions / phpstan

Method Netgen\Bundle\SiteBundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator::getVariationPath() should return string but returns string|null.
}
}
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'));

Check failure on line 21 in bundle/DependencyInjection/Compiler/WebpFormatVariationPathGeneratorDecoratorPass.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter #1 $array of function array_keys expects array, array|bool|float|int|string|null given.

if (!in_array('NetgenAdminUIBundle', $activatedBundles, true)) {
return;
}

if (!$container->has(BaseWebpFormatVariationPathGenerator::class)) {
return;
}

$container
->findDefinition(BaseWebpFormatVariationPathGenerator::class)
->setClass(WebpFormatVariationPathGenerator::class);
}
}
1 change: 1 addition & 0 deletions bundle/NetgenSiteBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 61b1928

Please sign in to comment.