From 7ebd16d3e9e11ca6e3ea39c22a6112af1a2c313d Mon Sep 17 00:00:00 2001 From: Kevin Hamer Date: Wed, 22 Apr 2015 12:07:00 -0400 Subject: [PATCH] Adding ->showErrorsAsCSS() to Leafo\ScssPhp\Server * enables displaying errors as a psuedo element via CSS --- src/Server.php | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/Server.php b/src/Server.php index f5333ee9..ec2ec446 100644 --- a/src/Server.php +++ b/src/Server.php @@ -21,6 +21,8 @@ */ class Server { + protected $showErrorsAsCSS = false; + /** * Join path components * @@ -203,6 +205,35 @@ protected function compile($in, $out) return array($css, $etag); } + protected function createErrorCSS($error) + { + $message = str_replace( + array("'", "\n"), + array("\\'", "\\A"), + $error->getfile() . ":\n\n" . $error->getMessage() + ); + + return "body { display: none !important; } + html:after { + background: white; + color: black; + content: '$message'; + display: block !important; + font-family: mono; + padding: 1em; + white-space: pre; + }"; + } + + /** + * Render errors as a psuedo-element within valid CSS, displaying the errors on any + * page that includes this CSS. + */ + public function showErrorsAsCSS() + { + $this->showErrorsAsCSS = true; + } + /** * Compile requested scss and serve css. Outputs HTTP response. * @@ -231,10 +262,15 @@ public function serve($salt = '') echo $css; } catch (\Exception $e) { - header($protocol . ' 500 Internal Server Error'); - header('Content-type: text/plain'); + if ($this->showErrorsAsCSS) { + header('Content-type: text/css'); + echo $this->createErrorCSS($e); + } else { + header($protocol . ' 500 Internal Server Error'); + header('Content-type: text/plain'); + echo 'Parse error: ' . $e->getMessage() . "\n"; + } - echo 'Parse error: ' . $e->getMessage() . "\n"; } return;