/
Css.php
207 lines (184 loc) · 6.19 KB
/
Css.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
<?php
/**
* Munee: Optimising Your Assets
*
* @copyright Cody Lundquist 2013
* @license http://opensource.org/licenses/mit-license.php
*/
namespace Munee\Asset\Type;
use Munee\Utils;
use Munee\Asset\Type;
use lessc;
use scssc;
/**
* Handles CSS
*
* @author Cody Lundquist
*/
class Css extends Type
{
/**
* Stores the Request options for this Asset Type
*
* @var array
*/
protected $options = array(
'lessifyAllCss' => false,
'scssifyAllCss' => false
);
/**
* Set additional headers just for CSS
*/
public function getHeaders()
{
$this->response->headerController->headerField('Content-Type', 'text/css');
}
/**
* Checks to see if cache exists and is the latest, if it does, return it
* It also checks to see if this is LESS cache and makes sure all imported files are the latest
*
* @param string $originalFile
* @param string $cacheFile
*
* @return bool|string|array
*/
protected function checkCache($originalFile, $cacheFile)
{
if (! $ret = parent::checkCache($originalFile, $cacheFile)) {
return false;
}
if (Utils::isSerialized($ret, $ret)) {
// Go through each file and make sure none of them have changed
foreach ($ret['files'] as $file => $lastModified) {
if (filemtime($file) > $lastModified) {
return false;
}
}
$ret = serialize($ret);
}
return $ret;
}
/**
* Callback method called before filters are run
*
* Overriding to run the file through LESS if need be.
* Also want to fix any relative paths for images.
*
* @param string $originalFile
* @param string $cacheFile
*
* @throws CompilationException
*/
protected function beforeFilter($originalFile, $cacheFile)
{
if ($this->isLess($originalFile)) {
$less = new lessc();
try {
$compiledLess = $less->cachedCompile($originalFile);
} catch (\Exception $e) {
throw new CompilationException('Error in LESS Compiler', 0, $e);
}
$compiledLess['compiled'] = $this->fixRelativeImagePaths($compiledLess['compiled'], $originalFile);
file_put_contents($cacheFile, serialize($compiledLess));
} elseif ($this->isScss($originalFile)) {
$scss = new scssc();
$scss->addImportPath(pathinfo($originalFile, PATHINFO_DIRNAME));
try {
$compiled = $scss->compile(file_get_contents($originalFile));
} catch (\Exception $e) {
throw new CompilationException('Error in SCSS Compiler', 0, $e);
}
$content = compact('compiled');
$parsedFiles = $scss->getParsedFiles();
$parsedFiles[] = $originalFile;
foreach ($parsedFiles as $file) {
$content['files'][$file] = filemtime($file);
}
file_put_contents($cacheFile, serialize($content));
} else {
$content = file_get_contents($originalFile);
file_put_contents($cacheFile, $this->fixRelativeImagePaths($content, $originalFile));
}
}
/**
* Callback method called after the content is collected and/or cached
* Check if the content is serialized. If it is, we have LESS cache
* and we want to return whats in the `compiled` array key
*
* @param string $content
*
* @return string
*/
protected function afterGetFileContent($content)
{
if (Utils::isSerialized($content, $content)) {
$content = $content['compiled'];
}
return $content;
}
/**
* Check if it's a LESS file or if we should run all CSS through LESS
*
* @param string $file
*
* @return boolean
*/
protected function isLess($file)
{
return 'less' == pathinfo($file, PATHINFO_EXTENSION) || $this->options['lessifyAllCss'];
}
/**
* Check if it's a SCSS file or if we should run all CSS through SCSS
*
* @param string $file
*
* @return boolean
*/
protected function isScss($file)
{
return 'scss' == pathinfo($file, PATHINFO_EXTENSION) || $this->options['scssifyAllCss'];
}
/**
* Fixes relative paths to absolute paths
*
* @param $content
* @param $originalFile
*
* @return string
*
* @throws CompilationException
*/
protected function fixRelativeImagePaths($content, $originalFile)
{
$regEx = '%(url[\\s]*\\()[\\s\'"]*([^\\)\'"]*)[\\s\'"]*(\\))%';
$webroot = $this->request->webroot;
$changedContent = preg_replace_callback($regEx, function ($match) use ($originalFile, $webroot) {
$filePath = trim($match[2]);
// Skip conversion if the first character is a '/' since it's already an absolute path
if ($filePath[0] !== '/') {
$basePath = str_replace($webroot, '', dirname($originalFile));
$basePathParts = array_reverse(array_filter(explode('/', $basePath)));
$numOfRecursiveDirs = substr_count($filePath, '../');
if ($numOfRecursiveDirs > count($basePathParts)) {
throw new CompilationException(
'Error in stylesheet <strong>' . $originalFile .
'</strong>. The following URL goes above webroot: <strong>' . $filePath .
'</strong>'
);
}
$basePathParts = array_slice($basePathParts, $numOfRecursiveDirs);
$basePath = implode('/', array_reverse($basePathParts));
if (! empty($basePath) && $basePath[0] != '/') {
$basePath = '/' . $basePath;
}
$filePath = $basePath . '/' . $filePath;
$filePath = str_replace(array('../', './'), '', $filePath);
}
return $match[1] . $filePath . $match[3];
}, $content);
if (null !== $changedContent) {
$content = $changedContent;
}
return $content;
}
}