forked from scssphp/scssphp
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c338a45
commit 1d2929f
Showing
5 changed files
with
56 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,9 +37,7 @@ | |
use ScssPhp\ScssPhp\Logger\StreamLogger; | ||
use ScssPhp\ScssPhp\Node\Number; | ||
use ScssPhp\ScssPhp\SourceMap\SourceMapGenerator; | ||
use ScssPhp\ScssPhp\Transforms\Resource; | ||
use ScssPhp\ScssPhp\Transforms\ResourceFactory; | ||
use ScssPhp\ScssPhp\Transforms\Transform; | ||
use ScssPhp\ScssPhp\Transforms\Transformer; | ||
use ScssPhp\ScssPhp\Util\Path; | ||
|
||
/** | ||
|
@@ -354,19 +352,14 @@ class Compiler | |
*/ | ||
private $warnedChildFunctions = []; | ||
|
||
/** | ||
* @var array<string, Transform> Map of transformations | ||
*/ | ||
private $transforms = []; | ||
|
||
/** | ||
* Optional file loader | ||
* | ||
* @var callable|null | ||
*/ | ||
private $fileLoader = null; | ||
|
||
private ?ResourceFactory $resourceFactory = null; | ||
private ?Transformer $transformer = null; | ||
|
||
/** | ||
* Constructor | ||
|
@@ -5812,13 +5805,8 @@ protected function importFile($path, OutputBlock $out) | |
|
||
// Apply webpack-style transforms to the tree | ||
if ($transforms) { | ||
$factory = ($this->resourceFactory ?? new ResourceFactory()); | ||
|
||
// A resource holds the ast tree & code, and is passed across the transforms | ||
$resource = $factory->createResource($path, $tree, $code); | ||
|
||
// The resource is transformed and the modified tree is returned | ||
$tree = $this->executeTransforms($transforms, $resource); | ||
// Apply the named transformations | ||
$tree = ($this->transformer ?? new Transformer())->applyTransformations($transforms, $path, $tree); | ||
} | ||
|
||
$this->importCache[$cacheKey] = $tree; | ||
|
@@ -10546,15 +10534,6 @@ protected function libScssphpGlob($args) | |
return [Type::T_LIST, ',', $listParts]; | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param Transform $transform | ||
* @return void | ||
*/ | ||
public function registerTransform(string $name, Transform $transform): void { | ||
$this->transforms[$name] = $transform; | ||
} | ||
|
||
/** | ||
* Set the file loader. | ||
* | ||
|
@@ -10566,29 +10545,10 @@ public function setFileLoader(?callable $fileLoader): void { | |
} | ||
|
||
/** | ||
* @param ResourceFactory|null $factory | ||
* @param Transformer|null $factory | ||
* @return void | ||
*/ | ||
public function setResourceFactory(?ResourceFactory $factory): void { | ||
$this->resourceFactory = $factory; | ||
} | ||
|
||
/** | ||
* @param array $transforms | ||
* @param Resource $resource | ||
* @return Block | ||
* @throws \Exception | ||
*/ | ||
protected function executeTransforms(array $transforms, Resource $resource): Block { | ||
// transforms execute from right to left (like webpack) | ||
$transforms = array_reverse($transforms, true); | ||
foreach ($transforms as $name => $transform) { | ||
if (!isset($this->transforms[$name])) { | ||
throw new \Exception('Unknown transform "' . $name . '"'); | ||
} | ||
$this->transforms[$name]->execute($resource); | ||
} | ||
|
||
return $resource->getAst(); | ||
public function setTransformer(?Transformer $transformer): void { | ||
Check failure on line 10551 in src/Compiler.php GitHub Actions / Static analysis
|
||
$this->transformer = $transformer; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
use ScssPhp\ScssPhp\Block; | ||
|
||
class Transformer { | ||
|
||
public function __construct(protected ?ResourceFactory $factory = null, protected array $transforms = []) { | ||
|
||
} | ||
|
||
public function setResourceFactory(?ResourceFactory $factory): void { | ||
$this->factory = $factory; | ||
} | ||
|
||
public function registerTransform(string $name, Transform $transform): void { | ||
$this->transforms[$name] = $transform; | ||
} | ||
|
||
public function applyTransformations(array $transforms, string $path, Block $tree): Block { | ||
// Make the resource | ||
$resource = ($this->factory ?? new ResourceFactory())->createResource($path, $tree); | ||
|
||
// transforms execute from right to left (like webpack) | ||
$transforms = array_reverse($transforms, true); | ||
foreach ($transforms as $name => $transform) { | ||
if (!isset($this->transforms[$name])) { | ||
throw new \Exception('Unknown transform "' . $name . '"'); | ||
} | ||
$this->transforms[$name]->execute($resource); | ||
} | ||
|
||
return $resource->getAst(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Tests; | ||
|
||
use ScssPhp\ScssPhp\Compiler; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class TransformationCompilerTest extends TestCase { | ||
|
||
} |