X-Git-Url: http://git.roojs.org/?p=Pman.Core;a=blobdiff_plain;f=Asset.php;h=92e43cbba844120cd4a1481d8489fcd7caa342bf;hp=1b356270166180a7543737b75924996dadcdfc02;hb=refs%2Fheads%2Fwip_alan_T5884_add_photo_to_report;hpb=8ebfde44dd6f8042339611a87cea8244d96ecbe5 diff --git a/Asset.php b/Asset.php index 1b356270..92e43cbb 100644 --- a/Asset.php +++ b/Asset.php @@ -22,8 +22,7 @@ require_once 'Pman.php'; class Pman_Core_Asset extends Pman { - - + var $types = array( 'css' => 'text/css', 'js' => 'text/javascript', @@ -35,9 +34,10 @@ class Pman_Core_Asset extends Pman { } - function get($s='') + function get($s='', $opts = Array()) { - + $this->sessionState(0); + $bits = explode('/', $s); if (empty($bits[0]) || empty($bits[1]) || !isset($this->types[$bits[0]])) { @@ -49,13 +49,9 @@ class Pman_Core_Asset extends Pman { $ui = posix_getpwuid(posix_geteuid()); $ff = HTML_FlexyFramework::get(); - $compile = session_save_path() . '/' . - $ui['name'] . '-' . $ff->project . '-' . $ff->version . '-'. $bits[0] . 'compile'; - - $fn = $compile . '/'. $s .'.'. $bits[0]; - - + $compile = self::getCompileDir($bits[0], '', false); + $fn = $compile . '/'. $s .'.'. $bits[0]; if (!file_exists($fn)) { header('Content-Type: '. $this->types[$bits[0]]); @@ -64,6 +60,9 @@ class Pman_Core_Asset extends Pman { exit; } + $supportsGzip = !empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos( $_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip' ) !== false; + + $last_modified_time = filemtime($fn); @@ -85,289 +84,114 @@ class Pman_Core_Asset extends Pman { header("Pragma: public"); - header('Content-Length: '. filesize($fn)); + header('Cache-Control: max-age=2592000, public'); header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() + 2592000)); header('Last-Modified: '.gmdate('D, d M Y H:i:s \G\M\T', $last_modified_time)); header('Etag: '. md5($fn)); - $fh = fopen($fn,'r'); - fpassthru($fh); - fclose($fh); + if ( $supportsGzip ) { + $content = gzencode( file_get_contents($fn) , 9); + + header('Content-Encoding: gzip'); + header('Vary: Accept-Encoding'); + header('Content-Length: '. strlen($content)); + + echo $content; + + } else { + + + $fh = fopen($fn,'r'); + fpassthru($fh); + fclose($fh); + $content = $data; + } + + + exit; } + function post($s='') { + if(!empty($_REQUEST['_clear_cache'])) { + $this->clearCompiledFilesCache(); + } + die('invalid'); } - -} - -// a little experimental... we are going to use the same name as the class. for these.. - -trait Pman_Core_Asset_Trait { - - - - /** - * usage in template - * {outputJavascriptDir(#Hydra#,#Hydra.js",#.......#)} - * - * call_user - * - */ - - function outputJavascriptDir($path) + static function getCompileDir($type, $module = '', $is_mkdir = true) { - - $relpath = $this->rootURL . '/' . $path .'/'; $ff = HTML_FlexyFramework::get(); - $dir = $this->rootDir.'/' . $path; - - $args = func_get_args(); - $ar = array(); - if (count($args) < 2) { - $ar = glob($dir . '/*.js'); - } else { - array_shift($args); - foreach($args as $f) { - if (strpos($f,'*') > -1) { - - $ar = array_merge($ar , glob($dir . '/'. $f)); - continue; - } - - $ar[] = $dir .'/'. $f; - } - - } - // cached version?? - how do we decide if it's expired? - // while scanning the directory is slow... - it's faster than serving every file... - - - //$path = $this->rootURL ."/Pman/$mod/"; - - - - $files = array(); - $arfiles = array(); - $maxtime = 0; - $mtime = 0; - foreach($ar as $fn) { - $f = basename($fn); - // got the 'module file..' - $mtime = filemtime($dir . '/'. $f); - $maxtime = max($mtime, $maxtime); - $arfiles[$fn] = $mtime; - $files[] = $relpath . $f . '?ts='.$mtime; - } - - ksort($arfiles); // just sort by name so it's consistant for serialize.. $ui = posix_getpwuid(posix_geteuid()); - - $compiledir = session_save_path() . '/' . - $ui['name'] . '-' . $ff->project . '-' . $ff->version . '-jscompile'; + $compile_dir = session_save_path() . "/"; - if (!file_exists($compiledir)) { - mkdir($compiledir,0700,true); + if (empty($module)) { + $module = $ff->project . (isset($ff->appNameShort) ? '_' . $ff->appNameShort : ''); } - - - $lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;'); - usort($files, $lsort); - - if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) { - echo "\n"; - $this->assetArrayToHtml($files,'js'); - return; + switch($type) { + case 'js': + case 'css': + $compile_dir .= implode("-", array( + $ui['name'], + $module, + $ff->version, + "{$type}compile" + )); + break; + // template config? + default: + return false; } + if (file_exists($compile_dir)) { + return $compile_dir; + } - $smod = str_replace('/','.',$path); - - $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.js'; - - - - // where are we going to write all of this.. - // This has to be done via a - if (!file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) { - require_once 'Pman/Core/JsCompile.php'; - $x = new Pman_Core_JsCompile(); - - $x->pack($arfiles,$compiledir.'/'.$output, false); - clearstatcache(); - if (!file_exists($compiledir.'/'.$output) || - !filesize($compiledir.'/'.$output)) { - echo "\n"; - $this->assetArrayToHtml($files,'js'); - return; - } - - } else { - // echo "\n"; + if(!$is_mkdir) { + return false; } - //$this->arrayToJsInclude( $files); - $this->assetArrayToHtml( array( - $this->baseURL.'/Asset/js/'. $output, - - ), 'js'); - } - - function assetArrayToHtml($ar, $type) - { - foreach($ar as $f) { - switch( $type) { - case 'js': - echo ''."\n"; - break; - case 'css': - echo ''."\n"; - break; - - } - + if(mkdir($compile_dir, 0700, true)) { + return $compile_dir; } + + return false; } - - /** - * usage in template - * {outputCssDir(#{Hydra/templates/images/css/#,#Hydra.js",#.......#)} - */ - - function outputCssDir($path) + function clearCompiledFilesCache() { - - $relpath = $this->rootURL . '/' . $path .'/'; - $ff = HTML_FlexyFramework::get(); - $dir = $this->rootDir.'/' . $path; - - $args = func_get_args(); - $ar = array(); - if (count($args) < 2) { - $ar = glob($dir . '/*.css'); - } else { - array_shift($args); - foreach($args as $f) { - if (strpos($f,'*') > -1) { - - $ar = array_merge($ar , glob($dir . '/'. $f)); - continue; - } - // what if the fiel does not exist??? - $ar[] = $dir .'/'. $f; - } - + $au = $this->getAuthUser(); + if (!$au && !in_array($_SERVER['REMOTE_ADDR'] , array('127.0.0.1','::1'))) { + $this->jerr("Cache can only be cleared by authenticated users"); } + require_once 'System.php'; + $ff = HTML_FlexyFramework::get(); - // cached version?? - how do we decide if it's expired? - // while scanning the directory is slow... - it's faster than serving every file... - + $mods = $this->modulesList(); + $mods[] = $ff->project; // Pman - this was the old format... + $mods[] = ''; // Pman + appshortname.. - //$path = $this->rootURL ."/Pman/$mod/"; + foreach ($mods as $module) { + $compile_dir = $this->getCompileDir('js', $module, false); - //print_R($ar);exit; - $missing_files = false; - $files = array(); - $arfiles = array(); - $relfiles = array(); // array of files without the path part... - $maxtime = 0; - $mtime = 0; - foreach($ar as $fn) { - $relfiles[] = substr($fn, strlen($dir)+1); - $f = basename($fn); - // got the 'module file..' - - if (!file_exists($dir . '/'. $f)) { - echo "\n"; - $files[] = $relpath . $f . '?ts=0'; - $missing_files = true; - continue; + if(!empty($compile_dir)) { + System::rm(array('-r', $compile_dir)); } - - $mtime = filemtime($dir . '/'. $f); - $maxtime = max($mtime, $maxtime); - $arfiles[$fn] = $mtime; - $files[] = $relpath . $f . '?ts='.$mtime; - - - - } - if ($missing_files) { - $this->assetArrayToHtml($files, 'css'); - return; - - } - - - //print_r($relfiles); - - $ui = posix_getpwuid(posix_geteuid()); - - - $compiledir = session_save_path() . '/' . - $ui['name'] . '-' . $ff->project . '-'. $ff->version . '-csscompile'; - - if (!file_exists($compiledir)) { - mkdir($compiledir,0700,true); - } + $compile_dir = $this->getCompileDir('css', $module, false); - - // no sorting??? - //$lsort = create_function('$a,$b','return strlen($a) > strlen($b) ? 1 : -1;'); - //usort($files, $lsort); - - - if (!empty($this->bootLoader->isDev) && !empty($_REQUEST['isDev'])) { - echo "\n"; - $this->assetArrayToHtml($files,'css'); - return; + if(!empty($compile_dir)) { + System::rm(array('-r', $compile_dir)); + } } - - - $smod = str_replace('/','.',$path); - - $output = date('Y-m-d-H-i-s-', $maxtime). $smod .'-'.md5(serialize($arfiles)) .'.css'; - - - // where are we going to write all of this.. - // This has to be done via a - if (true || !file_exists($compiledir.'/'.$output) || !filesize($compiledir.'/'.$output)) { - - - - require_once 'HTML/CSS/Minify.php'; - $x = new HTML_CSS_Minify(substr($relpath,0,-1), $dir, $relfiles); - - file_put_contents($compiledir.'/'.$output , $x->minify( $this->baseURL.'/Asset/css')); - clearstatcache(); - if (!file_exists($compiledir.'/'.$output) || - !filesize($compiledir.'/'.$routput)) { - echo "\n"; - $this->assetArrayToHtml($files,'css'); - return; - } - - } else { - // echo "\n"; - } - //$this->arrayToJsInclude( $files); - $this->assetArrayToHtml( array( - $this->baseURL.'/Asset/css/'. $output, - - ),'css'); - + $this->jok('DONE'); } - - - -} \ No newline at end of file +}