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
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.
For clarification, things I can't use or rely on
export
. Why? Integration, QA, and Staging might all be on the same boxOk, 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 ;)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With