function ChangeLogger::logChanges

Logs changes made by Package Manager.

Parameters

\Drupal\package_manager\Event\PostApplyEvent $event: The event being handled.

File

core/modules/package_manager/src/EventSubscriber/ChangeLogger.php, line 96

Class

ChangeLogger
Event subscriber to log changes that happen during the stage life cycle.

Namespace

Drupal\package_manager\EventSubscriber

Code

public function logChanges(PostApplyEvent $event) : void {
    $installed_at_start = $event->stage
        ->getMetadata(static::INSTALLED_PACKAGES_KEY);
    $installed_post_apply = $this->composerInspector
        ->getInstalledPackagesList($this->pathLocator
        ->getProjectRoot());
    // Compare the packages which were installed when the stage was created
    // against the package versions that were requested over all the stage's
    // require operations, and create a log entry listing all of it.
    $requested_log = [];
    $requested_packages = $event->stage
        ->getMetadata(static::REQUESTED_PACKAGES_KEY) ?? [];
    // Sort the requested packages by name, to make it easier to review a large
    // change list.
    ksort($requested_packages, SORT_NATURAL);
    foreach ($requested_packages as $name => $constraint) {
        $installed_version = $installed_at_start[$name]?->version;
        if ($installed_version === NULL) {
            // For clarity, make the "any version" constraint human-readable.
            if ($constraint === '*') {
                $constraint = $this->t('* (any version)');
            }
            $requested_log[] = $this->t('- Install @name @constraint', [
                '@name' => $name,
                '@constraint' => $constraint,
            ]);
        }
        else {
            $requested_log[] = $this->t('- Update @name from @installed_version to @constraint', [
                '@name' => $name,
                '@installed_version' => $installed_version,
                '@constraint' => $constraint,
            ]);
        }
    }
    // It's possible that $requested_log will be empty: for example, a custom
    // stage that only does removals, or some other operation, and never
    // dispatches PostRequireEvent.
    if ($requested_log) {
        $message = $this->t("Requested changes:\n@change_list", [
            '@change_list' => implode("\n", array_map('strval', $requested_log)),
        ]);
        $this->logger?->info($message);
    }
    // Create a separate log entry listing everything that actually changed.
    $applied_log = [];
    $updated_packages = $installed_post_apply->getPackagesWithDifferentVersionsIn($installed_at_start);
    // Sort the packages by name to make it easier to review large change sets.
    $updated_packages->ksort(SORT_NATURAL);
    foreach ($updated_packages as $name => $package) {
        $applied_log[] = $this->t('- Updated @name from @installed_version to @updated_version', [
            '@name' => $name,
            '@installed_version' => $installed_at_start[$name]->version,
            '@updated_version' => $package->version,
        ]);
    }
    $added_packages = $installed_post_apply->getPackagesNotIn($installed_at_start);
    $added_packages->ksort(SORT_NATURAL);
    foreach ($added_packages as $name => $package) {
        $applied_log[] = $this->t('- Installed @name @version', [
            '@name' => $name,
            '@version' => $package->version,
        ]);
    }
    $removed_packages = $installed_at_start->getPackagesNotIn($installed_post_apply);
    $removed_packages->ksort(SORT_NATURAL);
    foreach ($installed_at_start->getPackagesNotIn($installed_post_apply) as $name => $package) {
        $applied_log[] = $this->t('- Uninstalled @name', [
            '@name' => $name,
        ]);
    }
    $message = $this->t("Applied changes:\n@change_list", [
        '@change_list' => implode("\n", array_map('strval', $applied_log)),
    ]);
    $this->logger?->info($message);
}

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