Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add rename config property #51

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions classes/casset.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class Casset {
'inline' => false,
'attr' => array(),
'deps' => array(),
'rename' => false,
);

/**
Expand Down Expand Up @@ -176,6 +177,8 @@ public static function _init()

static::$deps_max_depth = \Config::get('casset.deps_max_depth', static::$deps_max_depth);

static::$default_options['rename'] = \Config::get('casset.rename', static::$default_options['rename']);

$group_sets = \Config::get('casset.groups', array());

foreach ($group_sets as $group_type => $groups)
Expand Down Expand Up @@ -796,7 +799,7 @@ public static function render_js($group = false, $options = array(), $attr_dep =

if (static::$groups['js'][$group_name]['combine'])
{
$filename = static::combine('js', $file_group, static::$groups['js'][$group_name]['min'], $inline);
$filename = static::combine('js', $file_group, static::$groups['js'][$group_name]['min'], $inline, $group_name);
if (!$inline && static::$show_files && $options['gen_tags'])
{
$ret .= '<!--'.PHP_EOL.'Group: '.$group_name.PHP_EOL.implode('', array_map(function($a){
Expand Down Expand Up @@ -897,7 +900,7 @@ public static function render_css($group = false, $options = array(), $attr_dep

if (static::$groups['css'][$group_name]['combine'])
{
$filename = static::combine('css', $file_group, static::$groups['css'][$group_name]['min'], $inline);
$filename = static::combine('css', $file_group, static::$groups['css'][$group_name]['min'], $inline, $group_name);
if (!$inline && static::$show_files && $options['gen_tags'])
{
$ret .= '<!--'.PHP_EOL.'Group: '.$group_name.PHP_EOL.implode('', array_map(function($a){
Expand Down Expand Up @@ -1150,7 +1153,7 @@ protected static function css_rewrite_uris($content, $filename, $destination_fil
* @param bool $minify whether to minify the files, as well as combining them
* @return string The path to the cache file which was written.
*/
protected static function combine($type, $file_group, $minify, $inline)
protected static function combine($type, $file_group, $minify, $inline, $group_name = null)
{
// Get the last modified time of all of the component files
$last_mod = 0;
Expand All @@ -1166,10 +1169,17 @@ protected static function combine($type, $file_group, $minify, $inline)
$last_mod = $mod;
}

$filename = md5(implode('', array_map(function($a) {
return $a['file'];
}, $file_group)).($minify ? 'min' : '').$last_mod).'.'.$type;

$filename = '';
if (static::$groups[$type][$group_name]['rename'] && $group_name)
{
$filename = $group_name.'.'.($minify ? 'min' : '').'.'.$type;
}
else
{
$filename = md5(implode('', array_map(function($a) {
return $a['file'];
}, $file_group)).($minify ? 'min' : '').$last_mod).'.'.$type;
}
$rel_filepath = static::$cache_path.'/'.$filename;
$abs_filepath = static::$root_path.$rel_filepath;
$needs_update = (!file_exists($abs_filepath));
Expand Down
15 changes: 13 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ To define a group in the config file, use the 'groups' key, eg:
'min' => false,
'inline' => true
),
'sample_group_name' => array(
'files' => array(
array('sample.js', 'sample.min.js'),
'sample2.js'
),
'combine' => true,
'min' => false,
'rename' => true
),
'group_name_2' => array(.....),
),
'css' => array(
Expand Down Expand Up @@ -154,7 +163,8 @@ array element.
**min**: This optional key allows you to override the 'min' config key on a per-group basis.
**inline**: Optional, allows you to render the group 'inline' -- that is, show the CSS directly in the file, rather than including a separate .css file. See the section on inling below.
**attr**: Optional, allows you to specify extra attributes to be added to the script/css/link tag generated. See the section on attributes below.
**deps**: (Optional) Specifies other groups to be rendered whenever this group is rendered, see the section below.
**deps**: (Optional) Specifies other groups to be rendered whenever this group is rendered, see the section below.
**rename**: This optional key allows you to override the 'rename' config key on a per-group basis. (default false)

Aside: You can specify any non-string value for the asset name, and it will be ignored.
This can be handy if you're doing something like `'files' => array(($var == $val) ? false : 'file.js')`.
Expand Down Expand Up @@ -187,10 +197,11 @@ $options = array(
'inline' => true/false,
'attr' => array(),
'deps' => array(),
'rename' => true/false,
);
```

The arguments are the same as for the config key -- if `'enabled'`, `'combine'` or `'min'` are omitted, the value specified in the config file are used. Eg:
The arguments are the same as for the config key -- if `'enabled'`, `'combine'` , `'min'` or `'rename'` are omitted, the value specified in the config file are used. Eg:

```php
Casset::add_group('test_group', array(
Expand Down