Last active
August 29, 2015 14:19
-
-
Save dereckson/b874c98645ae488ad0d9 to your computer and use it in GitHub Desktop.
php-fpm-env > /usr/local/etc/env.conf
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
#!/usr/bin/env php | |
<?php | |
/** | |
* Allows to get the content of a php-fpm environment configuration file | |
*/ | |
class PhpFpmEnvironment { | |
/** | |
* The temporary directory, used in TMP, TEMP and TMPDIR environment variables | |
* @var string | |
*/ | |
const TMP = '/tmp'; | |
/** | |
* The path where to find executables, where sbin should be excluded if you don't run PHP as root. | |
* @var string | |
*/ | |
const PATH = '/usr/local/bin:/usr/bin:/bin'; | |
/** | |
* The environment variables to discard | |
* @var Array | |
*/ | |
const VARIABLES_TO_DISCARD = [ | |
'_', // The caller executable script, not pertinent | |
'HOME', // Set correctly by php-fpm | |
'TERM', // Not pertinent in server context | |
'MYSQL_ENV_MYSQL_ROOT_PASSWORD', // from --link …:mysql | |
]; | |
/** | |
*ets an environment array from the current process environment, | |
* with PATH and temp variablesfiltered. | |
* | |
* @return Array | |
*/ | |
public static function getEnvironmentVariables () { | |
$variables = []; | |
foreach ($_ENV as $key => $value) { | |
if (static::mustIgnoreVariable($key)) { | |
continue; | |
} | |
$variables[$key] = $value; | |
} | |
$variables['PATH'] = static::PATH; | |
$variables['TMP'] = static::TMP; | |
$variables['TEMP'] = static::TMP; | |
$variables['TMPDIR'] = static::TMP; | |
return $variables; | |
} | |
/** | |
* Determines if the variable name must be ignored | |
* | |
* @return bool true if the variable must be ignored; otherwise, false. | |
*/ | |
public static function mustIgnoreVariable ($variableName) { | |
return in_array($variableName, static::VARIABLES_TO_DISCARD); | |
} | |
/** | |
* Gets the environment | |
*/ | |
public static function get () { | |
$variables = static::getEnvironmentVariables(); | |
foreach ($variables as $key => $value) { | |
echo 'env["', $key, '"] = "', $value, '"', PHP_EOL; | |
} | |
} | |
} | |
PhpFpmEnvironment::get(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment