From 8daea97d3f33bb6c5d02c6eaa41bc1f8889d021b Mon Sep 17 00:00:00 2001 From: franck Date: Mon, 17 Jun 2013 15:36:35 +0200 Subject: [PATCH] Integration de Assetic --- .../Template/Assets/AsseticManager.class.php | 124 ++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 core/lib/Thelia/Core/Template/Assets/AsseticManager.class.php diff --git a/core/lib/Thelia/Core/Template/Assets/AsseticManager.class.php b/core/lib/Thelia/Core/Template/Assets/AsseticManager.class.php new file mode 100644 index 000000000..acb1add3e --- /dev/null +++ b/core/lib/Thelia/Core/Template/Assets/AsseticManager.class.php @@ -0,0 +1,124 @@ +. */ +/* */ +/*************************************************************************************/ +namespace Thelia\Template\Assets; + +use Assetic\AssetManager; +use Assetic\FilterManager; +use Assetic\Filter; +use Assetic\Factory\AssetFactory; +use Assetic\Factory\Worker\CacheBustingWorker; +use Assetic\AssetWriter; +use Assetic\Asset\AssetCache; +use Assetic\Cache\FilesystemCache; + +class AsseticManager { + + protected $options; + + public function __construct($_options = array()) { + + $this->options = $_options; + } + + /** + * Generates assets from $asset_path in $output_path, using $filters. + * + * @param string $asset_path the full path to the asset file (or file collection) + * @param unknown $output_path the full disk path to the output directory (shoud be visible to web server) + * @param unknown $output_url the URL to the generated asset directory + * @param unknown $asset_type the asset type: css, js, ... The generated files will have this extension. Pass an empty string to use the asset source extension. + * @param unknown $filters a list of filters, as defined below (see switch($filter_name) ...) + * @param unknown $debug true / false + * @throws \Exception + * @return string The URL to the generated asset file. + */ + public function asseticize($asset_path, $output_path, $output_url, $asset_type, $filters, $debug) { + + $asset_name = basename($asset_path); + $asset_dir = dirname($asset_path); + + $am = new AssetManager(); + $fm = new FilterManager(); + + if (! empty($filters)) { + $filter_list = explode(',', $filters); + + foreach($filter_list as $filter_name) { + + $filter_name = trim($filter_name); + + switch($filter_name) { + case 'less' : + $fm->set('less', new Filter\LessphpFilter()); + break; + + case 'sass' : + $fm->set('less', new Filter\Sass\SassFilter()); + break; + + case 'cssembed' : + $fm->set('cssembed', new Filter\PhpCssEmbedFilter()); + break; + + case 'cssrewrite': + $fm->set('cssrewrite', new Filter\CssRewriteFilter()); + break; + + case 'cssimport': + $fm->set('cssimport', new Filter\CssImportFilter()); + break; + + default : + throw new \Exception("Unsupported Assetic filter: '$filter_name'"); + break; + } + } + } + else { + $filter_list = array(); + } + + // Factory setup + $factory = new AssetFactory($asset_dir); + + $factory->setAssetManager($am); + $factory->setFilterManager($fm); + + $factory->setDefaultOutput('*'.(! empty($asset_type) ? '.' : '').$asset_type); + + $factory->setDebug($debug); + + $factory->addWorker(new CacheBustingWorker()); + + // Prepare the assets writer + $writer = new AssetWriter($output_path); + + $asset = $factory->createAsset($asset_name, $filter_list); + + $cache = new AssetCache($asset, new FilesystemCache($output_path)); + + $writer->writeAsset($cache); + + return rtrim($output_url, '/').'/'.$asset->getTargetPath(); + } +} \ No newline at end of file