Skip to content

Commit

Permalink
Updating changelog and phpdocs (refs #83)
Browse files Browse the repository at this point in the history
  • Loading branch information
robocoder committed May 24, 2013
1 parent 3bf5917 commit e5a7921
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 17 deletions.
101 changes: 84 additions & 17 deletions scss.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
* @author Leaf Corcoran <leafot@gmail.com>
*/
class scssc {
static public $VERSION = "v0.0.5";
static public $VERSION = "v0.0.7";

static protected $operatorNames = array(
'+' => "add",
Expand Down Expand Up @@ -4174,36 +4174,80 @@ public function indentStr($n = 0) {
* @author Leaf Corcoran <leafot@gmail.com>
*/
class scss_server {

/**
* Join path components
*
* @param string $left Path component, left of the directory separator
* @param string $right Path component, right of the directory separator
*
* @return string
*/
protected function join($left, $right) {
return rtrim($left, "/") . "/" . ltrim($right, "/");
return rtrim($left, '/\\') . DIRECTORY_SEPARATOR . ltrim($right, '/\\');
}

/**
* Get name of requested .scss file
*
* @return string|null
*/
protected function inputName() {
if (isset($_GET["p"])) return $_GET["p"];

if (isset($_SERVER["PATH_INFO"])) return $_SERVER["PATH_INFO"];
if (isset($_SERVER["DOCUMENT_URI"])) {
return substr($_SERVER["DOCUMENT_URI"], strlen($_SERVER["SCRIPT_NAME"]));
switch (true) {
case isset($_GET['p']):
return $_GET['p'];
case isset($_SERVER['PATH_INFO']):
return $_SERVER['PATH_INFO'];
case isset($_SERVER['DOCUMENT_URI']):
return substr($_SERVER['DOCUMENT_URI'], strlen($_SERVER['SCRIPT_NAME']));
}
}

/**
* Get path to requested .scss file
*
* @return string
*/
protected function findInput() {
if ($input = $this->inputName()) {
if ($input = $this->inputName()
&& strpos($input, '..') === false
&& substr($input, -5) === '.scss'
) {
$name = $this->join($this->dir, $input);
if (is_readable($name)) return $name;

if (is_file($name) && is_readable($name)) {
return $name;
}
}

return false;
}

/**
* Get path to cached .css file
*
* @return string
*/
protected function cacheName($fname) {
return $this->join($this->cacheDir, md5($fname) . ".css");
return $this->join($this->cacheDir, md5($fname) . '.css');
}

/**
* Get path to cached imports
*
* @return string
*/
protected function importsCacheName($out) {
return $out . ".imports";
return $out . '.imports';
}

/**
* Determine whether .scss file needs to be re-compiled.
*
* @param string $in Input path
* @param string $out Output path
*
* @return boolean True if compile required.
*/
protected function needsCompile($in, $out) {
if (!is_file($out)) return true;

Expand All @@ -4221,13 +4265,21 @@ protected function needsCompile($in, $out) {
return false;
}

/**
* Compile .scss file
*
* @param string $in Input path (.scss)
* @param string $out Output path (.css)
*
* @return string
*/
protected function compile($in, $out) {
$start = microtime(true);
$css = $this->scss->compile(file_get_contents($in), $in);
$elapsed = round((microtime(true) - $start), 4);

$v = scssc::$VERSION;
$t = date("r");
$t = date('r');
$css = "/* compiled by scssphp $v on $t (${elapsed}s) */\n\n" . $css;

file_put_contents($out, $css);
Expand All @@ -4236,17 +4288,20 @@ protected function compile($in, $out) {
return $css;
}

/**
* Compile requested scss and serve css. Outputs HTTP response.
*/
public function serve() {
if ($input = $this->findInput()) {
$output = $this->cacheName($input);
header("Content-type: text/css");
header('Content-type: text/css');

if ($this->needsCompile($input, $output)) {
try {
echo $this->compile($input, $output);
} catch (Exception $e) {
header('HTTP/1.1 500 Internal Server Error');
echo "Parse error: " . $e->getMessage() . "\n";
echo 'Parse error: ' . $e->getMessage() . "\n";
}
} else {
header('X-SCSS-Cache: true');
Expand All @@ -4257,16 +4312,23 @@ public function serve() {
}

header('HTTP/1.0 404 Not Found');
header("Content-type: text");
header('Content-type: text');
$v = scssc::$VERSION;
echo "/* INPUT NOT FOUND scss $v */\n";
}

/**
* Constructor
*
* @param string $dir Root directory to .scss files
* @param string $cacheDir Cache directory
* @param \scssc|null $scss SCSS compiler instance
*/
public function __construct($dir, $cacheDir=null, $scss=null) {
$this->dir = $dir;

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

$this->cacheDir = $cacheDir;
Expand All @@ -4279,6 +4341,11 @@ public function __construct($dir, $cacheDir=null, $scss=null) {
$this->scss = $scss;
}

/**
* Helper method to serve compiled scss
*
* @param string $path Root path
*/
static public function serveFrom($path) {
$server = new self($path);
$server->serve();
Expand Down
15 changes: 15 additions & 0 deletions site/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,21 @@ Find any issues? I'd love to fix them for you, post about them on [the issues tr
<a name="changelog"></a>
## Changelog

* **0.0.7**
* Forgot to bump the version number. Re-tagged as v0.0.7.

* **0.0.6**
* Port various fixes from leafo/lessphp.
* Improve filter precision.
* Parsing large image data-urls does not work.
* Add == and != ops for colors.
* @if and @while directives should treat null like false.
* Add pscss as bin in composer.json (Christian Lück).
* Fix !default bug (James Shannon, Alberto Aldegheri).
* Fix mixin content includes (James Shannon, Christian Brandt).
* Fix passing of varargs to another mixin.
* Fix interpolation bug in expToString() (Matti Jarvinen).

* **0.0.5** -- March 11, 2013
* Better compile time errors
* Fix top level properties inside of a nested `@media` (Anthon Pang)
Expand Down

0 comments on commit e5a7921

Please sign in to comment.