scss_server

SCSS server.

Defined (1)

The class is defined in the following location(s).

/modules/custom-css/custom-css/preprocessors/scss.inc.php  
  1. class scss_server { 
  2. /** 
  3. * Join path components 
  4. * @param string $left Path component, left of the directory separator 
  5. * @param string $right Path component, right of the directory separator 
  6. * @return string 
  7. */ 
  8. protected function join($left, $right) { 
  9. return rtrim($left, '/\\') . DIRECTORY_SEPARATOR . ltrim($right, '/\\'); 
  10.  
  11. /** 
  12. * Get name of requested .scss file 
  13. * @return string|null 
  14. */ 
  15. protected function inputName() { 
  16. switch (true) { 
  17. case isset($_GET['p']): 
  18. return $_GET['p']; 
  19. case isset($_SERVER['PATH_INFO']): 
  20. return $_SERVER['PATH_INFO']; 
  21. case isset($_SERVER['DOCUMENT_URI']): 
  22. return substr($_SERVER['DOCUMENT_URI'], strlen($_SERVER['SCRIPT_NAME'])); 
  23.  
  24. /** 
  25. * Get path to requested .scss file 
  26. * @return string 
  27. */ 
  28. protected function findInput() { 
  29. if (($input = $this->inputName()) 
  30. && strpos($input, '..') === false 
  31. && substr($input, -5) === '.scss' 
  32. ) { 
  33. $name = $this->join($this->dir, $input); 
  34.  
  35. if (is_file($name) && is_readable($name)) { 
  36. return $name; 
  37.  
  38. return false; 
  39.  
  40. /** 
  41. * Get path to cached .css file 
  42. * @return string 
  43. */ 
  44. protected function cacheName($fname) { 
  45. return $this->join($this->cacheDir, md5($fname) . '.css'); 
  46.  
  47. /** 
  48. * Get path to cached imports 
  49. * @return string 
  50. */ 
  51. protected function importsCacheName($out) { 
  52. return $out . '.imports'; 
  53.  
  54. /** 
  55. * Determine whether .scss file needs to be re-compiled. 
  56. * @param string $in Input path 
  57. * @param string $out Output path 
  58. * @return boolean True if compile required. 
  59. */ 
  60. protected function needsCompile($in, $out) { 
  61. if (!is_file($out)) return true; 
  62.  
  63. $mtime = filemtime($out); 
  64. if (filemtime($in) > $mtime) return true; 
  65.  
  66. // look for modified imports 
  67. $icache = $this->importsCacheName($out); 
  68. if (is_readable($icache)) { 
  69. $imports = unserialize(file_get_contents($icache)); 
  70. foreach ($imports as $import) { 
  71. if (filemtime($import) > $mtime) return true; 
  72. return false; 
  73.  
  74. /** 
  75. * Compile .scss file 
  76. * @param string $in Input path (.scss) 
  77. * @param string $out Output path (.css) 
  78. * @return string 
  79. */ 
  80. protected function compile($in, $out) { 
  81. $start = microtime(true); 
  82. $css = $this->scss->compile(file_get_contents($in), $in); 
  83. $elapsed = round((microtime(true) - $start), 4); 
  84.  
  85. $v = scssc::$VERSION; 
  86. $t = date('r'); 
  87. $css = "/* compiled by scssphp $v on $t (${elapsed}s) */\n\n" . $css; 
  88.  
  89. file_put_contents($out, $css); 
  90. file_put_contents($this->importsCacheName($out),  
  91. serialize($this->scss->getParsedFiles())); 
  92. return $css; 
  93.  
  94. /** 
  95. * Compile requested scss and serve css. Outputs HTTP response. 
  96. * @param string $salt Prefix a string to the filename for creating the cache name hash 
  97. */ 
  98. public function serve($salt = '') { 
  99. if ($input = $this->findInput()) { 
  100. $output = $this->cacheName($salt . $input); 
  101. header('Content-type: text/css'); 
  102.  
  103. if ($this->needsCompile($input, $output)) { 
  104. try { 
  105. echo $this->compile($input, $output); 
  106. } catch (Exception $e) { 
  107. header('HTTP/1.1 500 Internal Server Error'); 
  108. echo 'Parse error: ' . $e->getMessage() . "\n"; 
  109. } else { 
  110. header('X-SCSS-Cache: true'); 
  111. echo file_get_contents($output); 
  112.  
  113. return; 
  114.  
  115. header('HTTP/1.0 404 Not Found'); 
  116. header('Content-type: text'); 
  117. $v = scssc::$VERSION; 
  118. echo "/* INPUT NOT FOUND scss $v */\n"; 
  119.  
  120. /** 
  121. * Constructor 
  122. * @param string $dir Root directory to .scss files 
  123. * @param string $cacheDir Cache directory 
  124. * @param \scssc|null $scss SCSS compiler instance 
  125. */ 
  126. public function __construct($dir, $cacheDir=null, $scss=null) { 
  127. $this->dir = $dir; 
  128.  
  129. if (is_null($cacheDir)) { 
  130. $cacheDir = $this->join($dir, 'scss_cache'); 
  131.  
  132. $this->cacheDir = $cacheDir; 
  133. if (!is_dir($this->cacheDir)) mkdir($this->cacheDir, 0755, true); 
  134.  
  135. if (is_null($scss)) { 
  136. $scss = new scssc(); 
  137. $scss->setImportPaths($this->dir); 
  138. $this->scss = $scss; 
  139.  
  140. /** 
  141. * Helper method to serve compiled scss 
  142. * @param string $path Root path 
  143. */ 
  144. static public function serveFrom($path) { 
  145. $server = new self($path); 
  146. $server->serve();