Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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
3 changes: 3 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,8 @@ jobs:
- name: Add vips
run: composer require rokka/imagine-vips

- name: Add asset mapper
run: composer require symfony/asset-mapper

- name: Run PHPStan
run: vendor/bin/phpstan analyze
86 changes: 86 additions & 0 deletions Binary/Locator/AssetMapperLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?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;
use Psr\Cache\CacheItemPoolInterface;

/**
* Class responsible for locating assets in the asset mapper.
* Implements the LocatorInterface to resolve asset paths.
* This Class should be used only in dev environment.
*/
class AssetMapperLocator implements LocatorInterface
Comment thread
tito10047 marked this conversation as resolved.
{

private $assetMapper;
private $cacheMapCache = null;

public function __construct(
AssetMapperInterface $assetMapper,
Comment thread
dbu marked this conversation as resolved.
Outdated
?CacheItemPoolInterface $cacheMapCache = null
) {
$this->cacheMapCache = $cacheMapCache;
$this->assetMapper = $assetMapper;
}

/**
* Locates an asset by its public path.
*
* This method attempts to retrieve an asset using its public path. It first
* checks for a cached version of the asset, and if none is found, it iterates
* through all available assets to find a match. If no matching asset is located,
* an exception is thrown.
*
* Inspired by Symfony's AssetMapper component @see https://github.com/symfony/asset-mapper/blob/7.3/AssetMapperDevServerSubscriber.php#L179.
*
* @param string $path The public path of the asset to locate.
*
* @return string The located asset.
*
* @throws NotLoadableException If no asset with the specified public path is found.
*/
public function locate(string $path): string
{
$pathInfo = '/'.ltrim($path, '/');
$cachedAsset = null;
if (null !== $this->cacheMapCache) {
$cachedAsset = $this->cacheMapCache->getItem(hash('xxh128', $pathInfo));
Comment thread
tito10047 marked this conversation as resolved.
Outdated
$asset = $cachedAsset->isHit() ? $this->assetMapper->getAsset($cachedAsset->get()) : null;

if (null !== $asset && $asset->publicPath === $pathInfo) {
return $asset;
}
}

// we did not find a match
$asset = null;
foreach ($this->assetMapper->allAssets() as $assetCandidate) {
if ($pathInfo === $assetCandidate->publicPath) {
$asset = $assetCandidate;
break;
}
}

if (null === $asset) {
throw new NotLoadableException(\sprintf('Asset with public path "%s" not found.', $path));
}

if (null !== $cachedAsset) {
$cachedAsset->set($asset->logicalPath);
$this->cacheMapCache->save($cachedAsset);
}

return $asset;
}
}
50 changes: 50 additions & 0 deletions DependencyInjection/Factory/Loader/AssetMapperLoaderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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'));
$locatorDefinition->replaceArgument(1, new Reference('cache.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
Loading
Loading