From 7d20c29f02cf3c5f3e21af7053aeaa4dc4ee1856 Mon Sep 17 00:00:00 2001 From: dleffler Date: Thu, 6 Jan 2022 09:19:22 -0500 Subject: [PATCH 1/2] Updates to better work with SCSSPHP v1.9.0+ 1. Add missing @throws to function comments 2. Adds sourcemap element to array returned by compileFile() since the sourcemap must now be saved by calling program 3. Adds current file to list of files returned by compile() and compileFile(), Issue #7 --- src/Server.php | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/Server.php b/src/Server.php index 429e2c3..9d743bd 100644 --- a/src/Server.php +++ b/src/Server.php @@ -226,7 +226,7 @@ protected function compile($in, $out) $this->metadataName($out), serialize([ 'etag' => $etag, - 'imports' => $this->makeParsedFilesFromIncludeFiles($result->getIncludedFiles()), + 'imports' => $this->makeParsedFilesFromIncludeFiles(array_merge([$in], $result->getIncludedFiles())), 'vars' => crc32(serialize($this->scss->getVariables())), ]) ); @@ -321,7 +321,11 @@ public function compileFile($in, $out = null) $compiled = $result->getCss(); if (is_null($out)) { - return array('compiled' => $compiled, 'files' => $this->makeParsedFilesFromIncludeFiles($result->getIncludedFiles()),); + return array( + 'compiled' => $compiled, + 'files' => $this->makeParsedFilesFromIncludeFiles(array_merge([$in], $result->getIncludedFiles())), + 'map' => $result->getSourceMap() + ); } return file_put_contents($out, $compiled); @@ -334,6 +338,8 @@ public function compileFile($in, $out = null) * @param string $out Output file (.css) * * @return bool + * + * @throws \ScssPhp\Server\ServerException */ public function checkedCompile($in, $out) { @@ -431,7 +437,7 @@ public function serve($salt = '') * * @return string Compiled CSS results * - * @throws \ScssPhp\ScssPhp\Exception\ServerException + * @throws \ScssPhp\Server\ServerException */ public function checkedCachedCompile($in, $out, $force = false) { @@ -472,6 +478,8 @@ public function checkedCachedCompile($in, $out, $force = false) * @param boolean $force Force rebuild? * * @return array scssphp cache structure + * + * @throws \ScssPhp\Server\ServerException */ public function cachedCompile($in, $force = false) { @@ -498,7 +506,7 @@ public function cachedCompile($in, $force = false) } } else { // TODO: Throw an exception? We got neither a string nor something - // that looks like a compatible lessphp cache structure. + // that looks like a compatible scssphp cache structure. return null; } @@ -522,6 +530,8 @@ public function cachedCompile($in, $force = false) * @param string $dir Root directory to .scss files * @param string $cacheDir Cache directory * @param \ScssPhp\ScssPhp\Compiler|null $scss SCSS compiler instance + * + * @throws \ScssPhp\Server\ServerException */ public function __construct($dir, $cacheDir = null, $scss = null) { From 1533d287cf280f5239f6a4af9f65d8e5f4127ec3 Mon Sep 17 00:00:00 2001 From: dleffler Date: Wed, 1 Mar 2023 12:03:54 -0500 Subject: [PATCH 2/2] Fix issue where scssphp/Server ALWAYS sets default timezone to UTC, which is only needed if the server doesn't have it set Adds some PHPDoc 'exception' statements Makes code more PHP v8 friendly --- src/Server.php | 36 ++++++++++++++++++++++++------------ 1 file changed, 24 insertions(+), 12 deletions(-) diff --git a/src/Server.php b/src/Server.php index d677259..77959ec 100644 --- a/src/Server.php +++ b/src/Server.php @@ -1,5 +1,4 @@ scss->compileString(file_get_contents($in), $in); $css = $result->getCss(); - $elapsed = round((microtime(true) - $start), 4); $v = Version::VERSION; @@ -234,7 +232,6 @@ protected function compile($in, $out) return [$css, $etag]; } - /** * Adds to list of parsed files * @@ -257,7 +254,6 @@ protected function makeParsedFilesFromIncludeFiles($paths) return $parsedFiles; } - /** * Format error as a pseudo-element in CSS * @@ -321,7 +317,11 @@ public function compileFile($in, $out = null) $compiled = $result->getCss(); if (is_null($out)) { - return array('compiled' => $compiled, 'files' => $this->makeParsedFilesFromIncludeFiles(array_merge([$in], $result->getIncludedFiles())),); + return array( + 'compiled' => $compiled, + 'files' => $this->makeParsedFilesFromIncludeFiles(array_merge([$in], $result->getIncludedFiles())), + 'map' => $result->getSourceMap() + ); } return file_put_contents($out, $compiled); @@ -334,6 +334,8 @@ public function compileFile($in, $out = null) * @param string $out Output file (.css) * * @return bool + * + * @throws \ScssPhp\Server\ServerException */ public function checkedCompile($in, $out) { @@ -431,7 +433,7 @@ public function serve($salt = '') * * @return string Compiled CSS results * - * @throws \ScssPhp\ScssPhp\Exception\ServerException + * @throws \ScssPhp\Server\ServerException */ public function checkedCachedCompile($in, $out, $force = false) { @@ -472,6 +474,8 @@ public function checkedCachedCompile($in, $out, $force = false) * @param boolean $force Force rebuild? * * @return array scssphp cache structure + * + * @throws \ScssPhp\Server\ServerException */ public function cachedCompile($in, $force = false) { @@ -495,10 +499,14 @@ public function cachedCompile($in, $force = false) break; } } +// if (!file_exists($in['root']) or filemtime($in['root']) > $in['updated']) { +// // The main file has changed so we should compile. +// $root = $in['root']; +// } } } else { // TODO: Throw an exception? We got neither a string nor something - // that looks like a compatible lessphp cache structure. + // that looks like a compatible scssphp cache structure. return null; } @@ -522,6 +530,8 @@ public function cachedCompile($in, $force = false) * @param string $dir Root directory to .scss files * @param string $cacheDir Cache directory * @param \ScssPhp\ScssPhp\Compiler|null $scss SCSS compiler instance + * + * @throws \ScssPhp\Server\ServerException */ public function __construct($dir, $cacheDir = null, $scss = null) { @@ -545,6 +555,8 @@ public function __construct($dir, $cacheDir = null, $scss = null) $this->scss = $scss; $this->showErrorsAsCSS = false; - date_default_timezone_set('UTC'); + if (!ini_get('date.timezone') && function_exists('date_default_timezone_set')) { // PHP >= 5.1.0 + date_default_timezone_set('UTC'); + } } }