Permalink
Browse files

Merge branch 'release/1.4'

* release/1.4:
  Bump version to v1.4
  Minor changes to the readme.
  Exception no longer thrown if asset doesn't exist. A sensible file_get_contents error gets thrown instead, and the check seems a bit unnecessary now that we're not actively searching for the asset.
  Require lock when writing cache file (should have done this a long time ago).
  Slight performance improvement to render_js() and render_css().
  add_path() no longer removes '../'
  Remove remotve_path() function.
  Document the paths stuff in the readme.
  Add method to set the current default path key.
  Changed my mind: The current path key has to be added when the file is added, not when the file is rendered.
  Asset it now searched for in the path specified, rather than all over the place.
  add_path and remove_path now use new path syntax
  Modify config file syntax for paths, to require path keys.
  Fuel_Exception was being looked for in the Casset namespace, rather than the global one.
  • Loading branch information...
2 parents 9f98b13 + a7a669c commit 990ccc4d7b284532f28691e08af3248a499ca6a7 @canton7 canton7 committed Jun 14, 2011
Showing with 141 additions and 53 deletions.
  1. +1 −1 bootstrap.php
  2. +44 −31 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. +15 −6 config/casset.php
  7. +78 −12 readme.md
View
@@ -4,7 +4,7 @@
* Casset: Convenient asset library for FuelPHP.
*
* @package Casset
- * @version v1.1
+ * @version v1.4
* @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.3
+ * @version v1.4
* @author Antony Male
* @license MIT License
* @copyright 2011 Antony Male
@@ -21,6 +21,11 @@ class Casset {
*/
protected static $asset_paths = array();
+ /*
+ * @var string The key in $asset_paths to use if no key is given
+ */
+ protected static $default_path_key = 'core';
+
/**
* @var string The URL to be prepanded to all assets.
*/
@@ -94,9 +99,9 @@ public static function _init()
$paths = \Config::get('casset.paths', array('assets/'));
- foreach($paths as $path)
+ foreach($paths as $key => $path)
{
- static::add_path($path);
+ static::add_path($key, $path);
}
static::$asset_url = \Config::get('casset.url', \Config::get('base_url'));
@@ -132,22 +137,21 @@ public static function _init()
*
* @param string $path the path to add.
*/
- public static function add_path($path)
+ public static function add_path($key, $path)
{
- array_unshift(static::$asset_paths, str_replace('../', '', $path));
+ static::$asset_paths[$key] = $path;
}
/**
- * Removes a path from the asset paths array.
+ * Set the current default path
*
- * @param string $path the path to remove.
+ * @param $path_key the path key to set the default to.
*/
- public static function remove_path($path)
+ public static function set_path($path_key = 'core')
{
- if (($key = array_search(str_replace('../', '', $path), static::$asset_paths)) !== false)
- {
- unset(static::$asset_paths[$key]);
- }
+ if (!array_key_exists($path_key, static::$asset_paths))
+ throw new \Fuel_Exception("Asset path key $path_key doesn't exist");
+ static::$default_path_key = $path_key;
}
/**
@@ -186,17 +190,14 @@ public static function find_file($file, $asset_type)
{
if (strpos($file, '//') === false)
{
+ $parts = explode('::', $file, 2);
+ $path = static::$asset_paths[$parts[0]];
+ $file = $parts[1];
+
$folder = static::$folders[$asset_type];
$file = ltrim($file, '/');
- foreach (static::$asset_paths as $path)
- {
- if (is_file($path.$folder.$file))
- {
- return $path.$folder.$file;
- }
- }
- throw new Fuel_Exception('Coult not find asset: '.$file);
+ return $path.$folder.$file;
}
else
{
@@ -327,14 +328,22 @@ private static function add_asset($type, $script, $script_min, $group)
// a pre-minified file.
if (!is_string($script_min))
$script_min = false;
+ $files = array($script, $script_min);
+ // If the user hasn't specified a path key, add $default_path_key
+ foreach ($files as &$file)
+ {
+ if ($file != false && strpos($file, '::') === false)
+ $file = static::$default_path_key.'::'.$file;
+ }
+
if (!array_key_exists($group, static::$groups[$type]))
{
// Assume they want the group enabled
- static::add_group($type, $group, array(array($script, $script_min)), true);
+ static::add_group($type, $group, array($files), true);
}
else
{
- array_push(static::$groups[$type][$group]['files'], array($script, $script_min));
+ array_push(static::$groups[$type][$group]['files'], $files);
}
}
@@ -412,9 +421,9 @@ public static function render_js($group = false, $inline = false, $attr = array(
$ret = '';
- foreach ($file_groups as $group_name => $file_group)
+ if ($min)
{
- if ($min)
+ foreach ($file_groups as $group_name => $file_group)
{
$filename = static::combine_and_minify('js', $file_group, $inline);
if (!$inline && static::$show_files)
@@ -431,7 +440,10 @@ public static function render_js($group = false, $inline = false, $attr = array(
'src' => static::$asset_url.static::$cache_path.$filename,
)+$attr, '').PHP_EOL;
}
- else
+ }
+ else
+ {
+ foreach ($file_groups as $group_name => $file_group)
{
foreach ($file_group as $file)
{
@@ -444,7 +456,6 @@ public static function render_js($group = false, $inline = false, $attr = array(
)+$attr, '').PHP_EOL;
}
}
-
}
return $ret;
}
@@ -473,9 +484,9 @@ public static function render_css($group = false, $inline = false, $attr = array
$ret = '';
- foreach ($file_groups as $group_name => $file_group)
+ if ($min)
{
- if ($min)
+ foreach ($file_groups as $group_name => $file_group)
{
$filename = static::combine_and_minify('css', $file_group, $inline);
if (!$inline && static::$show_files)
@@ -493,7 +504,10 @@ public static function render_css($group = false, $inline = false, $attr = array
'href' => static::$asset_url.static::$cache_path.$filename,
)+$attr).PHP_EOL;
}
- else
+ }
+ else
+ {
+ foreach ($file_groups as $group_name => $file_group)
{
foreach ($file_group as $file)
{
@@ -507,7 +521,6 @@ public static function render_css($group = false, $inline = false, $attr = array
)+$attr).PHP_EOL;
}
}
-
}
return $ret;
}
@@ -622,7 +635,7 @@ private static function combine_and_minify($type, $file_group, $inline)
}
}
}
- file_put_contents($filepath, $content);
+ file_put_contents($filepath, $content, LOCK_EX);
$mtime = time();
}
if (!$inline)
@@ -17,7 +17,7 @@
* This library is used as part of Casset.
*
* @package Casset
- * @version v1.3
+ * @version v1.4
* @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.3
+ * @version v1.4
* @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.3
+ * @version v1.4
* @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.3
+ * @version v1.4
* @author Antony Male
* @license MIT License
* @copyright 2011 Antony Male
@@ -14,12 +14,21 @@
return array(
/**
- * An array of paths that will be searched for assets. Each asset is a
- * RELATIVE path from the base_url WITH a trailing slash:
+ * An array of paths that will be searched for assets.
+ * Each path is assigned a name, which is used when referring to that asset.
+ * See the js() and css() docs for more info.
+ * Each asset is a RELATIVE path from the base_url WITH a trailing slash.
+ * There must be an entry with the key 'core'. This is used when no path
+ * is specified.
*
- * array('assets/')
+ * array(
+ * 'core' => 'assets/'
+ * )
*/
- 'paths' => array('assets/'),
+ 'paths' => array(
+ 'core' => 'assets/',
+ 'test' => 'assets/test/'
+ ),
/**
* URL to your Fuel root. Typically this will be your base URL,
@@ -63,7 +72,7 @@
* When minifying, whether to show the files names in each combined
* file in a comment before the tag to the file.
*/
- 'show_files' => false,
+ 'show_files' => true,
/**
* When minifying, whether to put comments in each minified file showing
Oops, something went wrong.

0 comments on commit 990ccc4

Please sign in to comment.