Note that there are some explanatory texts on larger screens.

plurals
  1. POSymfony2 - Console commands, how can I setup environment configuration?
    text
    copied!<p>I'm trying to use the Symfony2 Console component (without the entire Symfony2 framework) and not sure on the best practice way to load specific configuration files based on my environment.</p> <p>In my use case I am purely using the Console component (I can use others if needed) but not sure how to load a specific configuration file based on my environment.</p> <pre><code>&lt;?php // app/console // Include composer autloader $loader = require_once dirname(dirname(__FILE__)) . '/vendor/autoload.php'; require_once dirname(__FILE__) . '/AppKernel.php'; use Symfony\Component\Console\Input\ArgvInput; // Determine which environment we are on $input = new ArgvInput(); $env = $input-&gt;getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev'); $debug = getenv('SYMFONY_DEBUG') !== '0' &amp;&amp; !$input-&gt;hasParameterOption(array('--no-debug', '')) &amp;&amp; $env !== 'prod'; $kernel = new AppKernel($env, $debug); // The following line won't work because composer loader doesn't implement "Symfony\Component\Config\Loader\LoaderInterface" $kernel-&gt;registerContainerConfiguration($loader); $console = new Cli\Console($kernel); $console-&gt;setCatchExceptions(TRUE); $console-&gt;run(); </code></pre> <hr> <pre><code>&lt;?php // app/AppKernel.php use Symfony\Component\HttpKernel\Kernel; use Symfony\Component\Config\Loader\LoaderInterface; class AppKernel extends Kernel { public function registerBundles() { $bundles = array(); return $bundles; } public function registerContainerConfiguration(LoaderInterface $loader) { $loader-&gt;load(__DIR__.'/config/config_'.$this-&gt;getEnvironment().'.yml'); } } </code></pre> <hr> <pre><code>&lt;?php namespace Tel\Cli; use Symfony\Component\Console\Application; use Cli\Command; /** * Console application */ class Console extends Application { /** * Console constructor. */ public function __construct($kernel) { parent::__construct('Application name', '1.0'); $this-&gt;kernel = $kernel; // @TODO - Get these commands from DB/config file $this-&gt;addCommands(array( new Command\P1, new Command\P2, new Command\P3, )); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload