From 6b207eb4806708da435d7f605043fb9ad59fa674 Mon Sep 17 00:00:00 2001 From: Kuba Turek Date: Sat, 17 Jan 2015 23:29:12 +0100 Subject: [PATCH] Obtain environment variables --- Mage/Task/AbstractTask.php | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/Mage/Task/AbstractTask.php b/Mage/Task/AbstractTask.php index 491adfb..3637148 100644 --- a/Mage/Task/AbstractTask.php +++ b/Mage/Task/AbstractTask.php @@ -307,4 +307,53 @@ abstract class AbstractTask } return $result; } + + /** + * Returns the array of environment variables + * Returned array contains both system variables and variables set in config + * WARNING: To access system's variables you need to set proper value in your php.ini at variables_order key + * @see http://php.net/manual/en/ini.core.php#ini.variables-order + * + * @return array + */ + protected function getEnvVariables() + { + $configVars = array_merge( + $this->getConfig()->general('env', []), + $this->getConfig()->environmentConfig('env', []), + $this->getConfig()->getParameter('env', []) + ); + + if (isset($configVars['variables'])) { + $configVars = $configVars['variables']; + } + + $envVariables = array_merge( + $_ENV, + $configVars + ); + + return $envVariables; + } + + /** + * Returns ready to inject environment string + * The string is build from env vars array in schema: + * key1=value1 key2=value3 ... + * + * @return string + */ + protected function getEnvVarsString() + { + $envVarsArray = $this->getEnvVariables(); + $envVars = array_map( + function ($key, $value) { + return "$key=$value"; + }, + array_keys($envVarsArray), + $this->getEnvVariables() + ); + + return join(' ', $envVars); + } }