2016-11-15 24 views
3

我試圖通過將version作爲參數引入到每個資產來解決Symfony 3中的緩存問題。我正在使用AsseticSymfony 3 - 如何在框架加載之前設置參數

# 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%在「框架」塊下指出它找不到參數。

如何在「框架」初始化之前創建此參數?

回答

1

在爲每個擴展調用load()之前,還有一種預先配置的方法。見http://symfony.com/doc/current/components/dependency_injection/compilation.html#prepending-configuration-passed-to-the-extension

基本上,只要實現PrependExtensionInterfaceprepend()方法:

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

順便說一句,我做了類似的事情,前一段時間通過檢查最後提交ID與(如果你還沒有使用Git忽略這:)):

git log --pretty=oneline -1 --format="%H" 
相關問題