Symfony 2 - 多服务器配置 [英] Symfony 2 - multiple server configuration

查看:102
本文介绍了Symfony 2 - 多服务器配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们开始在工作中采用Symfony 2项目,这是伟大的,但有一个挑战,我正在努力解决,我几乎有,但不是。



Symfony将环境的概念视为单个服务器上的单独运行时。这是伟大的,因为你可以在不同的前端控制器(网络)或使用env开关(cli)在一个whim之间切换运行时。



但是,许多服务器作为开发过程的一部分。每个人都有一个本地VM,然后代码通过Integration,QA,Staging和finally生成。



所以,我们的环境概念是服务器。以下是此自定义配置的目标


  1. 在运行时环境切换方面维护Symfony的ootb功能

  2. 允许每个服务器公开(即开发人员控制)配置

  3. 根据服务器维护私人(即系统管理控制)配置

  4. 对于web和cli

这意味着我们不能100%依赖 parameters.ini 任何静态命名的文件,因为开发人员将需要控制每个服务器的配置,所有这些文件将在git中的eachother旁边生活。



,我会像这样做。向用于设置服务器环境的parameters.ini添加新值。类似这样的

 

[parameters]
server =int

在内核中,根据该值加载一个附加的配置文件。例如,我喜欢这个工作,但它不(因为容器在这一步还不存在)



app / AppKernel .php

  public function registerContainerConfiguration(LoaderInterface $ loader)
{
$ loader- > load(__ DIR __。'/ config / config _'。$ this-> getEnvironment()。'。yml');

//每个服务器配置
$ server = $ this-> getContainer() - > getParameter('server');
if($ server)
{
$ loader-> load(__ DIR __。'/ config / server /'.$ server。'。yml');
}
}

这将允许使用 app / config / server / int.yml ,开发人员可以使用它来控制非私有(即不是parameters.ini)配置值。



非常感谢阅读,让我知道是否有任何疑问。



EDIT



em>不能使用或依靠用户个人资料中的


  • * nix环境变量,或通过 export 。为什么? Integration,QA和Staging可能都在同一个框中

  • vhost配置中的任何内容(不适用于cli)

  • - 命名的文件(即,刚刚命名为 server.ini 的文件将无效)


解决方案

好的,我终于找出了怎么做。真的只需要对AppKernel进行基本修改



app / AppKernel.php

  public function registerContainerConfiguration(LoaderInterface $ loader)
{

// Symfony环境config
$ loader-> load(__ DIR__。 /config/config_'.$this->getEnvironment().'.yml');

//加载服务器配置(如果存在)
$ parameters = parse_ini_file(__DIR __。'/ config / parameters.ini',true);
if($ parameters&& isset($ parameters ['parameters'] ['server.env']))
{
$ serverConfig = __DIR __。'/ config / server / '。$ parameters ['parameters'] ['server.env']。'。yml';
if(file_exists($ serverConfig))
{
$ loader-> load($ serverConfig);
} else {
error_log('服务器配置定义,但没有找到配置文件。
}
}
}

app / config /parameters.ini

  [parameters] 
#...
server.env =int
server.title =Integration
server.name =Int 1


b $ b

现在我可以根据需要在 app / config / server / 中创建%server.env%.yml 文件。

$ b感谢那些读过的东西 - 我最初想到的东西更复杂,使这个简单的解决方案看不见一会儿);


We're starting to adopt Symfony 2 for projects here at work, which is great, but there's a challenge I'm working to solve that I've almost got, but not quite.

Symfony treats the concept of an environment as a separate runtime on a single server. This is great because you can switch between runtimes with different front controllers (web) or using the env switch (cli) at a whim.

However, our code is deployed across many servers as part of the development process. Everybody has a local VM, then code propagates up through Integration, QA, Staging, and finally Production.

So, our concept of an environment is server (virtual or physical). Here are the goals with this custom configuration

  1. Maintain Symfony's ootb functionality in respect to runtime environment switching
  2. Allow for public (i.e., developer controlled) configuration per-server
  3. Maintain private (i.e., sysad controlled) configuration per-server
  4. Will work for both web and cli

This means we can't just 100% rely on parameters.ini or any statically-named file, for that matter, since the developer will need control over configuration for each server plus all this files will be living next to eachother in git.

So, what I'd like to do is this. Add a new value to parameters.ini that sets the server environment. Something like this

app/config/parameters.ini

[parameters]
    server="int"

And then, in the kernel, load an additional configuration file based on that value. For example, I'd love for this to work but it doesn't (since the container doesn't yet exist at this step)

app/AppKernel.php

public function registerContainerConfiguration(LoaderInterface $loader)
{
  $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');

  // Per-server config
  $server = $this->getContainer()->getParameter( 'server' );        
  if ( $server )
  {
    $loader->load(__DIR__.'/config/server/'.$server.'.yml');
  }
}

This would enable the usage of a file like app/config/server/int.yml that the developer can use to control non-private (i.e., not parameters.ini) configuration values.

Thanks for reading, and let me know if anything is confusing.

EDIT

For clarification, things I can't use or rely on

  • *nix environment variables from the user's profile or via export. Why? Integration, QA, and Staging might all be on the same box
  • Anything in the vhost config (won't work for cli)
  • A statically-named file (i.e., something just named server.ini won't work)

解决方案

Ok, I finally figured out what to do for this. Really just required a basic modification to the AppKernel

app/AppKernel.php

public function registerContainerConfiguration(LoaderInterface $loader)
{

  // Symfony environment config
  $loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');

  // Load server config, if it exists
  $parameters = parse_ini_file( __DIR__.'/config/parameters.ini', true );
  if ( $parameters && isset( $parameters['parameters']['server.env'] ) )
  {
    $serverConfig = __DIR__.'/config/server/'.$parameters['parameters']['server.env'].'.yml';
    if ( file_exists( $serverConfig ) )
    {
      $loader->load( $serverConfig );
    } else {
      error_log( 'Server config defined, but no config file found. Looked for ' . $serverConfig );
    }
  }
}

app/config/parameters.ini

[parameters]
    # ...
    server.env="int"
    server.title="Integration"
    server.name="Int 1"

And now I can just create %server.env%.yml files in app/config/server/ as needed.

Thanks to those that read this - I was originally thinking of something much more complicated which made this simple solution invisible for a while ;)

这篇关于Symfony 2 - 多服务器配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