Skip to content

Commit

Permalink
Fix Database service
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrii Kondratenko committed May 20, 2024
1 parent 71c5cb5 commit 514a1e1
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 28 deletions.
2 changes: 1 addition & 1 deletion monitoring_tool_client.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ services:

monitoring_tool_client.database:
class: Drupal\monitoring_tool_client\Service\DatabaseService
arguments: ['@config.factory']
arguments: ['@config.factory', '@module_handler', '@theme_handler', '@string_translation']
106 changes: 79 additions & 27 deletions src/Service/DatabaseService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
namespace Drupal\monitoring_tool_client\Service;

use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\Core\Utility\ProjectInfo;

/**
* Class DatabaseService.
*
* The DatabaseService class.
*/
class DatabaseService implements DatabaseServiceInterface {

use StringTranslationTrait;

/**
* Configuration manager.
*
Expand All @@ -19,13 +24,46 @@ class DatabaseService implements DatabaseServiceInterface {
protected $configFactory;

/**
* CollectModulesService constructor.
* Update manager.
*
* @var \Drupal\update\UpdateManagerInterface
*/
protected $updateManager;

/**
* Module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;

/**
* Theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;

/**
* DatabaseService constructor.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Configuration manager.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* Module handler.
* @param \Drupal\Core\Extension\ThemeHandlerInterface $theme_handler
* Theme handler.
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation
* Translation service.
*/
public function __construct(ConfigFactoryInterface $config_factory) {
public function __construct(ConfigFactoryInterface $config_factory, ModuleHandlerInterface $module_handler, ThemeHandlerInterface $theme_handler, TranslationInterface $translation) {
$this->configFactory = $config_factory;
$this->moduleHandler = $module_handler;
$this->themeHandler = $theme_handler;
$this->stringTranslation = $translation;
if (\Drupal::moduleHandler()->moduleExists('update')) {
$this->updateManager = \Drupal::service('update.manager');
}
}

/**
Expand All @@ -37,37 +75,51 @@ public function getUpdates() {
$skip = $settings->get('skip_drupal_database_update');

if (!$skip) {
$updates = $this->getUpdateList();
$projects = $this->getProjects();

foreach ($projects as $project_name => $project_info) {
// Check if the project has pending updates.
if ($this->hasPendingUpdate($project_info)) {
$updates[$project_name] = $project_info;
}
}
}

return $updates;
}

/**
* Retrieves the list of pending database updates.
* Get a list of installed projects (modules and themes).
*
* @return array
* An array of installed projects.
*/
private function getUpdateList() {
$update_list = [];
// This assumes you have the update manager service, you may need to adjust
// this based on your actual service configuration.
if (\Drupal::moduleHandler()->moduleExists('update')) {
$update_manager = \Drupal::service('update.manager');
$pending_updates = $update_manager->getPendingUpdateInformation();

foreach ($pending_updates as $module => $updates) {
foreach ($updates as $update) {
$update_list[] = [
'module' => $module,
'description' => $update['description'],
'type' => $update['type'],
'status' => $update['status'],
];
}
}
protected function getProjects() {
$projects = [];
$module_data = $this->moduleHandler->getModuleList();
$theme_data = $this->themeHandler->listInfo();
$project_info = new ProjectInfo();
$project_info->processInfoList($projects, $module_data, 'module', TRUE);
$project_info->processInfoList($projects, $theme_data, 'theme', TRUE);
return $projects;
}

return $update_list;
/**
* Check if a project has pending updates.
*
* @param array $project_info
* Information about the project.
*
* @return bool
* TRUE if the project has pending updates, otherwise FALSE.
*/
protected function hasPendingUpdate(array $project_info) {
// Compare installed version with available version.
if (isset($project_info['status']) && $project_info['status'] === 'enabled' && isset($project_info['version'])) {
$installed_version = $project_info['version'];
$available_version = $this->updateManager->getExtensionVersion($project_info['name']);
return version_compare($installed_version, $available_version, '<');
}
return [];
return FALSE;
}

}

0 comments on commit 514a1e1

Please sign in to comment.