2

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
    }
}

1 Answer 1

3

Alternatively, you could write your own yiic shell wrapper for each environment that defined CRAFT_ENVIRONMENT.

Something like yiicstaging:

#!/usr/bin/env php
<?php

define('CRAFT_ENVIRONMENT', 'staging')
require_once('path/to/craft/app/etc/console/yiic.php');
1
  • That was going to be my next attempt. Thanks! Might be a nice feature in the future to be able to handle this natively. Commented Jan 6, 2017 at 22:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.