views:

260

answers:

3

I have a project built on Symfony 1.4 and it has multiple environments - each developer has their own copy 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

A: 

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).

richsage
Thanks, this should work - but is there a global way to do it, so that when I type symfony propel:insert-sql-diff, it automatically works out which environment to use?
SlappyTheFish
A: 

sfContext::getInstance()->getConfiguration()->getEnvironment()

laczika
A: 

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/

SlappyTheFish