diff --git a/config/services.php b/config/services.php index 2e147ea..755843a 100644 --- a/config/services.php +++ b/config/services.php @@ -35,7 +35,6 @@ ->args([ abstract_arg('path to scss files'), abstract_arg('path to css output directory'), - param('kernel.project_dir'), service('sass.builder'), ]) diff --git a/src/AssetMapper/SassCssCompiler.php b/src/AssetMapper/SassCssCompiler.php index cc26c15..63711ca 100644 --- a/src/AssetMapper/SassCssCompiler.php +++ b/src/AssetMapper/SassCssCompiler.php @@ -12,35 +12,39 @@ use Symfony\Component\AssetMapper\AssetMapperInterface; use Symfony\Component\AssetMapper\Compiler\AssetCompilerInterface; use Symfony\Component\AssetMapper\MappedAsset; -use Symfony\Component\Filesystem\Path; use Symfonycasts\SassBundle\SassBuilder; class SassCssCompiler implements AssetCompilerInterface { public function __construct( - private array $scssPaths, - private string $cssPathDirectory, - private string $projectDir, + /** + * Absolute paths to the .scss files. + * + * @var string[] $scssPaths + */ + private readonly array $scssPaths, + + /** + * Absolute path to the directory where the .css files are stored. + */ + private readonly string $cssPathDirectory, + private readonly SassBuilder $sassBuilder, ) { } public function supports(MappedAsset $asset): bool { - foreach ($this->scssPaths as $path) { - $absolutePath = Path::isAbsolute($path) ? $path : Path::makeAbsolute($path, $this->projectDir); - - if (realpath($asset->sourcePath) === realpath($absolutePath)) { - return true; - } + if (!str_ends_with($asset->sourcePath, '.scss')) { + return false; } - return false; + return \in_array(realpath($asset->sourcePath), $this->scssPaths, true); } public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string { - $cssFile = $this->sassBuilder->guessCssNameFromSassFile($asset->sourcePath, $this->cssPathDirectory); + $cssFile = $this->sassBuilder::guessCssNameFromSassFile($asset->sourcePath, $this->cssPathDirectory); $asset->addFileDependency($cssFile); diff --git a/src/DependencyInjection/SymfonycastsSassExtension.php b/src/DependencyInjection/SymfonycastsSassExtension.php index 07b397d..90d51ca 100644 --- a/src/DependencyInjection/SymfonycastsSassExtension.php +++ b/src/DependencyInjection/SymfonycastsSassExtension.php @@ -16,6 +16,7 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Extension\Extension; use Symfony\Component\DependencyInjection\Loader; +use Symfony\Component\Filesystem\Path; class SymfonycastsSassExtension extends Extension implements ConfigurationInterface { @@ -27,6 +28,15 @@ public function load(array $configs, ContainerBuilder $container): void $configuration = $this->getConfiguration($configs, $container); $config = $this->processConfiguration($configuration, $configs); + // Ensure paths are absolute + $normalizeRootSassPath = function ($path) use ($container) { + return Path::makeAbsolute( + $container->getParameterBag()->resolveValue($path), + $container->getParameter('kernel.project_dir') + ); + }; + $config['root_sass'] = array_map($normalizeRootSassPath, $config['root_sass']); + // BC Layer with SassBundle < 0.4 if (isset($config['embed_sourcemap'])) { $config['sass_options']['embed_source_map'] = $config['embed_sourcemap']; diff --git a/tests/AssetMapper/SassCssCompilerTest.php b/tests/AssetMapper/SassCssCompilerTest.php index cd6884c..a02f7e4 100644 --- a/tests/AssetMapper/SassCssCompilerTest.php +++ b/tests/AssetMapper/SassCssCompilerTest.php @@ -22,22 +22,12 @@ public function testSupports() $asset = new MappedAsset('assets/app.scss', __DIR__.'/../fixtures/assets/app.scss'); - $compilerAbsolutePath = new SassCssCompiler( - [__DIR__.'/../fixtures/assets/app.scss'], - __DIR__.'/../fixtures/var/sass', - __DIR__.'/../fixtures', + $compiler = new SassCssCompiler( + [realpath(__DIR__.'/../fixtures/assets/app.scss')], + realpath(__DIR__.'/../fixtures/var/sass'), $builder ); - $this->assertTrue($compilerAbsolutePath->supports($asset), 'Supports absolute paths'); - - $compilerRelativePath = new SassCssCompiler( - ['assets/app.scss'], - __DIR__.'/../fixtures/var/sass', - __DIR__.'/../fixtures', - $builder - ); - - $this->assertTrue($compilerRelativePath->supports($asset), 'Supportes relative paths'); + $this->assertTrue($compiler->supports($asset)); } }