0

我有几种方法可以将许多 CSS 资产组合为一个,缩小它们并将它们转储(缓存)到生成的文件名,如“/assetic/6bad22c.css”。我通过使用以下内容来实现这一点:

目前我使用 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;
}

并通过创建资产

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();
}

以下是参考的方法:

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')
    );
}

到目前为止,这里没有魔法。我的问题是,根据定义self::$stylesheets,数组只包含资产的路径字符串。但我需要像这样使用真正的资产资产:

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');
);

AssetFactory::createAsset()不接受自己的资产。我需要使用的可能性,StringAsset因为我必须更改服务器端 CSS / JS 中的一些值。

除了使用还有其他方法可以实现这一点AssetFactory::createAsset()吗?

4

1 回答 1

0

看起来您可能能够从本质上重现 createAsset 方法的内部工作原理,从而将给您带来问题的部分短路:

$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']));

...所有这些都应该足以取代您对 createAsset() 的调用。如果您有任何已添加到assetFactory 的工人,那么您也需要实现它。

于 2012-03-15T17:17:07.703 回答