Permalink
Browse files

Merge branch 'release/1.7'

* release/1.7:
  Bump version to v1.7
  Check in add_group to avoid overwriting the group if it exists already.
  Remote add_group documentation from the readme
  Make add_group private, as it no longer does anything useful to the end-user.
  Minor typo-fixes and reformatting of readme
  Move paths and namespacing and globbing above inlining and extra attributes in the readme
  Document globbing in the readme.
  Add globbing to filenames. That is, the following will now work: Casset::js('*.js'); // Adds assets/js/*.js Casset::css('admin::admin_*.css'); // Adds assets/admin/css/admin_*.css, assuming config'ed correctly.
  OOPS! If no files were in the config for a group, the first file added to that group would be added under the 'enabled' key... Meaning it would never show up.
  Fix typo in readme to do with namespace paths
  • Loading branch information...
2 parents 1cc4d6b + 746cdc3 commit 0dce6be7848d3e6bbce0d56ad2b17aab4184d07f @canton7 canton7 committed Jun 29, 2011
Showing with 108 additions and 89 deletions.
  1. +1 −1 bootstrap.php
  2. +18 −14 classes/casset.php
  3. +1 −1 classes/casset/csscompressor.php
  4. +1 −1 classes/casset/cssurirewriter.php
  5. +1 −1 classes/casset/jsmin.php
  6. +1 −1 config/casset.php
  7. +85 −70 readme.md
