diff --git a/src/Compiler.php b/src/Compiler.php index 09c9dc83..46dde104 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -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,11 +352,6 @@ class Compiler */ private $warnedChildFunctions = []; - /** - * @var array Map of transformations - */ - private $transforms = []; - /** * Optional file loader * @@ -366,7 +359,7 @@ class Compiler */ 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 { + $this->transformer = $transformer; } } diff --git a/src/Transforms/Resource.php b/src/Transforms/Resource.php index e7562119..8d347c32 100644 --- a/src/Transforms/Resource.php +++ b/src/Transforms/Resource.php @@ -10,7 +10,7 @@ class Resource { private bool $modified = false; - public function __construct(protected string $path, protected Block $ast, protected string $code) { + public function __construct(protected string $path, protected Block $ast) { } diff --git a/src/Transforms/ResourceFactory.php b/src/Transforms/ResourceFactory.php index 9845a91d..9ae4747d 100644 --- a/src/Transforms/ResourceFactory.php +++ b/src/Transforms/ResourceFactory.php @@ -8,12 +8,9 @@ class ResourceFactory { /** * Create a new resource instance to be used in transformations. * - * @param string $path - * @param string $code - * @param Block $ast * @return Resource */ - public function createResource(string $path, string $code, Block $ast): Resource { - return new Resource($path, $ast, $code); + public function createResource(string $path, Block $ast): Resource { + return new Resource($path, $ast); } } diff --git a/src/Transforms/Transformer.php b/src/Transforms/Transformer.php new file mode 100644 index 00000000..a65003db --- /dev/null +++ b/src/Transforms/Transformer.php @@ -0,0 +1,36 @@ +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(); + } +} diff --git a/tests/TransformationCompilerTest.php b/tests/TransformationCompilerTest.php index e69de29b..db685590 100644 --- a/tests/TransformationCompilerTest.php +++ b/tests/TransformationCompilerTest.php @@ -0,0 +1,10 @@ +