Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ for a given releases. Unreleased, upcoming changes will be updated here periodic

# 2.x

## 2.19.0 (unreleased)

- Add a `format_extension` resolver configuration, so the `FormatExtensionResolver` can decorate a configured resolver without declaring a service ([Amoifr](https://github.com/liip/LiipImagineBundle/pull/1675))

## [2.18.2](https://github.com/liip/LiipImagineBundle/tree/2.18.2)

- Make compatible with Twig 4: Add missing return type declaration ([winniezzz](https://github.com/liip/LiipImagineBundle/pull/1673))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\DependencyInjection\Factory\Resolver;

use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Decorates another configured resolver with the FormatExtensionResolver, so that cached images
* get the file extension of the format of their filter set.
*/
class FormatExtensionResolverFactory extends AbstractResolverFactory
{
public function create(ContainerBuilder $container, $resolverName, array $config)
{
$resolverDefinition = $this->getChildResolverDefinition();
$resolverDefinition->replaceArgument(0, new Reference('liip_imagine.cache.resolver.'.$config['resolver']));
$resolverDefinition->addTag('liip_imagine.cache.resolver', [
'resolver' => $resolverName,
]);

$resolverId = 'liip_imagine.cache.resolver.'.$resolverName;
$container->setDefinition($resolverId, $resolverDefinition);

return $resolverId;
}

public function getName()
{
return 'format_extension';
}

public function addConfiguration(ArrayNodeDefinition $builder): void
{
$builder
->children()
->scalarNode('resolver')
->info('Name of the configured resolver to decorate, for example "default".')
->isRequired()
->cannotBeEmpty()
->end()
->end();
}
}
2 changes: 2 additions & 0 deletions LiipImagineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\StreamLoaderFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\AwsS3ResolverFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FlysystemResolverFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FormatExtensionResolverFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory;
use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension;
use Liip\ImagineBundle\Imagine\Filter\Loader\LoaderInterface as LoaderLoaderInterface;
Expand Down Expand Up @@ -66,6 +67,7 @@ public function build(ContainerBuilder $container): void
$extension->addResolverFactory(new WebPathResolverFactory());
$extension->addResolverFactory(new AwsS3ResolverFactory());
$extension->addResolverFactory(new FlysystemResolverFactory());
$extension->addResolverFactory(new FormatExtensionResolverFactory());

$extension->addLoaderFactory(new StreamLoaderFactory());
$extension->addLoaderFactory(new FileSystemLoaderFactory());
Expand Down
9 changes: 9 additions & 0 deletions Resources/config/imagine.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
use Liip\ImagineBundle\Imagine\Cache\Resolver\CacheResolver;
use Liip\ImagineBundle\Imagine\Cache\Resolver\FlysystemResolver;
use Liip\ImagineBundle\Imagine\Cache\Resolver\FlysystemV2Resolver;
use Liip\ImagineBundle\Imagine\Cache\Resolver\FormatExtensionResolver;
use Liip\ImagineBundle\Imagine\Cache\Resolver\NoCacheWebPathResolver;
use Liip\ImagineBundle\Imagine\Cache\Resolver\ProxyResolver;
use Liip\ImagineBundle\Imagine\Cache\Resolver\PsrCacheResolver;
Expand Down Expand Up @@ -500,6 +501,14 @@
'',
]);

$services->set('liip_imagine.cache.resolver.prototype.format_extension', FormatExtensionResolver::class)
->public()
->abstract()
->args([
'', // will be injected by FormatExtensionResolverFactory
service('liip_imagine.filter.configuration'),
]);

$services->set('liip_imagine.cache.resolver.prototype.proxy', ProxyResolver::class)
->public()
->abstract()
Expand Down
18 changes: 18 additions & 0 deletions Resources/doc/basic-usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,24 @@ can configure the generation of all images in the WebP format.
default_filter_set_settings:
format: webp

The images are then encoded in WebP, but are still cached under the extension of the
source image (``cats.jpeg``), so a web server serving them directly may send them with
the wrong content type. To also give them the ``.webp`` extension, use the
:ref:`format extension resolver <cache-resolver-format-extension>`:

.. code-block:: yaml

# app/config/config.yml

liip_imagine:
resolvers:
format_extension:
format_extension:
resolver: default
cache: format_extension
default_filter_set_settings:
format: webp

Use WebP if supported
~~~~~~~~~~~~~~~~~~~~~

Expand Down
63 changes: 47 additions & 16 deletions Resources/doc/cache-resolver/format_extension.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,28 @@ FormatExtensionResolver
The ``FormatExtensionResolver`` cannot be used by itself. Instead, it is a "decorator" for
another resolver. It adds the ability to set the correct file extension when a filter converted the image format.

For example, with a filter set using ``format: webp``, the source image ``image.png`` is
stored and served as ``image.webp`` instead of a WebP image named ``image.png``.

Configuration
-------------

To use this cache resolver, you must first define the cache resolver it will decorate.
In this example, we will use the :ref:`Web Path Resolver <cache-resolver-web-path>`.

Next, we need to define a service for this cache resolver and inject the web path cache resolver service to decorate.
Define a resolver with the ``format_extension`` key, and give it the name of the configured
resolver to decorate. In this example, it decorates the ``default`` resolver, which is the
:ref:`Web Path Resolver <cache-resolver-web-path>` unless you configured another one:

.. code-block:: yaml

# app/config/services.yml

services:
acme.imagine.cache.format_extension:
class: Liip\ImagineBundle\Imagine\Cache\Resolver\FormatExtensionResolver
arguments:
- "@acme.imagine.cache.resolver.web_path"
- "@liip_imagine.filter.configuration"
tags:
- { name: "liip_imagine.cache.resolver", resolver: "format_extension" }
# app/config/config.yml

liip_imagine:
resolvers:
format_extension:
format_extension:
resolver: default

With this configuration, the format extension resolver will rewrite the extension to match the filter format.
For example, you have the source image ``image.png`` and you apply filter with format ``jpg`` you will get jpeg-image ``image.jpg``.
The decorated resolver must be defined in the ``resolvers`` section. The ``default``
resolver is always available, as the bundle defines it when it is not configured.

Usage
-----
Expand Down Expand Up @@ -61,5 +59,38 @@ filter set using the following configuration.
cache: ~
my_thumb:
cache: format_extension
format: webp
filters:
# the filter list

.. caution::

Changing the cache resolver changes the paths of the cached images. Images already
stored under their original extension are not found anymore and are generated again.

.. caution::

Do not combine this resolver with the ``webp.generate`` option on filter sets that
define a ``format`` other than ``webp``: with ``format: jpeg``, the WebP variant
``image.png.webp`` would be stored as ``image.png.jpeg``, a WebP image with the
extension of the format.

Usage with a Custom Service
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Before the ``format_extension`` resolver configuration was available, the resolver had to be
defined as a service. This still works, for example to decorate a resolver that is defined as
a service instead of in the ``resolvers`` section:

.. code-block:: yaml

# app/config/services.yml

services:
acme.imagine.cache.format_extension:
class: Liip\ImagineBundle\Imagine\Cache\Resolver\FormatExtensionResolver
arguments:
- "@acme.imagine.cache.resolver.custom"
- "@liip_imagine.filter.configuration"
tags:
- { name: "liip_imagine.cache.resolver", resolver: "format_extension" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\Tests\DependencyInjection\Factory\Resolver;

use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FormatExtensionResolverFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Config\Definition\Processor;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* @covers \Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FormatExtensionResolverFactory<extended>
*/
class FormatExtensionResolverFactoryTest extends TestCase
{
public function testImplementsResolverFactoryInterface(): void
{
$rc = new \ReflectionClass(FormatExtensionResolverFactory::class);

$this->assertTrue($rc->implementsInterface(ResolverFactoryInterface::class));
}

public function testReturnExpectedName(): void
{
$resolver = new FormatExtensionResolverFactory();

$this->assertSame('format_extension', $resolver->getName());
}

public function testCreateResolverDefinitionOnCreate(): void
{
$container = new ContainerBuilder();

$resolver = new FormatExtensionResolverFactory();

$resolverId = $resolver->create($container, 'the_resolver_name', [
'resolver' => 'the_decorated_resolver',
]);

$this->assertSame('liip_imagine.cache.resolver.the_resolver_name', $resolverId);
$this->assertTrue($container->hasDefinition($resolverId));

$resolverDefinition = $container->getDefinition($resolverId);
$this->assertInstanceOf(ChildDefinition::class, $resolverDefinition);
$this->assertSame('liip_imagine.cache.resolver.prototype.format_extension', $resolverDefinition->getParent());

$decorated = $resolverDefinition->getArgument(0);
$this->assertInstanceOf(Reference::class, $decorated);
$this->assertSame('liip_imagine.cache.resolver.the_decorated_resolver', (string) $decorated);

$this->assertSame(
[['resolver' => 'the_resolver_name']],
$resolverDefinition->getTag('liip_imagine.cache.resolver')
);
}

public function testProcessCorrectlyOptionsOnAddConfiguration(): void
{
$treeBuilder = new TreeBuilder('format_extension');
$resolver = new FormatExtensionResolverFactory();
$resolver->addConfiguration($treeBuilder->getRootNode());

$config = (new Processor())->process($treeBuilder->buildTree(), [
'format_extension' => ['resolver' => 'default'],
]);

$this->assertSame(['resolver' => 'default'], $config);
}

public function testThrowIfResolverNotSetOnAddConfiguration(): void
{
$treeBuilder = new TreeBuilder('format_extension');
$resolver = new FormatExtensionResolverFactory();
$resolver->addConfiguration($treeBuilder->getRootNode());

$this->expectException(InvalidConfigurationException::class);
$this->expectExceptionMessageMatches('/"resolver"/');

(new Processor())->process($treeBuilder->buildTree(), [
'format_extension' => [],
]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Liip\ImagineBundle\Tests\Functional\Imagine\Cache\Resolver;

use Liip\ImagineBundle\Imagine\Cache\Resolver\FormatExtensionResolver;
use Liip\ImagineBundle\Tests\Functional\AbstractSetupWebTestCase;

/**
* @covers \Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FormatExtensionResolverFactory
* @covers \Liip\ImagineBundle\Imagine\Cache\Resolver\FormatExtensionResolver
*/
class FormatExtensionResolverTest extends AbstractSetupWebTestCase
{
public function testCouldBeGetFromContainer(): void
{
$this->assertInstanceOf(
FormatExtensionResolver::class,
self::$kernel->getContainer()->get('liip_imagine.cache.resolver.format_extension')
);
}

public function testStoresTheFilteredImageWithTheExtensionOfTheFormat(): void
{
$container = static::getContainer();

// a filter set of its own, so that the commands tests iterating all filter sets are not affected
$filterConfiguration = $container->get('liip_imagine.filter.configuration');
$filterConfiguration->set('thumbnail_png', array_merge($filterConfiguration->get('thumbnail_default'), [
'cache' => 'format_extension',
'format' => 'png',
]));

$url = $container->get('liip_imagine.service.filter')->getUrlOfFilteredImage('images/cats.jpeg', 'thumbnail_png');

$this->assertSame('http://localhost/media/cache/thumbnail_png/images/cats.png', $url);
$this->assertFileExists($this->cacheRoot.'/thumbnail_png/images/cats.png');
$this->assertFileDoesNotExist($this->cacheRoot.'/thumbnail_png/images/cats.jpeg');
$this->assertSame('image/png', mime_content_type($this->cacheRoot.'/thumbnail_png/images/cats.png'));
}
}
4 changes: 4 additions & 0 deletions Tests/Functional/app/config/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ liip_imagine:
web_root: "%kernel.project_dir%/public"
cache_prefix: media/cache

format_extension:
format_extension:
resolver: default

filter_sets:

thumbnail_web_path:
Expand Down
2 changes: 2 additions & 0 deletions Tests/LiipImagineBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\StreamLoaderFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\AwsS3ResolverFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FlysystemResolverFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\FormatExtensionResolverFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\ResolverFactoryInterface;
use Liip\ImagineBundle\DependencyInjection\Factory\Resolver\WebPathResolverFactory;
use Liip\ImagineBundle\DependencyInjection\LiipImagineExtension;
Expand Down Expand Up @@ -119,6 +120,7 @@ public function testAddResolvers(): void
WebPathResolverFactory::class,
AwsS3ResolverFactory::class,
FlysystemResolverFactory::class,
FormatExtensionResolverFactory::class,
], $resolvers);
}

Expand Down
Loading