Permalink
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...
canton7 committed Jun 9, 2011
2 parents 3f004f1 + 5e2f233 commit aa3ffe2bf0269fd7953451399a19f36d2c8ac90d
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
@@ -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
@@ -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 */
File renamed without changes.
@@ -1,9 +1,4 @@
<?php
-/**
- * Class Minify_CSS_Compressor
- * @package Minify
- */
-
/**
* Compress CSS
*
@@ -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 {
/**
Oops, something went wrong.

0 comments on commit aa3ffe2

Please sign in to comment.