View source
<?php
namespace HookUpdateDeployTools;
class Modules {
public static function checkDisabled($modules = array()) {
$modules = (array) $modules;
$enabled_modules = array();
$t = get_t();
$enabled = $t('enabled');
$not_enabled = $t('disabled');
$report = array();
foreach ($modules as $module) {
if (module_exists($module)) {
$report[$module] = $enabled;
}
else {
$report[$module] = $not_enabled;
}
}
if (in_array($enabled, $report)) {
$message = 'The modules that were supposed to be disabled by this update, were not. Please investigate the problem and re-run this update. Report: !report';
$variables = array(
'!report' => $report,
);
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
else {
$message = "The requested modules are disabled. Report: !report";
$variables = array(
'!report' => $report,
);
return Message::make($message, $variables, WATCHDOG_INFO);
}
}
public static function checkEnabled($modules = array()) {
$modules = (array) $modules;
$t = get_t();
$enabled = $t('enabled');
$not_enabled = $t('not-enabled');
$report = array();
foreach ($modules as $module) {
if (!module_exists($module)) {
$report[$module] = $not_enabled;
}
else {
$report[$module] = $enabled;
}
}
if (in_array($not_enabled, $report)) {
$message = 'Some of the modules that were supposed to be enabled, are not showing as enabled. Please investigate the problem and re-run this update. Report: !report';
$variables = array(
'!report' => $report,
);
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
$module_list = implode(', ', $modules);
$message = "The requested modules were enabled successfully. Report: !report";
$variables = array(
'!report' => $report,
);
return Message::make($message, $variables, WATCHDOG_INFO);
}
public static function checkPresent($modules = array(), $check_dependencies = TRUE, $module_data = NULL, &$failed_flag = FALSE, &$modules_checked = array()) {
$modules = (array) $modules;
$report = array();
$first_run = is_null($module_data) ? TRUE : FALSE;
$module_data = empty($module_data) ? \system_rebuild_module_data() : $module_data;
foreach ($modules as $module) {
if (in_array($module, $modules_checked)) {
continue;
}
else {
$modules_checked[] = $module;
if (!isset($module_data[$module])) {
$report[$module] = t('missing');
$failed_flag = TRUE;
}
elseif ($check_dependencies === TRUE) {
if (!empty($module_data[$module]->requires) && !module_exists($module)) {
$dependencies = array_keys($module_data[$module]->requires);
$dependencies = array_diff($dependencies, $modules);
$missing_dependencies = self::checkPresent($dependencies, $check_dependencies, $module_data, $failed_flag, $modules_checked);
if (!empty($missing_dependencies)) {
$report[$module]['dependency'] = $missing_dependencies;
}
}
}
}
}
if ($first_run && $failed_flag) {
$message = 'The check for presence of modules failed. Please investigate the problem and re-run this update. Report: !report';
$variables = array(
'!report' => $report,
);
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
else {
return $report;
}
}
public static function enable($modules = array(), $enable_dependencies = TRUE) {
try {
$modules = (array) $modules;
$t = get_t();
$enabled = $t('enabled');
$failed = $t('failed');
self::checkPresent($modules, $enable_dependencies);
$report = array();
foreach ($modules as $module) {
$enable_good = module_enable(array(
$module,
), $enable_dependencies);
if ($enable_good) {
$report[$module] = $enabled;
}
else {
$report[$module] = $failed;
}
}
$variables = array(
'!report' => $report,
);
if (in_array($failed, $report)) {
$message = 'The modules to be enabled by this update, were not. Please investigate the problem and re-run this update. Report: !report';
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
$success = self::checkEnabled($modules);
return $success;
} catch (\Exception $e) {
$vars['!error'] = method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage();
if (!method_exists($e, 'logMessage')) {
$message = 'Modules::enable failed because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
throw new HudtException('Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
}
public static function disable($modules = array(), $disable_dependents = TRUE) {
try {
$modules = (array) $modules;
$disable_dependents = !empty($disable_dependents) ? TRUE : FALSE;
module_disable($modules, $disable_dependents);
$success = self::checkDisabled($modules);
drupal_flush_all_caches();
$success .= Message::make("Caches cleared and Registry Rebuilt.", array(), WATCHDOG_INFO);
return $success;
} catch (\Exception $e) {
$vars['!error'] = method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage();
if (!method_exists($e, 'logMessage')) {
$message = 'Modules::disable failed because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
throw new HudtException('Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
}
public static function uninstall($modules = array(), $uninstall_dependents = TRUE) {
try {
$modules = (array) $modules;
$uninstall_dependents = !empty($uninstall_dependents) ? TRUE : FALSE;
$t = get_t();
$uninstalled = $t('uninstalled');
$not_uninstalled = $t('not-uninstalled');
$enabled = $t('enabled');
$report = array();
foreach ($modules as $module) {
if (module_exists($module)) {
$report[$module] = $enabled;
}
}
if (empty($report)) {
drupal_uninstall_modules($modules, $uninstall_dependents);
include_once DRUPAL_ROOT . '/includes/install.inc';
$module_stati = drupal_get_installed_schema_version('', TRUE, TRUE);
foreach ($modules as $module) {
if (!isset($module_stati[$module])) {
$report[$module] = t('not found');
}
elseif ($module_stati[$module] === '-1') {
$report[$module] = $uninstalled;
}
else {
$report[$module] = $not_uninstalled;
}
}
}
$variables = array(
'!report' => $report,
);
if (in_array($enabled, $report) || in_array($not_uninstalled, $report)) {
$message = "The modules requested to uninstall were NOT uninstalled successfully. Report: !report";
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
else {
$message = "The requested modules were uninstalled successfully. Report: !report";
return Message::make($message, $variables, WATCHDOG_INFO);
}
} catch (\Exception $e) {
$vars['!error'] = method_exists($e, 'logMessage') ? $e
->logMessage() : $e
->getMessage();
if (!method_exists($e, 'logMessage')) {
$message = 'Modules::uninstall failed because: !error';
Message::make($message, $vars, WATCHDOG_ERROR);
}
throw new HudtException('Update aborted! !error', $vars, WATCHDOG_ERROR, FALSE);
}
}
public static function disableAndUninstall($modules = array(), $include_dependents = TRUE) {
$modules = (array) $modules;
$message = self::disable($modules, $include_dependents);
$message .= self::uninstall($modules, $include_dependents);
return $message;
}
}