mirror of
https://github.com/Pathduck/gallery3.git
synced 2026-07-11 17:53:50 -04:00
Refactor combine_xxx() functions together into combine_files() and use
html functions to generate the resulting elements. Add phpdoc.
This commit is contained in:
@@ -49,45 +49,6 @@ class Gallery_View_Core extends View {
|
||||
return $absolute_url ? url::abs_file($arg) : url::file($arg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine a series of Javascript files into a single one and cache it in the database, then
|
||||
* return a single <script> element to refer to it.
|
||||
*/
|
||||
protected function combine_script() {
|
||||
$links = array();
|
||||
$key = "";
|
||||
foreach (array_keys($this->scripts) as $file) {
|
||||
$path = DOCROOT . $file;
|
||||
if (file_exists($path)) {
|
||||
$stats = stat($path);
|
||||
$links[] = $path;
|
||||
// 7 == size, 9 == mtime, see http://php.net/stat
|
||||
$key = "{$key}$file $stats[7] $stats[9],";
|
||||
} else {
|
||||
Kohana::log("alert", "Javascript file missing: " . $file);
|
||||
}
|
||||
}
|
||||
|
||||
$key = md5($key);
|
||||
$cache = Cache::instance();
|
||||
$contents = $cache->get($key);
|
||||
if (empty($contents)) {
|
||||
$contents = "";
|
||||
foreach ($links as $link) {
|
||||
$contents .= file_get_contents($link);
|
||||
}
|
||||
$cache->set($key, $contents, array("javascript"), 30 * 84600);
|
||||
if (function_exists("gzencode")) {
|
||||
$cache->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP),
|
||||
array("javascript", "gzip"), 30 * 84600);
|
||||
}
|
||||
}
|
||||
|
||||
// Handcraft the script link because html::script will add a .js extenstion
|
||||
return "<script type=\"text/javascript\" src=\"" . url::site("combined/javascript/$key") .
|
||||
"\"></script>";
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a css file to the combined css list.
|
||||
* @param $file the relative path to a script from the gallery3 directory
|
||||
@@ -107,12 +68,11 @@ class Gallery_View_Core extends View {
|
||||
}
|
||||
|
||||
/**
|
||||
* Combine a series of Javascript files into a single one and cache it in the database, then
|
||||
* return a single <script> element to refer to it.
|
||||
* Combine a series of files into a single one and cache it in the database.
|
||||
*/
|
||||
protected function combine_css() {
|
||||
protected function combine_files($files, $type) {
|
||||
$links = array();
|
||||
$key = "";
|
||||
$key = array();
|
||||
|
||||
foreach (array_keys($this->css) as $file) {
|
||||
$path = DOCROOT . $file;
|
||||
@@ -120,13 +80,13 @@ class Gallery_View_Core extends View {
|
||||
$stats = stat($path);
|
||||
$links[] = $path;
|
||||
// 7 == size, 9 == mtime, see http://php.net/stat
|
||||
$key = "{$key}$file $stats[7] $stats[9],";
|
||||
$key[] = "$file $stats[7] $stats[9]";
|
||||
} else {
|
||||
Kohana::log("alert", "CSS file missing: " . $file);
|
||||
Kohana::log("error", "missing file ($type): $file");
|
||||
}
|
||||
}
|
||||
|
||||
$key = md5($key);
|
||||
$key = md5(join(" ", $key));
|
||||
$cache = Cache::instance();
|
||||
$contents = $cache->get($key);
|
||||
|
||||
@@ -134,18 +94,33 @@ class Gallery_View_Core extends View {
|
||||
if (empty($contents)) {
|
||||
$contents = "";
|
||||
foreach ($links as $link) {
|
||||
$contents .= $this->process_css($link);
|
||||
if ($type == "css") {
|
||||
$contents .= $this->process_css($link);
|
||||
} else {
|
||||
$contents .= file_get_contents($link);
|
||||
}
|
||||
}
|
||||
$cache->set($key, $contents, array("css"), 30 * 84600);
|
||||
|
||||
$cache->set($key, $contents, array($type), 30 * 84600);
|
||||
if (function_exists("gzencode")) {
|
||||
$cache->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP),
|
||||
array("css", "gzip"), 30 * 84600);
|
||||
array($type, "gzip"), 30 * 84600);
|
||||
}
|
||||
}
|
||||
return "<link media=\"screen, projection\" rel=\"stylesheet\" type=\"text/css\" href=\"" .
|
||||
url::site("combined/css/$key") . "\" />";
|
||||
|
||||
if ($type == "css") {
|
||||
return html::stylesheet("combined/css/$key", false, true);
|
||||
} else {
|
||||
// Handcraft the script link because html::script will add a .js extenstion
|
||||
return html::script("combined/javascript/$key", true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert relative references inside a CSS file to absolute ones so that when it's served from
|
||||
* a new location as part of a combined bundle the references are still correct.
|
||||
* @param string the path to the css file
|
||||
*/
|
||||
private function process_css($css_file) {
|
||||
static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#";
|
||||
$docroot_length = strlen(DOCROOT);
|
||||
@@ -159,8 +134,7 @@ class Gallery_View_Core extends View {
|
||||
$search[] = $match[0];
|
||||
$replace[] = "url('" . url::abs_file($relative) . "')";
|
||||
} else {
|
||||
Kohana::log("alert", sprintf("Missing URL reference '%s' in CSS file '%s' ",
|
||||
$match[1], $css_file));
|
||||
Kohana::log("error", "Missing URL reference '{$match[1]}' in CSS file '$css_file'");
|
||||
}
|
||||
}
|
||||
$css = str_replace($search, $replace, $css);
|
||||
@@ -171,7 +145,6 @@ class Gallery_View_Core extends View {
|
||||
if ($imports) {
|
||||
$search = $replace = array();
|
||||
foreach ($matches as $match) {
|
||||
Kohana::log("error", dirname($css_file) . "/$match[1]");
|
||||
$search[] = $match[0];
|
||||
$replace[] = $this->process_css(dirname($css_file) . "/$match[1]");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user