You are here

public function UpdateHookRegistry::getAvailableUpdates in Drupal 9

Same name and namespace in other branches
  1. 10 core/lib/Drupal/Core/Update/UpdateHookRegistry.php \Drupal\Core\Update\UpdateHookRegistry::getAvailableUpdates()

Returns an array of available schema versions for a module.

Parameters

string $module: A module name.

Return value

int[] An array of available updates sorted by version. Empty array returned if no updates available.

File

core/lib/Drupal/Core/Update/UpdateHookRegistry.php, line 75

Class

UpdateHookRegistry
Provides module updates versions handling.

Namespace

Drupal\Core\Update

Code

public function getAvailableUpdates(string $module) : array {
  if (!isset($this->allAvailableSchemaVersions[$module])) {
    $this->allAvailableSchemaVersions[$module] = [];
    foreach ($this->enabledModules as $enabled_module) {
      $this->allAvailableSchemaVersions[$enabled_module] = [];
    }

    // Prepare regular expression to match all possible defined
    // hook_update_N().
    $regexp = '/^(?<module>.+)_update_(?<version>\\d+)$/';
    $functions = get_defined_functions();

    // Narrow this down to functions ending with an integer, since all
    // hook_update_N() functions end this way, and there are other
    // possible functions which match '_update_'. We use preg_grep() here
    // since looping through all PHP functions can take significant page
    // execution time and this function is called on every administrative page
    // via system_requirements().
    foreach (preg_grep('/_\\d+$/', $functions['user']) as $function) {

      // If this function is a module update function, add it to the list of
      // module updates.
      if (preg_match($regexp, $function, $matches)) {
        $this->allAvailableSchemaVersions[$matches['module']][] = (int) $matches['version'];
      }
    }

    // Ensure that updates are applied in numerical order.
    array_walk($this->allAvailableSchemaVersions, static function (&$module_updates) {
      sort($module_updates, SORT_NUMERIC);
    });
  }
  return $this->allAvailableSchemaVersions[$module];
}