4 * Generic cached assset server... -- No security om this.. should only return compressed CSS/JS
6 * Does a few tricks with headers to improve caching...
9 * Also includes code to generate assets...
11 * methods outputJavascriptDir / outputCssDir generate links to
12 * BASEURL/Asset/css/xxxx.yyy.zzz
13 * BASEURL/Asset/js/xxxx.yyy.zzz
16 * we deliver the file from
17 * SESSION-DIR/{$www-user}-{$ff->project}-$ff->version}-{js|css}-compile/{filename}/PATH';
22 require_once 'Pman.php';
24 class Pman_Core_Asset extends Pman {
28 'js' => 'text/javascript',
40 $bits = explode('/', $s);
42 if (empty($bits[0]) || empty($bits[1]) || !isset($this->types[$bits[0]])) {
43 $this->jerr("invalid url");
46 $s = str_replace('/', '-', $bits[1]);
48 $ui = posix_getpwuid(posix_geteuid());
49 $ff = HTML_FlexyFramework::get();
51 $compile = session_save_path() . '/' .
52 $ui['name'] . '-' . $ff->project . '-' . $ff->version . '-'. $bits[0] . 'compile';
54 $fn = $compile . '/'. $s .'.'. $bits[0];
59 if (!file_exists($fn)) {
60 header('Content-Type: '. $this->types[$bits[0]]);
62 echo "// compiled file not found = $fn";
66 $last_modified_time = filemtime($fn);
71 isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
72 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
76 isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
77 trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($fn)
79 header("HTTP/1.1 304 Not Modified");
83 header('Content-Type: '. $this->types[$bits[0]]);
86 header("Pragma: public");
87 header('Content-Length: '. filesize($fn));
88 header('Cache-Control: max-age=2592000, public');
89 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
90 header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
91 header('Etag: '. md5($fn));
99 function post($s='') {
106 // a little experimental... we are going to use the same name as the class. for these..
108 trait Pman_Core_Asset_Trait {
114 * {outputJavascriptDir(#Hydra#,#Hydra.js",#.......#)}
120 function outputJavascriptDir($path)
123 $relpath = $this->rootURL . '/' . $path .'/';
124 $ff = HTML_FlexyFramework::get();
125 $dir = $this->rootDir.'/' . $path;
127 $args = func_get_args();
129 if (count($args) < 2) {
130 $ar = glob($dir . '/*.js');
133 foreach($args as $f) {
134 if (strpos($f,'*') > -1) {
136 $ar = array_merge($ar , glob($dir . '/'. $f));
140 $ar[] = $dir .'/'. $f;
144 // cached version?? - how do we decide if it's expired?
145 // while scanning the directory is slow... - it's faster than serving every file...
148 //$path = $this->rootURL ."/Pman/$mod/";
156 foreach($ar as $fn) {
158 // got the 'module file..'
159 $mtime = filemtime($dir . '/'. $f);
160 $maxtime = max($mtime, $maxtime);
161 $arfiles[$fn] = $mtime;
162 $files[] = $relpath . $f . '?ts='.$mtime;
165 ksort($arfiles); // just sort by name so it's consistant for serialize..
167 $ui = posix_getpwuid(posix_geteuid());
170 $compiledir = session_save_path() . '/' .
171 $ui['name'] . '-' . $ff->project . '-' . $ff->version . '-jscompile';
173 if (!file_exists($compiledir)) {
174 mkdir($compiledir,0700,true);
179 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
180 usort($files, $lsort);
183 if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) {
184 echo "<!-- Javascript compile turned off (isDev on) -->\n";
185 $this->assetArrayToHtml($files,'js');
190 $smod = str_replace('/','.',$path);
192 $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js';
196 // where are we going to write all of this..
197 // This has to be done via a
198 if (!file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
199 require_once 'Pman/Core/JsCompile.php';
200 $x = new Pman_Core_JsCompile();
202 $x->pack($arfiles,$compiledir.'/'.$output, false);
204 if (!file_exists($compiledir.'/'.$output) ||
205 !filesize($compiledir.'/'.$output)) {
206 echo "<!-- compile did not generate files : ". basename($compiledir) ."/{$output} -->\n";
207 $this->assetArrayToHtml($files,'js');
212 // echo "<!-- file already exists: {$basedir}/{$output} -->\n";
214 //$this->arrayToJsInclude( $files);
215 $this->assetArrayToHtml( array(
216 $this->baseURL.'/Asset/js/'. $output,
222 function assetArrayToHtml($ar, $type)
227 echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
230 echo '<link rel="stylesheet" href="'. $f. '"/>'."\n";
241 * {outputCssDir(#{Hydra/templates/images/css/#,#Hydra.js",#.......#)}
244 function outputCssDir($path)
247 $relpath = $this->rootURL . '/' . $path .'/';
248 $ff = HTML_FlexyFramework::get();
249 $dir = $this->rootDir.'/' . $path;
251 $args = func_get_args();
253 if (count($args) < 2) {
254 $ar = glob($dir . '/*.css');
257 foreach($args as $f) {
258 if (strpos($f,'*') > -1) {
260 $ar = array_merge($ar , glob($dir . '/'. $f));
263 // what if the fiel does not exist???
264 $ar[] = $dir .'/'. $f;
270 // cached version?? - how do we decide if it's expired?
271 // while scanning the directory is slow... - it's faster than serving every file...
274 //$path = $this->rootURL ."/Pman/$mod/";
277 $missing_files = false;
280 $relfiles = array(); // array of files without the path part...
283 foreach($ar as $fn) {
284 $relfiles[] = substr($fn, strlen($dir)+1);
286 // got the 'module file..'
288 if (!file_exists($dir . '/'. $f)) {
289 echo "<!-- missing {$relpath}{$f} -->\n";
290 $files[] = $relpath . $f . '?ts=0';
291 $missing_files = true;
295 $mtime = filemtime($dir . '/'. $f);
296 $maxtime = max($mtime, $maxtime);
297 $arfiles[$fn] = $mtime;
298 $files[] = $relpath . $f . '?ts='.$mtime;
303 if ($missing_files) {
304 $this->assetArrayToHtml($files, 'css');
310 //print_r($relfiles);
312 $ui = posix_getpwuid(posix_geteuid());
315 $compiledir = session_save_path() . '/' .
316 $ui['name'] . '-' . $ff->project . '-'. $ff->version . '-csscompile';
318 if (!file_exists($compiledir)) {
319 mkdir($compiledir,0700,true);
324 //$lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
325 //usort($files, $lsort);
328 if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) {
329 echo "<!-- CSS compile turned off (isDev on) -->\n";
330 $this->assetArrayToHtml($files,'css');
335 $smod = str_replace('/','.',$path);
337 $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.css';
341 // where are we going to write all of this..
342 // This has to be done via a
343 if (true || !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
347 require_once 'HTML/CSS/Minify.php';
348 $x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles);
350 file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.'/Asset/css'));
352 if (!file_exists($compiledir.'/'.$output) ||
353 !filesize($compiledir.'/'.$routput)) {
354 echo "<!-- compile did not generate files : " . basename($compiledir) . "/{$output} -->\n";
355 $this->assetArrayToHtml($files,'css');
360 // echo "<!-- file already exists: {$basedir}/{$output} -->\n";
362 //$this->arrayToJsInclude( $files);
363 $this->assetArrayToHtml( array(
364 $this->baseURL.'/Asset/css/'. $output,