This works great! The problem I’m facing now is to use the same thing on my console application, but there I don’t have any $_SERVER[‘HTTP_HOST’]. How to check in console.php file that I should connect to a live or local db?
/**
* Common configuration file.
*/
/**
* CliCheck
*
* Helper class to check if we are on the command line.
*/
class CliCheck
{
/**
* Advanced PHP-CLI mode check.
*
* @return boolean Returns true if PHP is running from the CLI or else false.
*
* @access public
* @static
*/
public static function isCli()
{
// If STDIN was not defined and PHP is running as CGI module
// we can test for the environment variable TERM. This
// should be a right way how to test the circumstance under
// what mode PHP is running.
if(!defined('STDIN') && self::isCgi()) {
// STDIN was not defined, but if the environment variable TERM
// is set, it is save to say that PHP is running from CLI.
if(getenv('TERM')) {
return true;
}
// Now return false, because TERM was not set.
return false;
}
return defined('STDIN');
}
/**
* Simple PHP-CGI mode check.
*
* (DSO = Dynamic Shared Object)
*
* @link http://httpd.apache.org/docs/current/dso.html DSO
* @link http://www.php.net/manual/en/function.php-sapi-name.php PHP_SAPI
*
* @return boolean Returns true if PHP is running as CGI module or else false.
*
* @access public
* @static
*/
public static function isCgi()
{
if (substr(PHP_SAPI, 0, 3) == 'cgi') {
return true;
} else {
return false;
}
return false;
}
}
if(!CliCheck::isCli()) {
//$host = $_SERVER['HTTP_HOST']; // Alternative, switched to SERVER_NAME myself.
$host = getenv('SERVER_NAME');
} else {
$host=gethostname();
}
/** Check if there is a site configuration */
$siteconf = dirname(__FILE__).DIRECTORY_SEPARATOR.$host.'_common.php';
if(file_exists($siteconf)) {
$siteconf=require($siteconf);
} else {
print "No site configuration $siteconf";
$siteconf=array();
}
global $isProduction;
if(!isset($isProduction)) {
$isProduction=false;
}
I set my database information in the ‘…_common.php’ or elsewhere. You can do