function SandboxDatabaseUpdatesValidator::getUpdateFunctions

Returns a list of all update functions for a module.

This method only exists because the API in core that scans for available updates can only examine the active (running) code base, but we need to be able to scan the staged code base as well to compare it against the active one.

Parameters

string $root_dir: The root directory of the Drupal code base.

\Drupal\Core\Extension\Extension $extension: The module to check.

Return value

string[] The names of the update functions in the module's .install and .post_update.php files.

1 call to SandboxDatabaseUpdatesValidator::getUpdateFunctions()
SandboxDatabaseUpdatesValidator::hasStagedUpdates in core/modules/package_manager/src/Validator/SandboxDatabaseUpdatesValidator.php
Determines if a staged extension has changed update functions.

File

core/modules/package_manager/src/Validator/SandboxDatabaseUpdatesValidator.php, line 108

Class

SandboxDatabaseUpdatesValidator
Flags a warning if there are database updates in a staged update.

Namespace

Drupal\package_manager\Validator

Code

private function getUpdateFunctions(string $root_dir, Extension $extension) : array {
    $name = $extension->getName();
    $path = implode(DIRECTORY_SEPARATOR, [
        $root_dir,
        $extension->getPath(),
        $name,
    ]);
    $function_names = [];
    $patterns = [
        '.install' => '/^' . $name . '_update_[0-9]+$/i',
        '.post_update.php' => '/^' . $name . '_post_update_.+$/i',
    ];
    foreach ($patterns as $suffix => $pattern) {
        $file = $path . $suffix;
        if (!file_exists($file)) {
            continue;
        }
        // Parse the file and scan for named functions which match the pattern.
        $code = file_get_contents($file);
        $tokens = token_get_all($code);
        for ($i = 0; $i < count($tokens); $i++) {
            $chunk = array_slice($tokens, $i, 3);
            if ($this->tokensMatchFunctionNamePattern($chunk, $pattern)) {
                $function_names[] = $chunk[2][1];
            }
        }
    }
    return $function_names;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.