View source
<?php
if (module_exists('context')) {
include_once 'plugins/environment.context.inc';
}
if (module_exists('token')) {
include_once 'plugins/environment.token.inc';
}
function environment_init() {
if (module_exists('context') && function_exists('context_get_plugin') && ($plugin = context_get_plugin('condition', 'environment'))) {
$plugin
->execute((array) variable_get('environment', array()));
}
}
function environment_menu() {
$items = array();
$items['admin/settings/environment'] = array(
'title' => 'Environment',
'description' => 'Settings for Environment.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'environment_admin_settings',
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'environment.admin.inc',
);
$items['admin/settings/environment/switch/%environment'] = array(
'title' => 'Environment Switch',
'description' => 'Switch the current environment.',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'environment_switch_confirm',
4,
),
'access arguments' => array(
'administer site configuration',
),
'file' => 'environment.admin.inc',
);
return $items;
}
function environment_environment() {
return array(
'production' => array(
'label' => t('Production'),
'allowed' => array(
'default' => TRUE,
),
),
'development' => array(
'label' => t('Development'),
'allowed' => array(
'default' => FALSE,
),
),
);
}
function environment_environment_workflow() {
return array(
'default' => array(
'label' => t('Default'),
),
);
}
function environment_allowed($name, $category = 'other', $default = FALSE, $workflow = 'default') {
$env = environment_current($workflow, NULL, TRUE);
if (!empty($env)) {
if (!empty($env[$category])) {
if (isset($env[$category][$name])) {
$result = $env[$category][$name];
}
elseif (isset($env[$category]['default'])) {
$result = $env[$category]['default'];
}
}
if (!isset($result) && isset($env['default'])) {
$result = $env['default'];
}
}
if (!isset($result)) {
$result = $default;
}
return $result;
}
function environment_switch($target_env, $force = FALSE) {
$result = FALSE;
$messages = array();
$target_state = environment_load($target_env);
$workflow = $target_state['workflow'];
$current_env = environment_current($workflow);
$override = variable_get('environment_override', '');
if ($current_env == $target_env) {
drupal_set_message(t("The current environment is already set to '!environment'.", array(
'!environment' => $target_env,
)), 'error');
if (function_exists('drush_print')) {
drush_print("To force the environment switch to run anyway, use the '--force' flag.");
}
}
if (!$force && !empty($override)) {
drupal_set_message(t("The current environment is overriden with '!override'.", array(
'!override' => $override,
)), 'error');
if (function_exists('drush_print')) {
drush_print("To force the environment switch to run anyway, use the '--force' flag.");
}
}
elseif ($current_env != $target_env || $force) {
if (empty($target_state)) {
drupal_set_message(t('Environment !environment does not exist.', array(
'!environment' => $target_env,
)), 'warning');
}
else {
environment_set($target_env);
module_invoke_all('environment_switch', $target_env, $current_env, $workflow);
drupal_flush_all_caches();
drupal_set_message('Cleared cache.');
$result = TRUE;
}
}
return $result;
}
function environment_current($workflow = 'default', $default = NULL, $load = FALSE) {
$current = variable_get('environment', array());
if (!is_array($current)) {
$current = array(
'default' => $current,
);
}
if (is_null($workflow)) {
$current = empty($current) ? $default : $current;
return $load ? environment_load($current) : $current;
}
elseif (isset($current[$workflow])) {
$current[$workflow] = empty($current[$workflow]) ? $default : $current[$workflow];
return $load ? environment_load($current[$workflow]) : $current[$workflow];
}
return $default;
}
function environment_set($new_env) {
$environment = variable_get('environment', array());
$new_state = environment_load($new_env);
if (!is_array($environment)) {
$environment = array(
'default' => $environment,
);
}
$environment[$new_state['workflow']] = $new_state['name'];
variable_set('environment', $environment);
}
function environment_load($env = NULL, $reset = FALSE) {
static $environments;
if (!isset($environments) || $reset) {
$environments = module_invoke_all('environment');
$environments = array_merge($environments, variable_get('environment_definitions', array()));
foreach ($environments as $name => $environment) {
$environments[$name]['name'] = $name;
if (!isset($environments[$name]['workflow'])) {
$environments[$name]['workflow'] = 'default';
}
}
drupal_alter('environment', $environments);
}
if (empty($env)) {
return $environments;
}
elseif (is_array($env)) {
return array_intersect_key($environments, array_flip($env));
}
else {
return isset($environments[$env]) ? $environments[$env] : FALSE;
}
}
function environment_workflow_load($name = NULL, $reset = FALSE) {
static $workflows;
if (empty($workflows) || $reset) {
$workflows = module_invoke_all('environment_workflow');
drupal_alter('environment_workflow', $workflows);
}
return isset($name) ? $workflows[$name] : $workflows;
}
function _environment_state_options($workflow = 'default', $prefix = '', $reset = FALSE) {
static $options;
if (empty($options) || $reset) {
$environments = environment_load();
foreach ($environments as $name => $state) {
$options[$state['workflow']][$name] = $prefix . $state['label'];
}
}
return empty($options[$workflow]) ? array() : $options[$workflow];
}
function _environment_state_options_all($reset = FALSE) {
$options = array();
$workflows = environment_workflow_load();
foreach ($workflows as $name => $workflow) {
$options[$name] = $workflow['label'];
$w_opts = _environment_state_options($name, TRUE, '-- ', $reset);
$options = array_merge($options, $w_opts);
}
return $options;
}