/
plugin.css.php
executable file
·255 lines (191 loc) · 6.09 KB
/
plugin.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
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
<?php
include('csscompiler.php');
/*
Class:
CSS
The goal of this plugin is to minimize the amount of needed requests for CSS-files. The goal is to keep the number of requests down to 2.
This plugin uses the <CSSCompiler> to compile CSS files.
*/
class CSS extends Plugin
{
// Property: CSS::$cache
// The path to the cache, used in <Cache>-objects. Will always hold the value of <paths.css.cache>
private $cache;
// Property: CSS::$url_dir
// The directory for which requests are pointed to. Will always hold the value of <paths.urls.css>
private $url_dir;
// Property: CSS::$packaged_dir
// The directory for which packaged css requests go to. Used for production
private $packaged_dir;
// Property: CSS::$force_update
// A boolean value set to true if CSS files should be updated on every request, nifty for development enivroments. Holds the value of <plugins.css.force_update>.
// However, if set to false, the compiled CSS file will still be updated if the source CSS is updated, but if any dependency is updated, it will not get updated.
private $force_update;
// Property: CSS::$is_package
// Boolean that will be true if the file requested is a packaged file
private $is_package = false;
/*
Constructor
Initialize everything. Will also call <CSS::loadSiteCSS>.
*/
public function __construct()
{
list($dir, $url_dir, $cache, $packaged_dir, $force_update, $release_tag)
= Current::$config->gets('paths.top', 'paths.urls.css', 'plugins.css.cache',
'paths.urls.css_packaged', 'plugins.css.force_update', 'release_tag');
CSSCompiler::setDir($dir);
$this->url_dir = COWL_BASE . $url_dir;
$this->packaged_dir = COWL_BASE . $packaged_dir;
$this->cache = 'tag.' . $release_tag . '.' . $cache;
$this->force_update = $force_update;
Current::$request->setInfo('css', array());
}
/*
Method:
CSS::commandRun
Get the commands specified and CSS packages
*/
public function postCommandRun(Command $command, $method, $args)
{
$mode = Current::$config->get('mode');
$packages = $command->getCSS();
if ( $mode == 'production' )
{
$this->packageForProduction($packages);
}
else
{
$this->packageForDevelopment($packages);
}
}
private function packageForDevelopment($packages)
{
$assets = Current::$config->get('css');
$files = Current::$request->getInfo('css');
// Merge all files into an array
foreach ( $packages as $package )
{
$files = array_merge($files, $assets[$package]);
}
list($app_css_path, $css_path) = Current::$config->gets('paths.app_css', 'paths.urls.css');
// Transform into correct paths
foreach ( $files as $key => $file )
{
$files[$key] = Cowl::url(str_replace($app_css_path, $css_path, $file));
}
Current::$request->setInfo('css', $files);
}
private function packageForProduction($packages)
{
$files = Current::$request->getInfo('css');
foreach ( $packages as $package )
{
$files[] = $this->packaged_dir . $package . '.css';
}
Current::$request->setInfo('css', $files);
}
/*
Method:
CSS::getFilesForPackage
Get files for a certain package. Returns false if it doesn't exist.
Parameters:
$package - The package name, as defined in the 'css' => array() in your config file
Returns:
An array of files, or false if it doesn't exist
*/
private function getFilesForPackage($package)
{
$assets = Current::$config->get('css');
if ( ! isset($assets[$package]) )
{
return false;
}
return $assets[$package];
}
/*
Method:
CSS:prePathParse
Checks if is a request to a release CSS file and routes it accordingly.
Packaging if necessesary.
*/
public function prePathParse(Controller $controller, StaticServer $server)
{
$path = strtolower($server->getPath());
// Path begins with release path
if ( strncmp($path, $this->packaged_dir, strlen($this->packaged_dir)) !== 0 )
return;
// Get package name from request path
$package = preg_replace('#\.css$#', '', substr($path, strlen($this->packaged_dir)));
$filepath = $this->buildPackage($package);
$server->setPath($filepath);
$controller->setPath($filepath);
// Prevent the path from being changed now
$server->lockPath();
$this->is_package = true;
}
/*
Method:
CSS::preStaticServe
This hook will be called when a CSS file has been requested
Parameters:
$args - The $argv array of the request.
*/
public function preStaticServe(StaticServer $server)
{
// If file was a package file, don't do anything
if ( $this->is_package ) return;
// If the type isn't css we don't touch it
if ( $server->getType() != 'css' )
return;
$path = $server->getPath();
$cache_path = $this->cache . '.' . preg_replace('#\W#', '', $path);
// Compile and cache CSS file.
$cache = new FileCache($cache_path, $path);
$cache->setExtension('css');
if ( $cache->isOutDated() || $this->force_update )
{
$contents = file_get_contents($path);
$compiler = new CSSCompiler($contents);
$updated = $compiler->compile();
$cache->update($updated);
}
// Change the path to be the cached file instead
$server->setPath($cache->getFile());
}
private function buildPackage($package)
{
$files = $this->getFilesForPackage($package);
// Package actually exists
if ( ! $files )
return;
$cache_path = $this->cache . '.' . $package;
$cache = new Cache($cache_path, 60*60*24*365);
$cache->setExtension('css');
if ( $cache->isOutDated() || $this->force_update )
{
// Make the compiler compile all the files together
$contents = '';
foreach ( $files as $file )
{
$contents .= file_get_contents($file);
}
$compiler = new CSSCompiler($contents);
$compiler->compile();
$updated = $compiler->compress();
$cache->update($updated);
}
return $cache->getFile();
}
/*
Method:
buildAll
Build all assets into production files.
*/
public function buildAll($force = true)
{
$this->force_update = $force;
$packages = array_keys(Current::$config->get('css'));
$this->packageForProduction($packages);
array_walk($packages, array($this, 'buildPackage'));
}
}