Skip to content

Commit

Permalink
Address feedback from review of pull/445:
Browse files Browse the repository at this point in the history
- Pull temporary code into its own file includes/dependencies.inc
- Make use of bee_instant_message() where appropriate
- Add clarity to user message when a dependency of a dependency is disabled
- Fix code comment typo abled() -> enabled()
- Updated changelog

Issue #164
  • Loading branch information
elisseck committed Oct 10, 2024
1 parent 1e49a66 commit 79cf5cb
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 98 deletions.
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-09

### 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
4 changes: 2 additions & 2 deletions commands/projects.bee.inc
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ function enable_bee_callback($arguments, $options) {
foreach ($dependencies as $key => $dependency) {
if (!in_array($dependency, $unique_dependencies)) {
// Report info on dependencies to be enabled.
bee_message(bt("The '!dependency' module will also be enabled, as it is required by the '!current' module.", [
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');
Expand Down Expand Up @@ -367,7 +367,7 @@ function disable_bee_callback($arguments, $options) {
foreach ($dependencies as $key => $dependency) {
if (!in_array($dependency, $unique_dependencies)) {
// Report info on dependencies to be disabled.
bee_message(bt("The '!dependency' module will also be disabled, as it depends on the '!current' module.", [
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');
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;
}
95 changes: 0 additions & 95 deletions includes/miscellaneous.inc
Original file line number Diff line number Diff line change
Expand Up @@ -206,98 +206,3 @@ function bee_format_date($timestamp, $format = 'Y-m-d H:i:s T', $timezone = NULL
}
return $date->format($format);
}

/**
* 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_able()
* 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;
}

0 comments on commit 79cf5cb

Please sign in to comment.