DBFarmRole::applyGlobalVarsToValue PHP Method

applyGlobalVarsToValue() public method

Apply FarmRole global variables to a value
public applyGlobalVarsToValue ( $value ) : string
return string
    public function applyGlobalVarsToValue($value)
    {
        if (empty($this->globalVariablesCache)) {
            $formats = \Scalr::config("scalr.system.global_variables.format");
            $systemVars = array('env_id' => $this->GetFarmObject()->EnvID, 'env_name' => $this->GetFarmObject()->GetEnvironmentObject()->name, 'farm_team' => $this->GetFarmObject()->teamId ? (new Scalr_Account_Team())->loadById($this->GetFarmObject()->teamId)->name : '', 'farm_id' => $this->GetFarmObject()->ID, 'farm_name' => $this->GetFarmObject()->Name, 'farm_hash' => $this->GetFarmObject()->Hash, 'farm_owner_email' => $this->GetFarmObject()->createdByUserEmail, 'farm_role_id' => $this->ID, 'farm_role_alias' => $this->Alias, 'cloud_location' => $this->CloudLocation);
            if (\Scalr::getContainer()->analytics->enabled) {
                $projectId = $this->GetFarmObject()->GetSetting(Entity\FarmSetting::PROJECT_ID);
                if ($projectId) {
                    /* @var $project ProjectEntity */
                    $project = ProjectEntity::findPk($projectId);
                    $systemVars['project_id'] = $projectId;
                    $systemVars['project_bc'] = $project->getProperty(ProjectPropertyEntity::NAME_BILLING_CODE);
                    $systemVars['project_name'] = $project->name;
                    $ccId = $project->ccId;
                }
                if ($ccId) {
                    /* @var $cc CostCentreEntity */
                    $cc = CostCentreEntity::findPk($ccId);
                    if ($cc) {
                        $systemVars['cost_center_id'] = $ccId;
                        $systemVars['cost_center_bc'] = $cc->getProperty(CostCentrePropertyEntity::NAME_BILLING_CODE);
                        $systemVars['cost_center_name'] = $cc->name;
                    } else {
                        throw new Exception("Cost center {$ccId} not found");
                    }
                }
            }
            // Get list of Server system vars
            foreach ($systemVars as $name => $val) {
                $name = "SCALR_" . strtoupper($name);
                $val = trim($val);
                if (isset($formats[$name])) {
                    $val = @sprintf($formats[$name], $val);
                }
                $this->globalVariablesCache[$name] = $val;
            }
            // Add custom variables
            $gv = new Scalr_Scripting_GlobalVariables($this->GetFarmObject()->ClientID, $this->GetFarmObject()->EnvID, ScopeInterface::SCOPE_FARMROLE);
            $vars = $gv->listVariables($this->RoleID, $this->FarmID, $this->ID);
            foreach ($vars as $v) {
                $this->globalVariablesCache[$v['name']] = $v['value'];
            }
        }
        //Parse variable
        $keys = array_map(function ($item) {
            return "{" . $item . "}";
        }, array_keys($this->globalVariablesCache));
        $values = array_values($this->globalVariablesCache);
        $retval = str_replace($keys, $values, $value);
        // Strip undefined variables & return value
        return preg_replace("/{[A-Za-z0-9_-]+}/", "", $retval);
    }