summaryrefslogtreecommitdiff
path: root/modules/gallery/libraries/Gallery_View.php
diff options
context:
space:
mode:
authorBharat Mediratta <bharat@menalto.com>2009-06-29 18:12:53 -0700
committerBharat Mediratta <bharat@menalto.com>2009-06-29 18:12:53 -0700
commitfa8ca2f7ad21980f0b45e7acc67be4be296f9f87 (patch)
tree3aaa1468c51e48c73986c56719f247da22e4fa8d /modules/gallery/libraries/Gallery_View.php
parent3080317d6e5e4ea9e56b1fd5444c4bcf5852c362 (diff)
Refactor combine_xxx() functions together into combine_files() and use
html functions to generate the resulting elements. Add phpdoc.
Diffstat (limited to 'modules/gallery/libraries/Gallery_View.php')
-rw-r--r--modules/gallery/libraries/Gallery_View.php81
1 files changed, 27 insertions, 54 deletions
diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php
index 9d4fe5c0..48cf8089 100644
--- a/modules/gallery/libraries/Gallery_View.php
+++ b/modules/gallery/libraries/Gallery_View.php
@@ -50,45 +50,6 @@ 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.
- */
- 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]");
}