View
@@ -4,7 +4,7 @@
* Casset: Convenient asset library for FuelPHP.
*
* @package Casset
- * @version v1.6
+ * @version v1.7
* @author Antony Male
* @license MIT License
* @copyright 2011 Antony Male
View
@@ -4,7 +4,7 @@
* Casset: Convenient asset library for FuelPHP.
*
* @package Casset
- * @version v1.6
+ * @version v1.7
* @author Antony Male
* @license MIT License
* @copyright 2011 Antony Male
@@ -169,8 +169,11 @@ public static function set_path($path_key = 'core')
* @param bool $enabled Whether the group is enabled. Enabled groups will be
* rendered with render_js / render_css
*/
- public static function add_group($group_type, $group_name, $enabled = true)
+ private static function add_group($group_type, $group_name, $enabled = true)
{
+ // If it already exists, don't overwrite it
+ if (array_key_exists($group_name, static::$groups[$group_type]))
+ return;
static::$groups[$group_type][$group_name] = array(
'files' => array(),
'enabled' => $enabled,
@@ -179,7 +182,6 @@ public static function add_group($group_type, $group_name, $enabled = true)
/**
* Searches the asset paths to locate a file.
- * Throws an exception if the asset can't be found.
*
* @param string $file The name of the asset to search for
* @param string $asset_type 'css', 'js' or 'img'
@@ -338,12 +340,9 @@ private static function add_asset($type, $script, $script_min, $group)
if (!array_key_exists($group, static::$groups[$type]))
{
// Assume they want the group enabled
- static::add_group($type, $group, array($files), true);
- }
- else
- {
- array_push(static::$groups[$type][$group]['files'], $files);
+ static::add_group($type, $group, true);
}
+ array_push(static::$groups[$type][$group]['files'], $files);
}
/**
@@ -562,17 +561,22 @@ private static function files_to_render($type, $group, $min)
{
if ($min)
{
- $file = static::find_file(($file_set[1]) ? $file_set[1] : $file_set[0], $type);
+ $file_pattern = static::find_file(($file_set[1]) ? $file_set[1] : $file_set[0], $type);
$minified = ($file_set[1] != false);
}
else
{
- $file = static::find_file($file_set[0], $type);
+ $file_pattern = static::find_file($file_set[0], $type);
+ }
+ $glob_files = glob($file_pattern);
+ if (!$glob_files || !count($glob_files))
+ throw new \Fuel_Exception("Found no files matching $file_pattern");
+ foreach ($glob_files as $file) {
+ array_push($files[$group_name], array(
+ 'file' => $file,
+ 'minified' => $minified,
+ ));
}
- array_push($files[$group_name], array(
- 'file' => $file,
- 'minified' => $minified,
- ));
}
// In javascript, file order is important (as there might be deps)
// However in CSS it isn't, so we can safely take any order of css files
@@ -17,7 +17,7 @@
* This library is used as part of Casset.
*
* @package Casset
- * @version v1.6
+ * @version v1.7
* @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.6
+ * @version v1.7
* @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.6
+ * @version v1.7
* @author Antony Male
* @license MIT License
* @link http://github.com/canton7/fuelphp-casset
View
@@ -4,7 +4,7 @@
* Casset: Convenient asset library for FuelPHP.
*
* @package Casset
- * @version v1.6
+ * @version v1.7
* @author Antony Male
* @license MIT License
* @copyright 2011 Antony Male
View
155 readme.md
@@ -132,7 +132,7 @@ array element.
**enabled**: Whether a group is enabled. A group will only be rendered when it is enabled.
Groups can be enabled using `Casset::enable_js('group_name')`, and disabled using `Casset::disable_js('group_name')`. CSS equivalents also exist.
-The shortcuts `Casset::enable('group_name')` and `Casset::disable('group_name')` also exist, which will enable/disable both the js and css groups of the given name, if they exist.
+The shortcuts `Casset::enable('group_name')` and `Casset::disable('group_name')` also exist, which will enable/disable both the js and css groups of the given name, if they are defined.
You can also pass an array of groups to enable/disable.
Specific groups can be rendered using eg `Casset::render_js('group_name')`. If no group name is passed, *all* groups will be rendered.
@@ -147,12 +147,7 @@ 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.)
-If the group name doesn't exist, the group is created, and 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:
-
-```php
-Casset::add_group('js', 'group_name', array('file1.js', array('file2.js', 'file2.min.js')), $enabled);
-```
+If the group name doesn't exist, the group is created, and 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.
@@ -161,6 +156,86 @@ If this isn't working for you, or you want something a bit more explicit, try th
NOTE: Calling ``Casset::js('file.js')`` will add that file to the "global" group. Use / abuse as you need!
+Paths and namespacing
+---------------------
+
+The Asset library searches through all of the items in the 'paths' config key until it finds the first matching file.
+However, this approach was undesirable, as it means that if you had the directory structure below, and tried to include 'index.js', the file that was included would be determined by the order of the
+entries in the paths array.
+
+```
+assets/
+ css/
+ js/
+ index.js
+ img/
+ admin/
+ css/
+ js/
+ index.js
+ img/
+```
+
+Casset brings decent namespacing to the rescue!
+For the above example, you can specify the following in your config file:
+
+```
+'paths' => array(
+ 'core' => 'assets/',
+ 'admin' => 'assets/admin/',
+),
+```
+
+Which path to use is then decided by prefixing the asset filename with the key of the path to use. Note that if you omit the path key, the current default path key (initially 'core') is used.
+
+```php
+Casset::js('index.js');
+// Or
+Casset::js('core::index.js');
+// Will add assets/js/index.js
+
+Casset::js('admin::index.js');
+// Will add assets/admin/js/index.js
+
+echo Casset::img('test.png', 'An image');
+// <img src="...assets/img/test.png" alt="An image" />
+
+echo Casset::img('admin::test.png', 'An image');
+// <img src="...assets/admin/img/test.png" alt="An image" />
+```
+
+If you wish, you can change the current default path key using `Casset::set_path('path_key')`. This can be useful if you know that all of the assets in a given file will be from a given path. For example:
+
+```php
+Casset::set_path('admin);
+Casset::js('index.js');
+// Will add assets/admin/js/index.js
+```
+
+The "core" path can be restored by calling `Casset::set_path()` with no arguments (or calling `Casset::set_path('core')`).
+
+You can also namespace the files listed in the config file's 'groups' section, in the same manner.
+Note that these are loaded before the namespace is changed from 'core', so any files not in the core namespace will have to by explicitely prepended with the namespace name.
+
+Globbing
+--------
+
+As well as filenames, you can specify [http://php.net/glob](glob patterns). This will act exactly the same as if each file which the glob matches had been added individually.
+For example:
+
+```php
+Casset::css('*.css');
+// Runs glob('assets/css/*.css') and adds all matches.
+
+Casset::css('admin::admin_*.css');
+// (Assuming the paths configuration in the "Paths and namespacing" section)
+// Executes glob('adders/admin/css/admin_*.css') and adds all matches
+
+Casset::js('*.js', '*.js');
+// Adds all js files in assets/js, ensuring that none of them are minified.
+```
+
+An exception is thrown when no files can be matched.
Inlining
--------
@@ -252,74 +327,14 @@ the browser to properly use its cache.
NOTE: If you change the contents of a group, a new cache file will be generated. However the old one will not be removed (groups are mutable, so cassed doesn't know whether a page still uses the old cache file).
Therefore an occasional clearout of `public/assets/cache/` is recommended. See the section below on clearing the cache.
-Paths and namespacing
----------------------
-
-The Asset library searches through all of the items in the 'paths' config key until it finds the first matching file.
-However, this approach was undesirable, as it means that if you had the directory structure below, and tried to include 'index.js', the file that was included would be determined by the order of the
-entries in the paths array.
-
-```
-assets/
- css/
- js/
- index.js
- img/
- admin/
- css/
- js/
- index.js
- img/
-```
-
-Casset brings decent namespacing to the rescue!
-For the above example, you can specify the following in your config file:
-
-```
-'paths' => array(
- 'core' => 'assets/',
- 'admin' => 'assets/admin',
-),
-```
-
-Which path to use is then decided by prefixing the asset filename with the key of the path to use. Note that if you omit the path key, the current default path key (initially 'core') is used.
-
-```php
-Casset::js('index.js');
-// Or
-Casset::js('core::index.js');
-// Will add assets/js/index.js
-
-Casset::js('admin::index.js');
-// Will add assets/admin/js/index.js
-
-echo Casset::img('test.png', 'An image');
-// <img src="...assets/img/test.png" alt="An image" />
-
-echo Casset::img('admin::test.png', 'An image');
-// <img src="...assets/admin/img/test.png" alt="An image" />
-```
-
-If you wish, you can change the current default path key using `Casset::set_path('path_key')`. This can be useful if you know that all of the assets in a given file will be from a given path. For example:
-
-```php
-Casset::set_path('admin);
-Casset::js('index.js');
-// Will add assets/admin/js/index.js
-```
-
-The "core" path can be restored by calling `Casset::set_path()` with no arguments.
-
-You can also namespace the files listed in the config file's 'groups' section, in the same manner.
-Note that these are loaded before the namespace is changed from 'core', so any files not in the core namespace will have to by explicitely prepended with the namespace name.
-
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 method have been provided to remove cache files.
+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.
`Casset::clear_cache()` will clear all cache files, while `Casset::clear_js_cache()` and `Casset::clear_css_cache()` will remove just JS and CSS files respectively.
All of the above functions optionally accept an argument allowing you to only delete cache files last modified before a certain time. This time is specified as a
-[strtotime](http://php.net/strtotime)-formatted string, for example "2 hours ago", "last Tuesday", or "20110609". For example:
+[strtotime](http://php.net/strtotime)-formatted string, for example "2 hours ago", "last Tuesday", or "20110609".
+For example:
```php
Casset::clear_js_cache('2 hours ago');

0 comments on commit 0dce6be

Please sign in to comment.