Commit 83e27c41 authored by Jon Pugh's avatar Jon Pugh

Merge branch '3073299-hook-environment' into 7.x-3.x-devshop

parents de9302a1 917c8c68
......@@ -647,6 +647,9 @@ function provision_parse_info_file($filename) {
* The real credentials are stored in the Apache vhost of the relevant site, to prevent leaking of
* sensitive data to site administrators with PHP access who might otherwise access such credentials
* potentially of other sites' settings.php in a multisite set-up.
*
* Fire hook_provision_prepare_environment() so other scripts can react to this
* step and add more information or take other actions.
*/
function provision_prepare_environment() {
$fields = array('db_type', 'db_host', 'db_user', 'db_passwd', 'db_name', 'db_port');
......@@ -658,6 +661,11 @@ function provision_prepare_environment() {
if (drush_drupal_major_version() >= 7) {
$_SERVER['db_type'] = ($_SERVER['db_type'] == 'mysqli') ? 'mysql' : $_SERVER['db_type'];
}
// Invoke provision_prepare_environment: Allow other scripts to react to the
// preparation of the environment variables.
drush_command_invoke_all('provision_prepare_environment');
}
......
......@@ -418,3 +418,34 @@ function hook_provision_mysql_regex_alter(&$regexes) {
'#/\*!50001 CREATE ALGORITHM=UNDEFINED \*/#' => "/*!50001 CREATE */",
);
}
/**
* Implements hook_provision_prepare_environment()
*
* React to the setting up of $_SERVER variables such as db_name and db_passwd.
*
* Runs right after writing sites/$URI/drushrc.php.
* Database credentials are available in the $_SERVER variables.
*
* @see provision_prepare_environment()
*/
function hook_provision_prepare_environment() {
// Write a .env file in the root of the project with the Drupal DB credentials.
// This file could be used by other tools to access the site's database.
$file_name = d()->root . '/.env';
$file_contents = <<<ENV
MYSQL_DATABASE={$_SERVER['db_name']}
MYSQL_USER={$_SERVER['db_name']}
MYSQL_PASSWORD={$_SERVER['db_name']}
ENV;
// Make writable, then write the file.
if (file_exists($file_name) && !is_writable($file_name)) {
provision_file()->chmod($file_name, 0660);
}
file_put_contents($file_name, $file_contents);
// Hide sensitive information from any other users.
provision_file()->chmod($file_name, 0400);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment