-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathConverter.php
97 lines (74 loc) · 2.28 KB
/
Converter.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
<?php
/**
* @author: Singrana
* @email: [email protected]
* Date: 24.04.2014
*/
namespace singrana\assets;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\FileHelper;
class Converter extends \yii\web\AssetConverter
{
protected $defaultParsersOptions =
[
'less' =>
[
'class' => 'singrana\assets\converter\Less',
'output' => 'css',
'options' =>
[
'auto' => true
]
]
];
public $parsers = [];
public $force = false; // Set to true of need always rebuild less files
public function convert($asset, $basePath)
{
$this->parsers = ArrayHelper::merge($this->defaultParsersOptions, $this->parsers);
$pos = strrpos($asset, '.');
if ($pos === false)
return parent::convert($asset, $basePath);
$ext = substr($asset, $pos + 1);
if (!isset($this->parsers[$ext]))
return parent::convert($asset, $basePath);
$resultFile = FileHelper::normalizePath(DIRECTORY_SEPARATOR . substr($asset, 0, $pos + 1) . $this->parsers[$ext]['output']);
$from = $basePath . DIRECTORY_SEPARATOR . $asset;
$to = $basePath . $resultFile;
if (!$this->needRecompile($from, $to))
return trim($resultFile, DIRECTORY_SEPARATOR);
$this->checkDestinationDir($basePath, $resultFile);
$asConsoleCommand = isset($this->parsers[$ext]['asConsoleCommand']) && $this->parsers[$ext]['asConsoleCommand'];
if ($asConsoleCommand)
{
if (isset($this->commands[$ext]))
{
list ($distExt, $command) = $this->commands[$ext];
$this->runCommand($command, $basePath, $asset, $resultFile);
}
}
else
{
$class=$this->parsers[$ext]['class'];
$parser = new $class($this->parsers[$ext]['options']);
$parserOptions = isset($this->parsers[$ext]['options']) ? $this->parsers[$ext]['options'] : array();
$parser->parse($from, $to, $parserOptions);
}
if (YII_DEBUG)
Yii::info("Converted $asset into $resultFile ", __CLASS__);
//$resultFile=str_replace(Yii::getAlias('@webroot'), '', $to);
$resultFile = trim(FileHelper::normalizePath($resultFile, '/'), '/');
return $resultFile;
}
public function needRecompile($from, $to)
{
return $this->force || (@filemtime($to) < filemtime($from));
}
public function checkDestinationDir($basePath, $file)
{
$distDir = dirname($basePath . $file);
if (!is_dir($distDir))
mkdir($distDir, 0777, true);
}
}