Skip to content

Commit

Permalink
Simplifying the transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
codyfinegan committed Apr 18, 2024
1 parent c338a45 commit 1d2929f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 53 deletions.
54 changes: 7 additions & 47 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*
Expand All @@ -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

View workflow job for this annotation

GitHub Actions / Static analysis

PHPDoc tag @param references unknown parameter: $factory
$this->transformer = $transformer;
}
}
2 changes: 1 addition & 1 deletion src/Transforms/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

}

Expand Down
7 changes: 2 additions & 5 deletions src/Transforms/ResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
36 changes: 36 additions & 0 deletions src/Transforms/Transformer.php
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();
}
}
10 changes: 10 additions & 0 deletions tests/TransformationCompilerTest.php
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 {

}

0 comments on commit 1d2929f

Please sign in to comment.