Automatically detects modifications to your css and javascript files and regenerates the bundles automatically.
Bundle contents can be modified by filters for css url rewriting to avoid broken images, code minification and compression etc. (A Google Closure Compiler filter using the Service API comes with the library).
Installation
BundleFu can be installed using the Composer tool. You can either add dotsunited/bundlefu to the dependencies in your composer.json, or if you want to install BundleFu as standalone, go to the main directory and run:, (*6)
You can then use the composer-generated autoloader to access the BundleFu classes:, (*7)
<?php
require 'vendor/autoload.php';
?>
Usage
Configure a Bundle instance:, (*8)
setDocRoot('/path/to/your/document_root')
// Set the css cache path (relative to the document root)
->setCssCachePath('css/cache')
// Set the javascript cache path (relative to the document root)
->setJsCachePath('js/cache');
?>
Alternatively, you can pass an options array to the constructor (or use the method setOptions later):, (*9)
Output the bundle <script> and <link> tags wherever you want:, (*11)
and tags
echo $bundle->render();
// Renders the tag only
echo $bundle->renderCss();
// Renders the
Using the Factory
You can also use a factory to create bundle instances. The advantage is, that the factory can hold global options (like bypass and doc_root) which are shared across all created bundles:, (*12)
'/path/to/your/document_root',
'css_cache_path' => 'css/cache',
'js_cache_path' => 'js/cache',
);
$factory = new \DotsUnited\BundleFu\Factory($options);
// $bundle1 and $bundle2 use the same doc_root, css_cache_path and js_cache_path options
$bundle1 = $factory->createBundle();
$bundle2 = $factory->createBundle();
?>
You can pass specific options to the createBundle method (global factory options will be overwritten):, (*13)
The factory also lets you define name aliases for filters. You can then define the string alias for the css_filter and js_filter options instead of passing a filter instance:, (*14)
new \DotsUnited\BundleFu\Filter\ClosureCompilerServiceFilter()
);
$factory = new \DotsUnited\BundleFu\Factory(array(), $filters);
$bundle1 = $factory->createBundle(array('js_filter' => 'js_closure_compiler'));
?>
Filters
You can manipulate the loaded css/javascript files and the bundled css/javascript code with filters. Filters are classes which implement DotsUnited\BundleFu\Filter\FilterInterface., (*15)
The DotsUnited\BundleFu\Filter\CallbackFilter can filter by using any PHP callback. If you want to compress your CSS using YUI Compressor you can either write a custom filter or use the following code leveraging the Callback filter:, (*22)
All content inside of $bundle->start() and $bundle->end() will be lost. Be sure to only put css/javascript includes inside of the block.
Scripts/stylesheets are detected by parsing the output and looking for include files. HTML comments are ignored, so if you comment out a script like this:, (*23)