From beab3105ef00c15dd1c0546f1151c5f246594a41 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 19:47:34 +0100 Subject: [PATCH 01/12] Remove '?$mtime' from filename when rending the file inline. Was causing a bug where the file couldn't be opened for reading. --- classes/casset.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/classes/casset.php b/classes/casset.php index ab848df..6989366 100644 --- a/classes/casset.php +++ b/classes/casset.php @@ -390,7 +390,7 @@ public function render_js($group = false, $inline = false, $attr = array(), $min { if ($min) { - $filename = static::combine_and_minify('js', $file_group); + $filename = static::combine_and_minify('js', $file_group, $inline); if (!$inline && static::$show_files) { $ret .= ''.PHP_EOL; } if ($inline) - $ret .= html_tag('script', array('type' => 'text/javascript')+$attr, PHP_EOL.file_get_contents(DOCROOT.static::$cache_path.'/'.$filename).PHP_EOL).PHP_EOL; + $ret .= html_tag('script', array('type' => 'text/javascript')+$attr, PHP_EOL.file_get_contents(DOCROOT.static::$cache_path.$filename).PHP_EOL).PHP_EOL; else $ret .= html_tag('script', array( 'type' => 'text/javascript', @@ -454,7 +454,7 @@ public function render_css($group = false, $inline = false, $attr = array(), $mi }, $file_group)).'-->'.PHP_EOL; } if ($inline) - $ret .= html_tag('style', array('type' => 'text/css')+$attr, PHP_EOL.file_get_contents(DOCROOT.static::$cache_path.'/'.$filename).PHP_EOL).PHP_EOL; + $ret .= html_tag('style', array('type' => 'text/css')+$attr, PHP_EOL.file_get_contents(DOCROOT.static::$cache_path.$filename).PHP_EOL).PHP_EOL; else $ret .= html_tag('link', array( 'rel' => 'stylesheet', From f7ba31de99ce2c6a73ea9a3bfd2aea21e14ab0ea Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 21:57:06 +0100 Subject: [PATCH 03/12] Add "Comparison to Assetic" section to the readme. --- readme.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/readme.md b/readme.md index 64dec51..92b4084 100644 --- a/readme.md +++ b/readme.md @@ -242,6 +242,17 @@ Casset::clear_cache('yesterday'); // Removes all cache files last modified yesterday ``` +Comparison to Assetic +--------------------- + +A frequent question is how Casset differs from kriswallsmith's [Assetic](https://github.com/fuel-packages/fuel-assetic). InCasset and Assetic have completely different goals. + +* Assetic is a very powerful asset mangement framework. It allows you to perform minification, compression and compilation on your assets, although learning it will take time. +* Casset is designed to make assets very easy to handle. You call `Casset::js()` then `Casset::render_js()`, and everything is taken care of. + +If you're a developer tasked with fully optimising your site's page load time, for example, go with Assetic. If you want a very easy way to manage your assets, with some minification +thrown in for free, (and have no need for Assetic's complex features), go with Casset. + Examples -------- From 4030a89de0d34f3a164bc672a9d3eafe1a7b3f1d Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 19:46:41 +0100 Subject: [PATCH 04/12] Allow the user to enter any value for some function argument defaults, rather than remember that eg 'false' must be used. --- classes/casset.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/classes/casset.php b/classes/casset.php index a48b3b6..c100121 100644 --- a/classes/casset.php +++ b/classes/casset.php @@ -323,6 +323,10 @@ public static function css($sheet, $sheet_min = false, $group = 'global') */ private static function add_asset($type, $script, $script_min, $group) { + // Don't force the user to remember that 'false' is used when not supplying + // a pre-minified file. + if (!is_string($script_min)) + $script_min = false; if (!array_key_exists($group, static::$groups[$type])) { // Assume they want the group enabled @@ -379,6 +383,11 @@ private static function add_asset_inline($type, $content) */ public function render_js($group = false, $inline = false, $attr = array(), $min = null) { + // 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)) + $attr = array(); if ($min === null) $min = static::$min; @@ -435,6 +444,11 @@ public function render_js($group = false, $inline = false, $attr = array(), $min */ public function render_css($group = false, $inline = false, $attr = array(), $min = null) { + // 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)) + $attr = array(); if ($min === null) $min = static::$min; From b951dc1b3b6b217fbd62ebb47f587b55f18b26e9 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 22:06:01 +0100 Subject: [PATCH 05/12] Document the ability to pass any non-x (bool, array) value for some default function arguments. --- readme.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 92b4084..5470bf2 100644 --- a/readme.md +++ b/readme.md @@ -128,6 +128,8 @@ Casset::js('myfile.js', 'myfile.min.js', 'group_name'); Casset::css('myfile.css', false, 'group_name'); ``` +(As an aside, you can pass any non-string value instead of 'false' in the second example, and Casset will behave the same: generate your minified file for you.) + Groups can also be declared on the fly, by specifying a group name which doesn't yet exist. The group is assumed to be enabled. You can also use a slightly more involved syntax for creating groups, which allows you to specify multiple files and whether the group is enabled, as shown below: @@ -213,9 +215,11 @@ Similarly, you can choose to put comments inside each minified file, saying whic The following will minify the 'group_name' group, even if minification is turned off in the config file. ```php -echo Casset::render_js('group_name', false, array(), true); +echo Casset::render_js(false, false, array(), true); ``` +(Again, you can pass any non-string value for the first argument, and any non-array value for the third, and Casset will treat them the same as if the default argument (false and array() respectively) had been passed.) + When minifying CSS files, urls are rewritten to take account of the fact that your css file has effectively moved into `public/assets/cache`. With JS files, changing the order in which files were added to the group will re-generate the cache file, with the files in their new positions. However with CSS, it will not. From cbb6e6e75a77aa3d73cbc3cf8b51706c062e755c Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 22:12:08 +0100 Subject: [PATCH 06/12] Add instrunctions on how to load casset to the readme. --- readme.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 5470bf2..f26f476 100644 --- a/readme.md +++ b/readme.md @@ -15,7 +15,8 @@ Installation 2. Stick in fuel/packages/ 3. Optionally edit fuel/packages/casset/config/casset.php (the defaults are sensible) 4. Create public/assets/cache -5. Enjoy +5. Add 'casset' to the 'always_load/packages' array in app/config/config.php (or call `Fuel::add_package('casset')` whenever you want to use it). +6. Enjoy :) Basic usage ----------- From 580260995131d72f31d788adceb7a0056a36c664 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 22:21:00 +0100 Subject: [PATCH 07/12] Some functions weren't declared static. PHP man claims E_STICT should be thrown, but that didn't happen. Fixed anyway! --- classes/casset.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/casset.php b/classes/casset.php index c100121..a0ed24a 100644 --- a/classes/casset.php +++ b/classes/casset.php @@ -182,7 +182,7 @@ public static function add_group($group_type, $group_name, $files, $enabled = tr * @param string $asset_type 'css', 'js' or 'img' * @return string The path to the asset, relative to $asset_url */ - public function find_file($file, $asset_type) + public static function find_file($file, $asset_type) { if (strpos($file, '//') === false) { @@ -381,7 +381,7 @@ private static function add_asset_inline($type, $content) * @param bool $min True to minify the javascript files. null to use the config value * @return string The javascript tags to be written to the page */ - public function render_js($group = false, $inline = false, $attr = array(), $min = null) + public static function render_js($group = false, $inline = false, $attr = array(), $min = null) { // Don't force the user to remember that false is used for ommitted non-bool arguments if (!is_string($group)) @@ -442,7 +442,7 @@ public function render_js($group = false, $inline = false, $attr = array(), $min * @param bool $min True to minify the css files. null to use the config value * @return string The css tags to be written to the page */ - public function render_css($group = false, $inline = false, $attr = array(), $min = null) + public static function render_css($group = false, $inline = false, $attr = array(), $min = null) { // Don't force the user to remember that false is used for ommitted non-bool arguments if (!is_string($group)) From bcda3476440e7bb826ef2c066c1a05622b052e63 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 22:25:51 +0100 Subject: [PATCH 08/12] Create render() function, which calls render_css() then render_js(). --- classes/casset.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/classes/casset.php b/classes/casset.php index a0ed24a..70643f5 100644 --- a/classes/casset.php +++ b/classes/casset.php @@ -371,6 +371,23 @@ private static function add_asset_inline($type, $content) array_push(static::$inline_assets[$type], $content); } + /** + * Shortcut to render_js() and render_css(). + * + * @param string $group Which group to render. If omitted renders all groups + * @param bool $inline 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 bool $min True to minify the javascript files. null to use the config value + * @return string The javascript tags to be written to the page + */ + public static function render($group = false, $inline = false, $attr = array(), $min = null) + { + $r = static::render_css($group, $inline, $attr, $min); + $r.= static::render_js($group, $inline, $attr, $min); + return $r; + } + /** * Renders the specific javascript group, or all groups if no group specified. * From 78461877d4f26daf7363b64026a967fac6d5aa64 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 22:27:44 +0100 Subject: [PATCH 09/12] Document render() function in readme --- readme.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/readme.md b/readme.md index f26f476..bc57038 100644 --- a/readme.md +++ b/readme.md @@ -56,6 +56,8 @@ can pass this as the second argument, eg: Casset::js('myfile.js', 'myfile.min.js'); ``` +Some folks like css and js tags to be together. `Casset::render()` is a shortcut which calls `Casset::render_css()` then `Casset::render_js()`. + Images ------ From 46414ba31712a7b277db30a636e63a0b7eef4081 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 22:29:13 +0100 Subject: [PATCH 10/12] Remove declaration of 'global' group. The upshot of this is that the global group is now created when a file is first added to it, rather than at the beginning of the program. Since groups are rendered in the order in which they appear in the groups array, this means that non-global groups can be rendered first, which might be useful for satisfying dependancies. --- classes/casset.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/classes/casset.php b/classes/casset.php index 70643f5..f601262 100644 --- a/classes/casset.php +++ b/classes/casset.php @@ -45,8 +45,8 @@ class Casset { * @var array Holds groups of assets. Is documenented fully in the config file. */ protected static $groups = array( - 'css' => array('global' => array('files' => array(), 'enabled' => true)), - 'js' => array('global' => array('files' => array(), 'enabled' => true)), + 'css' => array(), + 'js' => array(), ); /** From 51a41f479abd4c4f3bbda677ac47d6e144e6c60b Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 22:35:20 +0100 Subject: [PATCH 11/12] Document order in which files and groups are rendered, and the consequences for satisfying JS deps. --- readme.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/readme.md b/readme.md index bc57038..c199974 100644 --- a/readme.md +++ b/readme.md @@ -140,6 +140,11 @@ You can also use a slightly more involved syntax for creating groups, which allo Casset::add_group('js', 'group_name', array('file1.js', array('file2.js', 'file2.min.js')), $enabled); ``` +When you call `Casset::render()` (or the js- and css-specific varients), the order that groups are rendered is determined by the order in which they were created, with groups present in the config file appearing first. +Similarly (for JS files only), the order in which files appear is determined by the order in which they were added. +This allows you a degree of control over what order your files are included in your page, which may be necessary when satisfying dependancies. +If this isn't working for you, or you want something a bit more explicit, try this: If file A depends on B, add B to its own group and explicitely render it first. + NOTE: Calling ``Casset::js('file.js')`` will add that file to the "global" group. Use / abuse as you need! From a7274e7115b169e897dd7db247f6118ffdc5df87 Mon Sep 17 00:00:00 2001 From: Antony Male Date: Sat, 11 Jun 2011 22:41:02 +0100 Subject: [PATCH 12/12] Bump version to v1.3 --- classes/casset.php | 2 +- classes/casset/csscompressor.php | 2 +- classes/casset/cssurirewriter.php | 2 +- classes/casset/jsmin.php | 2 +- config/casset.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/classes/casset.php b/classes/casset.php index f601262..82ef920 100644 --- a/classes/casset.php +++ b/classes/casset.php @@ -4,7 +4,7 @@ * Casset: Convenient asset library for FuelPHP. * * @package Casset - * @version v1.2 + * @version v1.3 * @author Antony Male * @license MIT License * @copyright 2011 Antony Male diff --git a/classes/casset/csscompressor.php b/classes/casset/csscompressor.php index dbe7785..210db8a 100644 --- a/classes/casset/csscompressor.php +++ b/classes/casset/csscompressor.php @@ -17,7 +17,7 @@ * This library is used as part of Casset. * * @package Casset - * @version v1.2 + * @version v1.3 * @author Antony Male * @license MIT License * @link http://github.com/canton7/fuelphp-casset diff --git a/classes/casset/cssurirewriter.php b/classes/casset/cssurirewriter.php index 6ab0fd5..b0ed173 100644 --- a/classes/casset/cssurirewriter.php +++ b/classes/casset/cssurirewriter.php @@ -10,7 +10,7 @@ * This library is used as part of Casset. * * @package Casset - * @version v1.2 + * @version v1.3 * @author Antony Male * @license MIT License * @link http://github.com/canton7/fuelphp-casset diff --git a/classes/casset/jsmin.php b/classes/casset/jsmin.php index ee943f5..2b75c3b 100644 --- a/classes/casset/jsmin.php +++ b/classes/casset/jsmin.php @@ -52,7 +52,7 @@ /** * This library is used as part of Casset. * @package Casset - * @version v1.2 + * @version v1.3 * @author Antony Male * @license MIT License * @link http://github.com/canton7/fuelphp-casset diff --git a/config/casset.php b/config/casset.php index d8dae0d..ce68f26 100644 --- a/config/casset.php +++ b/config/casset.php @@ -4,7 +4,7 @@ * Casset: Convenient asset library for FuelPHP. * * @package Casset - * @version v1.2 + * @version v1.3 * @author Antony Male * @license MIT License * @copyright 2011 Antony Male