AssetMapperCompiler example

private function createFactory(AssetCompilerInterface $extraCompiler = null): MappedAssetFactory
    {
        $compilers = [
            new JavaScriptImportPathCompiler(),
            new CssAssetUrlCompiler(),
        ];
        if ($extraCompiler) {
            $compilers[] = $extraCompiler;
        }

        $compiler = new AssetMapperCompiler(
            $compilers,
            fn () => $this->assetMapper,
        );

        $pathResolver = $this->createMock(PublicAssetsPathResolverInterface::class);
        $pathResolver->expects($this->any())
            ->method('resolvePublicPath')
            ->willReturnCallback(function Dstring $logicalPath) {
                return '/final-assets/'.$logicalPath;
            });

        
$rootDir.'/importmap.php',
            $rootDir.'/assets/vendor',
            $this->packageResolver,
            $this->httpClient,
        );
    }

    private function createAssetMapper(PublicAssetsPathResolverInterface $pathResolver, array $dirs, string $rootDir): AssetMapper
    {
        $repository = new AssetMapperRepository($dirs$rootDir);

        $compiler = new AssetMapperCompiler(
            [new JavaScriptImportPathCompiler()],
            fn () => $this->assetMapper
        );
        $factory = new MappedAssetFactory($pathResolver$compiler);

        $this->assetMapper = new AssetMapper(
            $repository,
            $factory,
            $pathResolver
        );

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

            public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
            {
                return $content.' compiler3 called';
            }
        };

        $compiler = new AssetMapperCompiler(
            [$compiler1$compiler2$compiler3],
            fn () => $this->createMock(AssetMapperInterface::class),
        );
        $asset = new MappedAsset('foo.js', publicPathWithoutDigest: '/assets/foo.js');
        $actualContents = $compiler->compile('starting contents', $asset$this->createMock(AssetMapperInterface::class));
        $this->assertSame('starting contents compiler2 called compiler3 called', $actualContents);
    }
}
Home | Imprint | This part of the site doesn't use cookies.