Jump to content

$config->httpHosts Programmatically


joer80
 Share

Recommended Posts

  • 1 year later...

i came up with this solution today:

WARNING: your site/config.php should NOT be writable by your php user, so you would have to be careful about file permissions: https://processwire.com/docs/security/file-permissions/

usage:

updateConfig('/path/to/your/site/config.php', [
    'httpHosts' => ['domain1.example.com', 'domain2.example.com'],
    'dbName' => 'my_db',
    'dbUser' => 'my_db_user',
    'dbPass' => 'my_db_password',
]);

function:

/**
 * update config file
 */
function updateConfig($file, $options = []) {
    if(!is_file($file)) return 'file not found';
    $content = file_get_contents($file);

    // loop all set options
    foreach($options as $key => $val) {
        $search = '/\$config->'.$key.'(.*);/';
        
        if(is_array($val)) $replace = '$config->'.$key.' = array(\'' . implode("','", $val) . '\');';
        elseif(is_int($val)) $replace = '$config->'.$key.' = ' . $val . ';';
        elseif(is_string($val)) $replace = '$config->'.$key.' = \'' . $val . '\';';
        else return 'value must be array, integer or string';

        $content = preg_replace($search, $replace, $content);
    }

    return file_put_contents($file, $content);
}

maybe it helps someone :)

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...