-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PHP CSS and SCSS parser
- Loading branch information
Showing
12 changed files
with
541 additions
and
49 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
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,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ItalyStrap\ThemeJsonGenerator\Domain\Input\Styles; | ||
|
||
/** | ||
* @psalm-api | ||
*/ | ||
interface CssInterface | ||
{ | ||
public const M_AMPERSAND_MUST_NOT_BE_AT_THE_BEGINNING = 'CSS cannot begin with an ampersand (&)'; | ||
|
||
public function expanded(): self; | ||
|
||
public function parse(string $css, string $selector = ''): string; | ||
} |
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,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace ItalyStrap\ThemeJsonGenerator\Domain\Input\Styles; | ||
|
||
use ItalyStrap\Tests\Unit\Domain\Input\Styles\ScssTest; | ||
use ItalyStrap\ThemeJsonGenerator\Domain\Input\Settings\NullPresets; | ||
use ItalyStrap\ThemeJsonGenerator\Domain\Input\Settings\PresetsInterface; | ||
use ScssPhp\ScssPhp\Compiler; | ||
use ScssPhp\ScssPhp\OutputStyle; | ||
|
||
/** | ||
* @psalm-api | ||
* @see ScssTest | ||
*/ | ||
class Scss implements CssInterface | ||
{ | ||
private Css $css; | ||
private Compiler $compiler; | ||
private PresetsInterface $presets; | ||
|
||
/** | ||
* @var 'compressed'|'expanded' | ||
*/ | ||
private string $outputStyle = OutputStyle::COMPRESSED; | ||
|
||
public function __construct( | ||
Css $css, | ||
Compiler $compiler, | ||
PresetsInterface $presets = null | ||
) { | ||
$this->css = $css; | ||
$this->compiler = $compiler; | ||
$this->presets = $presets ?? new NullPresets(); | ||
} | ||
|
||
public function expanded(): self | ||
{ | ||
$this->css->expanded(); | ||
$this->outputStyle = OutputStyle::EXPANDED; | ||
return $this; | ||
} | ||
|
||
public function parse(string $css, string $selector = ''): string | ||
{ | ||
if (\str_starts_with(\trim($css), '&')) { | ||
throw new \RuntimeException(CssInterface::M_AMPERSAND_MUST_NOT_BE_AT_THE_BEGINNING); | ||
} | ||
|
||
$this->css->stopResolveVariables(); | ||
$css = $this->presets->parse($css); | ||
|
||
$selector = \trim($selector); | ||
|
||
if ($selector === '') { | ||
return $css; | ||
} | ||
|
||
$this->compiler->setOutputStyle($this->outputStyle); | ||
$cssCompiled = $this->compiler->compileString($css); | ||
|
||
return $this->css->parse($cssCompiled->getCss(), $selector); | ||
} | ||
} |
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
Oops, something went wrong.