Permalink
Browse files

Merge branch 'release/1.14'

* release/1.14:
  Update changelog
  Bump version to v1.13
  Add reporter (gnodeb) to contributers
  Fix bug with combine() and minified files.
  Document new gen_tags option
  Add gen_tags option to render_js and render_css.
  • Loading branch information...
2 parents 1493087 + e9f8a08 commit 170c32b7f19a4df5201e4eebe4de04a5f4e59ce7 @canton7 committed Feb 17, 2012
Showing with 133 additions and 41 deletions.
  1. +1 −1 bootstrap.php
  2. +5 −0 changelog.md
  3. +80 −33 classes/casset.php
  4. +1 −1 classes/casset/csscompressor.php
  5. +1 −1 classes/casset/cssurirewriter.php
  6. +1 −1 classes/casset/jsmin.php
  7. +1 −1 config/casset.php
  8. +9 −3 quickref.md
  9. +34 −0 readme.md
View
@@ -4,7 +4,7 @@
* Casset: Convenient asset library for FuelPHP.
*
* @package Casset
- * @version v1.13
+ * @version v1.14
* @author Antony Male
* @license MIT License
* @copyright 2011 Antony Male
View
@@ -3,6 +3,11 @@ Changelog
This file lists the important changes between versions. For a list of minor changes, check the log.
+v1.14
+-----
+ - Add gen_tags option to render_js and render_css.
+ - Fix bug with combine() and groups containing pre-minified files.
+
v1.13
-----
- Add quickref.md -- ideal for quickly accessing the API, assuming you know roughly what you're looking for.
View
@@ -4,7 +4,7 @@
* Casset: Convenient asset library for FuelPHP.
*
* @package Casset
- * @version v1.13
+ * @version v1.14
* @author Antony Male
* @license MIT License
* @copyright 2011 Antony Male
@@ -717,22 +717,35 @@ public static function render($group = false, $inline_dep = null, $attr = array(
* Renders the specific javascript group, or all groups if no group specified.
*
* @param string $group Which group to render. If omitted renders all groups
+ * @param array $options An array of options to use. Only supported option is 'gen_tags'
* @param bool $inline_dep DEPRECATED. If true, the result is printed inline. If false, is
* written to a file and linked to. In fact, $inline = true also causes
* a cache file to be written for speed purposes
* @return string The javascript tags to be written to the page
*/
- public static function render_js($group = false, $inline_dep = null, $attr_dep = array())
+ public static function render_js($group = false, $options = array(), $attr_dep = array())
{
// Don't force the user to remember that false is used for ommitted non-bool arguments
if (!is_string($group))
$group = false;
if (!is_array($attr_dep))
$attr_dep = array();
+ // Backwards compat: We used to have $inline where $options currently sits
+ // So, if $options isn't an array, then assume they're still passing $inline
+ $inline_dep = null;
+ if (!is_array($options))
+ {
+ $inline_dep = $options;
+ $options = array();
+ }
+ $options += array(
+ 'gen_tags' => true,
+ );
$file_groups = static::files_to_render('js', $group);
- $ret = '';
+ // If not generating tags, return an array of content
+ $ret = ($options['gen_tags']) ? '' : array();
foreach ($file_groups as $group_name => $file_group)
{
@@ -751,36 +764,51 @@ public static function render_js($group = false, $inline_dep = null, $attr_dep =
if (static::$groups['js'][$group_name]['combine'])
{
$filename = static::combine('js', $file_group, static::$groups['js'][$group_name]['min'], $inline);
- if (!$inline && static::$show_files)
+ if (!$inline && static::$show_files && $options['gen_tags'])
{
$ret .= '<!--'.PHP_EOL.'Group: '.$group_name.PHP_EOL.implode('', array_map(function($a){
return "\t".$a['file'].PHP_EOL;
}, $file_group)).'-->'.PHP_EOL;
}
if ($inline)
- $ret .= html_tag('script', $attr, PHP_EOL.file_get_contents(DOCROOT.static::$cache_path.$filename).PHP_EOL).PHP_EOL;
+ {
+ $content = file_get_contents(DOCROOT.static::$cache_path.$filename);
+ if ($options['gen_tags'])
+ $ret .= html_tag('script', $attr, PHP_EOL.$content.PHP_EOL).PHP_EOL;
+ else
+ $ret[] = $content;
+ }
+
else
{
- $filepath = static::process_filepath(static::$cache_path.$filename, 'js');
- $ret .= html_tag('script', array(
- 'src' => static::$asset_url.$filepath,
- )+$attr, '').PHP_EOL;
+ $filepath = static::$asset_url.static::process_filepath(static::$cache_path.$filename, 'js');
+ if ($options['gen_tags'])
+ $ret .= html_tag('script', array('src' => $filepath,)+$attr, '').PHP_EOL;
+ else
+ $ret[] = $filepath;
}
}
else
{
foreach ($file_group as $file)
{
if ($inline)
- $ret .= html_tag('script', $attr, PHP_EOL.file_get_contents($file['file']).PHP_EOL).PHP_EOL;
+ {
+ $content = static::load_file($file['file'], 'js');
+ if ($options['gen_tags'])
+ $ret .= html_tag('script', $attr, PHP_EOL.$content.PHP_EOL).PHP_EOL;
+ else
+ $ret[] = $content;
+ }
else
{
$remote = (strpos($file['file'], '//') !== false);
$base = ($remote) ? '' : static::$asset_url;
- $filepath = static::process_filepath($file['file'], 'js', $remote);
- $ret .= html_tag('script', array(
- 'src' => $base.$filepath,
- )+$attr, '').PHP_EOL;
+ $filepath = $base.static::process_filepath($file['file'], 'js', $remote);
+ if ($options['gen_tags'])
+ $ret .= html_tag('script', array('src' => $filepath,)+$attr, '').PHP_EOL;
+ else
+ $ret[] = $filepath;
}
}
}
@@ -792,22 +820,32 @@ public static function render_js($group = false, $inline_dep = null, $attr_dep =
* Renders the specific css group, or all groups if no group specified.
*
* @param string $group Which group to render. If omitted renders all groups
- * @param bool $inline DEPRECATED. If true, the result is printed inline. If false, is
- * written to a file and linked to. In fact, $inline = true also causes
- * a cache file to be written for speed purposes
+ * @param array $options ANnarray of options to use. Only supported option is 'gen_tags'
* @return string The css tags to be written to the page
*/
- public static function render_css($group = false, $inline_dep = null, $attr_dep = array())
+ public static function render_css($group = false, $options = array(), $attr_dep = array())
{
// Don't force the user to remember that false is used for ommitted non-bool arguments
if (!is_string($group))
$group = false;
if (!is_array($attr_dep))
$attr_dep = array();
+ // Backwards compat: We used to have $inline where $options currently sits
+ // So, if $options isn't an array, then assume they're still passing $inline
+ $inline_dep = null;
+ if (!is_array($options))
+ {
+ $inline_dep = $options;
+ $options = array();
+ }
+ $options += array(
+ 'gen_tags' => true,
+ );
$file_groups = static::files_to_render('css', $group);
- $ret = '';
+ // If not generating tags, return an array of content
+ $ret = ($options['gen_tags']) ? '' : array();
foreach ($file_groups as $group_name => $file_group)
{
@@ -827,21 +865,27 @@ public static function render_css($group = false, $inline_dep = null, $attr_dep
if (static::$groups['css'][$group_name]['combine'])
{
$filename = static::combine('css', $file_group, static::$groups['css'][$group_name]['min'], $inline);
- if (!$inline && static::$show_files)
+ if (!$inline && static::$show_files && $options['gen_tags'])
{
$ret .= '<!--'.PHP_EOL.'Group: '.$group_name.PHP_EOL.implode('', array_map(function($a){
return "\t".$a['file'].PHP_EOL;
}, $file_group)).'-->'.PHP_EOL;
}
if ($inline)
- $ret .= html_tag('style', $attr, PHP_EOL.file_get_contents(DOCROOT.static::$cache_path.$filename).PHP_EOL).PHP_EOL;
+ {
+ $content = file_get_contents(DOCROOT.static::$cache_path.$filename);
+ if ($options['gen_tags'])
+ $ret .= html_tag('style', $attr, PHP_EOL.$content.PHP_EOL).PHP_EOL;
+ else
+ $ret[] = $content;
+ }
else
{
- $filepath = static::process_filepath(static::$cache_path.$filename, 'css');
- $ret .= html_tag('link', array(
- 'rel' => 'stylesheet',
- 'href' => static::$asset_url.$filepath,
- )+$attr).PHP_EOL;
+ $filepath = static::$asset_url.static::process_filepath(static::$cache_path.$filename, 'css');
+ if ($options['gen_tags'])
+ $ret .= html_tag('link', array('rel' => 'stylesheet', 'href' => $filepath)+$attr).PHP_EOL;
+ else
+ $ret[] = $filepath;
}
}
else
@@ -851,17 +895,20 @@ public static function render_css($group = false, $inline_dep = null, $attr_dep
if ($inline)
{
$content = static::load_file($file['file'], 'css');
- $ret .= html_tag('style', $attr, PHP_EOL.$content.PHP_EOL).PHP_EOL;
+ if ($options['gen_tags'])
+ $ret .= html_tag('style', $attr, PHP_EOL.$content.PHP_EOL).PHP_EOL;
+ else
+ $ret[] = $content;
}
else
{
$remote = (strpos($file['file'], '//') !== false);
$base = ($remote) ? '' : static::$asset_url;
- $filepath = static::process_filepath($file['file'], 'css', $remote);
- $ret .= html_tag('link', array(
- 'rel' => 'stylesheet',
- 'href' => $base.$filepath,
- )+$attr).PHP_EOL;
+ $filepath = $base.static::process_filepath($file['file'], 'css', $remote);
+ if ($options['gen_tags'])
+ $ret .= html_tag('link', array('rel' => 'stylesheet', 'href' => $filepath)+$attr).PHP_EOL;
+ else
+ $ret[] = $filepath;
}
}
}
@@ -1072,7 +1119,7 @@ protected static function combine($type, $file_group, $minify, $inline)
if (static::$show_files_inline)
$content .= PHP_EOL.'/* '.$file['file'].' */'.PHP_EOL.PHP_EOL;
if ($file['minified'] || !$minify)
- $content = static::load_file($file['file'], $type, $file_group).PHP_EOL;
+ $content .= static::load_file($file['file'], $type, $file_group).PHP_EOL;
else
{
$file_content = static::load_file($file['file'], $type, $file_group);
@@ -17,7 +17,7 @@
* This library is used as part of Casset.
*
* @package Casset
- * @version v1.13
+ * @version v1.14
* @author Antony Male
* @license MIT License
* @link http://github.com/canton7/fuelphp-casset
@@ -10,7 +10,7 @@
* This library is used as part of Casset.
*
* @package Casset
- * @version v1.13
+ * @version v1.14
* @author Antony Male
* @license MIT License
* @link http://github.com/canton7/fuelphp-casset
View
@@ -52,7 +52,7 @@
/**
* This library is used as part of Casset.
* @package Casset
- * @version v1.13
+ * @version v1.14
* @author Antony Male
* @license MIT License
* @link http://github.com/canton7/fuelphp-casset
View
@@ -3,7 +3,7 @@
* Casset: Convenient asset library for FuelPHP.
*
* @package Casset
- * @version v1.13
+ * @version v1.14
* @author Antony Male
* @license MIT License
* @copyright 2011 Antony Male
View
@@ -298,28 +298,34 @@ A shortcut for `get_filepath`, with the type set to 'img'.
Renders all enabled js/css groups, or just the named groups (provided that they are enabled).
Returns a set of `<script>`/`<link>` tag(s), as required.
-`string render($group = false)`.
+`string render($group = false, $options = array())`.
`$group`: If false/null, render all groups. If a string, render just the named group.
+`$options': A set of options to control how/if the tags are rendered. See the docs for details.
+
<a name="qr-render-js"></a>
### render_js
Renders all enabled js groups, or just the named groups (provided that they are enabled).
Returns a set of `<script>` tag(s), as required.
-`string render($group = false)`.
+`string render($group = false, $options = array())`.
`$group`: If false/null, render all groups. If a string, render just the named group.
+`$options': A set of options to control how/if the tags are rendered. See the docs for details.
+
<a name="qr-render-css"></a>
### render_css
Renders all enabled css groups, or just the named groups (provided that they are enabled).
Returns a set of `<link>` tag(s), as required.
-`string render($group = false)`.
+`string render($group = false, $options = array())`.
`$group`: If false/null, render all groups. If a string, render just the named group.
+`$options': A set of options to control how/if the tags are rendered. See the docs for details.
+
<a name="qr-render-js-inline"></a>
### render_js_inline
Renders all js inline content, which was added with `js_inline`.
View
@@ -638,6 +638,39 @@ Casset::get_filepath($name, $type, $add_url = false, $force_array = false);
`$name`, `$add_url` and `$force_array` are the same as for `Casset::get_filepath_js()`, while the `$type` argument is one of 'js', 'css', or 'img'.
In the future there are plans to let you specify your own types, hence why this is exposed :)
+Controlling whether tags are generated
+--------------------------------------
+
+Thanks to antitoxic for motivating this feature.
+
+The `get_filepath_*` functions are useful if you want to link directly to an asset.
+However, this doesn't cover the case where you want to, for whatever reason, generate your own `<script>` or `<link>` tags, while still keeping Casset's minification/combining functionality.
+
+The solution to this is to passed `array('gen_tags' => false)` as the second argument of `render_css()`/`render_js()`.
+This will make `render_*` return an array of filenames / file contents (depending on whether you've turned on inlining for that group), rather then a string of tags (and maybe content).
+
+For example:
+
+```php
+Casset::js('test_file.js');
+Casset::render_js(false);
+// Returns <script type="text/javascript" src="http://....test_file.js"></script>
+Casset::render_js(false, array('gen_tags' => false));
+// Returns Array (
+// [0] => "http://....test_file.js"
+// )
+
+Casset::set_group_option('js', 'global', 'inline', true);
+Casset::render_js(false);
+// Returns <script type="text/javascript>Some javascript file content</script>
+Casset::render_js(false, array('gen_tags' => false));
+// Returns Array (
+// [0] => "Some javascript file content"
+// )
+```
+
+If more than one `<script>`/`<link>` tag would normally be generated, the array return will contain more than one element.
+
Clearing the cache
------------------
Since cache files are not automatically removed (Casset has no way of knowing whether a cache file might be needed again), a few methods have been provided to remove cache files.
@@ -875,6 +908,7 @@ The following people have helped Casset become what it is, so thank you!
- [Chris Meller](https://github.com/chrismeller)
- [monsonis](https://github.com/monsonis)
- [Anton Stoychev](https://github.com/antitoxic)
+ - [gnodeb](https://github.com/gnodeb)
Contributing
------------

0 comments on commit 170c32b

Please sign in to comment.