1 <?php
2
3 use Leafo\ScssPhp\Compiler;
4
5 6 7
8 class rex_scss_compiler
9 {
10 protected $root_dir;
11 protected $scss_file;
12 protected $css_file;
13 protected $formatter;
14 protected ;
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 42
43 public function setFormatter($value)
44 {
45 $this->formatter = $value;
46 }
47
48 public function ($value = true)
49 {
50 $this->strip_comments = $value;
51 }
52
53 54 55 56 57 58
59 public function compile()
60 {
61
62 ignore_user_abort(true);
63
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
89
90
91
92 $scss_compiler->setFormatter($this->formatter);
93
94
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
105 try {
106
107 $string_css = $scss_compiler->compile($string_sass) . "\n";
108
109
110
111
112 rex_file::put($this->css_file, $string_css);
113 } catch (Exception $e) {
114
115 echo $e->getMessage();
116 exit();
117 }
118 }
119 }
120