You are here

function acsf_init_default_settings_php_update in Acquia Cloud Site Factory Connector 8

Updates the default settings.php with the ACSF specific script include.

Parameters

string $default_settings_php_path: The path to the default settings.php file.

1 call to acsf_init_default_settings_php_update()
drush_acsf_init in acsf_init/acsf_init.drush.inc
Command callback: executes the required changes to this repository.

File

acsf_init/acsf_init.drush.inc, line 301
Provides drush commands to set up a site for Acquia Site Factory.

Code

function acsf_init_default_settings_php_update($default_settings_php_path) {
  $default_settings_php_contents = file_get_contents($default_settings_php_path);

  // Check if the default settings.php contains our code block. The m modifier
  // makes it possible to match text over multiple lines and the s modifier
  // allows the . wildcard to match newline characters.
  if (!preg_match('/' . preg_quote(ACSF_INIT_CODE_DELIMITER_START, '/') . '.*?' . preg_quote(ACSF_INIT_CODE_DELIMITER_END, '/') . '/ms', $default_settings_php_contents)) {

    // Code block not detected: add it after the opening php tag.
    drush_log(dt('ACSF include not detected in sites/default/settings.php.'), 'notice');

    // Using preg_replace instead of str_replace to be able to control how many
    // times the replace gets executed.
    $default_settings_php_contents = preg_replace('/<\\?php/', "<?php\n\n" . acsf_init_default_settings_php_include_get() . "\n", $default_settings_php_contents, 1, $count);
    if ($count === 0) {
      drush_log(dt('Could not find <?php tag in sites/default/settings.php.'), 'error');
    }
  }
  else {

    // Code block found: update it with the latest version.
    drush_log(dt('ACSF include detected in sites/default/settings.php.'), 'notice');
    $default_settings_php_contents = preg_replace('/' . preg_quote(ACSF_INIT_CODE_DELIMITER_START, '/') . '.*?' . preg_quote(ACSF_INIT_CODE_DELIMITER_END, '/') . '/ms', acsf_init_default_settings_php_include_get(), $default_settings_php_contents);
  }
  $result = file_put_contents($default_settings_php_path, $default_settings_php_contents);
  if ($result) {
    drush_log(dt('File edit success: sites/default/settings.php'), 'success');
  }
  else {
    drush_log(dt('File edit error: sites/default/settings.php'), 'error');
  }
}