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.
TL-40011: Adding in webkit-style transformation support
- Loading branch information
1 parent
344573c
commit 49dbab2
Showing
8 changed files
with
429 additions
and
8 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
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,33 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
use ScssPhp\ScssPhp\Block; | ||
|
||
/** | ||
* A resource represents the scss file that is in the process of transforming. | ||
*/ | ||
interface Resource | ||
{ | ||
/** | ||
* Reads back the specific code if it's still in a readable state. | ||
* If the AST has been generated an exception will be thrown instead. | ||
*/ | ||
public function getCode(): string; | ||
|
||
/** | ||
* Set the CSS code to transform. This will delete the internal AST tree, marking it ready for parsing again. | ||
*/ | ||
public function setCode(string $code): void; | ||
|
||
/** | ||
* Load the compiled tree. | ||
*/ | ||
public function getAst(): Block; | ||
|
||
public function setAst(Block $ast): void; | ||
|
||
public function markASTModified(): void; | ||
|
||
public function isASTOnly(): bool; | ||
} |
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,20 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
/** | ||
* A resource represents the scss file that is in the process of transforming. | ||
* This factory can be used to replace specific instances of resources. | ||
*/ | ||
interface ResourceFactory | ||
{ | ||
/** | ||
* Create a new instance of the resource based on the code & path provided. | ||
* | ||
* @param string $path | ||
* @param string $code | ||
* @param callable $astParserFactory | ||
* @return Resource | ||
*/ | ||
public function createResource(string $path, string $code, callable $astParserFactory): Resource; | ||
} |
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,11 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
/** | ||
* Contract for a transform action. Provided with the resource the transform can modify it in a chain. | ||
*/ | ||
interface Transform | ||
{ | ||
public function execute(Resource $resource): void; | ||
} |
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,67 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
use ScssPhp\ScssPhp\Block; | ||
|
||
/** | ||
* Base class that applies the transformation actions, an extension to the compiler. | ||
*/ | ||
class TransformCompiler | ||
{ | ||
protected ResourceFactory $resourceFactory; | ||
|
||
/** | ||
* @param array<string, Transform> $transforms | ||
*/ | ||
public function __construct(protected array $transforms = [], ?ResourceFactory $resourceFactory = null) | ||
{ | ||
$this->resourceFactory = $resourceFactory ?? new TransformResourceFactory(); | ||
} | ||
|
||
public function registerTransform(string $name, Transform $transform): void | ||
{ | ||
$this->transforms[$name] = $transform; | ||
} | ||
|
||
/** | ||
* @param string[] $transforms | ||
* @param string $path | ||
* @param string $code | ||
* @param callable $astParserFactory | ||
* @return Block | ||
*/ | ||
public function applyTransformations(array $transforms, string $path, string $code, callable $astParserFactory): Block | ||
{ | ||
// Make the resource | ||
$resource = $this->resourceFactory->createResource($path, $code, $astParserFactory); | ||
|
||
// transforms execute from right to left (like webpack) | ||
$transforms = array_reverse($transforms, true); | ||
foreach ($transforms as $name) { | ||
if (!isset($this->transforms[$name])) { | ||
throw new \Exception('Unknown transform "' . $name . '"'); | ||
} | ||
$this->transforms[$name]->execute($resource); | ||
} | ||
|
||
return $resource->getAst(); | ||
} | ||
|
||
/** | ||
* @param string $path | ||
* @return array{0: string, 1: string[]} | ||
*/ | ||
public function extractTransformsFromPath(string $path): array | ||
{ | ||
$pos = strrpos($path, '!'); | ||
$transforms = []; | ||
|
||
if ($pos !== false) { | ||
$pathTransforms = substr($path, 0, $pos); | ||
$transforms = !empty($pathTransforms) ? explode('!', $pathTransforms) : []; | ||
$path = substr($path, $pos + 1); | ||
} | ||
return [$path, $transforms]; | ||
} | ||
} |
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,83 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
use ScssPhp\ScssPhp\Block; | ||
|
||
/** | ||
* A resource represents the scss file that is in the process of transforming. | ||
*/ | ||
class TransformResource implements Resource | ||
{ | ||
private bool $modified = false; | ||
protected ?Block $ast = null; | ||
|
||
/** | ||
* @var callable | ||
*/ | ||
protected $parserFactory; | ||
|
||
public function __construct(protected string $path, protected string $code, callable $parserFactory) | ||
{ | ||
$this->parserFactory = $parserFactory; | ||
} | ||
|
||
public function getCode(): string | ||
{ | ||
if ($this->modified) { | ||
throw new \Exception('Cannot access code as it is in AST only mode'); | ||
} | ||
|
||
return $this->code; | ||
} | ||
|
||
public function setCode(string $code): void | ||
{ | ||
$this->code = $code; | ||
$this->ast = null; | ||
$this->modified = false; | ||
} | ||
|
||
/** | ||
* Return the modified Tree | ||
* | ||
* @return Block | ||
*/ | ||
public function getAst(): Block | ||
{ | ||
if ($this->ast === null) { | ||
if (empty($this->code)) { | ||
throw new \Exception('AST and source are both unavailable for this resource'); | ||
} | ||
|
||
$this->ast = ($this->parserFactory)($this->path, $this->code); | ||
} | ||
return $this->ast; | ||
} | ||
|
||
public function setAst(Block $ast): void | ||
{ | ||
$this->ast = $ast; | ||
$this->markASTModified(); | ||
} | ||
|
||
/** | ||
* Marks the AST has been modified | ||
* | ||
* @return void | ||
*/ | ||
public function markASTModified(): void | ||
{ | ||
$this->modified = true; | ||
} | ||
|
||
/** | ||
* Indicates if the resource AST has been modified or not. | ||
* | ||
* @return bool | ||
*/ | ||
public function isASTOnly(): bool | ||
{ | ||
return $this->modified; | ||
} | ||
} |
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,21 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
/** | ||
* Default factory to create a resource. | ||
*/ | ||
class TransformResourceFactory implements ResourceFactory | ||
{ | ||
/** | ||
* Create a new instance of the resource based on the code & path provided. | ||
* @param string $path | ||
* @param string $code | ||
* @param callable $astParserFactory | ||
* @return Resource | ||
*/ | ||
public function createResource(string $path, string $code, callable $astParserFactory): Resource | ||
{ | ||
return new TransformResource($path, $code, $astParserFactory); | ||
} | ||
} |
Oops, something went wrong.