Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor CSS creation, PHP 8.4 compatibility #972

Merged
merged 1 commit into from
Nov 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 37 additions & 23 deletions components/CssCompiler.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php

declare(strict_types=1);

namespace app\components;

use ScssPhp\ScssPhp\Parser;
use ScssPhp\ScssPhp\Compiler;
use ScssPhp\ScssPhp\OutputStyle;

/**
* This is a workaround for missing math.div support in scssphp.
Expand All @@ -13,36 +16,47 @@
*
* Relevant test case: appearance/CustomThemeCept
*/
class CssCompiler extends \ScssPhp\ScssPhp\Compiler
class CssCompiler
{
protected function parserFactory($path): Parser
public function compileCss(string $css): string
{
$cssOnly = false;
$tmpDir = \app\models\settings\AntragsgruenApp::getInstance()->getTmpDir();

if ($path !== null && substr($path, -4) === '.css') {
$cssOnly = true;
}
$this->copyAndFixScss($tmpDir);

$parser = new class($path, \count($this->sourceNames), $this->encoding, $this->cache, $cssOnly) extends Parser {
public function __construct($sourceName, $sourceIndex = 0, $encoding = 'utf-8', \ScssPhp\ScssPhp\Cache $cache = null, $cssOnly = false, \ScssPhp\ScssPhp\Logger\LoggerInterface $logger = null)
{
parent::__construct($sourceName, $sourceIndex, $encoding, $cache, $cssOnly, $logger);
}
$scss = new Compiler();
$scss->addImportPath($tmpDir . 'css-compile');
$scss->setOutputStyle(OutputStyle::COMPRESSED);

return $scss->compileString($css)->getCss();
}

public function parse($buffer)
{
$buffer = str_replace('@use "sass:math";', '', $buffer);
$buffer = preg_replace_callback('/math\.div\((?<first>[^,]*), (?<second>[^)]+)\)/siu', function ($matches): string {
return $matches['first'] . ' / ' . $matches['second'];
}, $buffer);
private function copyAndFixScss(string $tmpDir): void
{
$filesystem = new \Symfony\Component\Filesystem\Filesystem();
$filesystem->mirror(\Yii::$app->basePath . '/web/css', $tmpDir . 'css-compile');
$filesystem->mirror(\Yii::$app->basePath . '/web/fonts', $tmpDir . 'fonts');

$iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($tmpDir, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::SELF_FIRST);
foreach ($iterator as $file) {
if (!str_ends_with($file->getPathname(), 'scss')) {
continue;
}

return parent::parse($buffer);
$content = file_get_contents($file->getPathname());
if ($content) {
file_put_contents($file->getPathname(), $this->fixCss($content));
}
};
}
}

$this->sourceNames[] = $path;
$this->addParsedFile($path);
private function fixCss(String $css): string
{
$css = str_replace('@use "sass:math";', '', $css);
$css = preg_replace_callback('/math\.div\((?<first>[^,]*), (?<second>[^)]+)\)/siu', function ($matches): string {
return $matches['first'] . ' / ' . $matches['second'];
}, $css);

return $parser;
return $css;
}
}
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"phpoffice/phpspreadsheet": "^3.3",
"predis/predis": "^2.2",
"s1syphos/php-simple-captcha": "^2.3",
"scssphp/scssphp": "^1.13.0",
"scssphp/scssphp": "dev-main#12d3cdf0fc2a73e9438ded5a5a82fb353b68d718 as 1.13.0",
"setasign/fpdi": "^2.3.7",
"spomky-labs/otphp": "^11.3",
"symfony/amazon-mailer": "^5.4.23",
Expand Down
Loading