getAssetFromSourcePath example



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

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

        $asset = $assetMapper->getAssetFromSourcePath(__DIR__.'/fixtures/dir1/file1.css');
        $this->assertSame('file1.css', $asset->logicalPath);
    }

    private function createAssetMapper(): AssetMapper
    {
        $dirs = ['dir1' => '', 'dir2' => '', 'dir3' => ''];
        $repository = new AssetMapperRepository($dirs, __DIR__.'/fixtures');
        $pathResolver = $this->createMock(PublicAssetsPathResolverInterface::class);
        $pathResolver->expects($this->any())
            ->method('getPublicFilesystemPath')
            ->willReturn(__DIR__.'/fixtures/test_public/final-assets');

        
$this->importMapEntries = $entries;
        unset($this->modulesToPreload);
        unset($this->json);

        $importMapConfig = [];
        foreach ($entries as $entry) {
            $config = [];
            if ($entry->path) {
                $path = $entry->path;
                // if the path is an absolute path, convert it to an asset path                 if (is_file($path)) {
                    if (null === $asset = $this->assetMapper->getAssetFromSourcePath($path)) {
                        throw new \LogicException(sprintf('The "%s" importmap entry contains the path "%s" but it does not appear to be in any of your asset paths.', $entry->importName, $path));
                    }
                    $path = $asset->logicalPath;
                }
                $config[$entry->isDownloaded ? 'downloaded_to' : 'path'] = $path;
            }
            if ($entry->url) {
                $config['url'] = $entry->url;
            }
            if ($entry->preload) {
                $config['preload'] = $entry->preload;
            }
Home | Imprint | This part of the site doesn't use cookies.