我正在评估将一个相当大的网站从经典 ASP 迁移到 PHP,当然我首先会考虑所有潜在的问题和障碍。 PHP 版本最初仍将在 IIS 下运行。
开发涉及本地 mySQL 服务器和一些本地域,例如localhost.website.com、localhost.cdn.website.com、localhost.api.website.com 等,但实时系统不使用它们中的任何一个,而仅使用实时域等。
目前,在经典 ASP 中,系统通过使用仅存在于服务器上的信号量文件在“live”和“dev”之间切换。它在 global.asa 中执行此操作,然后根据文件的存在设置一堆应用程序变量。
我对 PHP 不是特别熟悉,但环顾四周,它似乎遵循“不共享”原则,因此没有“应用程序范围”的东西。
我想知道如何实现类似的环境,可以在本地上传代码而无需更改,并且系统自动有效地识别其所在的系统。
最佳答案
过去,我们使用此技术并取得了很大成功:
DevLevel.0
代表 Production,DevLevel.1
代表 Staging,DevLevel.2
代表开发。我们有一个 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)
(将 gitignore 替换为您的 SCM 系统使用的任何忽略机制)
将“示例”配置放入 example-local.php
中。然后,每当开发人员或发布工程师检查它的副本时,他们只需将 example-local.php
复制到 local.php
并进行调整即可。 p>
您的应用程序应该有一个全局包含文件(如 global.php),该文件在每个脚本周期中首先包含。它应该依次包含 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', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="12782152776a737f627e773c717d7f" rel="noreferrer noopener nofollow">[email protected]</a>');
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.');
}
}
关于php - 从经典 ASP 迁移到 PHP - 本地资源与实时资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14285798/