DBServer::applyGlobalVarsToValue PHP Method

applyGlobalVarsToValue() public method

public applyGlobalVarsToValue ( $value )
    public function applyGlobalVarsToValue($value)
    {
        if (empty($this->globalVariablesCache)) {
            $formats = \Scalr::config("scalr.system.global_variables.format");
            // Get list of Server system vars
            foreach ($this->GetScriptingVars() 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->clientId, $this->envId, ScopeInterface::SCOPE_SERVER);
            $vars = $gv->listVariables($this->GetFarmRoleObject()->RoleID, $this->farmId, $this->farmRoleId, $this->serverId);
            foreach ($vars as $v) {
                $this->globalVariablesCache[$v['name']] = $v['value'];
            }
        }
        //Parse variable
        $keys = array_keys($this->globalVariablesCache);
        $keys = array_map(function ($item) {
            return '{' . $item . '}';
        }, $keys);
        $values = array_values($this->globalVariablesCache);
        $retval = str_replace($keys, $values, $value);
        // Strip undefined variables & return value
        return preg_replace("/{[A-Za-z0-9_-]+}/", "", $retval);
    }

Usage Example

Example #1
0
 public function getConfiguration(DBServer $dbServer)
 {
     $config = new stdClass();
     $config->virtualHosts = array();
     $vhosts = $this->db->Execute("SELECT * FROM `apache_vhosts` WHERE farm_id = ? AND farm_roleid = ?", array($dbServer->farmId, $dbServer->farmRoleId));
     while ($vhost = $vhosts->FetchRow()) {
         if ($vhost['is_ssl_enabled']) {
             $itm = new stdClass();
             $itm->hostname = $vhost['name'];
             $itm->port = 80;
             $itm->template = $vhost['httpd_conf'];
             $itm->ssl = 0;
             $itm->sslCertificateId = null;
             $vars = unserialize($vhost['httpd_conf_vars']);
             $vars['host'] = $vhost['name'];
             $vKeys = array_keys($vars);
             $keys = array_map(function ($item) {
                 return '{$' . $item . '}';
             }, $vKeys);
             $vValues = array_values($vars);
             $itm->template = str_replace($keys, $vValues, $itm->template);
             $dbServer->applyGlobalVarsToValue($itm->template);
             array_push($config->virtualHosts, $itm);
         }
         $itm = new stdClass();
         $itm->hostname = $vhost['name'];
         $itm->port = $vhost['is_ssl_enabled'] ? 443 : 80;
         $itm->template = $vhost['is_ssl_enabled'] ? $vhost['httpd_conf_ssl'] : $vhost['httpd_conf'];
         $itm->ssl = $vhost['is_ssl_enabled'];
         $itm->sslCertificateId = $vhost['ssl_cert_id'];
         $vars = unserialize($vhost['httpd_conf_vars']);
         $vars['host'] = $vhost['name'];
         $vKeys = array_keys($vars);
         $keys = array_map(function ($item) {
             return '{$' . $item . '}';
         }, $vKeys);
         $vValues = array_values($vars);
         $itm->template = str_replace($keys, $vValues, $itm->template);
         $dbServer->applyGlobalVarsToValue($itm->template);
         array_push($config->virtualHosts, $itm);
     }
     return $config;
 }
All Usage Examples Of DBServer::applyGlobalVarsToValue