Symfony 1.4: Programmatically set environment for CLI
I have a project built on Symfony 1.4 and it has multiple environments - each developer has their own co开发者_JAVA百科py installed on their local machine and therefore has their own environment.
I am able to dynamically set the environment in index.php, but how can I do this for CLI tasks?
I could of course use --env=myenvironment on all tasks, but I would prefer it to be able to use the same logic as I have in index.php
sfContext::getInstance()->getConfiguration()->getEnvironment()
I see that this question has had a lot of views, so just for reference I am posting the solution I came up with.
I eventually came up with a solution for both web and CLI access, so each installation can determine which environment to use without affecting anything in Subversion:
http://www.4pmp.com/2010/10/programmatically-set-environment-for-symfony-project/
You could do something similar to this in your task perhaps:
class MyTask extends sfBaseTask
{
protected function configure()
{
// Env config file
$file = sfConfig::get("sf_config_dir") . DIRECTORY_SEPARATOR . "environment.cfg";
$env = (file_exists($file) ? file_get_contents($file) : "prod");
$this->addOptions(array(
new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', $env),
}
}
The above should work; it defaults to the 'prod' environment if the environment.cfg file doesn't exist. This also assumes that you have just the environment in the file (eg 'prod', 'dev', 'slappythefish' etc).
精彩评论