]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Consolidated the css and js pagehandlers with elgg_cachable_view_pagehandler() and...
authorbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Wed, 13 Apr 2011 14:39:21 +0000 (14:39 +0000)
committerbrettp <brettp@36083f99-b078-4883-b0ff-0f9b5a30f544>
Wed, 13 Apr 2011 14:39:21 +0000 (14:39 +0000)
git-svn-id: http://code.elgg.org/elgg/trunk@8985 36083f99-b078-4883-b0ff-0f9b5a30f544

engine/lib/elgglib.php

index 1aef48ef468129544a7f0d9246a2220adf1e8745..063e25fc4d0aa1603c9fd4746f1e16fa006381a2 100644 (file)
@@ -1685,21 +1685,7 @@ function _elgg_shutdown_hook() {
  * @elgg_pagehandler js
  */
 function elgg_js_page_handler($page) {
-       if (is_array($page) && sizeof($page)) {
-               $js = implode('/', $page);
-               $js = substr($js, 0, strpos($js, '.'));
-               $return = elgg_view('js/' . $js);
-
-               header('Content-type: text/javascript');
-
-               // @todo should js be cached when simple cache turned off
-               //header('Expires: ' . date('r', time() + 864000));
-               //header("Pragma: public");
-               //header("Cache-Control: public");
-               //header("Content-Length: " . strlen($return));
-
-               echo $return;
-       }
+       return elgg_cacheable_view_page_handler($page, 'js');
 }
 
 /**
@@ -1749,18 +1735,62 @@ function elgg_css_page_handler($page) {
                // default css
                $page[0] = 'elgg';
        }
+       
+       return elgg_cacheable_view_page_handler($page, 'css');
+}
+
+/**
+ * Serves a JS or CSS view with headers for caching.
+ *
+ * /<css||js>/name/of/view.<last_cache>.<css||js>
+ *
+ * @param array  $page  The page array
+ * @param string $type  The type: js or css
+ *
+ * @return mixed
+ */
+function elgg_cacheable_view_page_handler($page, $type) {
+
+       switch ($type) {
+               case 'js':
+                       $content_type = 'text/javascript';
+                       break;
+
+               case 'css':
+                       $content_type = 'text/css';
+                       break;
+
+               default:
+                       return false;
+                       break;
+       }
+
+       if ($page) {
+               // the view file names can have multiple dots
+               // eg: views/default/js/calendars/jquery.fullcalendar.min.php
+               // translates to the url /js/calendars/jquery.fullcalendar.min.<ts>.js
+               // and the view js/calendars/jquery.fullcalendar.min
+               // we ignore the last two dots for the ts and the ext.
+               $last_part = array_pop($page);
+               $last_part_bits = explode('.', $last_part);
+               $last_part_bits = array_slice($last_part_bits, 0, -2);
+               $page[] = implode('.', $last_part_bits);
 
-       $css = substr($page[0], 0, strpos($page[0], '.'));
-       $return = elgg_view("css/$css");
+               $view = implode('/', $page);
+               $return = elgg_view("$type/$view");
+
+               header("Content-type: $content_type");
 
-       header("Content-type: text/css", true);
+               // @todo should js be cached when simple cache turned off
+               //header('Expires: ' . date('r', time() + 864000));
+               //header("Pragma: public");
+               //header("Cache-Control: public");
+               //header("Content-Length: " . strlen($return));
 
-       // @todo should css be cached when simple cache is turned off
-       //header('Expires: ' . date('r', time() + 86400000), true);
-       //header("Pragma: public", true);
-       //header("Cache-Control: public", true);
+               echo $return;
+       }
 
-       echo $return;
+       return true;
 }
 
 /**