Skip to content

Commit

Permalink
Merged branch '4.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
alongosz committed Feb 17, 2023
2 parents 222bcb1 + 9738dfd commit 17a7f07
Show file tree
Hide file tree
Showing 3 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Bundle\Core\Imagine\VariationPathGenerator;

use Ibexa\Contracts\Core\Variation\VariationPathGenerator;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;

/**
* 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;
}

return $variationPath . '.webp';
}
}
6 changes: 6 additions & 0 deletions src/bundle/Core/Resources/config/image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,12 @@ services:
tags:
- { name: 'ibexa.media.images.variation.handler', identifier: 'alias' }

Ibexa\Bundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator:
decorates: ibexa.image_alias.variation_path_generator
arguments:
$innerVariationPathGenerator: '@.inner'
$filterConfiguration: '@liip_imagine.filter.configuration'

# SPI Aliases
Ibexa\Contracts\Core\Variation\VariationHandler: '@Ibexa\Bundle\Core\Variation\VariationHandlerResolver'

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Bundle\Core\Imagine\VariationPathGenerator;

use Ibexa\Bundle\Core\Imagine\Filter\FilterConfiguration;
use Ibexa\Bundle\Core\Imagine\VariationPathGenerator;
use Ibexa\Bundle\Core\Imagine\VariationPathGenerator\WebpFormatVariationPathGenerator;
use PHPUnit\Framework\TestCase;

final class WebpFormatVariationPathGeneratorTest extends TestCase
{
/** @var \Ibexa\Bundle\Core\Imagine\VariationPathGenerator|\PHPUnit\Framework\MockObject\MockObject */
private VariationPathGenerator $innerVariationPathGenerator;

/** @var \Ibexa\Bundle\Core\Imagine\Filter\FilterConfiguration|\PHPUnit\Framework\MockObject\MockObject */
private FilterConfiguration $filterConfiguration;

protected function setUp(): void
{
$this->innerVariationPathGenerator = $this->createMock(VariationPathGenerator::class);
$this->filterConfiguration = $this->createMock(FilterConfiguration::class);
}

public function testGetVariationPath(): void
{
$this->innerVariationPathGenerator
->method('getVariationPath')
->willReturn('tmp/variation/test.jpeg');

$this->filterConfiguration
->method('get')
->with('large')
->willReturn([
'format' => 'webp',
]);

$generator = new WebpFormatVariationPathGenerator(
$this->innerVariationPathGenerator,
$this->filterConfiguration
);

self::assertEquals(
'tmp/variation/test.jpeg.webp',
$generator->getVariationPath('tmp/original/test.jpeg', 'large')
);
}

public function testGetVariationNonWebpVariation(): void
{
$this->innerVariationPathGenerator
->method('getVariationPath')
->willReturn('tmp/variation/test.jpeg');

$this->filterConfiguration
->method('get')
->with('large')
->willReturn([
'format' => 'jpeg',
]);

$generator = new WebpFormatVariationPathGenerator(
$this->innerVariationPathGenerator,
$this->filterConfiguration
);

self::assertEquals(
'tmp/variation/test.jpeg',
$generator->getVariationPath('tmp/original/test.jpeg', 'large')
);
}
}

0 comments on commit 17a7f07

Please sign in to comment.