I have built a plugin with some console commands for automating some regular tasks that will be run by a cron job for my site.
In order to get the commands working, I had to replicate my database configuration for the console
environment in db.php
.
All was well until I realized that each of my environments, local, staging and production will have unique database credentials, but there is only one console environment.
Is there a way I can force the yiic
command to use a specific environment via a command line switch, either passing the domain name of an existing environment or additional additional arbitrarily named console environments?
Or is there another way to accomplish this so that the console environment has a unique configuration for each copy of the site?
For the time being I've been able to add an --environment
switch to craft/app/etc/console/yiic.php
which defines the CRAFT_ENVIRONMENT
constant ahead of it defaulting to console
, but this seems rather hacky and I'd hope there's a better way to do this.
// Existing switch check starting on line 18
if (isset($_SERVER['argv']))
{
foreach ($argv as $key => $arg)
{
if (strpos($arg, '--configPath=') !== false)
{
$parts = explode('=', $arg);
$frontConfigPath = rtrim($parts[1], '/').'/';
unset($_SERVER['argv'][$key]);
break;
}
// New environment switch added here
if (strpos($arg, '--environment=') !== false)
{
$parts = explode('=', $arg);
$environment = $parts[1];
define('CRAFT_ENVIRONMENT', $environment);
unset($_SERVER['argv'][$key]);
break;
}
// end new environment switch
}
}