Permalink
Browse files

Add skeleton: new rewriter class, and new config option

  • Loading branch information...
1 parent 0e1aec4 commit 8ce2431307db15495ef4de5658c8541042b2f2b8 @canton7 committed May 31, 2012
Showing with 81 additions and 1 deletion.
  1. +1 −0 bootstrap.php
  2. +34 −1 classes/casset.php
  3. +21 −0 classes/casset/cssurirewriterrelative.php
  4. +25 −0 config/casset.php
View
@@ -19,6 +19,7 @@
'Casset\\Casset_JSMin' => __DIR__.'/classes/casset/jsmin.php',
'Casset\\Casset_Csscompressor' => __DIR__.'/classes/casset/csscompressor.php',
'Casset\\Casset_Cssurirewriter' => __DIR__.'/classes/casset/cssurirewriter.php',
+ 'Casset\\Casset_Cssurirewriterrelative' => __DIR__.'/classes/casset/cssurirewriterrelative.php',
'Casset\\Casset_Addons_Twig' => __DIR__.'/classes/casset/addons/twig.php',
));
View
@@ -117,6 +117,11 @@ class Casset {
protected static $rendered_groups = array('js' => array(), 'css' => array());
/**
+ * @var string which css uri rewriter we want to use. Options are 'absolute', 'relative', 'none'
+ */
+ protected static $css_uri_rewriter = 'absolute';
+
+ /**
* @var bool Wether we've been initialized.
*/
public static $initialized = false;
@@ -182,6 +187,8 @@ public static function _init()
static::$filepath_callback = \Config::get('casset.filepath_callback', static::$filepath_callback);
+ static::$css_uri_rewriter = \Config::get('casset.css_uri_rewriter', static::$css_uri_rewriter);
+
static::$initialized = true;
}
@@ -1073,11 +1080,37 @@ protected static function load_file($filename, $type, $file_group = false)
$content = $func($content, $filename, $type, $file_group);
}
if ($type == 'css')
- $content = Casset_Cssurirewriter::rewrite($content, dirname($filename));
+ $content = static::css_rewrite_uris($content, $filename);
return $content;
}
/**
+ * Selects the correct css uri rewriter, and applies it
+ *
+ * @param string $content the contents of the file to rewrite
+ * @param string $filename the original location of the file
+ * @return string The re-written content
+ */
+ protected static function css_rewrite_uris($content, $filename) {
+ switch (static::$css_uri_rewriter) {
+ case 'absolute':
+ $rewritten = Casset_Cssurirewriter::rewrite($content, dirname($filename));
+ break;
+ case 'relative':
+ $rewritten = Casset_Cssurirewriterrelative::rewrite($content);
+ break;
+ case 'none':
+ $rewritten = $content;
+ break;
+ default:
+ throw new Casset_Exception('Unknown CSS URI rewriter: '.static::$css_uri_rewriter);
+ break;
+ }
+
+ return $rewritten;
+ }
+
+ /**
* Takes a list of files, and combines them into a single minified file.
* Doesn't bother if none of the files have been modified since the cache
* file was written.
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * This library is used as part of Casset.
+ *
+ * @package Casset
+ * @version v1.17
+ * @author Antony Male
+ * @license MIT License
+ * @link http://github.com/canton7/fuelphp-casset
+ */
+
+namespace Casset;
+
+class Casset_Cssurirewriterrelative {
+ public static function rewrite($content) {
+ return $content;
+ }
+}
+
+/* End of file casset/cssurirewriterrelative.php */
View
@@ -158,6 +158,31 @@
'filepath_callback' => null,
/**
+ * Which CSS URI rewriter to use.
+ * When the location of a CSS file is changed (which happens when the file is rewritten,
+ * combined, inlined, etc), all uris inside of that file (e.g. url(....)) have to be rewritten,
+ * otherwise they would break.
+ *
+ * Casset provides a number of different rewriting algorithms, each with their own advantages
+ * and disadvantages.
+ * The options are absolute, relative, and none.
+ *
+ * Absolute:
+ * This algorithm, written by Stephen Clay as part of his JSMin library, rewrites all relative
+ * uris to be absolute, relative to DOCUMENT_ROOT.
+ * However, this algorithm gets confused when you symlinkyour document root.
+ *
+ * Relative:
+ * This algorithm rewrites relative uris to take account of the changed position of the css
+ * file, while keeping the uri relative. This can handle symlinked document roots, but is not
+ * as extensively tested.
+ *
+ * None:
+ * This option turns off all uri rewriting.
+ */
+ 'css_uri_rewriter' => 'absolute',
+
+ /**
* Groups of scripts.
* You can predefine groups of js and css scripts which can be enabled/disabled
* and rendered.

0 comments on commit 8ce2431

Please sign in to comment.