/
sfLESS.class.php
309 lines (273 loc) · 6.77 KB
/
sfLESS.class.php
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
<?php
/*
* This file is part of the sfLESSPlugin.
* (c) 2010 Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* sfLESS is helper class to provide LESS compiling in symfony projects.
*
* @package sfLESSPlugin
* @subpackage lib
* @author Konstantin Kudryashov <ever.zet@gmail.com>
* @version 1.0.0
*/
class sfLESS
{
/**
* Array of LESS styles
*
* @var array
*/
protected static $results = array();
/**
* Errors of compiler
*
* @var array
*/
protected static $errors = array();
/**
* Current LESS file to be parsed. This var used to help output errors in callCompiler()
*
* @var string
*/
protected $currentFile;
/**
* LESS configuration manager
*
* @var LESSConfig
*/
protected $config;
/**
* Constructor
*
* @param LESSConfig $config configuration manager
*/
public function __construct(LESSConfig $config)
{
$this->config = $config;
}
/**
* Returns configuration manager
*
* @return LESSConfig configurator instance
*/
public function getConfig()
{
return $this->config;
}
/**
* Returns array of compiled styles info
*
* @return array
*/
public static function getCompileResults()
{
return self::$results;
}
/**
* Returns array of compiled styles errors
*
* @return array
*/
public static function getCompileErrors()
{
return self::$errors;
}
/**
* Returns all CSS files under the CSS directory
*
* @return array an array of CSS files
*/
public function findCssFiles()
{
return sfFinder::type('file')
->exec(array('sfLESSUtils', 'isCssLessCompiled'))
->name('*.css')
->in($this->config->getCssPaths());
}
/**
* Returns all LESS files under the LESS directories
*
* @return array an array of LESS files
*/
public function findLessFiles()
{
return sfFinder::type('file')
->name('*.less')
->discard('_*')
->follow_link()
->in($this->config->getLessPaths());
}
/**
* Returns CSS file path by its LESS alternative
*
* @param string $lessFile LESS file path
*
* @return string CSS file path
*/
public function getCssPathOfLess($lessFile)
{
return str_replace(
array($this->config->getLessPaths(), '.less'),
array($this->config->getCssPaths(), '.css'),
$lessFile
);
}
/**
* Compiles LESS file to CSS
*
* @param string $lessFile a LESS file
*
* @return boolean true if succesfully compiled & false in other way
*/
public function compile($lessFile)
{
// Creates timer
$timer = new sfTimer;
// Gets CSS file path
$cssFile = $this->getCssPathOfLess($lessFile);
// Checks if path exists & create if not
if (!is_dir(dirname($cssFile)))
{
mkdir(dirname($cssFile), 0777, true);
// PHP workaround to fix nested folders
chmod(dirname($cssFile), 0777);
}
// Is file compiled
$isCompiled = false;
// If we check dates - recompile only really old CSS
if ($this->config->isCheckDates())
{
try
{
$d = new sfLESSDependency(sfConfig::get('sf_web_dir'),
sfConfig::get('app_sf_less_plugin_check_dependencies', false));
if (!is_file($cssFile) || $d->getMtime($lessFile) > filemtime($cssFile))
{
$isCompiled = $this->callCompiler($lessFile, $cssFile);
}
}
catch (Exception $e)
{
$isCompiled = false;
}
}
else
{
$isCompiled = $this->callCompiler($lessFile, $cssFile);
}
// Adds debug info to debug array
self::$results[] = array(
'lessFile' => $lessFile,
'cssFile' => $cssFile,
'compTime' => $timer->getElapsedTime(),
'isCompiled' => $isCompiled
);
return $isCompiled;
}
/**
* Calls current LESS compiler for single file
*
* @param string $lessFile a LESS file
* @param string $cssFile a CSS file
*
* @return boolean true if succesfully compiled & false in other way
*/
public function callCompiler($lessFile, $cssFile)
{
// Setting current file. We will output this var if compiler throws error
$this->currentFile = $lessFile;
// Do not try to change the permission of an existing file which we might not own
$setPermission = !is_file($cssFile);
// Call compiler
$buffer = $this->callLesscCompiler($lessFile, $cssFile);
// Checks if compiler returns false
if (false === $buffer)
{
return $buffer;
}
// Compress CSS if we use compression
if ($this->config->isUseCompression())
{
$buffer = sfLESSUtils::getCompressedCss($buffer);
}
// Add compiler header to CSS & writes it to file
file_put_contents($cssFile, sfLESSUtils::getCssHeader() . "\n\n" . $buffer);
if ($setPermission)
{
// Set permissions for fresh files only
chmod($cssFile, 0666);
}
// Setting current file to null
$this->currentFile = null;
return true;
}
/**
* Calls lessc compiler for LESS file
*
* @param string $lessFile a LESS file
* @param string $cssFile a CSS file
*
* @return string output
*/
public function callLesscCompiler($lessFile, $cssFile)
{
// Compile with lessc
$fs = new sfFilesystem;
$command = sprintf('lessc "%s" "%s"', $lessFile, $cssFile);
if ('1.3.0' <= SYMFONY_VERSION)
{
try
{
$fs->execute($command, null, array($this, 'throwCompilerError'));
}
catch (RuntimeException $e)
{
return false;
}
}
else
{
$fs->sh($command);
}
return file_get_contents($cssFile);
}
/**
* Returns true if compiler can throw RuntimeException
*
* @return boolean
*/
public function canThrowExceptions()
{
return (('prod' !== sfConfig::get('sf_environment') || !sfConfig::get('sf_app')) &&
!(sfConfig::get('sf_web_debug') && sfConfig::get('app_sf_less_plugin_toolbar', true))
);
}
/**
* Throws formatted compiler error
*
* @param string $line error line
*
* @return boolean
*/
public function throwCompilerError($line)
{
// Generate error description
$errorDescription = sprintf("LESS parser error in \"%s\":\n\n%s", $this->currentFile, $line);
// Adds error description to list of errors
self::$errors[$this->currentFile] = $errorDescription;
// Throw exception if allowed & log error otherwise
if ($this->canThrowExceptions())
{
throw new sfException($errorDescription);
}
else
{
sfContext::getInstance()->getLogger()->err($errorDescription);
}
return false;
}
}