diff --git a/monitoring_tool_client.services.yml b/monitoring_tool_client.services.yml index ee16890..67d74c2 100644 --- a/monitoring_tool_client.services.yml +++ b/monitoring_tool_client.services.yml @@ -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'] diff --git a/src/Service/DatabaseService.php b/src/Service/DatabaseService.php index a0f630f..6a91187 100644 --- a/src/Service/DatabaseService.php +++ b/src/Service/DatabaseService.php @@ -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. * @@ -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'); + } } /** @@ -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; } - }