Skip to content
Browse files

Merge branch 'release/1.2'

* release/1.2: (24 commits)
  Bump version to v1.2
  Bootstrap now properly aligned using spaces
  Forgot to add the docblock header to the config file
  Document cache-clearing in the readme.
  Add functions to remove cache files.
  Document file order stuff in the readme.
  File order is preserved for javascript files -- css files are sorted.
  Somehow namespacing was incorrect inside of jsmin. Fixed!
  Document change to enable()/disable() in readme.
  enable() and the rest of the family will now accept an array of groups as well as a single group.
  Add copyright notice and copy of MIT license.
  Move LICENSE.txt -> LICENSE
  Change installation instructions to take account of the fact that Casset is now a package.
  Add an updated docblock to the top of all files.
  Add very basic bootstrap.php, allowing Casset to behave as a fuel package.
  Namespace everything to the 'Casset' namespace.
  Changes to the minifications and examples sections of the readme, for clarity.
  Fix another mistake in the "extra attributes" section of the readme
  Readme example in basic usage of what will happen if minification is turned off.
  Update ther readme to reflect the changes to img().
  ...
  • Loading branch information...
2 parents 3f004f1 + 5e2f233 commit aa3ffe2bf0269fd7953451399a19f36d2c8ac90d @canton7 canton7 committed
View
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (C) <2011> by <Antony Male>
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
View
24 bootstrap.php
@@ -0,0 +1,24 @@
+<?php
+
+/**
+ * Casset: Convenient asset library for FuelPHP.
+ *
+ * @package Casset
+ * @version v1.1
+ * @author Antony Male
+ * @license MIT License
+ * @copyright 2011 Antony Male
+ * @link http://github.com/canton7/fuelphp-casset
+ */
+
+
+Autoloader::add_core_namespace('Casset');
+
+Autoloader::add_classes(array(
+ 'Casset\\Casset' => __DIR__.'/classes/casset.php',
+ 'Casset\\Casset_JSMin' => __DIR__.'/classes/casset/jsmin.php',
+ 'Casset\\Casset_Csscompressor' => __DIR__.'/classes/casset/csscompressor.php',
+ 'Casset\\Casset_Cssurirewriter' => __DIR__.'/classes/casset/cssurirewriter.php',
+));
+
+/* End of file bootstrap.php */
View
160 classes/casset.php
@@ -1,19 +1,17 @@
<?php
+
/**
- * Fuel is a fast, lightweight, community driven PHP5 framework.
- *
- * This library provides an alternative to the built-in Asset library.
- * This library has been based on the original Asset library, although
- * extensive modifications and additions have been made.
+ * Casset: Convenient asset library for FuelPHP.
*
* @package Casset
- * @version 1.0
+ * @version v1.2
* @author Antony Male
* @license MIT License
* @copyright 2011 Antony Male
* @link http://github.com/canton7/fuelphp-casset
*/
+namespace Casset;
class Casset {
@@ -92,26 +90,26 @@ public static function _init()
return;
}
- Config::load('casset', true);
+ \Config::load('casset', true);
- $paths = Config::get('casset.paths', array('assets/'));
+ $paths = \Config::get('casset.paths', array('assets/'));
foreach($paths as $path)
{
static::add_path($path);
}
- static::$asset_url = Config::get('casset.url', Config::get('base_url'));
+ static::$asset_url = \Config::get('casset.url', \Config::get('base_url'));
static::$folders = array(
- 'css' => Config::get('casset.css_dir', static::$folders['css']),
- 'js' => Config::get('casset.js_dir', static::$folders['js']),
- 'img' => Config::get('casset.img_dir', static::$folders['img']),
+ 'css' => \Config::get('casset.css_dir', static::$folders['css']),
+ 'js' => \Config::get('casset.js_dir', static::$folders['js']),
+ 'img' => \Config::get('casset.img_dir', static::$folders['img']),
);
- static::$cache_path = Config::get('casset.cache_path', static::$cache_path);
+ static::$cache_path = \Config::get('casset.cache_path', static::$cache_path);
- $group_sets = Config::get('casset.groups', array());
+ $group_sets = \Config::get('casset.groups', array());
foreach ($group_sets as $group_type => $groups)
{
@@ -121,10 +119,10 @@ public static function _init()
}
}
- static::$min = Config::get('casset.min', static::$min);
+ static::$min = \Config::get('casset.min', static::$min);
- static::$show_files = Config::get('casset.show_files', static::$show_files);
- static::$show_files_inline = Config::get('casset.show_files_inline', static::$show_files_inline);
+ static::$show_files = \Config::get('casset.show_files', static::$show_files);
+ static::$show_files_inline = \Config::get('casset.show_files_inline', static::$show_files_inline);
static::$initialized = true;
}
@@ -209,77 +207,83 @@ public function find_file($file, $asset_type)
/**
* Enables both js and css groups of the given name.
*
- * @param string $group The group to enable.
+ * @param mixed $group The group to enable, or array of groups
*/
- public static function enable($group)
+ public static function enable($groups)
{
- static::asset_enabled('js', $group, true);
- static::asset_enabled('css', $group, true);
+ static::asset_enabled('js', $groups, true);
+ static::asset_enabled('css', $groups, true);
}
/**
* Disables both js and css groups of the given name.
*
- * @param string $group The group to disable.
+ * @param string $group The group to disable, or array of groups
*/
- public static function disable($group)
+ public static function disable($groups)
{
- static::asset_enabled('js', $group, false);
- static::asset_enabled('css', $group, false);
+ static::asset_enabled('js', $groups, false);
+ static::asset_enabled('css', $groups, false);
}
/**
* Enable a group of javascript assets.
*
- * @param string $group The group to enable.
+ * @param string $group The group to enable, or array of groups
*/
- public static function enable_js($group)
+ public static function enable_js($groups)
{
- static::asset_enabled('js', $group, true);
+ static::asset_enabled('js', $groups, true);
}
/**
* Disable a group of javascript assets.
*
- * @param string $group The group to disable.
+ * @param string $group The group to disable, or array of groups
*/
- public static function disable_js($group)
+ public static function disable_js($groups)
{
- static::asset_enabled('js', $group, false);
+ static::asset_enabled('js', $groups, false);
}
/**
* Enable a group of css assets.
*
- * @param string $group The group to enable.
+ * @param string $group The group to enable, or array of groups
*/
- public static function enable_css($group)
+ public static function enable_css($groups)
{
- static::asset_enabled('css', $group, true);
+ static::asset_enabled('css', $groups, true);
}
/**
* Disable a group of css assets.
*
- * @param string $group The group to disable.
+ * @param string $group The group to disable, or array of groups
*/
- public static function disable_css($group)
+ public static function disable_css($groups)
{
- static::asset_enabled('css', $group, false);
+ static::asset_enabled('css', $groups, false);
}
/**
* Enables / disables an asset.
*
* @param string $type 'css' / 'js'
- * @param string $group The group to enable/disable
+ * @param string $group The group to enable/disable, or array of groups
* @param bool $enabled True to enabel to group, false odisable
*/
- private static function asset_enabled($type, $group, $enabled)
+ private static function asset_enabled($type, $groups, $enabled)
{
- if (!array_key_exists($group, static::$groups[$type]))
- return;
- static::$groups[$type][$group]['enabled'] = $enabled;
+ if (!is_array($groups))
+ $groups = array($groups);
+ foreach ($groups as $group)
+ {
+ // If the group doesn't exist it's of no consequence
+ if (!array_key_exists($group, static::$groups[$type]))
+ continue;
+ static::$groups[$type][$group]['enabled'] = $enabled;
+ }
}
/**
@@ -527,10 +531,13 @@ private static function files_to_render($type, $group, $min)
'minified' => $minified,
));
}
- // If minifying, sort by filename
- uasort($files[$group_name], function($a, $b) {
- return ($a['file'] > $b['file']) ? 1 : -1;
- });
+ // 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
+ // and stick them into the same cache file
+ if ($type == 'css')
+ uasort($files[$group_name], function($a, $b) {
+ return ($a['file'] > $b['file']) ? 1 : -1;
+ });
}
return $files;
}
@@ -547,10 +554,9 @@ private static function files_to_render($type, $group, $min)
*/
private static function combine_and_minify($type, $file_group)
{
- $ext = '.'.$type;
$filename = md5(implode('', array_map(function($a) {
return $a['file'];
- }, $file_group))).$ext;
+ }, $file_group))).'.'.$type;
// Get the last modified time of all of the component files
$last_mod = 0;
foreach ($file_group as $file)
@@ -569,7 +575,7 @@ private static function combine_and_minify($type, $file_group)
foreach ($file_group as $file)
{
if (static::$show_files_inline)
- $content .= '/* '.$file['file'].' */'.PHP_EOL;
+ $content .= PHP_EOL.'/* '.$file['file'].' */'.PHP_EOL.PHP_EOL;
if ($file['minified'])
$content .= file_get_contents($file['file']).PHP_EOL;
else
@@ -625,13 +631,15 @@ public static function render_css_inline()
* Locates the given image(s), and returns the resulting <img> tag.
*
* @param mixed $images Image(s) to print. Can be string or array of strings
+ * @param string $alt The alternate text
* @param array $attr Attributes to apply to each image (eg alt)
* @return string The resulting <img> tag(s)
*/
- public static function img($images, $attr = array())
+ public static function img($images, $alt, $attr = array())
{
if (!is_array($images))
$images = array($images);
+ $attr['alt'] = $alt;
$ret = '';
foreach ($images as $image)
{
@@ -640,6 +648,58 @@ public static function img($images, $attr = array())
}
return $ret;
}
+
+ /**
+ * Cleares all cache files last modified before $before.
+ *
+ * @param type $before Time before which to delete files. Defaults to 'now'.
+ * Uses strtotime.
+ */
+ public static function clear_cache($before = 'now')
+ {
+ static::clear_cache_base('*', $before);
+ }
+
+ /**
+ * Cleares all JS cache files last modified before $before.
+ *
+ * @param type $before Time before which to delete files. Defaults to 'now'.
+ * Uses strtotime.
+ */
+ public static function clear_js_cache($before = 'now')
+ {
+ static::clear_cache_base('*.js', $before);
+ }
+
+ /**
+ * Cleares CSS all cache files last modified before $before.
+ *
+ * @param type $before Time before which to delete files. Defaults to 'now'.
+ * Uses strtotime.
+ */
+ public static function clear_css_cache($before = 'now')
+ {
+ static::clear_cache_base('*.css', $before);
+ }
+
+ /**
+ * Base cache clear function.
+ *
+ * @param type $filter Glob filter to use when selecting files to delete.
+ * @param type $before Time before which to delete files. Defaults to 'now'.
+ * Uses strtotime.
+ */
+ private static function clear_cache_base($filter = '*', $before = 'now')
+ {
+ $before = strtotime($before);
+ $files = glob(DOCROOT.static::$cache_path.$filter);
+ foreach ($files as $file)
+ {
+ if (filemtime($file) < $before)
+ unlink($file);
+ }
+ }
+
}
/* End of file casset.php */
View
0 classes/casset/LICENSE.txt → classes/casset/LICENSE
File renamed without changes.
View
19 classes/casset/csscompressor.php
@@ -1,10 +1,5 @@
<?php
/**
- * Class Minify_CSS_Compressor
- * @package Minify
- */
-
-/**
* Compress CSS
*
* This is a heavy regex-based removal of whitespace, unnecessary
@@ -14,10 +9,22 @@
* hacks involving comment tokens in 'content' value strings to break
* minimization badly. A test suite is available.
*
- * @package Minify
* @author Stephen Clay <steve@mrclay.org>
* @author http://code.google.com/u/1stvamp/ (Issue 64 patch)
*/
+
+/**
+ * This library is used as part of Casset.
+ *
+ * @package Casset
+ * @version v1.2
+ * @author Antony Male
+ * @license MIT License
+ * @link http://github.com/canton7/fuelphp-casset
+ */
+
+namespace Casset;
+
class Casset_Csscompressor {
/**
View
17 classes/casset/cssurirewriter.php
@@ -1,15 +1,24 @@
<?php
/**
- * Class Minify_CSS_UriRewriter
+ * Rewrite file-relative URIs as root-relative in CSS files
+ *
* @package Minify
+ * @author Stephen Clay <steve@mrclay.org>
*/
/**
- * Rewrite file-relative URIs as root-relative in CSS files
+ * This library is used as part of Casset.
*
- * @package Minify
- * @author Stephen Clay <steve@mrclay.org>
+ * @package Casset
+ * @version v1.2
+ * @author Antony Male
+ * @license MIT License
+ * @link http://github.com/canton7/fuelphp-casset
*/
+
+
+namespace Casset;
+
class Casset_Cssurirewriter {
/**
View
19 classes/casset/jsmin.php
@@ -40,7 +40,6 @@
* SOFTWARE.
* --
*
- * @package JSMin
* @author Ryan Grove <ryan@wonko.com> (PHP port)
* @author Steve Clay <steve@mrclay.org> (modifications + cleanup)
* @author Andrea Giammarchi <http://www.3site.eu> (spaceBeforeRegExp)
@@ -50,6 +49,18 @@
* @link http://code.google.com/p/jsmin-php/
*/
+/**
+ * This library is used as part of Casset.
+ * @package Casset
+ * @version v1.2
+ * @author Antony Male
+ * @license MIT License
+ * @link http://github.com/canton7/fuelphp-casset
+ */
+
+
+namespace Casset;
+
class Casset_JSMin {
const ORD_LF = 10;
const ORD_SPACE = 32;
@@ -309,8 +320,8 @@ protected function next()
}
}
-class JSMin_UnterminatedStringException extends Exception {}
-class JSMin_UnterminatedCommentException extends Exception {}
-class JSMin_UnterminatedRegExpException extends Exception {}
+class JSMin_UnterminatedStringException extends \Exception {}
+class JSMin_UnterminatedCommentException extends \Exception {}
+class JSMin_UnterminatedRegExpException extends \Exception {}
/* End of file casset/jsmin.php */
View
10 config/casset.php
@@ -1,18 +1,16 @@
<?php
+
/**
- * Fuel is a fast, lightweight, community driven PHP5 framework.
- *
- * This library provides an alternative to the built-in Asset library.
- * This library has been based on the original Asset library, although
- * extensive modifications and additions have been made.
+ * Casset: Convenient asset library for FuelPHP.
*
* @package Casset
- * @version 1.0
+ * @version v1.2
* @author Antony Male
* @license MIT License
* @copyright 2011 Antony Male
* @link http://github.com/canton7/fuelphp-casset
*/
+
return array(
/**
View
65 readme.md
@@ -10,9 +10,10 @@ Thanks to Stephen Clay (and Douglas Crockford) for writing the minification libr
Installation
------------
-1. Clone / download
-2. Copy classes/casset.php and classes/casset into app/classes/
-3. Copy config/casset.php into app/config/ (optional, casset has sensible defaults)
+### Manual
+1. Clone / [download](https://github.com/canton7/fuelphp-casset/zipball/master)
+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
@@ -29,7 +30,7 @@ Casset::js('myfile.js');
Casset::js('myfile2.js');
```
-By default, Casset will minify both of these files and combine them into a single file (which is written to public/assets/cache/<md5 hash>.js).
+By default, Casset will minify both of these files and combine them into a single file (which is written to public/assets/cache/\<md5 hash\>.js).
To include this file in your page, use the following:
```php
@@ -40,6 +41,13 @@ Returns something like
*/
```
+If you've got minification turned off (see the section at the bottom of this readme), you'll instead get:
+
+```php
+<script type="text/javascript" src="http://localhost/site/assets/js/myfile.js"></script>
+<script type="text/javascript" src="http://localhost/site/assets/js/myfile2.js"></script>
+```
+
If you have a specific file ("myfile.min.js") which you want Casset to use, rather than generating its own minified version, you
can pass this as the second argument, eg:
@@ -50,18 +58,18 @@ Casset::js('myfile.js', 'myfile.min.js');
Images
------
-Although the origin Asset library provided groups, etc, for dealing with images, I couldn't see the point.
+Although the original Asset library provided groups, etc, for dealing with images, I couldn't see the point.
-Therefore image handling is somewhat simpler, and can be summed up by the following line:
+Therefore image handling is somewhat simpler, and can be summed up by the following line, where the third argument is an optional array of attributes:
```php
-echo Casset::img('test.jpg', array('alt' => 'This is an image'));
+echo Casset::img('test.jpg', 'alt text', array('width' => 200));
```
You can also pass an array of images (which will all have to same attributes applied to them), eg:
```php
-echo Casset::img(array('test.jpg', 'test2.jpg'));
+echo Casset::img(array('test.jpg', 'test2.jpg'), 'Some thumbnails');
```
Groups
@@ -107,7 +115,8 @@ 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 exist.
+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.
Note that when a group is rendered, it is disabled. See the "Extra attributes" section for an application of this functionality.
@@ -185,7 +194,7 @@ echo Casset::render_css('screen', false, array('media' => 'screen');
// Render everything else, except the 'screen' group
echo Casset::render_css();
-// <link rel="stylesheet" type="text/css" href="http://...main.css" media="screen" />
+// <link rel="stylesheet" type="text/css" href="http://...main.css" />
```
Minification
@@ -193,7 +202,7 @@ Minification
Minification uses libraries from Stephen Clay's [Minify library](http://code.google.com/p/minify/).
-When minification is enabled (see the "min" key in the config file), when an enabled group is rendered, it is combined, minified, and stored in a file in public/assets/cache/.
+When an enabled group is rendered (and minification is turned on), the files in that group are minified combined, and stored in a file in public/assets/cache/.
This is an attempt to achieve a balance between spamming the browser with lots of files, and allowing the browser to cache files.
The assumption is that each group is likely to appear fairly independantly, so combining groups isn't worth it.
@@ -207,8 +216,31 @@ The following will minify the 'group_name' group, even if minification is turned
echo Casset::render_js('group_name', false, array(), true);
```
-NOTE: If you change the contents of a group, a new cache file will be generated. However the old one will not be removed (Casset doesn't know if you've got a single page where you add an extra file to a group).
-Therefore an occasional clearout of `public/assets/cache/` is recommended.
+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.
+This is because the order of JS files can be important, as dependancies may need to be satisfied. In CSS, no such dependancies exist.
+Bear this in mind when adding files to groups dynamically -- if you're changing the order of files in an otherwise identical group, you're not allowing
+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.
+
+Clearing the cache
+------------------
+Since cache files are not automatically removed (Casset has no way of knowing whether a cache file might be neede again), a few method 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 remote 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:
+
+```php
+Casset::clear_js_cache('2 hours ago');
+// Removes all js cache files last modified more than 2 hours ago
+
+Casset::clear_cache('yesterday');
+// Removes all cache files last modified yesterday
+```
Examples
--------
@@ -238,7 +270,7 @@ In the config file:
'files' => array(
'jquery-ui.css',
),
- 'endabled' => false,
+ 'enabled' => false,
),
),
),
@@ -258,6 +290,7 @@ In our template file:
echo Casset::render_js();
echo Casset::render_js_inline();
?>
+</body>
```
We can then turn the jquery-ui group on as we please.
@@ -284,6 +317,6 @@ Casset::enable('jquery-ui');
Contributing
------------
-Pull requests are gladly accepted!
+If you've got any issues/complaints/suggestions, please tell me and I'll do my best!
-This project uses [git flow](http://nvie.com/posts/a-successful-git-branching-model/), so please base your work on the tip of the `develop` branch, and rebase onto `develop` again before submitting the pull request.
+Pull requests are also gladly accepted. This project uses [git flow](http://nvie.com/posts/a-successful-git-branching-model/), so please base your work on the tip of the `develop` branch, and rebase onto `develop` again before submitting the pull request.

0 comments on commit aa3ffe2

Please sign in to comment.
Something went wrong with that request. Please try again.