42
votes

Hopefully a simple question - how does one specify which environment to use when running a console command in Symfony2. I've created a few commands, however I would like to run them in the context of my 'staging' environment when on my staging server and my 'prod' environment when on my production server (the different environments define different database connections). How do I configure and pass this information to my console command?

6

6 Answers

75
votes

You have two options that will help you out. You can specify the environment that the console will run in with the --env flag, and use --no-debug to disable debug mode.

php app/console --env=staging your:console:command or php app/console --env=prod your:console:command should do what you're looking for (the console runs in the dev environment with debug on by default).

You can look at the code of the app/console file for more info.

54
votes

You can also use the SYMFONY_ENV environment variable to set a default environment distinct to 'dev' (e.g. export SYMFONY_ENV=prod in ~/.bash_profile)

8
votes

To answer the question @croca had, to expand on what @Francesc Rosàs posted, and as @Problematic suggested.

If you look in app/console you should see $env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');

All this does is checks the input arguments passed to the console for --env or -e, checks the default value from getenv('SYMFONY_ENV'), or sets it to dev if neither are supplied.

It is then passed to $kernel = new AppKernel($env, $debug);

You could essentially either make changes directly to app/console to achieve your application's specific functionality or copy app/console to a separate file such as app/exec, then process the $env variable how you prefer to determine the desired environment.

Simple Example: app/exec

#!/usr/bin/env php
<?php
 /**
  * disabled debug mode, 
  * set environment based on requesting address being local 
  * otherwise the server's IP address
  * be sure to perform at least chmod(750) on the file to ensure it is executable
  * otherwise it must be prefixed with your php executable
  */     

set_time_limit(0);

require_once __DIR__.'/bootstrap.php.cache';
require_once __DIR__.'/AppKernel.php';

use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;

$input = new ArgvInput();
$env = ($_SERVER['REMOTE_ADDR'] === '127.0.0.1' || $_SERVER['REMOTE_ADDR'] === '::1' ? 'dev' :
    ($_SERVER['SERVER_ADDR'] === '192.168.2.1' ? 'test' : 'prod')
);
$kernel = new AppKernel($env, false);
$application = new Application($kernel);
$application->run($input);

Then call php app/exec namespace:command arguments --flags

Additionally you could process your own application instead of using the app/console AppKernel - instructions from Symfony can be found here: http://symfony.com/doc/current/components/console/introduction.html

5
votes

The official documentation says:

By default, console commands run in the dev environment and you may want to change this for some commands.

So, as @Problematic suggest, you can specify the environment for your command using the flag "--env=your_env" or its shortcut "-e your_env".

I wonder whether it is possible to force the default environment for a command. I mean, without having to use the flags. Any idea?

5
votes

There is another variant, not so convenient though, but works also with composer

SYMFONY_ENV=prod app/console cache:clear
1
votes

Maybe you could look at the implementation of the cache:clear command. This may give you some clues on how to choose the environment from a command.

app/console help cache:clear