createMappedAsset example

private readonly PublicAssetsPathResolverInterface $assetsPathResolver,
    ) {
    }

    public function getAsset(string $logicalPath): ?MappedAsset
    {
        $filePath = $this->mapperRepository->find($logicalPath);
        if (null === $filePath) {
            return null;
        }

        return $this->mappedAssetFactory->createMappedAsset($logicalPath$filePath);
    }

    public function allAssets(): iterable
    {
        foreach ($this->mapperRepository->all() as $logicalPath => $filePath) {
            $asset = $this->getAsset($logicalPath);
            if (null === $asset) {
                throw new \LogicException(sprintf('Asset "%s" could not be found.', $logicalPath));
            }
            yield $asset;
        }
    }
$this->cacheDir,
            true
        );

        $mappedAsset = new MappedAsset('file1.css', __DIR__.'/../fixtures/dir1/file1.css');

        $factory->expects($this->once())
            ->method('createMappedAsset')
            ->with('file1.css', '/anything/file1.css')
            ->willReturn($mappedAsset);

        $this->assertSame($mappedAsset$cachedFactory->createMappedAsset('file1.css', '/anything/file1.css'));

        // check that calling again does not trigger the inner call         // and, the objects will be equal, but not identical         $secondActualAsset = $cachedFactory->createMappedAsset('file1.css', '/anything/file1.css');
        $this->assertNotSame($mappedAsset$secondActualAsset);
        $this->assertSame('file1.css', $secondActualAsset->logicalPath);
        $this->assertSame(__DIR__.'/../fixtures/dir1/file1.css', $secondActualAsset->sourcePath);
    }

    public function testAssetIsNotBuiltWhenCached()
    {
        


    public function createMappedAsset(string $logicalPath, string $sourcePath): ?MappedAsset
    {
        $cachePath = $this->getCacheFilePath($logicalPath$sourcePath);
        $configCache = new ConfigCache($cachePath$this->debug);

        if ($configCache->isFresh()) {
            return unserialize(file_get_contents($cachePath));
        }

        $mappedAsset = $this->innerFactory->createMappedAsset($logicalPath$sourcePath);

        if (!$mappedAsset) {
            return null;
        }

        $resources = $this->collectResourcesFromAsset($mappedAsset);
        $configCache->write(serialize($mappedAsset)$resources);

        return $mappedAsset;
    }

    
use Symfony\Component\AssetMapper\MappedAsset;
use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolverInterface;

class MappedAssetFactoryTest extends TestCase
{
    private AssetMapperInterface&MockObject $assetMapper;

    public function testCreateMappedAsset()
    {
        $factory = $this->createFactory();

        $asset = $factory->createMappedAsset('file2.js', __DIR__.'/../fixtures/dir1/file2.js');
        $this->assertSame('file2.js', $asset->logicalPath);
        $this->assertMatchesRegularExpression('/^\/final-assets\/file2-[a-zA-Z0-9]{7,128}\.js$/', $asset->publicPath);
        $this->assertSame('/final-assets/file2.js', $asset->publicPathWithoutDigest);
    }

    public function testCreateMappedAssetRespectsPreDigestedPaths()
    {
        $assetMapper = $this->createFactory();
        $asset = $assetMapper->createMappedAsset('already-abcdefVWXYZ0123456789.digested.css', __DIR__.'/../fixtures/dir2/already-abcdefVWXYZ0123456789.digested.css');
        $this->assertSame('already-abcdefVWXYZ0123456789.digested.css', $asset->logicalPath);
        $this->assertSame('/final-assets/already-abcdefVWXYZ0123456789.digested.css', $asset->publicPath);
        
Home | Imprint | This part of the site doesn't use cookies.