Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add functions copied from Backdrop core module_enable() and #445

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ and this project follows the
which is based on the major version of Backdrop CMS with a semantic version
system for each contributed module, theme and layout.

## [Unreleased] - 2024-09-25
## [Unreleased] - 2024-10-10

### Added
- An option for the `db-import` command to allow import from newer MariaDB
Expand All @@ -20,6 +20,10 @@ database or client does not support it.
- Unhandled errors and warnings if commands run outside Backdrop root and/or
before installing Backdrop.

### Changed
- Bee will now notify the user of additional modules that will be enabled or disabled
based on module dependencies when using the enable and disable commands.

## [1.x-1.1.0] - 2024-09-07

### Added
Expand Down
1 change: 1 addition & 0 deletions bee.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
require_once __DIR__ . '/includes/input.inc';
require_once __DIR__ . '/includes/globals.inc';
require_once __DIR__ . '/includes/telemetry.inc';
require_once __DIR__ . '/includes/dependencies.inc';

// Main execution code.
bee_initialize_server();
Expand Down
53 changes: 53 additions & 0 deletions commands/projects.bee.inc
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ function enable_bee_callback($arguments, $options) {
$disabled_layouts = array();
$enable_dependencies = !isset($options['no-dependency-checking']);
$flush_caches = FALSE;
$unique_dependencies = $unique_successful_dependencies = $arguments['projects'];

foreach ($arguments['projects'] as $project) {
$already_enabled = FALSE;
Expand All @@ -222,9 +223,33 @@ function enable_bee_callback($arguments, $options) {
break;
}

if ($enable_dependencies) {
$dependencies = bee_get_required_modules($modules, [$module->name]);
// Don't report twice because we're checking projects one by one.
foreach ($dependencies as $key => $dependency) {
if (!in_array($dependency, $unique_dependencies)) {
// Report info on dependencies to be enabled.
bee_instant_message(bt("The '!dependency' module will also be enabled, as it is required by the '!current' module or one of the modules it requires.", [
'!dependency' => $modules[$dependency]->info['name'],
'!current' => $module->info['name'],
]), 'info');
$unique_dependencies[] = $dependency;
}
}
}

// Enable module.
if (module_enable(array($project), $enable_dependencies)) {
$success = TRUE;
// Report success on dependencies enabled.
foreach ($dependencies as $key => $enabled_module) {
if (!in_array($enabled_module, $unique_successful_dependencies)) {
bee_message(bt("The '!dependency' module was enabled.", [
'!dependency' => $modules[$enabled_module]->info['name'],
]), 'success');
$unique_successful_dependencies[] = $enabled_module;
}
}
}
else {
bee_message(bt("The '!name' module could not be enabled due to missing dependencies.", array(
Expand Down Expand Up @@ -310,6 +335,7 @@ function disable_bee_callback($arguments, $options) {
$disabled_layouts = array();
$disable_dependents = !isset($options['no-dependency-checking']);
$flush_caches = FALSE;
$unique_dependencies = $unique_successful_dependencies = $arguments['projects'];

foreach ($arguments['projects'] as $project) {
$already_disabled = FALSE;
Expand All @@ -335,8 +361,34 @@ function disable_bee_callback($arguments, $options) {
break;
}

if ($disable_dependents) {
$dependencies = bee_get_dependent_modules($modules, [$module->name]);
// Don't report twice because we're checking projects one by one.
foreach ($dependencies as $key => $dependency) {
if (!in_array($dependency, $unique_dependencies)) {
// Report info on dependencies to be disabled.
bee_instant_message(bt("The '!dependency' module will also be disabled, as it depends on the '!current' module or one of the modules it requires.", [
'!dependency' => $modules[$dependency]->info['name'],
'!current' => $module->info['name'],
]), 'info');
$unique_dependencies[] = $dependency;
}
}
}

// Disable module.
module_disable(array($project), $disable_dependents);

// Report success on dependencies disabled.
$dependencies = array_unique($dependencies);
foreach ($dependencies as $disabled_module) {
if (!in_array($disabled_module, $unique_successful_dependencies)) {
bee_message(bt("The '!dependency' module was disabled.", [
'!dependency' => $modules[$disabled_module]->info['name'],
]), 'success');
$unique_successful_dependencies[] = $disabled_module;
}
}
$success = TRUE;

break;
Expand All @@ -353,6 +405,7 @@ function disable_bee_callback($arguments, $options) {

// Disable theme.
theme_disable(array($project));

$success = TRUE;

break;
Expand Down
100 changes: 100 additions & 0 deletions includes/dependencies.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php
/**
* @file
* Dependencies on Backdrop Core. These will be removed when Backdrop Core
* moves these features into hooks available for the contrib space.
*/

/**
* Builds the full list of dependent modules based on input.
* This function is copied from Backdrop Core module_disable()
* until its functionality can be broken out for contrib access.
*
* @param array $module_data
* A list of all installed modules.
* @param array $module_list
* A list of modules for which to check dependencies.
*
* @return array $module_list
* A list of modules and their dependent modules.
*/
function bee_get_dependent_modules(array $module_data, array $module_list) {
// Create an associative array with weights as values.
$module_list = array_flip(array_values($module_list));

$profile = backdrop_get_profile();
// The array is iterated over manually (instead of using a foreach) because
// modules may be added to the list within the loop and we need to process
// them.
while ($module = key($module_list)) {
next($module_list);
if (!isset($module_data[$module]) || !$module_data[$module]->status) {
// This module doesn't exist or is already disabled, skip it.
unset($module_list[$module]);
continue;
}
$module_list[$module] = $module_data[$module]->sort;

// Add dependent modules to the list, with a placeholder weight.
// The new modules will be processed as the while loop continues.
foreach ($module_data[$module]->required_by as $dependent => $dependent_data) {
if (!isset($module_list[$dependent]) && $dependent != $profile) {
$module_list[$dependent] = 0;
}
}
}
// Sort the module list by pre-calculated weights.
asort($module_list);
$module_list = array_keys($module_list);

return $module_list;
}

/**
* Builds the full list of module requirements based on input.
* This function is copied from Backdrop Core module_enable()
* until its functionality can be broken out for contrib access.
*
* @param array $module_data
* A list of all installed modules.
* @param array $module_list
* A list of modules for which to check requirements.
*
* @return array $module_list
* A list of modules and their required modules.
*/
function bee_get_required_modules(array $module_data, array $module_list) {
// Create an associative array with weights as values.
$module_list = array_flip(array_values($module_list));

// The array is iterated over manually (instead of using a foreach) because
// modules may be added to the list within the loop and we need to process
// them.
while ($module = key($module_list)) {
next($module_list);
if (!isset($module_data[$module])) {
// This module is not found in the filesystem, abort.
return FALSE;
}
if ($module_data[$module]->status) {
// Skip already enabled modules.
unset($module_list[$module]);
continue;
}
$module_list[$module] = $module_data[$module]->sort;

// Add dependencies to the list, with a placeholder weight.
// The new modules will be processed as the while loop continues.
foreach (array_keys($module_data[$module]->requires) as $dependency) {
if (!isset($module_list[$dependency])) {
$module_list[$dependency] = 0;
}
}
}

// Sort the module list by pre-calculated weights.
arsort($module_list);
$module_list = array_keys($module_list);

return $module_list;
}
Loading