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
a9ede0d
commit 8763d10
Showing
6 changed files
with
319 additions
and
9 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,47 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
use ScssPhp\ScssPhp\Block; | ||
|
||
/** | ||
* A resource represents the scss file that is in the process of transforming. | ||
*/ | ||
class Resource | ||
{ | ||
private bool $modified = false; | ||
|
||
public function __construct(protected string $path, protected Block $ast) | ||
{ | ||
} | ||
|
||
/** | ||
* Return the modified Tree | ||
* | ||
* @return Block | ||
*/ | ||
public function getAst(): Block | ||
{ | ||
return $this->ast; | ||
} | ||
|
||
/** | ||
* 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,18 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
use ScssPhp\ScssPhp\Block; | ||
|
||
class ResourceFactory | ||
{ | ||
/** | ||
* Create a new resource instance to be used in transformations. | ||
* | ||
* @return Resource | ||
*/ | ||
public function createResource(string $path, Block $ast): Resource | ||
{ | ||
return new Resource($path, $ast); | ||
} | ||
} |
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,12 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
interface Transform | ||
{ | ||
/** | ||
* @param Resource $resource | ||
* @return void | ||
*/ | ||
public function execute(\ScssPhp\ScssPhp\Transforms\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,66 @@ | ||
<?php | ||
|
||
namespace ScssPhp\ScssPhp\Transforms; | ||
|
||
use ScssPhp\ScssPhp\Block; | ||
|
||
class Transformer | ||
{ | ||
/** | ||
* @param ResourceFactory|null $factory | ||
* @param array<string, Transform> $transforms | ||
*/ | ||
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; | ||
} | ||
|
||
/** | ||
* @param string[] $transforms | ||
* @param string $path | ||
* @param Block $tree | ||
* @return Block | ||
*/ | ||
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) { | ||
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]; | ||
} | ||
} |
Oops, something went wrong.