0

多くの CSS アセットを 1 つに結合し、それらを縮小して、"/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により、配列にはアセットへのパス文字列だけが含まれていることです。しかし、次のように実際の Assetic Assets を使用する必要があります。

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()、独自のアセットは受け入れません。StringAssetCSS / 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 に答える