Assetic AssetFactory::createAsset(array $input, …) - $input doesn't accept \Assetic\Asset\* elements
Question
I have a couple of methods which combine many CSS assets to one, minify them and dump them (cached) to a generated filename like "/assetic/6bad22c.css". I achieve this by making use of the following:
Currently I use AssetFactory
private static function getCssAssetFactory()
{
$fm = new FilterManager();
$fm->set('less', new Filter\LessphpFilter());
$fm->set('import', new Filter\CssImportFilter());
$fm->set('rewrite', new Filter\CssRewriteFilter());
$fm->set('min', new Filter\CssMinFilter());
$factory = new AssetFactory(self::getAssetBuildPath());
$factory->setFilterManager($fm);
return $factory;
}
and create an Asset via
public static function dumpStylesheets()
{
$asset = self::getCssAssetFactory()->createAsset
( self::$stylesheets
, array
( 'less' // Less CSS Compiler
, 'import' // Solves @imports
, 'rewrite' // Rewrites Base URLs when moving to another URL
, 'min' // Minifies the script
)
, array('output' => 'assetic/*.css')
);
$cache = self::getAssetCache($asset);
self::getAssetWriter()->writeAsset($cache);
return self::basePath().'/'.$asset->getTargetPath();
}
Here are the referenced methods:
private static function getAssetWriter()
{
if (is_null(self::$AssetWriter))
{
self::$AssetWriter = new AssetWriter(self::getAssetBuildPath());
}
return self::$AssetWriter;
}
private static function getAssetCache($asset)
{
return new AssetCache
( $asset
, new FilesystemCache(self::getAssetBuildPath().'/cache')
);
}
No magic here so far. My problem is, that the self::$stylesheets
array, by definition, just contains path-strings to the assets. But I need to use real Assetic Assets like this way:
self::$stylesheets = array
( new Asset\FileAsset('path/to/style.css')
, new Asset\StringAsset('.some-class {text-decoration: none;}');
, new Asset\HttpAsset('http://domain.tld/assets/style.css');
);
But AssetFactory::createAsset()
doesn't accept its own Assets. I need the possibility to use StringAsset
because I have to change some values in CSS / JS, serverside.
Is there another way to achieve this other than using AssetFactory::createAsset()
?
Solution
It looks like you may be able to essentially reproduce the inner workings of the createAsset method, short circuiting the parts that are giving you problems:
$asset = new AssetCollection(self::$stylesheets);
$filters = array
( 'less' // Less CSS Compiler
, 'import' // Solves @imports
, 'rewrite' // Rewrites Base URLs when moving to another URL
, 'min' // Minifies the script
);
$options = array('output' => 'assetic/*.css');
foreach ($filters as $filter) {
if ('?' != $filter[0]) {
$asset->ensureFilter(self::getCssAssetFactory()->getFilterManager()->get($filter));
} elseif (!$options['debug']) {
$asset->ensureFilter(self::getCssAssetFactory()->getFilterManager()->get(substr($filter, 1)));
}
}
$asset->setTargetPath(str_replace('*', self::getCssAssetFactory()->generateAssetName(self::$stylesheets, $filters, $options), $options['output']));
... all of that should adequately replace your call to createAsset(). If you have any workers that you've added to the assetFactory, then you'll need to implement that as well.