-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCI_less_pre_sys.php
More file actions
106 lines (69 loc) · 2.33 KB
/
CI_less_pre_sys.php
File metadata and controls
106 lines (69 loc) · 2.33 KB
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
<?php
//require_once dirname(__file__) . '/../vendor/less/lessc.inc.php';
class CI_less_pre_sys
{
protected $path, $lessPath, $files, $env, $minv;
function __construct ()
{
$path = dirname(dirname(dirname(dirname(dirname(__file__)))));
$this->path = rtrim($path, '/') . DIRECTORY_SEPARATOR;
}
public function initialize ($args)
{
$this->lessPath = rtrim($args[0], '/') . DIRECTORY_SEPARATOR;;
$this->files = $args[1];
$this->env = strtoupper($args[2]);
$this->minv = $args[3];
if (!empty($this->files) && is_array($this->files))
{
foreach ($this->files as $lessFile => $outputFileName)
{
$lessFullPath = $this->path . $this->lessPath . $lessFile;
$outputFullPath = $this->path . $this->lessPath . $outputFileName;
if (file_exists($lessFullPath))
{
if (!file_exists($outputFullPath)) $this->_compileLessFile($lessFullPath, $outputFullPath);
else
{
// if new min version, then it will rebuild
// if in dev mode, will recompile the less each page load
//if (self::_checkRebuild($this->path . $this->lessPath, $this->minv) || $this->env == 'DEVELOPMENT')
if (self::_checkRebuild($this->path . $this->lessPath, $this->minv))
{
$this->_compileLessFile($lessFullPath, $outputFullPath);
}
}
}
}
}
}
private function _compileLessFile ($file, $output)
{
if (file_exists($output)) @unlink($output); // removes old output file
$cmd = "lessc --global-var=\"domain='{$_SERVER['HTTP_HOST']}'\" {$file} {$output}";
system($cmd);
if (!empty($this->minv)) self::_writeMinV($this->path . $this->lessPath, $this->minv);
return true;
}
private static function _writeMinV ($path, $minv)
{
$file = $path . '.min_version';
$t = touch($file);
if ($t === false) throw new Exception("Unable to create ${file}");
$w = file_put_contents($file, $minv);
if ($w === false) throw new Exception("Unable to write to {$file}!");
return true;
}
private static function _getLastMinV ($path)
{
$file = $path . '.min_version';
return intval(file_get_contents($file));
}
private static function _checkRebuild ($path, $currentV)
{
if (empty($currentV)) return false;
$lastV = self::_getLastMinV($path);
if ((int) $currentV > (int) $lastV) return true;
return false;
}
}