[英]Symfony 3 - How to set Parameter before Framework loads
I am trying to solve the Cache problem in Symfony 3 by introducing version as the parameter to every asset. 我试图通过将版本作为参数引入每个资产来解决Symfony 3中的Cache问题。 I am using Assetic
我正在使用Assetic
# app/config/config.yml
parameters:
version: 'v1.0'
framework:
# ...
assets:
version: '%version%'
This works fine. 这很好用。 However, the problem is that I have to edit the
parameters.yml
each time manually when I deploy some release to production. 但问题是,当我将一些版本部署到生产环境时,我必须每次手动编辑
parameters.yml
。 So I need this to be automatically generated/updated each time when a deployment happens. 所以每次部署发生时我都需要自动生成/更新它。
One way I can think of is to generate a MD5 string based on a file's last change. 我能想到的一种方法是根据文件的最后一次更改生成MD5字符串。 So, lets say if I was able to get a version.
所以,让我们说如果我能够获得一个版本。 I want to replace the parameter with the version.
我想用版本替换参数。
Using CompilerPass
, I can add the version
parameter. 使用
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()
added as a test. uniqid()
作为测试添加。 However, this code works and adds the parameter "version" however AFTER "framework" configuration is initialized. 但是,此代码可以工作并添加参数“version”,但是AFTER“framework”配置已初始化。 Due to this,
%version%
under "framework" block states it cannot find the parameter. 因此,“framework”块下的
%version%
表示无法找到参数。
How do I create this parameter before "framework" initializes ? 如何在“framework”初始化之前创建此参数?
There's also a way to prepend configuration before calling load()
for each extension. 在为每个扩展调用
load()
之前,还有一种方法可以预先配置。 See http://symfony.com/doc/current/components/dependency_injection/compilation.html#prepending-configuration-passed-to-the-extension 请参阅http://symfony.com/doc/current/components/dependency_injection/compilation.html#prepending-configuration-passed-to-the-extension
Basically, just implement PrependExtensionInterface
and write prepend()
method: 基本上,只需实现
PrependExtensionInterface
并编写prepend()
方法:
public function prepend(ContainerBuilder $container)
{
// ...
}
Btw, I did something similar some time ago by checking that last commit id with (If you're not using git just ignore this :)): 顺便说一句,我前段时间通过检查最后一个提交ID来做类似事情(如果你不使用git就忽略这个:)):
git log --pretty=oneline -1 --format="%H"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.