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 {
29 'js' => 'text/javascript',
41 $bits = explode('/', $s);
43 if (empty($bits[0]) || empty($bits[1]) || !isset($this->types[$bits[0]])) {
44 $this->jerr("invalid url");
47 $s = str_replace('/', '-', $bits[1]);
49 $ui = posix_getpwuid(posix_geteuid());
50 $ff = HTML_FlexyFramework::get();
52 $compile = session_save_path() . '/' .
53 $ui['name'] . '-' . $ff->project . '-' . $ff->version . '-'. $bits[0] . 'compile';
55 $fn = $compile . '/'. $s .'.'. $bits[0];
60 if (!file_exists($fn)) {
61 header('Content-Type: '. $this->types[$bits[0]]);
63 echo "// compiled file not found = $fn";
67 $last_modified_time = filemtime($fn);
72 isset($_SERVER['HTTP_IF_MODIFIED_SINCE']) &&
73 strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']) == $last_modified_time
77 isset($_SERVER['HTTP_IF_NONE_MATCH']) &&
78 trim($_SERVER['HTTP_IF_NONE_MATCH']) == md5($fn)
80 header("HTTP/1.1 304 Not Modified");
84 header('Content-Type: '. $this->types[$bits[0]]);
87 header("Pragma: public");
88 header('Content-Length: '. filesize($fn));
89 header('Cache-Control: max-age=2592000, public');
90 header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000));
91 header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time));
92 header('Etag: '. md5($fn));
100 function post($s='') {
107 // a little experimental... we are going to use the same name as the class. for these..
109 trait Pman_Core_Asset_Trait {
115 * {outputJavascriptDir(#Hydra#,#Hydra.js",#.......#)}
121 function outputJavascriptDir($path)
124 $relpath = $this->rootURL . '/' . $path .'/';
125 $ff = HTML_FlexyFramework::get();
126 $dir = $this->rootDir.'/' . $path;
128 $args = func_get_args();
130 if (count($args) < 2) {
131 $ar = glob($dir . '/*.js');
134 foreach($args as $f) {
135 if (strpos($f,'*') > -1) {
137 $ar = array_merge($ar , glob($dir . '/'. $f));
141 $ar[] = $dir .'/'. $f;
145 // cached version?? - how do we decide if it's expired?
146 // while scanning the directory is slow... - it's faster than serving every file...
149 //$path = $this->rootURL ."/Pman/$mod/";
157 foreach($ar as $fn) {
159 // got the 'module file..'
160 $mtime = filemtime($dir . '/'. $f);
161 $maxtime = max($mtime, $maxtime);
162 $arfiles[$fn] = $mtime;
163 $files[] = $relpath . $f . '?ts='.$mtime;
166 ksort($arfiles); // just sort by name so it's consistant for serialize..
168 $ui = posix_getpwuid(posix_geteuid());
171 $compiledir = session_save_path() . '/' .
172 $ui['name'] . '-' . $ff->project . '-' . $ff->version . '-jscompile';
174 if (!file_exists($compiledir)) {
175 mkdir($compiledir,0700,true);
180 $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
181 usort($files, $lsort);
184 if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) {
185 echo "<!-- Javascript compile turned off (isDev on) -->\n";
186 $this->assetArrayToHtml($files,'js');
191 $smod = str_replace('/','.',$path);
193 $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js';
197 // where are we going to write all of this..
198 // This has to be done via a
199 if (!file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
200 require_once 'Pman/Core/JsCompile.php';
201 $x = new Pman_Core_JsCompile();
203 $x->pack($arfiles,$compiledir.'/'.$output, false);
205 if (!file_exists($compiledir.'/'.$output) ||
206 !filesize($compiledir.'/'.$output)) {
207 echo "<!-- compile did not generate files : {$compiledir}/{$output} -->\n";
208 $this->assetArrayToHtml($files,'js');
213 // echo "<!-- file already exists: {$basedir}/{$output} -->\n";
215 //$this->arrayToJsInclude( $files);
216 $this->assetArrayToHtml( array(
217 $this->baseURL.'/Asset/js/'. $output,
223 function assetArrayToHtml($ar, $type)
228 echo '<script type="text/javascript" src="'. $f. '"></script>'."\n";
231 echo '<link rel="stylesheet" href="'. $f. '"/>'."\n";
242 * {outputCssDir(#{Hydra/templates/images/css/#,#Hydra.js",#.......#)}
245 function outputCssDir($path)
248 $relpath = $this->rootURL . '/' . $path .'/';
249 $ff = HTML_FlexyFramework::get();
250 $dir = $this->rootDir.'/' . $path;
252 $args = func_get_args();
254 if (count($args) < 2) {
255 $ar = glob($dir . '/*.css');
258 foreach($args as $f) {
259 if (strpos($f,'*') > -1) {
261 $ar = array_merge($ar , glob($dir . '/'. $f));
264 // what if the fiel does not exist???
265 $ar[] = $dir .'/'. $f;
271 // cached version?? - how do we decide if it's expired?
272 // while scanning the directory is slow... - it's faster than serving every file...
275 //$path = $this->rootURL ."/Pman/$mod/";
278 $missing_files = false;
281 $relfiles = array(); // array of files without the path part...
284 foreach($ar as $fn) {
285 $relfiles[] = substr($fn, strlen($dir)+1);
287 // got the 'module file..'
289 if (!file_exists($dir . '/'. $f)) {
290 echo "<!-- missing {$dir}/{$f} -->\n";
291 $files[] = $relpath . $f . '?ts='.$mtime;
292 $missing_files = true;
296 $mtime = filemtime($dir . '/'. $f);
297 $maxtime = max($mtime, $maxtime);
298 $arfiles[$fn] = $mtime;
304 if ($missing_files) {
305 $this->assetArrayToHtml($files, 'css');
311 //print_r($relfiles);
313 $ui = posix_getpwuid(posix_geteuid());
316 $compiledir = session_save_path() . '/' .
317 $ui['name'] . '-' . $ff->project . '-'. $ff->version . '-csscompile';
319 if (!file_exists($compiledir)) {
320 mkdir($compiledir,0700,true);
325 //$lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;');
326 //usort($files, $lsort);
329 if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) {
330 echo "<!-- CSS compile turned off (isDev on) -->\n";
331 $this->assetArrayToHtml($files,'css');
336 $smod = str_replace('/','.',$path);
338 $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.css';
342 // where are we going to write all of this..
343 // This has to be done via a
344 if (true || !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) {
348 require_once 'HTML/CSS/Minify.php';
349 $x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles);
351 file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.'/Asset/css'));
353 if (!file_exists($compiledir.'/'.$output) ||
354 !filesize($compiledir.'/'.$routput)) {
355 echo "<!-- compile did not generate files : {$compiledir}/{$output} -->\n";
356 $this->assetArrayToHtml($files,'css');
361 // echo "<!-- file already exists: {$basedir}/{$output} -->\n";
363 //$this->arrayToJsInclude( $files);
364 $this->assetArrayToHtml( array(
365 $this->baseURL.'/Asset/css/'. $output,