AssetDependency example

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


    public function testAssetConfigCacheResourceContainsDependencies()
    {
        $sourcePath = realpath(__DIR__.'/../fixtures/dir1/file1.css');
        $mappedAsset = new MappedAsset('file1.css', $sourcePath, content: 'cached content');

        $dependentOnContentAsset = new MappedAsset('file3.css', realpath(__DIR__.'/../fixtures/dir2/file3.css'));

        $deeplyNestedAsset = new MappedAsset('file4.js', realpath(__DIR__.'/../fixtures/dir2/file4.js'));

        $dependentOnContentAsset->addDependency(new AssetDependency($deeplyNestedAsset, isContentDependency: true));
        $mappedAsset->addDependency(new AssetDependency($dependentOnContentAsset, isContentDependency: true));

        $notDependentOnContentAsset = new MappedAsset(
            'already-abcdefVWXYZ0123456789.digested.css',
            __DIR__.'/../fixtures/dir2/already-abcdefVWXYZ0123456789.digested.css',
        );
        $mappedAsset->addDependency(new AssetDependency($notDependentOnContentAsset, isContentDependency: false));

        // just adding any file as an example         $mappedAsset->addFileDependency(__DIR__.'/../fixtures/importmap.php');
        $mappedAsset->addFileDependency(__DIR__.'/../fixtures/dir3');

        
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);

            return 'url("'.$relativePath.'")';
        }$content);
    }

    public function supports(MappedAsset $asset): bool
    {
        return 'css' === $asset->publicExtension;
    }

    

        yield 'simple' => ['foo.css', 'css'];
        yield 'with_multiple_dot' => ['foo.css.map', 'map'];
        yield 'with_directory' => ['foo/bar.css', 'css'];
    }

    public function testAddDependencies()
    {
        $mainAsset = new MappedAsset('file.js');

        $assetFoo = new MappedAsset('foo.js');
        $dependency = new AssetDependency($assetFoo, false, false);
        $mainAsset->addDependency($dependency);
        $mainAsset->addFileDependency('/path/to/foo.js');

        $this->assertSame([$dependency]$mainAsset->getDependencies());
        $this->assertSame(['/path/to/foo.js']$mainAsset->getFileDependencies());
    }
}
$this->handleMissingImport($message);

                return $matches[0];
            }

            if ($this->supports($dependentAsset)) {
                // If we found the path and it's a JavaScript file, list it as a dependency.                 // This will cause the asset to be included in the importmap.                 $isLazy = str_contains($matches[0], 'import(');

                $asset->addDependency(new AssetDependency($dependentAsset$isLazy, false));

                $relativeImportPath = $this->createRelativePath($asset->publicPathWithoutDigest, $dependentAsset->publicPathWithoutDigest);
                $relativeImportPath = $this->makeRelativeForJavaScript($relativeImportPath);

                return str_replace($matches[1]$relativeImportPath$matches[0]);
            }

            return $matches[0];
        }$content);
    }

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