Created
March 7, 2012 09:41
-
-
Save otar/1992236 to your computer and use it in GitHub Desktop.
Detect current environment and define as a global constant.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Detect current environment and define as a global constant. | |
* @param array $environments Associative-Array where the keys are environment | |
* names and it's values the URI's that should be matched. Environment URI's can | |
* be string or an array of strings. If no environment URI is matched sets | |
* current environment as a first key element of an environments array. | |
* @param string $constant_name Name of the constant enfironment should be | |
* assigned to. | |
* @return boolean TRUE if an environment URI was matched, FALSE otherwise. | |
* @author Otar Chekurishvili | |
* @version 1.0 | |
* @license http://www.opensource.org/licenses/MIT The MIT License | |
*/ | |
function envirions($environments = array(), $constant_name = 'ENV') | |
{ | |
foreach ($environments AS $environment => $uri) | |
{ | |
$current_uri = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']; | |
if (is_string($uri) AND FALSE !== strpos($current_uri, $uri)) | |
{ | |
define($constant_name, $environment); | |
break; | |
} | |
elseif (is_array($uri)) | |
{ | |
foreach ($uri AS $sub_uri) | |
{ | |
if (FALSE !== strpos($current_uri, $sub_uri)) | |
{ | |
define($constant_name, $environment); | |
break; | |
} | |
} | |
if (defined($constant_name)) | |
{ | |
break; | |
} | |
else | |
{ | |
continue; | |
} | |
} | |
else | |
{ | |
continue; | |
} | |
} | |
if (!defined($constant_name)) | |
{ | |
if (empty($environments)) | |
{ | |
return false; | |
} | |
else | |
{ | |
$environments = array_keys($environments); | |
define($constant_name, $environments[0]); | |
} | |
} | |
return TRUE; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'envirions.php'; | |
envirions(array( | |
'development' => array( | |
'localhost.com', | |
'local.domain' | |
), | |
'testing' => 'test.domain.com', | |
'production' => array( | |
'domain.com', | |
'alternative.domain.com' | |
) | |
)); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment