簡體   English   中英

Symfony 3 - 如何在加載框架之前設置參數

[英]Symfony 3 - How to set Parameter before Framework loads

我試圖通過將版本作為參數引入每個資產來解決Symfony 3中的Cache問題。 我正在使用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()作為測試添加。 但是,此代碼可以工作並添加參數“version”,但是AFTER“framework”配置已初始化。 因此,“framework”塊下的%version%表示無法找到參數。

如何在“framework”初始化之前創建此參數?

在為每個擴展調用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"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM