Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
c30473d
Added AssetMapperLoaderFactory and AssetMapperLocator
tito10047 Dec 23, 2025
09003a9
removed duplicated loader and use filesystem instead
tito10047 Dec 25, 2025
b7bebb8
add test
tito10047 Dec 25, 2025
34b38d6
php-cs-fixer run
tito10047 Dec 25, 2025
a79384c
updated documentation
tito10047 Dec 25, 2025
2e44bff
updated documentation
tito10047 Dec 25, 2025
fbf6adf
removed unused code
tito10047 Dec 25, 2025
ef9c955
removed unbused variable and readonly keyword
tito10047 Dec 25, 2025
1dd541c
enhanced `AssetMapperLoaderFactory` and `AssetMapperLocator` to suppo…
tito10047 Jan 5, 2026
1864225
add Symfony Asset Mapper to PHPStan workflow
tito10047 Jan 5, 2026
6a524a1
add tests for `AssetMapperLocator` and `AssetMapperLoaderFactory`, fi…
tito10047 Jan 5, 2026
7801018
Update Resources/doc/data-loader/asset_mapper.rst
tito10047 Jan 5, 2026
eeea561
Update Resources/doc/data-loader/asset_mapper.rst
tito10047 Jan 5, 2026
68b8d96
cs fixer
tito10047 Jan 5, 2026
5954695
Merge remote-tracking branch 'origin/asset-mapper' into asset-mapper
tito10047 Jan 5, 2026
a9b7667
Merge branch '2.x' into asset-mapper
tito10047 Jan 5, 2026
0a3730c
add asset mapper dev requirements
tito10047 Jan 5, 2026
61c6271
add asset mapper dev requirements
tito10047 Jan 5, 2026
61f03c2
remove `symfony/asset-mapper` from dependencies, add conditional dev…
tito10047 Jan 5, 2026
20f3061
Update .github/workflows/phpstan.yml
tito10047 Jan 5, 2026
299bbea
Update .github/workflows/phpunit.yml
tito10047 Jan 5, 2026
ae06e8f
Remove cache from asset AssetMapperLocator.php and add better documen…
tito10047 Jan 5, 2026
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
34 changes: 34 additions & 0 deletions Binary/Locator/AssetMapperLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?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\Binary\Locator;

use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException;
use Symfony\Component\AssetMapper\AssetMapperInterface;

class AssetMapperLocator implements LocatorInterface
Comment thread
tito10047 marked this conversation as resolved.
{
public function __construct(
private AssetMapperInterface $assetMapper,

Check failure on line 20 in Binary/Locator/AssetMapperLocator.php

View workflow job for this annotation

GitHub Actions / phpstan

Property Liip\ImagineBundle\Binary\Locator\AssetMapperLocator::$assetMapper has unknown class Symfony\Component\AssetMapper\AssetMapperInterface as its type.

Check failure on line 20 in Binary/Locator/AssetMapperLocator.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $assetMapper of method Liip\ImagineBundle\Binary\Locator\AssetMapperLocator::__construct() has invalid type Symfony\Component\AssetMapper\AssetMapperInterface.
) {
}

public function locate(string $path): string
{
$path = '/'.mb_ltrim($path, '/');
foreach ($this->assetMapper->allAssets() as $assetCandidate) {
Comment thread
tito10047 marked this conversation as resolved.
if ($path === $assetCandidate->publicPath) {
return $assetCandidate->sourcePath;
}
}
throw new NotLoadableException(\sprintf('Asset with public path "%s" not found.', $path));
}
}
49 changes: 49 additions & 0 deletions DependencyInjection/Factory/Loader/AssetMapperLoaderFactory.php
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\DependencyInjection\Factory\Loader;

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

class AssetMapperLoaderFactory extends AbstractLoaderFactory
{
public function create(ContainerBuilder $container, $loaderName, array $config)
{
$locatorDefinition = new ChildDefinition('liip_imagine.binary.locator.asset_mapper');
$locatorDefinition->replaceArgument(0, new Reference('asset_mapper'));

$definition = $this->getChildLoaderDefinition('filesystem');

if ($container->hasDefinition('liip_imagine.mime_types')) {
$mimeTypes = $container->getDefinition('liip_imagine.mime_types');
$definition->replaceArgument(0, $mimeTypes);
$definition->replaceArgument(1, $mimeTypes);
}
$definition->replaceArgument(2, $locatorDefinition);

return $this->setTaggedLoaderDefinition($loaderName, $definition, $container);
}

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

public function addConfiguration(ArrayNodeDefinition $builder)
{
$builder
->children()
->end();
}
}
2 changes: 2 additions & 0 deletions LiipImagineBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Liip\ImagineBundle\DependencyInjection\Compiler\NonFunctionalFilterExceptionPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\PostProcessorsCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\ResolversCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\AssetMapperLoaderFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\ChainLoaderFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FileSystemLoaderFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FlysystemLoaderFactory;
Expand Down Expand Up @@ -69,6 +70,7 @@ public function build(ContainerBuilder $container): void
$extension->addLoaderFactory(new StreamLoaderFactory());
$extension->addLoaderFactory(new FileSystemLoaderFactory());
$extension->addLoaderFactory(new FlysystemLoaderFactory());
$extension->addLoaderFactory(new AssetMapperLoaderFactory());
$extension->addLoaderFactory(new ChainLoaderFactory());

