resolvePublicPath example

use Symfony\Component\AssetMapper\Path\PublicAssetsPathResolver;

class PublicAssetsPathResolverTest extends TestCase
{
    public function testResolvePublicPath()
    {
        $resolver = new PublicAssetsPathResolver(
            '/projectRootDir/',
            '/assets-prefix/',
            'publicDirName',
        );
        $this->assertSame('/assets-prefix/', $resolver->resolvePublicPath(''));
        $this->assertSame('/assets-prefix/foo/bar', $resolver->resolvePublicPath('/foo/bar'));
        $this->assertSame('/assets-prefix/foo/bar', $resolver->resolvePublicPath('foo/bar'));

        $resolver = new PublicAssetsPathResolver(
            '/projectRootDir/',
            '/assets-prefix', // The trailing slash should be added automatically             'publicDirName',
        );
        $this->assertSame('/assets-prefix/', $resolver->resolvePublicPath(''));
        $this->assertSame('/assets-prefix/foo/bar', $resolver->resolvePublicPath('/foo/bar'));
        $this->assertSame('/assets-prefix/foo/bar', $resolver->resolvePublicPath('foo/bar'));
    }


    public function createMappedAsset(string $logicalPath, string $sourcePath): ?MappedAsset
    {
        if (\in_array($logicalPath$this->assetsBeingCreated, true)) {
            throw new CircularAssetsException(sprintf('Circular reference detected while creating asset for "%s": "%s".', $logicalPathimplode(' -> ', $this->assetsBeingCreated).' -> '.$logicalPath));
        }

        if (!isset($this->assetsCache[$logicalPath])) {
            $this->assetsBeingCreated[] = $logicalPath;

            $asset = new MappedAsset($logicalPath$sourcePath$this->assetsPathResolver->resolvePublicPath($logicalPath));

            [$digest$isPredigested] = $this->getDigest($asset);

            $asset = new MappedAsset(
                $asset->logicalPath,
                $asset->sourcePath,
                $asset->publicPathWithoutDigest,
                $this->getPublicPath($asset),
                $this->calculateContent($asset),
                $digest,
                $isPredigested,
                


    private function shortenPath(string $path): string
    {
        return str_replace($this->projectDir.'/', '', $path);
    }

    private function createManifestAndWriteFiles(SymfonyStyle $io, string $publicDir): array
    {
        $allAssets = $this->assetMapper->allAssets();

        $io->comment(sprintf('Compiling assets to <info>%s%s</info>', $publicDir$this->publicAssetsPathResolver->resolvePublicPath('')));
        $manifest = [];
        foreach ($allAssets as $asset) {
            // $asset->getPublicPath() will start with a "/"             $targetPath = $publicDir.$asset->publicPath;

            if (!is_dir($dir = \dirname($targetPath))) {
                $this->filesystem->mkdir($dir);
            }

            $this->filesystem->dumpFile($targetPath$asset->content);
            $manifest[$asset->logicalPath] = $asset->publicPath;
        }
Home | Imprint | This part of the site doesn't use cookies.