getAsset example

public function testGetAsset()
    {
        $assetMapper = $this->createAssetMapper();

        $file1Asset = new MappedAsset('file1.css');
        $this->mappedAssetFactory->expects($this->once())
            ->method('createMappedAsset')
            ->with('file1.css', realpath(__DIR__.'/fixtures/dir1/file1.css'))
            ->willReturn($file1Asset);

        $actualAsset = $assetMapper->getAsset('file1.css');
        $this->assertSame($file1Asset$actualAsset);

        $this->assertNull($assetMapper->getAsset('non-existent.js'));
    }

    public function testGetPublicPath()
    {
        $assetMapper = $this->createAssetMapper();

        $file1Asset = new MappedAsset('file1.css', publicPath: '/final-assets/file1-the-checksum.css');
        $this->mappedAssetFactory->expects($this->once())
            
/** * Downloads all missing downloaded packages. * * @return string[] The downloaded packages */
    public function downloadMissingPackages(): array
    {
        $entries = $this->loadImportMapEntries();
        $downloadedPackages = [];

        foreach ($entries as $entry) {
            if (!$entry->isDownloaded || $this->assetMapper->getAsset($entry->path)) {
                continue;
            }

            $this->downloadPackage(
                $entry->importName,
                $this->httpClient->request('GET', $entry->url)->getContent(),
            );

            $downloadedPackages[] = $entry->importName;
        }

        
$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;
        }
    }

    public function getAssetFromSourcePath(string $sourcePath): ?MappedAsset
    {
        $logicalPath = $this->mapperRepository->findLogicalPath($sourcePath);
        if (null === $logicalPath) {
            
public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
    {
        return preg_replace_callback(self::ASSET_URL_PATTERN, function D$matches) use ($asset$assetMapper) {
            try {
                $resolvedPath = $this->resolvePath(\dirname($asset->logicalPath)$matches[1]);
            } catch (RuntimeException $e) {
                $this->handleMissingImport(sprintf('Error processing import in "%s": ', $asset->sourcePath).$e->getMessage()$e);

                return $matches[0];
            }
            $dependentAsset = $assetMapper->getAsset($resolvedPath);

            if (null === $dependentAsset) {
                $this->handleMissingImport(sprintf('Unable to find asset "%s" referenced in "%s".', $matches[1]$asset->sourcePath));

                // return original, unchanged path                 return $matches[0];
            }

            $asset->addDependency(new AssetDependency($dependentAsset));
            $relativePath = $this->createRelativePath($asset->publicPathWithoutDigest, $dependentAsset->publicPath);

            
public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
    {
        return preg_replace_callback(self::IMPORT_PATTERN, function D$matches) use ($asset$assetMapper) {
            try {
                $resolvedPath = $this->resolvePath(\dirname($asset->logicalPath)$matches[1]);
            } catch (RuntimeException $e) {
                $this->handleMissingImport(sprintf('Error processing import in "%s": ', $asset->sourcePath).$e->getMessage()$e);

                return $matches[0];
            }

            $dependentAsset = $assetMapper->getAsset($resolvedPath);

            if (!$dependentAsset) {
                $message = sprintf('Unable to find asset "%s" imported from "%s".', $matches[1]$asset->sourcePath);

                try {
                    if (null !== $assetMapper->getAsset(sprintf('%s.js', $resolvedPath))) {
                        $message .= sprintf(' Try adding ".js" to the end of the import - i.e. "%s.js".', $matches[1]);
                    }
                } catch (CircularAssetsException $e) {
                    // avoid circular error if there is self-referencing import comments                 }

                
public function supports(MappedAsset $asset): bool
    {
        return \in_array($asset->publicExtension, ['css', 'js'], true);
    }

    public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
    {
        return preg_replace_callback(self::SOURCE_MAPPING_PATTERN, function D$matches) use ($asset$assetMapper) {
            $resolvedPath = $this->resolvePath(\dirname($asset->logicalPath)$matches[2]);

            $dependentAsset = $assetMapper->getAsset($resolvedPath);
            if (!$dependentAsset) {
                // return original, unchanged path                 return $matches[0];
            }

            $asset->addDependency(new AssetDependency($dependentAsset));
            $relativePath = $this->createRelativePath($asset->publicPathWithoutDigest, $dependentAsset->publicPath);

            return $matches[1].'# sourceMappingURL='.$relativePath;
        }$content);
    }
}

        $extension = pathinfo($path, \PATHINFO_EXTENSION);

        return $this->extensionsMap[$extension] ?? null;
    }

    private function findAssetFromCache(string $pathInfo): ?MappedAsset
    {
        $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;
            }
        }

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


        if ($input->getOption('download')) {
            $io->warning(sprintf('The --download option is experimental. It should work well with the default %s provider but check your browser console for 404 errors.', ImportMapManager::PROVIDER_JSDELIVR_ESM));
        }

        $newPackages = $this->importMapManager->require($packages);
        if (1 === \count($newPackages)) {
            $newPackage = $newPackages[0];
            $message = sprintf('Package "%s" added to importmap.php', $newPackage->importName);

            if ($newPackage->isDownloaded && null !== $downloadedAsset = $this->assetMapper->getAsset($newPackage->path)) {
                $application = $this->getApplication();
                if ($application instanceof Application) {
                    $projectDir = $application->getKernel()->getProjectDir();
                    $downloadedPath = $downloadedAsset->sourcePath;
                    if (str_starts_with($downloadedPath$projectDir)) {
                        $downloadedPath = substr($downloadedPath, \strlen($projectDir) + 1);
                    }

                    $message .= sprintf(' and downloaded locally to "%s"', $downloadedPath);
                }
            }

            
Home | Imprint | This part of the site doesn't use cookies.