过去,我们使用这种技术取得了很大的成功:
DevLevel.0
代表Production,DevLevel.1
代表Staging,DevLevel.2
代表Development。我们有一个 PHP 全局包含检查__FILE__
魔术常量并查看我们所在的位置,然后DevLevel
相应地设置一个常量。然后在不同的地方,我们会有:
if(DevLevel == 0)
run credit card in live mode;
else
run credit card in test mode;
但问题是我们已经超出了它。有时站点有 2 个暂存位置和 10 个开发位置,但存在细微差别。
从那时起,我们开发了一个自动化脚本来检查环境并在项目中发出一个 Local.php,它不受版本控制。它非常灵活,允许我们在任何环境中使用一个命令自动生成 apache、nginx、mysql、postgresql、redis、ssl 和项目配置。
但回到你的项目。这是我的建议:
/path/to/project/conf/example-local.php
/path/to/project/conf/local.php
/path/to/project/conf/.gitignore (contents: local.php)
(用您的 SCM 系统使用的任何忽略机制替换 git ignore)
将“示例”配置放入example-local.php
. 然后,任何时候开发人员或发布工程师检查它的副本,他们只需要复制example-local.php
并local.php
调整它。
您的应用程序应该有一个全局包含文件(如 global.php),它首先包含在每个脚本 PERIOD 中。它应该依次包含 local.php,然后验证正确的常量是否已定义且有效。
这是 global.php 的片段
<?
///////////////////////////////////////////////////////////////////////////////
define('App_Path', dirname(dirname(__FILE__)));
define('CORE_PATH', App_Path.'/Web/');
ini_set("max_execution_time", "0");
// For the purposes of including pear embedded in the application
set_include_path(App_Path . '/PHP/pear' . PATH_SEPARATOR . get_include_path() );
require_once(App_Path . "/AutoConf/Local.php");
///////////////////////////////////////////////////////////////////////////////
// Utility function to assert that a constant has been defined.
function DefinedOrDie($constant, $message)
{
if(! defined($constant))
die("Constant '$constant' not defined in site configuration. $message");
}
// Utility function to check if a constant is defined, and if not, define it.
function DefinedOrDefault($constant, $default)
{
if(! defined($constant))
define($constant, $default);
}
/////////////////////////////////////////////////////////////////////////////////
// These are the constants which MUST be defined in the AutoConf file
DefinedOrDie('DevLevel', "DevelopmentLevel. 0=Production, 1=Preview, 2=Devel.");
if(DevLevel !== 0 && DevLevel !== 1 && DevLevel !== 2)
die("DevLevel constant must be defined as one of [0, 1, 2].");
DefinedOrDie('DEFAULT_HOSTNAME', "Canonical hostname of the site. No trailing slash or leading protocol!");
DefinedOrDie('DB_HOST', "Database server hostname.");
DefinedOrDie('DB_NAME', "Database name.");
DefinedOrDie('DB_USER', "Database login username.");
DefinedOrDie('DB_PASS', "Database password.");
if(DevLevel > 0){
DefinedOrDie('Email_OnlySendIfMatch', "Preg regex email must match to be sent.");
DefinedOrDie('Email_OverrideAddress', "Email address to send all emails to.");
DefinedOrDie('Fax_OnlySendIfMatch', "Preg regex fax number must match to be sent.");
DefinedOrDie('Fax_OverrideNumber', "Fax number to send all faxes to.");
DefinedOrDie('Text_OnlySendIfMatch', "Preg regex text message must match to be sent.");
DefinedOrDie('Text_OverrideNumber', "Text number to send all texts to.");
DefinedOrDie('Phone_OnlySendIfMatch', "Preg regex phone number must match to be sent.");
DefinedOrDie('Phone_OverrideNumber', "Phone number to place all phone calls to.");
DefinedOrDefault('PROCESS_BILLING', false);
DefinedOrDefault('MEMCACHE_ENABLED', false);
}else{
DefinedOrDefault('PROCESS_BILLING', true);
DefinedOrDefault('MEMCACHE_ENABLED', true);
}
///////////////////////////////////////////////////////////////////////////////
// These are the constants which MAY be defined in the AutoConf file
// Default settings for live site
if(DevLevel == 0)
{
define('DEVEL', false);
define('DEVEL_DEBUG', false);
error_reporting (E_RECOVERABLE_ERROR | E_ERROR | E_PARSE);
ini_set("display_errors", false);
}
// Default settings for preview site
elseif(DevLevel == 1)
{
define('DEVEL', true);
define('DEVEL_DEBUG', false);
error_reporting (E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
ini_set("display_errors", true);
}
// Default settings for development and other sites
else
{
// TODO: remove E_DEPRECATED, and take care of all warnings
define('DEVEL', true);
define('DEVEL_DEBUG', false);
error_reporting (E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED);
ini_set("display_errors", true);
}
// Memcache settings are required, here are defaults
DefinedOrDefault('MEMCACHE_HOST', 'localhost');
DefinedOrDefault('MEMCACHE_PORT', '11211');
这是一个更复杂(但匹配)的 local.php 示例
<?php
define('DevLevel', 1);
define('DB_HOST', 'localhost');
define('DB_NAME', '*******');
define('DB_USER', '*******');
define('DB_PASS', '*******');
define('DEFAULT_HOSTNAME', '***********');
define('MEMCACHE_SESSION_HOST', 'localhost');
//define users who will be notified when different alerts are sent by the system. Separate users with a |
define('ALERT_LEVEL_NOTICE_SMS', '122342342319');
define('ALERT_LEVEL_NOTICE_EMAIL', 'j3@example.com');
define('PROCESS_BILLING', false);
define('Email_OnlySendIfMatch', '/(appcove.com|example.com)$/');
define('Email_OverrideAddress', false);
define('FAXES_LIVE', true); //false to use phaxio's test keys (no faxes are sent)
function HN_LiveToDev($host){
return str_replace('.', '--', $host).'.e1.example.net';
}
function HN_DevToLive($host){
$count = 0;
$host = str_replace('.e1.example.net', '', $host, $count);
if($count > 0){
return str_replace('--', '.', $host);
}else{
die('URL does not look like a dev URL. Could not translate DevToLive.');
}
}