$container->registerForAutoconfiguration(LoaderLoaderInterface::class)->addTag('liip_imagine.filter.loader');
Expand Down
8 changes: 8 additions & 0 deletions Resources/config/imagine.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Liip\ImagineBundle\Binary\Loader\FlysystemLoader;
use Liip\ImagineBundle\Binary\Loader\FlysystemV2Loader;
use Liip\ImagineBundle\Binary\Loader\StreamLoader;
use Liip\ImagineBundle\Binary\Locator\AssetMapperLocator;
use Liip\ImagineBundle\Binary\Locator\FileSystemInsecureLocator;
use Liip\ImagineBundle\Binary\Locator\FileSystemLocator;
use Liip\ImagineBundle\Binary\SimpleMimeTypeGuesser;
Expand Down Expand Up @@ -429,6 +430,13 @@
'', // will be injected by FilesystemLoaderFactory
])
->tag('liip_imagine.binary.locator', ['shared' => false]);
$services->set('liip_imagine.binary.locator.asset_mapper', AssetMapperLocator::class)

@dmaicher dmaicher Jan 5, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should mark this ->abstract() (it would also fix #1648)

->share(true)
->public()
->args([
'',
Comment thread
tito10047 marked this conversation as resolved.
Outdated
])
->tag('liip_imagine.binary.locator', ['shared' => false]);

$services->set('liip_imagine.binary.locator.filesystem_insecure', FileSystemInsecureLocator::class)
->share(false)
Expand Down
6 changes: 4 additions & 2 deletions Resources/doc/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ There are several configuration options available:
* ``cache`` - default cache resolver. Default value: ``web_path`` (which means
the standard web_path resolver is used)
* ``data_loader`` - name of a custom data loader. Default value: ``filesystem``
(which means the standard filesystem loader is used).
(which means the standard filesystem loader is used). Built-in loaders include:
``filesystem``, ``chain``, ``flysystem``, ``stream``, and ``asset_mapper``.
* ``twig.mode`` - Twig filter integration. ``none`` disables the twig filters, ``lazy`` enables
Twig using the Twig runtime for lazy loading. The default value is ``legacy`` and enables the
old Twig integration that is loaded on each request. Version 3 will drop ``legacy`` and default
Expand All @@ -99,7 +100,8 @@ There are several configuration options available:
* ``cache`` - default cache resolver. Default value: ``web_path`` (which means
the standard web_path resolver is used)
* ``data_loader`` - name of a custom data loader. Default value: ``filesystem``
(which means the standard filesystem loader is used).
(which means the standard filesystem loader is used). Built-in loaders include:
``filesystem``, ``chain``, ``flysystem``, ``stream``, and ``asset_mapper``.
* ``post_processors`` - sets post-processors to be applied on filtered image
(see Post-Processors section in the :doc:`filters chapter <filters>` for details).
* ``driver`` - one of the drivers: ``gd``, ``imagick``, ``gmagick``, ``vips``.
Expand Down
29 changes: 29 additions & 0 deletions Resources/doc/data-loader/asset_mapper.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

.. _data-loaders-asset-mapper:

Asset Mapper Loader
Comment thread
tito10047 marked this conversation as resolved.
Outdated
===================
Comment thread
tito10047 marked this conversation as resolved.
Outdated

The ``AssetMapper`` data loader allows for loading images using Symfony's AssetMapper component.

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

To enable the Asset Mapper loader, you need to configure it in your loaders section.
A common use case is to use it in combination with the default filesystem loader using a chain loader,
especially in development environment.

.. code-block:: yaml

# config/packages/liip_imagine.yaml
when@dev:
liip_imagine:
loaders:
asset_mapper:
asset_mapper: ~
chain:
chain:
loaders: [ asset_mapper, default ]
data_loader: chain

This configuration creates an ``asset_mapper`` loader and a ``chain`` loader that first tries to find the asset via AssetMapper, and then falls back to the ``default`` loader. Finally, it sets the global ``data_loader`` to use this new ``chain`` loader.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\Loader;

use Liip\ImagineBundle\DependencyInjection\Factory\Loader\AssetMapperLoaderFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\LoaderFactoryInterface;
use Liip\ImagineBundle\Tests\DependencyInjection\Factory\FactoryTestCase;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* @covers \Liip\ImagineBundle\DependencyInjection\Factory\Loader\AssetMapperLoaderFactory<extended>
*/
class AssetMapperLoaderFactoryTest extends FactoryTestCase
{
public function testImplementsLoaderFactoryInterface(): void
{
$this->assertInstanceOf(LoaderFactoryInterface::class, new AssetMapperLoaderFactory());
}

public function testReturnsExpectedName(): void
{
$this->assertSame('asset_mapper', (new AssetMapperLoaderFactory())->getName());
}

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

$loader = new AssetMapperLoaderFactory();
$loader->create($container, 'the_loader_name', [
]);

$this->assertTrue($container->hasDefinition('liip_imagine.binary.loader.the_loader_name'));

/** @var ChildDefinition $loaderDefinition */
$loaderDefinition = $container->getDefinition('liip_imagine.binary.loader.the_loader_name');

$this->assertInstanceOfChildDefinition($loaderDefinition);
$this->assertSame('liip_imagine.binary.loader.prototype.filesystem', $loaderDefinition->getParent());
}
}
2 changes: 2 additions & 0 deletions Tests/LiipImagineBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Liip\ImagineBundle\DependencyInjection\Compiler\NonFunctionalFilterExceptionPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\PostProcessorsCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Compiler\ResolversCompilerPass;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\AssetMapperLoaderFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\ChainLoaderFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FileSystemLoaderFactory;
use Liip\ImagineBundle\DependencyInjection\Factory\Loader\FlysystemLoaderFactory;
Expand Down Expand Up @@ -150,6 +151,7 @@ public function testAddLoaders(): void
StreamLoaderFactory::class,
FileSystemLoaderFactory::class,
FlysystemLoaderFactory::class,
AssetMapperLoaderFactory::class,
ChainLoaderFactory::class,
], $loaders);
}
Expand Down
Loading