1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
<?php
/* Icinga Web 2 | (c) 2013 Icinga Development Team | GPLv2+ */
namespace Icinga\Web;
use Icinga\Application\Logger;
use Icinga\Util\LessParser;
use Less_Exception_Parser;
/**
* Compile LESS into CSS
*
* Comments will be removed always. lessc is messing them up.
*/
class LessCompiler
{
/**
* lessphp compiler
*
* @var LessParser
*/
protected $lessc;
/**
* Array of LESS files
*
* @var string[]
*/
protected $lessFiles = array();
/**
* Array of module LESS files indexed by module names
*
* @var array[]
*/
protected $moduleLessFiles = array();
/**
* LESS source
*
* @var string
*/
protected $source;
/**
* Path of the LESS theme
*
* @var string
*/
protected $theme;
/**
* Path of the LESS theme mode
*
* @var string
*/
protected $themeMode;
/**
* Create a new LESS compiler
*/
public function __construct()
{
$this->lessc = new LessParser();
}
/**
* Add a Web 2 LESS file
*
* @param string $lessFile Path to the LESS file
*
* @return $this
*/
public function addLessFile($lessFile)
{
$this->lessFiles[] = realpath($lessFile);
return $this;
}
/**
* Add a module LESS file
*
* @param string $moduleName Name of the module
* @param string $lessFile Path to the LESS file
*
* @return $this
*/
public function addModuleLessFile($moduleName, $lessFile)
{
if (! isset($this->moduleLessFiles[$moduleName])) {
$this->moduleLessFiles[$moduleName] = array();
}
$this->moduleLessFiles[$moduleName][] = realpath($lessFile);
return $this;
}
/**
* Get the list of LESS files added to the compiler
*
* @return string[]
*/
public function getLessFiles()
{
$lessFiles = $this->lessFiles;
foreach ($this->moduleLessFiles as $moduleLessFiles) {
$lessFiles = array_merge($lessFiles, $moduleLessFiles);
}
if ($this->theme !== null) {
$lessFiles[] = $this->theme;
}
if ($this->themeMode !== null) {
$lessFiles[] = $this->themeMode;
}
return $lessFiles;
}
/**
* Set the path to the LESS theme
*
* @param ?string $theme Path to the LESS theme
*
* @return $this
*/
public function setTheme($theme)
{
if ($theme === null || (is_file($theme) && is_readable($theme))) {
$this->theme = $theme;
} else {
Logger::error('Can\t load theme %s. Make sure that the theme exists and is readable', $theme);
}
return $this;
}
/**
* Set the path to the LESS theme mode
*
* @param string $themeMode Path to the LESS theme mode
*
* @return $this
*/
public function setThemeMode($themeMode)
{
if (is_file($themeMode) && is_readable($themeMode)) {
$this->themeMode = $themeMode;
} else {
Logger::error('Can\t load theme mode %s. Make sure that the theme mode exists and is readable', $themeMode);
}
return $this;
}
/**
* Instruct the compiler to minify CSS
*
* @return $this
*/
public function compress()
{
$this->lessc->setFormatter('compressed');
return $this;
}
/**
* Render to CSS
*
* @return string
*/
public function render()
{
foreach ($this->lessFiles as $lessFile) {
$this->source .= file_get_contents($lessFile);
}
$moduleCss = '';
$exportedVars = [];
foreach ($this->moduleLessFiles as $moduleName => $moduleLessFiles) {
$moduleCss .= '.icinga-module.module-' . $moduleName . ' {';
foreach ($moduleLessFiles as $moduleLessFile) {
$content = file_get_contents($moduleLessFile);
$pattern = '/^@exports:\s*{((?:\s*@[^:}]+:[^;]*;\s+)+)};$/m';
if (preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) {
foreach ($matches as $match) {
$content = str_replace($match[0], '', $content);
foreach (explode("\n", trim($match[1])) as $line) {
list($name, $value) = explode(':', $line, 2);
$exportedVars[trim($name)] = trim($value, ' ;');
}
}
}
$moduleCss .= $content;
}
$moduleCss .= '}';
}
$this->source .= $moduleCss;
$varExports = '';
foreach ($exportedVars as $name => $value) {
$varExports .= sprintf("%s: %s;\n", $name, $value);
}
// exported vars are injected at the beginning to avoid that they are
// able to override other variables, that's what themes are for
$this->source = $varExports . "\n\n" . $this->source;
if ($this->theme !== null) {
$this->source .= file_get_contents($this->theme);
}
if ($this->themeMode !== null) {
$this->source .= file_get_contents($this->themeMode);
}
try {
return preg_replace(
'/(\.icinga-module\.module-[^\s]+) (#layout\.[^\s]+)/m',
'\2 \1',
$this->lessc->compile($this->source)
);
} catch (Less_Exception_Parser $e) {
$excerpt = substr($this->source, $e->index - 500, 1000);
$lines = [];
$found = false;
$pos = $e->index - 500;
foreach (explode("\n", $excerpt) as $i => $line) {
if ($i === 0) {
$pos += strlen($line);
$lines[] = '.. ' . $line;
} else {
$pos += strlen($line) + 1;
$sep = ' ';
if (! $found && $pos > $e->index) {
$found = true;
$sep = '!! ';
}
$lines[] = $sep . $line;
}
}
$lines[] = '..';
$excerpt = join("\n", $lines);
return sprintf("%s\n%s\n\n\n%s", $e->getMessage(), $e->getTraceAsString(), $excerpt);
}
}
}
|