views:

184

answers:

1

I have a staging and development environment on the same machine. I would like to configure a different memcached port in ProjectConfiguration.class.php depending on my environment. I imagine its not safe to use $SERVER['HTTP_HOST'] inside of the ProjectConfiguration file because that won't account for tasks run from the command line.

What would be the best way to accomplish what this bit of code intends:

public function configureDoctrine(Doctrine_Manager $manager) {

    if ($_SERVER['HTTP_HOST'] == 'dev.example.com') {
      $memcached_port = 11211;
    } else {
      $memcached_port = 11212;
    }

    $servers = array(
        'host' => '127.0.0.1',
        'port' => $memcached_port,
        'persistent' => true);
}
+1  A: 

I use http host in the web controller to detect the environment and pass it to the project configuration instance, as per here:

<?php

require_once(dirname(__FILE__).'/../config/ProjectConfiguration.class.php');

$env = ProjectConfiguration::getEnvironment();
if (!in_array($env, array('dev'))) throw new Exception('Not Allowed');

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', $env, true);
sfContext::createInstance($configuration)->dispatch();

This is from a frontend_dev.php - so it also ensures that you can't access the dev controller anywhere but dev.

My project configuration class contains the referenced method, which does the work:

public function getEnvironment() {
  if ($_SERVER ['HTTP_HOST'] == 'dev.example.com') {
    return 'dev';
  } else {
    return 'prod';
  }
}

As you rightly say, there are also command line tasks to consider - but almost all symfony tasks will take --env=xxx arguments. I use those. They all default to dev anyway, which is where I do most of my work, so its not as arduous as it sounds.

I'd then use if (sfConfig::get('sf_environment') == 'dev') in your if statement, rather than the HTTP_HOST directly, which will work from both cmd line and web.

benlumley