Skip to content

Commit aa47da8

Browse files
authored
Fix root_sass relative paths (#8 #43) (#64)
1 parent 29eef1b commit aa47da8

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
"php": ">=8.1",
1515
"symfony/asset-mapper": "^6.3|^7.0",
1616
"symfony/console": "^5.4|^6.3|^7.0",
17+
"symfony/filesystem": "^5.4|^6.3|^7.0",
1718
"symfony/http-client": "^5.4|^6.3|^7.0",
1819
"symfony/process": "^5.4|^6.3|^7.0"
1920
},
2021
"require-dev": {
2122
"matthiasnoback/symfony-config-test": "^5.0",
22-
"symfony/filesystem": "^6.3|^7.0",
2323
"symfony/framework-bundle": "^6.3|^7.0",
2424
"symfony/phpunit-bridge": "^6.3|^7.0",
2525
"phpstan/phpstan-symfony": "^1.4"

config/services.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
->args([
3636
abstract_arg('path to scss files'),
3737
abstract_arg('path to css output directory'),
38+
param('kernel.project_dir'),
3839
service('sass.builder'),
3940
])
4041

src/AssetMapper/SassCssCompiler.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@
1212
use Symfony\Component\AssetMapper\AssetMapperInterface;
1313
use Symfony\Component\AssetMapper\Compiler\AssetCompilerInterface;
1414
use Symfony\Component\AssetMapper\MappedAsset;
15+
use Symfony\Component\Filesystem\Path;
1516
use Symfonycasts\SassBundle\SassBuilder;
1617

1718
class SassCssCompiler implements AssetCompilerInterface
1819
{
1920
public function __construct(
2021
private array $scssPaths,
2122
private string $cssPathDirectory,
23+
private string $projectDir,
2224
private readonly SassBuilder $sassBuilder
2325
) {
2426
}
2527

2628
public function supports(MappedAsset $asset): bool
2729
{
2830
foreach ($this->scssPaths as $path) {
29-
if (realpath($asset->sourcePath) === realpath($path)) {
31+
$absolutePath = Path::isAbsolute($path) ? $path : Path::makeAbsolute($path, $this->projectDir);
32+
33+
if (realpath($asset->sourcePath) === realpath($absolutePath)) {
3034
return true;
3135
}
3236
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the SymfonyCasts SassBundle package.
5+
* Copyright (c) SymfonyCasts <https://symfonycasts.com/>
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
namespace Symfonycasts\SassBundle\Tests\AssetMapper;
11+
12+
use PHPUnit\Framework\TestCase;
13+
use Symfony\Component\AssetMapper\MappedAsset;
14+
use Symfonycasts\SassBundle\AssetMapper\SassCssCompiler;
15+
use Symfonycasts\SassBundle\SassBuilder;
16+
17+
class SassCssCompilerTest extends TestCase
18+
{
19+
public function testSupports()
20+
{
21+
$builder = $this->createMock(SassBuilder::class);
22+
23+
$asset = new MappedAsset('assets/app.scss', __DIR__.'/../fixtures/assets/app.scss');
24+
25+
$compilerAbsolutePath = new SassCssCompiler(
26+
[__DIR__.'/../fixtures/assets/app.scss'],
27+
__DIR__.'/../fixtures/var/sass',
28+
__DIR__.'/../fixtures',
29+
$builder
30+
);
31+
32+
$this->assertTrue($compilerAbsolutePath->supports($asset), 'Supports absolute paths');
33+
34+
$compilerRelativePath = new SassCssCompiler(
35+
['assets/app.scss'],
36+
__DIR__.'/../fixtures/var/sass',
37+
__DIR__.'/../fixtures',
38+
$builder
39+
);
40+
41+
$this->assertTrue($compilerRelativePath->supports($asset), 'Supportes relative paths');
42+
}
43+
}

0 commit comments

Comments
 (0)