From 6ec293dfe70665c528d803b78a2bb295633496ec Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 29 Jun 2009 08:24:42 -0700 Subject: *Note* work in progress. Implement the combined css functionality. Local url references and replace with absolute urls instead of relative. --- modules/gallery/libraries/Gallery_View.php | 47 ++++++++++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'modules/gallery/libraries/Gallery_View.php') diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php index 7000d3de..1c1dec38 100644 --- a/modules/gallery/libraries/Gallery_View.php +++ b/modules/gallery/libraries/Gallery_View.php @@ -113,12 +113,53 @@ class Gallery_View_Core extends View { protected function combine_css() { $links = array(); $key = ""; + static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; + foreach (array_keys($this->css) as $file) { - $links[] = ""; + $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", "CSS file missing: " . $file); + } + } + $key = md5($key); + $cache = Cache::instance(); + $contents = $cache->get($key); + $docroot_length = strlen(DOCROOT); + + if (empty($contents)) { + $contents = ""; + foreach ($links as $link) { + $css = file_get_contents($link); + if (preg_match_all($PATTERN, $css, $matches, PREG_SET_ORDER)) { + $search = $replace = array(); + foreach ($matches as $match) { + $relative = substr(realpath(dirname($link) . "/$match[1]"), $docroot_length); + if (!empty($relative)) { + $search[] = $match[1]; + $replace[] = url::abs_file($relative); + } else { + Kohana::log("alert", sprintf("Missing URL reference '%s' in CSS file '%s' ", + $match[1], $link)); + } + } + $css = str_replace($search, $replace, $css); + } + $contents .= $css; + } + $cache->set($key, $contents, array("css"), 30 * 84600); + if (function_exists("gzencode")) { + $cache->set("{$key}_gz", gzencode($contents, 9, FORCE_GZIP), + array("css", "gzip"), 30 * 84600); + } } - return implode("\n", $links); + return ""; } } \ No newline at end of file -- cgit v1.2.3 From 34c76c0906a639321dedfe0e77d9fd123ed7c792 Mon Sep 17 00:00:00 2001 From: Tim Almdal Date: Mon, 29 Jun 2009 12:32:11 -0700 Subject: A Combined javascript seems to work. 1) CSS files are added to the combined version by use of $theme->css() or $theme->css_theme() methods 2) url references in the css are converted to full paths as opposed to relative 3) @import statements in the css are resolved as well. 4) need to move the [if IE] statements into the css files so the will be honored in the browser. currently the ie fix css are always included. --- modules/gallery/controllers/combined.php | 5 --- modules/gallery/libraries/Admin_View.php | 1 + modules/gallery/libraries/Gallery_View.php | 54 ++++++++++++++++++++---------- 3 files changed, 37 insertions(+), 23 deletions(-) (limited to 'modules/gallery/libraries/Gallery_View.php') diff --git a/modules/gallery/controllers/combined.php b/modules/gallery/controllers/combined.php index 50fe77c4..399a60f4 100644 --- a/modules/gallery/controllers/combined.php +++ b/modules/gallery/controllers/combined.php @@ -99,10 +99,5 @@ class Combined_Controller extends Controller { Kohana::close_buffers(false); print $content; } - - public function __call($function, $args) { - array_unshift($args, $function); - print ""; - } } diff --git a/modules/gallery/libraries/Admin_View.php b/modules/gallery/libraries/Admin_View.php index 01496c0d..5e0d5feb 100644 --- a/modules/gallery/libraries/Admin_View.php +++ b/modules/gallery/libraries/Admin_View.php @@ -99,6 +99,7 @@ class Admin_View_Core extends Gallery_View { } if ($function == "admin_head") { + array_unshift($blocks, $this->combine_css()); array_unshift($blocks, $this->combine_script()); } diff --git a/modules/gallery/libraries/Gallery_View.php b/modules/gallery/libraries/Gallery_View.php index 1c1dec38..b4cfde46 100644 --- a/modules/gallery/libraries/Gallery_View.php +++ b/modules/gallery/libraries/Gallery_View.php @@ -113,7 +113,6 @@ class Gallery_View_Core extends View { protected function combine_css() { $links = array(); $key = ""; - static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; foreach (array_keys($this->css) as $file) { $path = DOCROOT . $file; @@ -130,27 +129,12 @@ class Gallery_View_Core extends View { $key = md5($key); $cache = Cache::instance(); $contents = $cache->get($key); - $docroot_length = strlen(DOCROOT); + $contents = ""; if (empty($contents)) { $contents = ""; foreach ($links as $link) { - $css = file_get_contents($link); - if (preg_match_all($PATTERN, $css, $matches, PREG_SET_ORDER)) { - $search = $replace = array(); - foreach ($matches as $match) { - $relative = substr(realpath(dirname($link) . "/$match[1]"), $docroot_length); - if (!empty($relative)) { - $search[] = $match[1]; - $replace[] = url::abs_file($relative); - } else { - Kohana::log("alert", sprintf("Missing URL reference '%s' in CSS file '%s' ", - $match[1], $link)); - } - } - $css = str_replace($search, $replace, $css); - } - $contents .= $css; + $contents .= $this->process_css($link); } $cache->set($key, $contents, array("css"), 30 * 84600); if (function_exists("gzencode")) { @@ -162,4 +146,38 @@ class Gallery_View_Core extends View { url::site("combined/css/$key") . "\" />"; } + private function process_css($css_file) { + static $PATTERN = "#url\(\s*['|\"]{0,1}(.*?)['|\"]{0,1}\s*\)#"; + $docroot_length = strlen(DOCROOT); + + $css = file_get_contents($css_file); + if (preg_match_all($PATTERN, $css, $matches, PREG_SET_ORDER)) { + $search = $replace = array(); + foreach ($matches as $match) { + $relative = substr(realpath(dirname($css_file) . "/$match[1]"), $docroot_length); + if (!empty($relative)) { + $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)); + } + } + $css = str_replace($search, $replace, $css); + } + $imports = preg_match_all("#@import\s*['|\"]{0,1}(.*?)['|\"]{0,1};#", + $css, $matches, PREG_SET_ORDER); + + 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]"); + } + $css = str_replace($search, $replace, $css); + } + + return $css; + } } \ No newline at end of file -- cgit v1.2.3 From fa8ca2f7ad21980f0b45e7acc67be4be296f9f87 Mon Sep 17 00:00:00 2001 From: Bharat Mediratta Date: Mon, 29 Jun 2009 18:12:53 -0700 Subject: Refactor combine_xxx() functions together into combine_files() and use html functions to generate the resulting elements. Add phpdoc. --- modules/gallery/controllers/combined.php | 2 + modules/gallery/libraries/Admin_View.php | 6 +-- modules/gallery/libraries/Gallery_View.php | 81 ++++++++++-------------------- modules/gallery/libraries/Theme_View.php | 4 +- 4 files changed, 34 insertions(+), 59 deletions(-) (limited to 'modules/gallery/libraries/Gallery_View.php') diff --git a/modules/gallery/controllers/combined.php b/modules/gallery/controllers/combined.php index f6c6d60b..9df74638 100644 --- a/modules/gallery/controllers/combined.php +++ b/modules/gallery/controllers/combined.php @@ -22,6 +22,7 @@ class Combined_Controller extends Controller { * Return the combined Javascript bundle associated with the given key. */ public function javascript($key) { + $key = substr($key, 0, strlen($key) - 3); // strip off the trailing .js return $this->_emit("javascript", $key); } @@ -29,6 +30,7 @@ class Combined_Controller extends Controller { * Return the combined CSS bundle associated with the given key. */ public function css($key) { + $key = substr($key, 0, strlen($key) - 4); // strip off the trailing .css return $this->_emit("css", $key); } diff --git a/modules/gallery/libraries/Admin_View.php b/modules/gallery/libraries/Admin_View.php index 5e0d5feb..f7de96bf 100644 --- a/modules/gallery/libraries/Admin_View.php +++ b/modules/gallery/libraries/Admin_View.php @@ -98,9 +98,9 @@ class Admin_View_Core extends Gallery_View { } } - if ($function == "admin_head") { - array_unshift($blocks, $this->combine_css()); - array_unshift($blocks, $this->combine_script()); + if ($function == "head") { + array_unshift($blocks, $this->combine_files($this->css, "css")); + array_unshift($blocks, $this->combine_files($this->css, "javascript")); } if (Session::instance()->get("debug")) { 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 @@ -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 "; - } - /** * 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