1 <?php
  2 
  3 use Leafo\ScssPhp\Compiler;
  4 
  5 /**
  6  * @package redaxo\be-style
  7  */
  8 class rex_scss_compiler
  9 {
 10     protected $root_dir;
 11     protected $scss_file;
 12     protected $css_file;
 13     protected $formatter;
 14     protected $strip_comments;
 15 
 16     public function __construct()
 17     {
 18         $this->root_dir = rex_path::addon('be_style');
 19         $this->scss_file = rex_path::addon('be_style', 'assets') . 'styles.scss';
 20         $this->css_file = rex_path::addon('be_style', 'assets') . 'styles.css';
 21         $this->formatter = 'Leafo\ScssPhp\Formatter\Compressed';
 22         $this->strip_comments = true;
 23     }
 24 
 25     public function setRootDir($value)
 26     {
 27         $this->root_dir = $value;
 28     }
 29 
 30     public function setScssFile($value)
 31     {
 32         $this->scss_file = $value;
 33     }
 34 
 35     public function setCssFile($value)
 36     {
 37         $this->css_file = $value;
 38     }
 39 
 40     /*
 41      * @param string $value scss_formatter (default) or scss_formatter_nested or scss_formatter_compressed
 42     */
 43     public function setFormatter($value)
 44     {
 45         $this->formatter = $value;
 46     }
 47 
 48     public function setStripComments($value = true)
 49     {
 50         $this->strip_comments = $value;
 51     }
 52 
 53     /*
 54      * @param string $scss_folder source folder where you have your .scss files
 55      * @param string $scss_global_file
 56      * @param string $format_style CSS output format
 57      * @param bool $strip_comments
 58      */
 59     public function compile()
 60     {
 61         // go on even if user "stops" the script by closing the browser, closing the terminal etc.
 62         ignore_user_abort(true);
 63         // set script running time to unlimited
 64         set_time_limit(0);
 65 
 66         $root_dir = $this->root_dir;
 67 
 68         $scss_compiler = new Compiler();
 69         $scss_compiler->setNumberPrecision(10);
 70         $scss_compiler->stripComments = $this->strip_comments;
 71 
 72         $scss_compiler->addImportPath(function ($path) use ($root_dir) {
 73             $path = $root_dir . $path . '.scss';
 74 
 75             $path_parts = pathinfo($path);
 76             $underscore_file = $path_parts['dirname'] . '/_' . $path_parts['basename'];
 77 
 78             if (file_exists($underscore_file)) {
 79                 $path = $underscore_file;
 80             }
 81 
 82             if (!file_exists($path)) {
 83                 return null;
 84             }
 85 
 86             return $path;
 87         });
 88         // set the path to your to-be-imported mixins. please note: custom paths are coming up on future releases!
 89         //$scss_compiler->setImportPaths($scss_folder);
 90 
 91         // set css formatting (normal, nested or minimized), @see http://leafo.net/scssphp/docs/#output_formatting
 92         $scss_compiler->setFormatter($this->formatter);
 93 
 94         // get .scss's content, put it into $string_sass
 95         $string_sass = '';
 96         if (is_array($this->scss_file)) {
 97             foreach ($this->scss_file as $scss_file) {
 98                 $string_sass .= rex_file::get($scss_file);
 99             }
100         } else {
101             $string_sass = rex_file::get($this->scss_file);
102         }
103 
104         // try/catch block to prevent script stopping when scss compiler throws an error
105         try {
106             // compile this SASS code to CSS
107             $string_css = $scss_compiler->compile($string_sass) . "\n";
108 
109             // $string_css = csscrush_string($string_css, $options = array('minify' => true));
110 
111             // write CSS into file with the same filename, but .css extension
112             rex_file::put($this->css_file, $string_css);
113         } catch (Exception $e) {
114             // here we could put the exception message, but who cares ...
115             echo $e->getMessage();
116             exit();
117         }
118     }
119 }
120