Symfony 如何将Assetic与CacheBastingWorker和twig一起使用

Symfony 如何将Assetic与CacheBastingWorker和twig一起使用,symfony,twig,assetic,Symfony,Twig,Assetic,正如我所见,Assetic在缓存破坏方面取得了一些进展: 但我真的不明白该如何使用它。 这可以从细枝中使用: {% stylesheets 'bundles/mybundle/css/fonts.css' 'bundles/mybundle/css/style.css' 'bundles/mybundle/css/screen.css' filter='cssrewrite' %} <l

正如我所见,Assetic在缓存破坏方面取得了一些进展:

但我真的不明白该如何使用它。
这可以从细枝中使用:

{% stylesheets 'bundles/mybundle/css/fonts.css' 
               'bundles/mybundle/css/style.css'
               'bundles/mybundle/css/screen.css'
               filter='cssrewrite'
 %}
    <link rel="stylesheet" type="text/css" href="{{ asset_url }}" />
{% endstylesheets %}
{%stylesheets'bundles/mybundle/css/fonts.css'
'bundles/mybundle/css/style.css'
'bundles/mybundle/css/screen.css'
filter='cssrerewrite'
%}
{%endstylesheets%}
使用通常的
assetic:dump
命令


我应该在哪里找到破解缓存的工作人员?

我最近一直在研究如何做到这一点

我提出的解决方案是用我自己的类覆盖Symfony的AssetFactory,并在其构造函数中添加CacheBastingWorker。基本上,您可以创建如下所示的文件:

<?php
namespace YourSite\YourBundle\Factory;

use Symfony\Bundle\AsseticBundle\Factory\AssetFactory as BaseAssetFactory;
use Assetic\Factory\Worker\CacheBustingWorker;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\KernelInterface;

class AssetFactory extends BaseAssetFactory
{
    public function __construct(KernelInterface $kernel, ContainerInterface $container, ParameterBagInterface $parameterBag, $baseDir, $debug = false)
    {
        parent::__construct($kernel, $container, $parameterBag, $baseDir, $debug);
        // Add CacheBustingWorker
        $this->addWorker(new CacheBustingWorker(CacheBustingWorker::STRATEGY_CONTENT));
    }
}

在当前的assetic实现中,我需要将您的代码更新为以下内容以使其正常工作。另外请注意,如果使用xdebug,则必须将最大嵌套级别-xdebug.max_nesting_level=200提高到100以上

<?php

namespace YourSite\YourBundle\Factory;

use Symfony\Bundle\AsseticBundle\Factory\AssetFactory as BaseAssetFactory;
use Assetic\Factory\LazyAssetManager;
use Assetic\Factory\Worker\CacheBustingWorker;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\KernelInterface;

class AssetFactory extends BaseAssetFactory
{
    public function __construct(KernelInterface $kernel, ContainerInterface $container, ParameterBagInterface $parameterBag, $baseDir, $debug = false)
    {
        parent::__construct($kernel, $container, $parameterBag, $baseDir, $debug);
        // Add CacheBustingWorker
        $this->addWorker(new CacheBustingWorker(new LazyAssetManager(new BaseAssetFactory($kernel, $container, $parameterBag, $baseDir, $debug))));
    }
}

随着assetic代码再次更改,主分支上的LazyAssetManager不需要策略

不要忘记更改您的
composer.json
文件:

{
    "kriswallsmith/assetic": "dev-master@dev",
    "symfony/assetic-bundle": "dev-master@dev"
}
你现在只需要这个:

namespace YourSite\YourBundle\Factory;

use Symfony\Bundle\AsseticBundle\Factory\AssetFactory as BaseAssetFactory;
use Assetic\Factory\Worker\CacheBustingWorker;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpKernel\KernelInterface;

class AssetFactory extends BaseAssetFactory
{
    public function __construct(
        KernelInterface $kernel,
        ContainerInterface $container,
        ParameterBagInterface $parameterBag,
        $baseDir,
        $debug = false
    ) { 
        parent::__construct($kernel, $container, $parameterBag, $baseDir, $debug);
        // Add CacheBustingWorker
        $this->addWorker(new CacheBustingWorker());
    }
} 

在一次性转储资产之前,不要忘记
php应用程序/控制台缓存:清除-e prod
,以避免生成标准文件名。

缓存buster现在是symfony/AsseticBundle的一部分(版本>=2.5.0)

更改composer.json中的AsseticBundle版本,如下所示:

"symfony/assetic-bundle": "2.5.0",
web/bundles/js/projectname-876f9ee.js
并激活config.yml文件中资产的缓存破坏,如下所示

assetic:
    workers:
        cache_busting: ~
我的JS文件现在看起来是这样的:

"symfony/assetic-bundle": "2.5.0",
web/bundles/js/projectname-876f9ee.js


是答案。

谢谢,我还发现还有一个PR有待集成到AssetBundle中:这肯定是评分最高的答案。超级简单的解决方案,完全符合我的要求。为什么Symfony文档中没有这个?是的,Juergen的答案中已经有了。我把它改成了公认的答案。