#210 JS/CSS import의 optimizer의 부분 적용 문법과 media 적용 문법 추가(getJSFile, getCSSFile의 함수 용법 변화)

git-svn-id: http://xe-core.googlecode.com/svn/sandbox@3926 201d5d3c-b55e-5fd7-737f-ddc643e51545
This commit is contained in:
wdlee91 2008-03-11 02:20:35 +00:00
parent 586c6d9f70
commit 55c2a0d8de
4 changed files with 74 additions and 39 deletions

View file

@ -714,18 +714,33 @@
/**
* @brief js file을 추가
**/
function addJsFile($file) {
function addJsFile($file, $optimized = true) {
$oContext = &Context::getInstance();
return $oContext->_addJsFile($file);
return $oContext->_addJsFile($file, $optimized);
}
/**
* @brief js file을 추가
**/
function _addJsFile($file) {
function _addJsFile($file, $optimized) {
if(in_array($file, $this->js_files)) return;
//if(!preg_match('/^http:\/\//i',$file)) $file = str_replace(realpath("."), ".", realpath($file));
$this->js_files[] = $file;
$this->js_files[] = array('file' => $file, 'optimized' => $optimized);
}
/**
* @brief array_unique와 동작은 동일하나 file 첨자에 대해서만 동작함
**/
function _getUniqueFileList($files) {
$filenames = array();
$size = count($files);
for($i = 0; $i < $size; ++ $i)
{
if(in_array($files[$i]['file'], $filenames))
unset($files[$i]);
$filenames[] = $files[$i]['file'];
}
return $files;
}
/**
@ -742,25 +757,25 @@
function _getJsFile() {
require_once("./classes/optimizer/Optimizer.class.php");
$oOptimizer = new Optimizer();
return $oOptimizer->getOptimizedFiles(array_unique($this->js_files), "js");
return $oOptimizer->getOptimizedFiles($this->_getUniqueFileList($this->js_files), "js");
}
/**
* @brief CSS file 추가
**/
function addCSSFile($file) {
function addCSSFile($file, $optimized = true, $media = 'all') {
$oContext = &Context::getInstance();
return $oContext->_addCSSFile($file);
return $oContext->_addCSSFile($file, $optimized, $media);
}
/**
* @brief CSS file 추가
**/
function _addCSSFile($file) {
function _addCSSFile($file, $optimized, $media) {
if(in_array($file, $this->css_files)) return;
//if(preg_match('/^http:\/\//i',$file)) $file = str_replace(realpath("."), ".", realpath($file));
$this->css_files[] = $file;
$this->css_files[] = array('file' => $file, 'optimized' => $optimized, 'media' => $media);
}
/**
@ -777,7 +792,7 @@
function _getCSSFile() {
require_once("./classes/optimizer/Optimizer.class.php");
$oOptimizer = new Optimizer();
return $oOptimizer->getOptimizedFiles(array_unique($this->css_files), "css");
return $oOptimizer->getOptimizedFiles($this->_getUniqueFileList($this->css_files), "css");
}
/**

View file

@ -22,31 +22,38 @@
}
}
/**
* @brief 파일 목록 배열에서 optimized 첨자를 제거한 return
**/
function _getOptimizedRemoved($files) {
foreach($files as $key => &$val) unset($key['optimized']);
return $files;
}
/**
* @brief optimize 대상 파일을 받아서 처리 optimize 파일이름을 return
**/
function getOptimizedFiles($source_files, $type = "js") {
if(!is_array($source_files) || !count($source_files)) return;
if(!is_array($source_files) || !count($source_files)) return;
// $source_files의 역슬래쉬 경로를 슬래쉬로 변경 (윈도우즈 대비)
foreach($source_files as $key => $file) $source_files[$key] = str_replace("\\","/",$file);
// $source_files의 역슬래쉬 경로를 슬래쉬로 변경 (윈도우즈 대비)
foreach($source_files as $key => $file) $source_files[$key]['file'] = str_replace("\\","/",$file['file']);
// 관리자 설정시 설정이 되어 있지 않으면 패스
$db_info = Context::getDBInfo();
if($db_info->use_optimizer == 'N') return $source_files;
if($db_info->use_optimizer == 'N') return $this->_getOptimizedRemoved($source_files);
// 캐시 디렉토리가 없으면 실행하지 않음
if(!is_dir($this->cache_path)) return $source_files;
if(!is_dir($this->cache_path)) return $this->_getOptimizedRemoved($source_files);
if(!count($source_files)) return;
foreach($source_files as $file) {
if(!$file) continue;
$file = str_replace("\\","/",$file);
if(preg_match('/^http:\/\//i', $file) || $file == './common/css/button.css') $files[] = $file;
if(!$file || !$file['file']) continue;
if(empty($file['optimized']) || preg_match('/^https?:\/\//i', $file['file']) || $file['file'] == './common/css/button.css') $files[] = $file;
else $targets[] = $file;
}
if(!count($targets)) return $files;
if(!count($targets)) return $this->_getOptimizedRemoved($files);
$optimized_info = $this->getOptimizedInfo($targets);
@ -55,10 +62,17 @@
$this->doOptimizedFile($path, $filename, $targets, $type);
$files[] = $path.'/'.$filename;
$files[] = array('file' => $path.'/'.$filename, 'media' => 'all');
return $this->_getOptimizedRemoved($files);
}
/**
* @brief 파일 목록 배열에서 file을 제외한 나머지 첨자를 제거하여 return
**/
function _getOnlyFileList($files) {
foreach($files as $key => $val) $files[$key] = $val['file'];
return $files;
}
/**
@ -70,11 +84,11 @@
$count = count($files);
$last_modified = 0;
for($i=0;$i<$count;$i++) {
$mtime = filemtime($files[$i]);
$mtime = filemtime($files[$i]['file']);
if($last_modified < $mtime) $last_modified = $mtime;
}
$buff = implode("\n", $files);
$buff = implode("\n", $this->_getOnlyFileList($files));
return array(md5($buff), $last_modified);
}
@ -102,16 +116,19 @@
**/
// 대상 파일의 내용을 구해오고 css 파일일 경우 url()내의 경로를 변경
foreach($targets as $file) {
$str = FileHandler::readFile($file);
$str = FileHandler::readFile($file['file']);
$str = Context::convertEncodingStr($str);
// css 일경우 background:url() 변경
if($type == "css") $str = $this->replaceCssPath($file, $str);
// css 일경우 background:url() 변경 / media 적용
if($type == 'css') {
$str = $this->replaceCssPath($file['file'], $str);
if($file['media'] != 'all') $str = '@media '.$file['media'].' {'."\n".$str."\n".'}';
}
$content_buff .= $str."\n";
}
if($type == "css") $content_buff = '@charset "utf-8";'."\n".$content_buff;
if($type == 'css') $content_buff = '@charset "UTF-8";'."\n".$content_buff;
$content_filename = substr($filename, 0, -4);
FileHandler::writeFile($path.'/'.$content_filename, $content_buff);
@ -121,7 +138,7 @@
**/
// 확장자별 content-type 체크
if($type == 'css') $content_type = 'text/css';
elseif($type == 'js') $content_type = 'application/x-javascript';
elseif($type == 'js') $content_type = 'text/javascript';
// 캐시를 위한 처리
$unique = crc32($content_filename);
@ -154,7 +171,7 @@ if( preg_match("/MSIE 6.0/i",$_SERVER["HTTP_USER_AGENT"]) || strpos($_SERVER["HT
header("Content-Encoding: gzip");
}
header("Content-Type: '.$content_type.'; charset=utf-8");
header("Content-Type: '.$content_type.'; charset=UTF-8");
header("Date: '.substr(gmdate('r'), 0, -5).'GMT");
header("Expires: '.substr(gmdate('r', strtotime('+1 MONTH')), 0, -5).'GMT");
header("Cache-Control: private, max-age=2592000");
@ -168,7 +185,6 @@ if(!$cached) {
fpassthru($f);
} else print $buff;
}
?>';
FileHandler::writeFile($path.'/'.$filename, $header_buff);
}

View file

@ -115,8 +115,8 @@
// <!--@, --> 의 변경
$buff = preg_replace_callback('!<\!--@(.*?)-->!is', array($this, '_compileFuncToCode'), $buff);
// import xml filter/ css/ js/ 언어파일 <!--%filename-->
$buff = preg_replace_callback('!<\!--%import\(\"([^\"]*?)\"\)-->!is', array($this, '_compileImportCode'), $buff);
// import xml filter/ css/ js/ 언어파일 <!--%import("filename"[,optimized=true|false[,media="media"]]--> (media는 css에만 적용)
$buff = preg_replace_callback('!<\!--%import\(\"([^\"]*?)\"(,optimized\=(true|false)(,media\=\"([^\"]*)\")?)?\)-->!is', array($this, '_compileImportCode'), $buff);
// 파일에 쓰기 전에 직접 호출되는 것을 방지
$buff = sprintf('%s%s%s','<?php if(!defined("__ZBXE__")) exit();?>',"\n",$buff);
@ -259,6 +259,10 @@
$base_path = $this->tpl_path;
$given_file = trim($matches[1]);
if(!$given_file) return;
$optimized = strtolower(trim(@$matches[3]));
if(!$optimized) $optimized = 'true';
$media = trim(@$matches[5]);
if(!$media) $media = 'all';
// given_file이 lang으로 끝나게 되면 언어팩을 읽도록 함
if(substr($given_file, -4)=='lang') {
@ -303,12 +307,12 @@
// css file
case 'css' :
$meta_file = sprintf('%s%s', $base_path, $filename);
$output = sprintf('<?php Context::addCSSFile("%s%s"); ?>', $base_path, $filename);
$output = sprintf('<?php Context::addCSSFile("%s%s", %s, "%s"); ?>', $base_path, $filename, $optimized, $media);
break;
// js file
case 'js' :
$meta_file = sprintf('%s%s', $base_path, $filename);
$output = sprintf('<?php Context::addJsFile("%s%s"); ?>', $base_path, $filename);
$output = sprintf('<?php Context::addJsFile("%s%s", %s); ?>', $base_path, $filename, $optimized);
break;
}
}