-
Notifications
You must be signed in to change notification settings - Fork 386
Add AssetMapper support #1645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Add AssetMapper support #1645
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
c30473d
Added AssetMapperLoaderFactory and AssetMapperLocator
tito10047 09003a9
removed duplicated loader and use filesystem instead
tito10047 b7bebb8
add test
tito10047 34b38d6
php-cs-fixer run
tito10047 a79384c
updated documentation
tito10047 2e44bff
updated documentation
tito10047 fbf6adf
removed unused code
tito10047 ef9c955
removed unbused variable and readonly keyword
tito10047 1dd541c
enhanced `AssetMapperLoaderFactory` and `AssetMapperLocator` to suppo…
tito10047 1864225
add Symfony Asset Mapper to PHPStan workflow
tito10047 6a524a1
add tests for `AssetMapperLocator` and `AssetMapperLoaderFactory`, fi…
tito10047 7801018
Update Resources/doc/data-loader/asset_mapper.rst
tito10047 eeea561
Update Resources/doc/data-loader/asset_mapper.rst
tito10047 68b8d96
cs fixer
tito10047 5954695
Merge remote-tracking branch 'origin/asset-mapper' into asset-mapper
tito10047 a9b7667
Merge branch '2.x' into asset-mapper
tito10047 0a3730c
add asset mapper dev requirements
tito10047 61c6271
add asset mapper dev requirements
tito10047 61f03c2
remove `symfony/asset-mapper` from dependencies, add conditional dev…
tito10047 20f3061
Update .github/workflows/phpstan.yml
tito10047 299bbea
Update .github/workflows/phpunit.yml
tito10047 ae06e8f
Remove cache from asset AssetMapperLocator.php and add better documen…
tito10047 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| <?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 Psr\Cache\CacheItemPoolInterface; | ||
| use Symfony\Component\AssetMapper\AssetMapperInterface; | ||
|
|
||
| /** | ||
| * 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 | ||
|
tito10047 marked this conversation as resolved.
|
||
| { | ||
| private $assetMapper; | ||
| private $cacheMapCache; | ||
|
|
||
| public function __construct( | ||
| AssetMapperInterface $assetMapper, | ||
| ?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 | ||
| * | ||
| * @throws NotLoadableException if no asset with the specified public path is found | ||
| * | ||
| * @return string the located asset | ||
| */ | ||
| public function locate(string $path): string | ||
| { | ||
| $pathInfo = '/'.ltrim($path, '/'); | ||
| $cachedAsset = null; | ||
| if (null !== $this->cacheMapCache) { | ||
| $cachedAsset = $this->cacheMapCache->getItem(hash('xxh128', $pathInfo)); | ||
| $asset = $cachedAsset->isHit() ? $this->assetMapper->getAsset($cachedAsset->get()) : null; | ||
|
|
||
| if (null !== $asset && $asset->publicPath === $pathInfo) { | ||
| return $asset->sourcePath; | ||
| } | ||
| } | ||
|
|
||
| // we did not find a match | ||
| $asset = null; | ||
| foreach ($this->assetMapper->allAssets() as $assetCandidate) { | ||
|
tito10047 marked this conversation as resolved.
|
||
| if ($pathInfo === $assetCandidate->publicPath) { | ||
| $asset = $assetCandidate; | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| if (null === $asset) { | ||
| throw new NotLoadableException(\sprintf('Asset with public path "%s" not found.', $pathInfo)); | ||
| } | ||
|
|
||
| if (null !== $cachedAsset) { | ||
| $cachedAsset->set($asset->logicalPath); | ||
| $this->cacheMapCache->save($cachedAsset); | ||
| } | ||
|
|
||
| return $asset->sourcePath; | ||
| } | ||
| } | ||
50 changes: 50 additions & 0 deletions
50
DependencyInjection/Factory/Loader/AssetMapperLoaderFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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')); | ||
|
dbu marked this conversation as resolved.
Outdated
|
||
|
|
||
| $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(); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -429,6 +430,14 @@ | |
| '', // will be injected by FilesystemLoaderFactory | ||
| ]) | ||
| ->tag('liip_imagine.binary.locator', ['shared' => false]); | ||
| $services->set('liip_imagine.binary.locator.asset_mapper', AssetMapperLocator::class) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should mark this |
||
| ->share(true) | ||
| ->public() | ||
| ->args([ | ||
| '', // will be injected by AssetMapperLoaderFactory | ||
| '', // will be injected by AssetMapperLoaderFactory | ||
| ]) | ||
| ->tag('liip_imagine.binary.locator', ['shared' => false]); | ||
|
|
||
| $services->set('liip_imagine.binary.locator.filesystem_insecure', FileSystemInsecureLocator::class) | ||
| ->share(false) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
|
|
||
| .. _data-loaders-asset-mapper: | ||
|
|
||
| Asset Mapper Loader (for dev) | ||
| ============================ | ||
|
|
||
| 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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| <?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\Binary\Locator; | ||
|
|
||
| use Liip\ImagineBundle\Binary\Locator\AssetMapperLocator; | ||
| use Liip\ImagineBundle\Binary\Locator\LocatorInterface; | ||
| use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; | ||
| use PHPUnit\Framework\TestCase; | ||
| use Psr\Cache\CacheItemInterface; | ||
| use Psr\Cache\CacheItemPoolInterface; | ||
| use Symfony\Component\AssetMapper\AssetMapperInterface; | ||
| use Symfony\Component\AssetMapper\MappedAsset; | ||
|
|
||
| /** | ||
| * @covers \Liip\ImagineBundle\Binary\Locator\AssetMapperLocator | ||
| */ | ||
| class AssetMapperLocatorTest extends TestCase | ||
| { | ||
| protected function setUp(): void | ||
| { | ||
| if (!interface_exists(AssetMapperInterface::class)) { | ||
| $this->markTestSkipped('symfony/asset-mapper is required for this test.'); | ||
| } | ||
| } | ||
|
|
||
| public function testImplementsLocatorInterface(): void | ||
| { | ||
| $assetMapper = $this->createMock(AssetMapperInterface::class); | ||
| $this->assertInstanceOf(LocatorInterface::class, new AssetMapperLocator($assetMapper)); | ||
| } | ||
|
|
||
| public function testLocateWithoutCache(): void | ||
| { | ||
| $path = 'images/logo.png'; | ||
| $pathInfo = '/images/logo.png'; | ||
| $sourcePath = '/path/to/logo.png'; | ||
| $logicalPath = 'logo.png'; | ||
|
|
||
| $asset = new MappedAsset($logicalPath, $sourcePath, $pathInfo, $pathInfo); | ||
|
|
||
| $assetMapper = $this->createMock(AssetMapperInterface::class); | ||
| $assetMapper->expects($this->once()) | ||
| ->method('allAssets') | ||
| ->willReturn([$asset]); | ||
|
|
||
| $locator = new AssetMapperLocator($assetMapper); | ||
| $result = $locator->locate($path); | ||
|
|
||
| $this->assertSame($sourcePath, $result); | ||
| } | ||
|
|
||
| public function testLocateWithCacheHit(): void | ||
| { | ||
| $path = 'images/logo.png'; | ||
| $pathInfo = '/images/logo.png'; | ||
| $logicalPath = 'logo.png'; | ||
| $sourcePath = '/path/to/logo.png'; | ||
|
|
||
| $asset = new MappedAsset($logicalPath, $sourcePath, $pathInfo, $pathInfo); | ||
|
|
||
| $cacheItem = $this->createMock(CacheItemInterface::class); | ||
| $cacheItem->expects($this->once()) | ||
| ->method('isHit') | ||
| ->willReturn(true); | ||
| $cacheItem->expects($this->once()) | ||
| ->method('get') | ||
| ->willReturn($logicalPath); | ||
|
|
||
| $cache = $this->createMock(CacheItemPoolInterface::class); | ||
| $cache->expects($this->once()) | ||
| ->method('getItem') | ||
| ->with(hash('xxh128', $pathInfo)) | ||
| ->willReturn($cacheItem); | ||
|
|
||
| $assetMapper = $this->createMock(AssetMapperInterface::class); | ||
| $assetMapper->expects($this->once()) | ||
| ->method('getAsset') | ||
| ->with($logicalPath) | ||
| ->willReturn($asset); | ||
| $assetMapper->expects($this->never()) | ||
| ->method('allAssets'); | ||
|
|
||
| $locator = new AssetMapperLocator($assetMapper, $cache); | ||
| $result = $locator->locate($path); | ||
|
|
||
| $this->assertSame($sourcePath, $result); | ||
| } | ||
|
|
||
| public function testLocateWithCacheMissAndSave(): void | ||
| { | ||
| $path = 'images/logo.png'; | ||
| $pathInfo = '/images/logo.png'; | ||
| $logicalPath = 'logo.png'; | ||
| $sourcePath = '/path/to/logo.png'; | ||
|
|
||
| $asset = new MappedAsset($logicalPath, $sourcePath, $pathInfo, $pathInfo); | ||
|
|
||
| $cacheItem = $this->createMock(CacheItemInterface::class); | ||
| $cacheItem->expects($this->once()) | ||
| ->method('isHit') | ||
| ->willReturn(false); | ||
| $cacheItem->expects($this->once()) | ||
| ->method('set') | ||
| ->with($logicalPath); | ||
|
|
||
| $cache = $this->createMock(CacheItemPoolInterface::class); | ||
| $cache->expects($this->once()) | ||
| ->method('getItem') | ||
| ->with(hash('xxh128', $pathInfo)) | ||
| ->willReturn($cacheItem); | ||
| $cache->expects($this->once()) | ||
| ->method('save') | ||
| ->with($cacheItem); | ||
|
|
||
| $assetMapper = $this->createMock(AssetMapperInterface::class); | ||
| $assetMapper->expects($this->once()) | ||
| ->method('allAssets') | ||
| ->willReturn([$asset]); | ||
|
|
||
| $locator = new AssetMapperLocator($assetMapper, $cache); | ||
| $result = $locator->locate($path); | ||
|
|
||
| $this->assertSame($sourcePath, $result); | ||
| } | ||
|
|
||
| public function testThrowsIfAssetNotFound(): void | ||
| { | ||
| $path = 'images/logo.png'; | ||
|
|
||
| $assetMapper = $this->createMock(AssetMapperInterface::class); | ||
| $assetMapper->expects($this->once()) | ||
| ->method('allAssets') | ||
| ->willReturn([]); | ||
|
|
||
| $locator = new AssetMapperLocator($assetMapper); | ||
|
|
||
| $this->expectException(NotLoadableException::class); | ||
| $this->expectExceptionMessage('Asset with public path "/images/logo.png" not found.'); | ||
|
|
||
| $locator->locate($path); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.