Skip to content

Commit

Permalink
make scssserver compatible with scssphp 1.5.0 (#5)
Browse files Browse the repository at this point in the history
Authored-by: Tim Schoondergang <[email protected]>

Squashed commits:
* some little changes because op Deprecation warnings
* make it oldstyle again
* better example in readme
* force scssphp 1.5.0 and up
  • Loading branch information
timmit-nl authored Jul 6, 2021
1 parent 8539d9f commit a55b7d4
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 22 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ use ScssPhp\ScssPhp\Compiler;
use ScssPhp\Server\Server;
$scss = new Compiler();
$scss->setFormatter('ScssPhp\ScssPhp\Formatter\Compressed');
$scss->setOutputStyle(\ScssPhp\ScssPhp\OutputStyle::COMPRESSED);
$server = new Server('stylesheets', null, $scss);
$server->serve();
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"psr-4": { "ScssPhp\\Server\\Tests\\": "tests/" }
},
"require": {
"scssphp/scssphp": "^1.0"
"scssphp/scssphp": "^1.5"
},
"require-dev": {
"squizlabs/php_codesniffer": "~2.5",
Expand Down
67 changes: 47 additions & 20 deletions src/Server.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* SCSSPHP
*
Expand All @@ -12,7 +13,6 @@
namespace ScssPhp\Server;

use ScssPhp\ScssPhp\Compiler;
use ScssPhp\ScssPhp\Exception\ServerException;
use ScssPhp\ScssPhp\Version;

/**
Expand Down Expand Up @@ -130,7 +130,7 @@ protected function metadataName($out)
*/
protected function needsCompile($out, &$etag)
{
if (! is_file($out)) {
if (!is_file($out)) {
return true;
}

Expand Down Expand Up @@ -210,7 +210,10 @@ protected function getIfNoneMatchHeader()
protected function compile($in, $out)
{
$start = microtime(true);
$css = $this->scss->compile(file_get_contents($in), $in);
$result = $this->scss->compileString(file_get_contents($in), $in);

$css = $result->getCss();

$elapsed = round((microtime(true) - $start), 4);

$v = Version::VERSION;
Expand All @@ -223,14 +226,38 @@ protected function compile($in, $out)
$this->metadataName($out),
serialize([
'etag' => $etag,
'imports' => $this->scss->getParsedFiles(),
'imports' => $this->makeParsedFilesFromIncludeFiles($result->getIncludedFiles()),
'vars' => crc32(serialize($this->scss->getVariables())),
])
);

return [$css, $etag];
}


/**
* Adds to list of parsed files
*
* @internal
*
* @param array|null $paths
*
* @return array
*/
protected function makeParsedFilesFromIncludeFiles($paths)
{
$parsedFiles = array();
if (!\is_null($paths) && !empty($paths)) {
foreach ($paths as $path) {
if (!\is_null($path) && is_file($path)) {
$parsedFiles[realpath($path)] = filemtime($path);
}
}
}
return $parsedFiles;
}


/**
* Format error as a pseudo-element in CSS
*
Expand Down Expand Up @@ -275,24 +302,26 @@ public function showErrorsAsCSS($show = true)
* @param string $in Input file (.scss)
* @param string $out Output file (.css) optional
*
* @return string|bool
* @return array|bool
*
* @throws \ScssPhp\ScssPhp\Exception\ServerException
* @throws \ScssPhp\Server\ServerException
*/
public function compileFile($in, $out = null)
{
if (! is_readable($in)) {
if (!is_readable($in)) {
throw new ServerException('load error: failed to find ' . $in);
}

$pi = pathinfo($in);

$this->scss->addImportPath($pi['dirname'] . '/');

$compiled = $this->scss->compile(file_get_contents($in), $in);
$result = $this->scss->compileString(file_get_contents($in), $in);

$compiled = $result->getCss();

if (is_null($out)) {
return $compiled;
return array('compiled' => $compiled, 'files' => $this->makeParsedFilesFromIncludeFiles($result->getIncludedFiles()),);
}

return file_put_contents($out, $compiled);
Expand All @@ -308,7 +337,7 @@ public function compileFile($in, $out = null)
*/
public function checkedCompile($in, $out)
{
if (! is_file($out) || filemtime($in) > filemtime($out)) {
if (!is_file($out) || filemtime($in) > filemtime($out)) {
$this->compileFile($in, $out);

return true;
Expand Down Expand Up @@ -406,11 +435,11 @@ public function serve($salt = '')
*/
public function checkedCachedCompile($in, $out, $force = false)
{
if (! is_file($in) || ! is_readable($in)) {
if (!is_file($in) || !is_readable($in)) {
throw new ServerException('Invalid or unreadable input file specified.');
}

if (is_dir($out) || ! is_writable(file_exists($out) ? $out : dirname($out))) {
if (is_dir($out) || !is_writable(file_exists($out) ? $out : dirname($out))) {
throw new ServerException('Invalid or unwritable output file specified.');
}

Expand Down Expand Up @@ -452,14 +481,14 @@ public function cachedCompile($in, $force = false)
if (is_string($in)) {
$root = $in;
} elseif (is_array($in) and isset($in['root'])) {
if ($force or ! isset($in['files'])) {
if ($force or !isset($in['files'])) {
// If we are forcing a recompile or if for some reason the
// structure does not contain any file information we should
// specify the root to trigger a rebuild.
$root = $in['root'];
} elseif (isset($in['files']) and is_array($in['files'])) {
foreach ($in['files'] as $fname => $ftime) {
if (! file_exists($fname) or filemtime($fname) > $ftime) {
if (!file_exists($fname) or filemtime($fname) > $ftime) {
// One of the files we knew about previously has changed
// so we should look at our incoming root again.
$root = $in['root'];
Expand All @@ -480,10 +509,8 @@ public function cachedCompile($in, $force = false)
}

// If we have a root value which means we should rebuild.
$out = [];
$out = $this->compileFile($root);
$out['root'] = $root;
$out['compiled'] = $this->compileFile($root);
$out['files'] = $this->scss->getParsedFiles();
$out['updated'] = time();

return $out;
Expand All @@ -500,17 +527,17 @@ public function __construct($dir, $cacheDir = null, $scss = null)
{
$this->dir = $dir;

if (! isset($cacheDir)) {
if (!isset($cacheDir)) {
$cacheDir = $this->join($dir, 'scss_cache');
}

$this->cacheDir = $cacheDir;

if (! is_dir($this->cacheDir)) {
if (!is_dir($this->cacheDir)) {
throw new ServerException('Cache directory doesn\'t exist: ' . $cacheDir);
}

if (! isset($scss)) {
if (!isset($scss)) {
$scss = new Compiler();
$scss->setImportPaths($this->dir);
}
Expand Down
24 changes: 24 additions & 0 deletions src/ServerException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

/**
* SCSSPHP
*
* @copyright 2012-2020 Leaf Corcoran
*
* @license http://opensource.org/licenses/MIT MIT
*
* @link http://scssphp.github.io/scssphp
*/

namespace ScssPhp\Server;

use ScssPhp\ScssPhp\Exception\SassException;

/**
* Server Exception
*
* @author Anthon Pang <[email protected]>
*/
class ServerException extends \Exception implements SassException
{
}

0 comments on commit a55b7d4

Please sign in to comment.