从经典ASP转向PHP - 本地vs实时资源

时间:2013-01-11 20:02:03

标签: php asp-classic

我正在评估将一个相当大的网站从经典ASP移动到PHP,当然我首先考虑所有潜在的问题和速度突击。 PHP版本最初仍将在IIS下运行。

开发涉及本地mySQL服务器和一些本地域,例如localhost.website.com,localhost.cdn.website.com,localhost.api.website.com等,但实时系统不使用任何一个,而只是使用实时域等。

在经典ASP中,系统通过使用仅存在于服务器上的信号量文件在“live”和“dev”之间切换。它在global.asa中执行此操作,并继续根据文件的存在设置一堆应用程序变量。

我对PHP并不是特别熟悉,但环顾四周似乎操作了“无分享”原则,所以没有“应用范围”的东西。

我想知道如何实现类似的环境,在这种环境中本地代码可以无需更改即可上传,并且系统会以有效的方式自动识别系统。

2 个答案:

答案 0 :(得分:2)

过去,我们使用这项技术取得了很大的成功:

DevLevel.0代表生产,DevLevel.1代表临时,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)

(用你的SCM系统使用的任何忽略机制替换git ignore)

example-local.php中添加“示例”配置。然后,只要开发人员或发布工程师签出副本,他们只需将example-local.php复制到local.php并进行调整即可。

您的应用程序应该有一个全局包含文件(如global.php),该文件在每个脚本PERIOD上都包含FIRST。它应该包含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.');
   }
 }

答案 1 :(得分:1)

我不熟悉IIS,但是在apache上我已经让web服务器只在我的dev box webserver config上设置了一个环境变量。然后在php:

define('DEV', isset($_ENV['my_env_var']));

这比检查文件更快,因为从来没有任何IO。它还具有理想的属性,即在没有环境变量的情况下默认为“实时”模式。

$_ENV是预定义变量:http://php.net/manual/en/reserved.variables.php

要注意 - 在php中,未定义的常量被解释为不带引号的字符串,并将强制转换为布尔值true。只要确保常量始终定义为你就没事了。