Skip to content

Commit a55b7d4

Browse files
authored
make scssserver compatible with scssphp 1.5.0 (#5)
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
1 parent 8539d9f commit a55b7d4

File tree

4 files changed

+74
-22
lines changed

4 files changed

+74
-22
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,9 @@ use ScssPhp\ScssPhp\Compiler;
6262
use ScssPhp\Server\Server;
6363
6464
$scss = new Compiler();
65-
$scss->setFormatter('ScssPhp\ScssPhp\Formatter\Compressed');
65+
$scss->setOutputStyle(\ScssPhp\ScssPhp\OutputStyle::COMPRESSED);
6666
6767
$server = new Server('stylesheets', null, $scss);
68+
6869
$server->serve();
6970
```

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"psr-4": { "ScssPhp\\Server\\Tests\\": "tests/" }
1010
},
1111
"require": {
12-
"scssphp/scssphp": "^1.0"
12+
"scssphp/scssphp": "^1.5"
1313
},
1414
"require-dev": {
1515
"squizlabs/php_codesniffer": "~2.5",

src/Server.php

+47-20
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
/**
34
* SCSSPHP
45
*
@@ -12,7 +13,6 @@
1213
namespace ScssPhp\Server;
1314

1415
use ScssPhp\ScssPhp\Compiler;
15-
use ScssPhp\ScssPhp\Exception\ServerException;
1616
use ScssPhp\ScssPhp\Version;
1717

1818
/**
@@ -130,7 +130,7 @@ protected function metadataName($out)
130130
*/
131131
protected function needsCompile($out, &$etag)
132132
{
133-
if (! is_file($out)) {
133+
if (!is_file($out)) {
134134
return true;
135135
}
136136

@@ -210,7 +210,10 @@ protected function getIfNoneMatchHeader()
210210
protected function compile($in, $out)
211211
{
212212
$start = microtime(true);
213-
$css = $this->scss->compile(file_get_contents($in), $in);
213+
$result = $this->scss->compileString(file_get_contents($in), $in);
214+
215+
$css = $result->getCss();
216+
214217
$elapsed = round((microtime(true) - $start), 4);
215218

216219
$v = Version::VERSION;
@@ -223,14 +226,38 @@ protected function compile($in, $out)
223226
$this->metadataName($out),
224227
serialize([
225228
'etag' => $etag,
226-
'imports' => $this->scss->getParsedFiles(),
229+
'imports' => $this->makeParsedFilesFromIncludeFiles($result->getIncludedFiles()),
227230
'vars' => crc32(serialize($this->scss->getVariables())),
228231
])
229232
);
230233

231234
return [$css, $etag];
232235
}
233236

237+
238+
/**
239+
* Adds to list of parsed files
240+
*
241+
* @internal
242+
*
243+
* @param array|null $paths
244+
*
245+
* @return array
246+
*/
247+
protected function makeParsedFilesFromIncludeFiles($paths)
248+
{
249+
$parsedFiles = array();
250+
if (!\is_null($paths) && !empty($paths)) {
251+
foreach ($paths as $path) {
252+
if (!\is_null($path) && is_file($path)) {
253+
$parsedFiles[realpath($path)] = filemtime($path);
254+
}
255+
}
256+
}
257+
return $parsedFiles;
258+
}
259+
260+
234261
/**
235262
* Format error as a pseudo-element in CSS
236263
*
@@ -275,24 +302,26 @@ public function showErrorsAsCSS($show = true)
275302
* @param string $in Input file (.scss)
276303
* @param string $out Output file (.css) optional
277304
*
278-
* @return string|bool
305+
* @return array|bool
279306
*
280-
* @throws \ScssPhp\ScssPhp\Exception\ServerException
307+
* @throws \ScssPhp\Server\ServerException
281308
*/
282309
public function compileFile($in, $out = null)
283310
{
284-
if (! is_readable($in)) {
311+
if (!is_readable($in)) {
285312
throw new ServerException('load error: failed to find ' . $in);
286313
}
287314

288315
$pi = pathinfo($in);
289316

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

292-
$compiled = $this->scss->compile(file_get_contents($in), $in);
319+
$result = $this->scss->compileString(file_get_contents($in), $in);
320+
321+
$compiled = $result->getCss();
293322

294323
if (is_null($out)) {
295-
return $compiled;
324+
return array('compiled' => $compiled, 'files' => $this->makeParsedFilesFromIncludeFiles($result->getIncludedFiles()),);
296325
}
297326

298327
return file_put_contents($out, $compiled);
@@ -308,7 +337,7 @@ public function compileFile($in, $out = null)
308337
*/
309338
public function checkedCompile($in, $out)
310339
{
311-
if (! is_file($out) || filemtime($in) > filemtime($out)) {
340+
if (!is_file($out) || filemtime($in) > filemtime($out)) {
312341
$this->compileFile($in, $out);
313342

314343
return true;
@@ -406,11 +435,11 @@ public function serve($salt = '')
406435
*/
407436
public function checkedCachedCompile($in, $out, $force = false)
408437
{
409-
if (! is_file($in) || ! is_readable($in)) {
438+
if (!is_file($in) || !is_readable($in)) {
410439
throw new ServerException('Invalid or unreadable input file specified.');
411440
}
412441

413-
if (is_dir($out) || ! is_writable(file_exists($out) ? $out : dirname($out))) {
442+
if (is_dir($out) || !is_writable(file_exists($out) ? $out : dirname($out))) {
414443
throw new ServerException('Invalid or unwritable output file specified.');
415444
}
416445

@@ -452,14 +481,14 @@ public function cachedCompile($in, $force = false)
452481
if (is_string($in)) {
453482
$root = $in;
454483
} elseif (is_array($in) and isset($in['root'])) {
455-
if ($force or ! isset($in['files'])) {
484+
if ($force or !isset($in['files'])) {
456485
// If we are forcing a recompile or if for some reason the
457486
// structure does not contain any file information we should
458487
// specify the root to trigger a rebuild.
459488
$root = $in['root'];
460489
} elseif (isset($in['files']) and is_array($in['files'])) {
461490
foreach ($in['files'] as $fname => $ftime) {
462-
if (! file_exists($fname) or filemtime($fname) > $ftime) {
491+
if (!file_exists($fname) or filemtime($fname) > $ftime) {
463492
// One of the files we knew about previously has changed
464493
// so we should look at our incoming root again.
465494
$root = $in['root'];
@@ -480,10 +509,8 @@ public function cachedCompile($in, $force = false)
480509
}
481510

482511
// If we have a root value which means we should rebuild.
483-
$out = [];
512+
$out = $this->compileFile($root);
484513
$out['root'] = $root;
485-
$out['compiled'] = $this->compileFile($root);
486-
$out['files'] = $this->scss->getParsedFiles();
487514
$out['updated'] = time();
488515

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

503-
if (! isset($cacheDir)) {
530+
if (!isset($cacheDir)) {
504531
$cacheDir = $this->join($dir, 'scss_cache');
505532
}
506533

507534
$this->cacheDir = $cacheDir;
508535

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

513-
if (! isset($scss)) {
540+
if (!isset($scss)) {
514541
$scss = new Compiler();
515542
$scss->setImportPaths($this->dir);
516543
}

src/ServerException.php

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
/**
4+
* SCSSPHP
5+
*
6+
* @copyright 2012-2020 Leaf Corcoran
7+
*
8+
* @license http://opensource.org/licenses/MIT MIT
9+
*
10+
* @link http://scssphp.github.io/scssphp
11+
*/
12+
13+
namespace ScssPhp\Server;
14+
15+
use ScssPhp\ScssPhp\Exception\SassException;
16+
17+
/**
18+
* Server Exception
19+
*
20+
* @author Anthon Pang <[email protected]>
21+
*/
22+
class ServerException extends \Exception implements SassException
23+
{
24+
}

0 commit comments

Comments
 (0)