Commit d931dcc0 authored by Philippe Mouchel's avatar Philippe Mouchel
Browse files

Initial commit, first drupal 6 version

parents
<?php
/**
* @file
* Per-environment configuration module,
* drush commands.
*/
/**
* Implementation of hook_drush_help().
*/
function env_conf_drush_help($section) {
switch ($section) {
case 'drush:deploy-configuration':
return dt('Deploy a selected environment configuration.');
}
}
/**
* Implements hook_drush_command().
*/
function env_conf_drush_command() {
return array(
'deploy-configuration' => array(
'description' => 'Deploy a selected environment configuration.',
'aliases' => array('d-conf'),
'callback' => 'drush_deploy_environment_configuration',
'arguments' => array(
'selected-environment' => 'Environment you want to deploy the configuration.',
),
),
);
}
function drush_deploy_environment_configuration($selected_environment = NULL) {
module_load_include('inc', 'env_conf');
// Alert if selected environment isn't available
$available_envs = _get_simple_available_environments();
// Determine default environment if not selected
if ($selected_environment === NULL) {
$selected_environment = variable_get('environment', NULL);
if ($selected_environment === NULL) {
drupal_set_message(t('No environment configured in <i>settings.php</i>. You should edit it and add "$conf[\'environment\'] = \'env_name\';".'), 'error');
drupal_set_message(t('Unable to deploy any environment configuration.'), 'error');
return FALSE;
}
else {
drupal_set_message(t('Environment detected in <i>settings.php</i>: "!env".', array('!env' => $selected_environment)));
}
}
elseif (!in_array($selected_environment, $available_envs)) {
drupal_set_message(t('Selected environment is "!env", but available environments are "!available_envs". Please select one of them or define "!env" as an available environment by implementing "hook_env_conf_available_env()".', array('!env' => $selected_environment, '!available_envs' => implode(', ', $available_envs))), 'error');
return FALSE;
}
// Deploy selected configuration
drupal_set_message(t('Starting deploy "!env" configuration.', array('!env' => $selected_environment)));
_deploy_environment_configuration($selected_environment);
drupal_set_message(t('"!env" configuration deployed.', array('!env' => $selected_environment)));
}
<?php
/**
* @file
* Per-environment configuration module,
* forms.
*/
/**
* Allow user to lauch configuration deployment,
* with default, or manually defined, parameter.
*/
function env_conf_form() {
module_load_include('inc', 'env_conf');
$form = array();
// List available environments
$available_envs = _get_simple_available_environments();
// Get current environment name (normally in settings.php,
// but possibly in a Drupal variable,
// so let's get it with variable_get function)
// and alert if there is a problem
$environment = variable_get('environment', NULL);
if ($environment === NULL) {
drupal_set_message(t('No environment configured in <i>settings.php</i>. You should edit it and add <strong>$conf[\'environment\'] = \'env_name\';</strong>.'), 'error');
}
elseif (!in_array($environment, $available_envs)) {
drupal_set_message(t('Environment is defined as <strong>!env</strong> in <i>settings.php</i>, but available environments are <strong>!available_envs</strong>. You should edit <i>settings.php</i> or define <strong>!env</strong> as an available environment by implementing <strong>hook_env_conf_available_env()</strong>.', array('!env' => $environment, '!last_deployed_conf' => $last_deployed_conf, '!available_envs' => implode(', ', $available_envs))), 'warning');
}
else {
// Alert if last deployed configuration doesn't match
// the one defined in settings.php or if configuration
// has to be deployed.
$last_deployed_conf = variable_get('env_conf_last_deployed_conf', NULL);
if ($last_deployed_conf === NULL || empty($last_deployed_conf)) {
drupal_set_message(t('Default environment is defined as <strong>!env</strong> in <i>settings.php</i>.', array('!env' => $environment)));
drupal_set_message(t('But no environment configuration has been deployed. You should do it now!'), 'error');
}
elseif ($last_deployed_conf !== $environment) {
drupal_set_message(t('Environment is defined as <strong>!env</strong> in <i>settings.php</i>.', array('!env' => $environment)));
drupal_set_message(t('But last deployed configuration is <strong>!last_deployed_conf</strong>.', array('!last_deployed_conf' => $last_deployed_conf)), 'warning');
}
elseif ($last_deployed_conf === $environment) {
drupal_set_message(t('Last deployed configuration is <strong>!last_deployed_conf</strong>, as it\'s defined in <i>settings.php</i>.', array('!last_deployed_conf' => $last_deployed_conf)));
}
// Manually select environment to execute
$form['select_env'] = array(
'#type' => 'select',
'#title' => t('Configuration to deploy'),
'#description' => t('Select which configuration you want to deploy. Default value is defined in <i>settings.php</i>.'),
'#default_value' => 'default',
'#options' => array('default' => 'default') + $available_envs,
);
if ($environment === NULL) {
$form['select_env']['#disabled'] = TRUE;
}
// Deploy button
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#value' => t('Deploy configuration'),
);
if ($environment === NULL) {
$form['buttons']['submit']['#disabled'] = TRUE;
}
}
return $form;
}
/**
* Previous form submission
* @see env_conf_form()
*/
function env_conf_form_submit($form, &$form_state) {
module_load_include('inc', 'env_conf');
// Clean flash messages queue
drupal_get_messages();
// Define configuration to deploy
$selected_environment = $form_state['values']['select_env'];
if ($selected_environment === 'default') {
$selected_environment = variable_get('environment', NULL);
}
// Deploy configuration
_deploy_environment_configuration($selected_environment);
}
<?php
/**
* @file
* Per-environment configuration module,
* helper functions.
*/
/**
* Helper function to list all available environments,
* ordered by "weight" value.
*/
function _get_available_environments() {
$available_envs = module_invoke_all('env_conf_available_env');
drupal_alter('env_conf_available_env', $available_envs);
if (uasort($available_envs, '_ordered_by_weight_value')) {
return $available_envs;
}
return FALSE;
}
/**
* Helper function to sort two environments,
* using "weight" value.
* @see _get_available_environments()
*/
function _ordered_by_weight_value($a, $b) {
if ($a['weight'] == $b['weight']) {
return 0;
}
return ($a['weight'] < $b['weight']) ? -1 : 1;
}
/**
* Helper function to list all available environments (names only),
* ordered by "weight" value.
* @see _get_available_environments()
*/
function _get_simple_available_environments() {
if ($full_list = _get_available_environments()) {
$envs = array();
foreach ($full_list as $key => $env) {
$envs[$key] = $key;
}
return $envs;
}
return FALSE;
}
/**
* Process functions to deploy an selected environment configuration
*/
function _deploy_environment_configuration($selected_environment) {
// List available environments configurations
$available_envs = _get_available_environments();
// Include file to define callback function(s)
$file = $available_envs[$selected_environment]['include_file'];
if (isset($file['type']) && isset($file['module'])) {
if (isset($file['name'])) {
module_load_include($file['type'], $file['module'], $file['name']);
}
else {
module_load_include($file['type'], $file['module']);
}
}
else {
drupal_set_message(t('Impossible to include file where functions are defined.'), 'warning');
}
// Execute each functions or alert
$errors = 0;
foreach ($available_envs[$selected_environment]['callbacks'] as $callback) {
if (function_exists($callback)) {
$callback();
drupal_set_message(t('<strong>!function</strong>() function has been processed.', array('!function' => $callback)));
}
else {
drupal_set_message(t('Call to undefined function <strong>!function</strong>().', array('!function' => $callback)), 'error');
$errors++;
}
}
// Alert if error(s) while processing configurations functions
if ($errors > 1) {
drupal_set_message(t('!nb functions of <strong>!env</strong> configuration are undefined. Your site is probably misconfigured.', array('!nb' => $errors, '!env' => $selected_environment)), 'error');
}
elseif ($errors === 1) {
drupal_set_message(t('One function of <strong>!env</strong> configuration is undefined. Your site is probably misconfigured.', array('!nb' => $errors, '!env' => $selected_environment)), 'error');
}
// Store deployed configuration
variable_set('env_conf_last_deployed_conf', $selected_environment);
}
name = Per-environment configuration
description = Allow admin to deploy specific configuration per environment, by using a Drush command or an admin page
core = 6.x
<?php
/**
* @file
* Per-environment configuration module,
* hooks implementations.
*/
/**
* Implements hook_perm().
*/
function env_conf_perm() {
return array(
'access deploy-configuration feature',
);
}
/**
* Implements hook_menu().
*/
function env_conf_menu() {
return array(
'admin/settings/environment-configuration' => array(
'title' => 'Per-environment configuration',
'description' => 'Page where you can deploy a selected environment configuration.',
'page callback' => 'drupal_get_form',
'page arguments' => array('env_conf_form'),
'access arguments' => array('access deploy-configuration feature'),
'file' => 'env_conf.forms.inc',
)
);
}
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