Symfony 3 - 在框架加载之前如何设置参数

3
我正在尝试通过在每个资产中引入version作为参数来解决Symfony 3中的缓存问题。我正在使用Assetic
# app/config/config.yml
parameters:
    version: 'v1.0'
framework:
    # ...
    assets:
        version: '%version%'

这很好用。然而问题在于,每次我将一些发布部署到生产环境时,都需要手动编辑parameters.yml。所以,我需要每次部署时自动生成/更新它。
我能想到的一种方法是根据文件最近修改生成MD5字符串。假设我能得到一个版本号,我想用该版本号替换参数。
使用CompilerPass,我可以添加version参数。
//AppBundle/AppBundle.php

use AppBundle\DependencyInjection\Compiler\Version;

class AppBundle extends Bundle
{
    public function build(ContainerBuilder $container)
    {
        parent::build($container); // TODO: Change the autogenerated stub

        $container->addCompilerPass(new Version());
    }    
}




//AppBundle/DependencyInjection/Compiler/Version.php
namespace AppBundle\DependencyInjection\Compiler;


use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class Version implements CompilerPassInterface
{
    public function process(ContainerBuilder $container)
    {
        $container->setParameter('version', uniqid());

    }
}

uniqid() 被添加为一个测试。然而,这段代码在 "framework" 配置初始化之后才会工作并添加参数 "version"。因此,在 "framework" 块下的 %version% 表示无法找到该参数。

如何在 "framework" 初始化之前创建此参数?

1个回答

1

在调用load()加载每个扩展之前,还有一种方法可以在其前面添加配置。请参见http://symfony.com/doc/current/components/dependency_injection/compilation.html#prepending-configuration-passed-to-the-extension

基本上,只需实现PrependExtensionInterface并编写prepend()方法:

public function prepend(ContainerBuilder $container)
{
    // ...
}

顺便说一下,我之前也做过类似的事情,通过检查最后提交的ID来实现(如果您没有使用git,请忽略此内容 :)):

git log --pretty=oneline -1 --format="%H"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接