canton7 / fuelphp-casset

Better asset management library for fuelphp (with minification!)
MIT License
103 stars 29 forks source link

Added functionality to remove assets programatically #47

Open leemason opened 10 years ago

leemason commented 10 years ago

im working on a fuel app, which is using the theme class, and i want themes to able to remove/add assets as needed.

your package is brilliant and will help in explaining the steps to theme debs (which i why i chose it over Asset).

the one thing missing for me (i was considering ie if, but found the agent class does it pretty well). anyway back on track, the one thing missing is the ability to remove assets once added.

sure you can disable a group, but say i have a page i dont want "table-styling.css" loaded on, but the file is loaded in my "theme" group.

the problem with this is i cant disable the entire group.

i know this can be handled by not adding the file to the group per page, but trying to be efficient i ended up extending the class and adding these functions:

   public static function remove_css($sheet, $group){
        static::remove_asset('css', $sheet, $group);
    }

    public static function remove_js($sheet, $group){
        static::remove_asset('js', $sheet, $group);
    }

    public static function remove_asset($type, $sheet, $group){
        foreach(static::$groups[$type][$group]['files'] as $key => $filegroup ){
            if($filegroup[0] == $group.'::'.$sheet || $filegroup[1] == $group.'::'.$sheet){
                unset(static::$groups[$type][$group]['files'][$key]);
            }
        }
    }

quick and dirty removing of the files from the array deals with this well.

it probably needs a bit more work to deal with none namespaced assets, but it works for me, maybe you could add it to the next version?

might even be an idea to put the values in a "disabled" array, so it could then be loaded after (although i cant see this being needed once removed).