/
plugin.js.php
executable file
·262 lines (201 loc) · 6.02 KB
/
plugin.js.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
256
257
258
259
260
261
262
<?php
include('jsmin.php');
class JS extends Plugin
{
// Property: JS::$force_update
// Force update compilation and compression of JS files.
private $force_update = false;
// Property: JS::$files
// Array of files for the request
private $files = array();
// Property: JS::$packaged_dir
// The directory for which packaged JS requests go to. Used for production
private $packaged_dir;
// Property: JS::$is_package
private $is_package = true;
public function __construct()
{
// Just set some paths and config options
list($packaged_dir, $cache, $release_tag, $force_update) = Current::$config->gets(
'paths.urls.js_packaged', 'plugins.js.cache', 'release_tag',
'plugins.js.force_update'
);
$this->packaged_dir = $packaged_dir;
$this->cache = 'tag.' . $release_tag . '.' . $cache;
$this->force_update = $force_update;
}
/*
Method:
JS::commandRun
This method is called after a command is run to determine which JS files to be included.
*/
public function postCommandRun(Command $command, $method, $request)
{
$mode = Current::$config->get('mode');
$js_packages = $command->getJS();
if ( $mode == 'production' )
{
$this->packageForProduction($js_packages);
}
else
{
$this->packageForDevelopment($js_packages, $command, $request);
}
// This cannot change with the new packaging
// Unless we package all pages into one file... which would be nice. But cost in performance?
// --
// See if this command has page.commandname.js
$page_name = 'app/' . $request->app_directory . 'page.' . end($request->pieces) . '.js';
$page_name = $this->parsePath($page_name);
if ( file_exists($page_name) )
{
$this->files[] = $page_name;
Current::$request->setInfo('js_fire', substr(get_class($command), 0, -strlen('command')));
Current::$request->setInfo('js_page_path', Cowl::url($page_name));
}
// --
$this->setFiles();
}
/*
Method:
JS::packageForDevelopment
Package files for development. This will just include each file seperately.
Parameters:
(array) $js_packages - Packages from assets.php
(Command) $command - The command that was run
(RequestData) $request - Request data for current request
*/
public function packageForDevelopment($js_packages, Command $command, RequestData $request)
{
$packages = Current::$config->get('js');
// Just flatten the packages files to include them without magic
foreach ( $js_packages as $package )
{
$this->files = array_merge($this->files, $packages[$package]);
}
// Parse JS paths
$this->files = array_map(array($this, 'parsePath'), $this->files);
}
/*
Method:
JS::packageForProduction
Package files for production. This method takes the package names and transforms
them based on how the config file wants them to look. E.g:
$ js/release_core.js
Parameters:
(array) $js_packages - All packages from assets.php
*/
public function packageForProduction($js_packages)
{
$packages = Current::$config->get('js');
$this->files = Current::$request->getInfo('js');
foreach ( $js_packages as $package )
{
$this->files[] = $this->packaged_dir . $package . '.js';
}
}
/*
Method:
JS::setFiles
Set files from JS::$files to the request info.
*/
private function setFiles()
{
if ( ! is_array($this->files) )
return;
foreach ( $this->files as $file )
{
Current::$request->setInfo('js[]', $file);
}
}
/*
Method:
JS::getFilesForPackage
Get files for a certain package. Returns false if it doesn't exist.
Parameters:
$package - The package name, as defined in the 'js' => 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('js');
if ( ! isset($assets[$package]) )
{
return false;
}
return $assets[$package];
}
/*
Method:
JS:prePathParse
Checks if is a request to a release JS file and routes it accordingly.
Packaging if necessesary.
*/
public function prePathParse(Controller $controller, StaticServer $server)
{
$path = strtolower($server->getPath());
$url_path = COWL_BASE . $this->packaged_dir;
// Path begins with release path, then we build it if need be
if ( strncmp($path, $url_path, strlen($url_path)) !== 0 )
return;
// Get package name from request path
$package = preg_replace('#\.js$#', '', substr($path, strlen($url_path)));
$filepath = $this->buildPackage($package);
// No package here
if ( ! $filepath )
return;
$server->setPath($filepath);
$controller->setPath($filepath);
$server->lockPath();
$this->is_package = true;
}
/*
Method:
JS::buildPackage
Build package. Takes the package name, minifies and combines the related files to one file.
Returns:
The path to the file on disk
*/
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('js');
if ( $cache->isOutDated() || $this->force_update )
{
// Make the compiler compile all the files together
$contents = '';
foreach ( $files as $file )
{
$contents .= file_get_contents($this->parsePath($file));
}
$updated = JSMin::minify($contents);
$cache->update($updated);
}
return $cache->getFile();
}
/*
Method:
JS::buildAll
Build all packages at once. Useful for automated building.
*/
public function buildAll($force = true)
{
$this->force_update = $force;
$packages = array_keys(Current::$config->get('js'));
$this->packageForProduction($packages);
array_walk($packages, array($this, 'buildPackage'));
}
private function parsePath($path)
{
list($app_js, $system_js) = Current::$config->gets('paths.app_js', 'paths.system_js');
$path = preg_replace('#^app/#', $app_js, $path);
$path = preg_replace('#^cowl/#', $system_js, $path);
return $path;
}
}