Index: branches/5.2.x/core/units/helpers/minifiers/minify_helper.php =================================================================== diff -u -N -r14628 -r14818 --- branches/5.2.x/core/units/helpers/minifiers/minify_helper.php (.../minify_helper.php) (revision 14628) +++ branches/5.2.x/core/units/helpers/minifiers/minify_helper.php (.../minify_helper.php) (revision 14818) @@ -1,6 +1,6 @@ debugMode = $this->Application->isDebugMode(false); - $this->infoFile = WRITEABLE . '/cache/compress_info.txt'; - - if ( file_exists($this->infoFile) ) { - $this->compressInfo = unserialize( file_get_contents($this->infoFile) ); - } + $this->resourceFolder = WRITEABLE . '/cache'; } /** @@ -78,31 +68,19 @@ $files = $this->_getTemplatePaths( array_map('trim', explode('|', $files)) ); $extension = pathinfo($files[0], PATHINFO_EXTENSION); - $hash = ($this->debugMode ? 'd' : 'c') . '_' . $this->_getHash($files); - $file_mask = 'cache/' . $hash . '_%s.' . $extension; + $save_as = isset($params['save_as']) ? $params['save_as'] : false; + $dst_file = $this->resourceFolder . DIRECTORY_SEPARATOR . ($this->debugMode ? 'd' : 'c') . '_'; - $was_compressed = array_key_exists($hash, $this->compressInfo); - - if ( $was_compressed ) { - $current_file = WRITEABLE . '/' . sprintf($file_mask, $this->compressInfo[$hash]); - - if ( !file_exists($current_file) ) { - // when info exists, but file doesn't -> re-compress - $was_compressed = false; - } - - if ( $this->debugMode ) { - // check if any of listed files was changed since compressed file was created (only, when in debug mode) - foreach ($files as $file) { - if ( filemtime($file) > $this->compressInfo[$hash] ) { - $was_compressed = false; - break; - } - } - } + if ( $save_as ) { + $dst_file .= $save_as . ( strpos($save_as, '.') === false ? '.' . $extension : '' ); } + else { + $dst_file .= $this->_getHash($files) . '.' . $extension; + } - if ( !$was_compressed ) { + $was_compressed = file_exists($dst_file); + + if ( !$was_compressed || ($this->debugMode && filemtime($dst_file) < $this->_getMaxFileDate($files)) ) { $string = ''; $path_length = strlen(FULL_PATH) + 1; @@ -117,45 +95,52 @@ $string .= file_get_contents($file) . "\n\n"; } - // remove previous version of compressed file - if ( isset($current_file) ) { - if ( file_exists($current_file) ) { - unlink($current_file); - } - } - // replace templates base $templates_base = $this->Application->ProcessParsedTag('m', 'TemplatesBase', Array ()); $templates_base = preg_replace('/^' . preg_quote($this->Application->BaseURL(), '/') . '/', BASE_PATH . '/', $templates_base); $string = str_replace('@templates_base@', rtrim($templates_base, '/'), $string); - // compress collected data - $this->compressInfo[$hash] = adodb_mktime(); - if ( !$this->debugMode ) { // don't compress merged js/css file in debug mode to allow js/css debugging $this->compressString($string, $extension); } // save compressed file - $fp = fopen(WRITEABLE . '/' . sprintf($file_mask, $this->compressInfo[$hash]), 'w'); - fwrite($fp, $string); - fclose($fp); + file_put_contents($dst_file, $string); + } - $this->_saveInfo(); + $file_helper =& $this->Application->recallObject('FileHelper'); + /* @var $file_helper FileHelper */ + + return $file_helper->pathToUrl($dst_file) . '?ts=' . filemtime($dst_file); + } + + /** + * Returns maximal modification date across given files + * + * @param Array $files + * @return int + * @access protected + */ + protected function _getMaxFileDate($files) + { + $ret = 0; + + foreach ($files as $file) { + $ret = max($ret, filemtime($file)); } - // got compressed file -> use it - return $this->Application->BaseURL( str_replace(DIRECTORY_SEPARATOR, '/', WRITEBALE_BASE)) . sprintf($file_mask, $this->compressInfo[$hash]); + return $ret; } /** * Returns hash string based on given files * * @param Array $files * @return int + * @access protected */ - function _getHash($files) + protected function _getHash($files) { $hash = $files; @@ -170,25 +155,20 @@ } /** - * Saves file with compress information - * - */ - function _saveInfo() - { - $fp = fopen($this->infoFile, 'w'); - fwrite($fp, serialize($this->compressInfo)); - fclose($fp); - } - - /** * Deletes compression info file * * @todo also delete all listed there files + * @access public */ - function delete() + public function delete() { - if (file_exists($this->infoFile)) { - unlink($this->infoFile); + $iterator = new DirectoryIterator($this->resourceFolder); + /* @var $file_info DirectoryIterator */ + + foreach ($iterator as $file_info) { + if ( !$file_info->isDir() && preg_match('/^(c|d)_.*.(css|js)$/', $file_info->getFilename()) ) { + unlink( $file_info->getPathname() ); + } } } @@ -197,8 +177,10 @@ * * @param string $string * @param string $extension + * @return void + * @access protected */ - function compressString(&$string, $extension) + public function compressString(&$string, $extension) { $tmp_file = tempnam('/tmp', 'to_compress_'); $fp = fopen($tmp_file, 'w'); @@ -236,18 +218,19 @@ * * @param Array $templates * @return Array + * @access protected */ - function _getTemplatePaths($templates) + protected function _getTemplatePaths($templates) { $ret = Array (); $reg_exp = '/^' . preg_quote($this->Application->BaseURL(), '/') . '(.*)/'; foreach ($templates as $template) { - if (!$template) { + if ( !$template ) { continue; } - if (preg_match($reg_exp, $template, $regs)) { + if ( preg_match($reg_exp, $template, $regs) ) { // full url (from current domain) to a file $path = FULL_PATH . '/' . $regs[1]; }