开发者

How can I refer to different environment's inside Symfony's ProjectConfiguration.class.php

开发者 https://www.devze.com 2022-12-21 13:25 出处:网络
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

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


There are 2 ways to accomplish this.

To start off, you should be setting your environment separately in index.php. What we usually do is set it to prod, and then check in the file, and manually change it to dev on our dev servers and local machines in order to make deployment easier. I also recommend put this file in .gitignore so it never accidentally gets changed.

  1. Use the app.yml configuration file. In your app.yml config, set up a key called 'memcache_port' and set the value separately for each environment. The yaml file would look something like this:

    prod:
      memcached_port: 11211
    
    dev:
      memcached_port: 11212
    

    Then to access this value in the code you would say:

    $memcached_port = sfConfig::get('app_memcached_port');
    
  2. Get the environment name manually through the symfony context

    $env = sfContext::getInstance()->getConfiguration()->getEnvironment();
    if($env == 'dev') {
      $memcached_port = 11211;
    } else if($env == 'prod') {
      $memcached_port = 11212;
    }
    

I personally prefer option 1, but it's totally up to you how you want to handle this.

Note: When I refer to the environment, I am referring to the symfony environment defined in the index.php file.

Hope this helps.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消