forked from nizsheanez/yii2-asset-converter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Less.php
42 lines (38 loc) · 1.32 KB
/
Less.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
<?php
namespace nizsheanez\assetConverter;
use Yii;
use yii\caching\FileCache;
class Less extends Parser
{
public $auto = false;
/**
* Parse a Less file to CSS
*/
public function parse($src, $dst, $options)
{
$this->auto = isset($options['auto']) ? $options['auto'] : $this->auto;
try {
if ($this->auto) {
/* @var FileCache $cacheMgr */
$cacheMgr = Yii::createObject('yii\caching\FileCache');
$cacheMgr->init();
$cacheId = 'less#' . $dst;
$cache = $cacheMgr->get($cacheId);
if ($cache === false || (@filemtime($dst) < @filemtime($src))) {
$cache = $src;
}
$less = new \lessc();
$newCache = $less->cachedCompile($cache);
if (!is_array($cache) || ($newCache["updated"] > $cache["updated"])) {
$cacheMgr->set($cacheId, $newCache);
file_put_contents($dst, $newCache['compiled']);
}
} else {
$less = new \lessc();
$less->compileFile($src, $dst);
}
} catch (Exception $e) {
throw new Exception(__CLASS__ . ': Failed to compile less file : ' . $e->getMessage() . '.');
}
}
}