function ComposerPatchesValidator::validate

Validates the status of the patcher plugin.

Parameters

\Drupal\package_manager\Event\SandboxValidationEvent $event: The event object.

File

core/modules/package_manager/src/Validator/ComposerPatchesValidator.php, line 63

Class

ComposerPatchesValidator
Validates the configuration of the cweagans/composer-patches plugin.

Namespace

Drupal\package_manager\Validator

Code

public function validate(SandboxValidationEvent $event) : void {
  $messages = [];
  [
    $plugin_installed_in_active,
    $is_active_root_requirement,
    $active_configuration_ok,
  ] = $this->computePatcherStatus($this->pathLocator
    ->getProjectRoot());
  if ($event instanceof PreApplyEvent) {
    [
      $plugin_installed_in_stage,
      $is_stage_root_requirement,
      $stage_configuration_ok,
    ] = $this->computePatcherStatus($event->sandboxManager
      ->getSandboxDirectory());
    $has_staged_update = TRUE;
  }
  else {
    // No staged update exists.
    $has_staged_update = FALSE;
  }
  // If there's a staged update and the patcher has been installed or removed
  // in the stage directory, that's a problem.
  if ($has_staged_update && $plugin_installed_in_active !== $plugin_installed_in_stage) {
    if ($plugin_installed_in_stage) {
      $message = $this->t('It cannot be installed by Package Manager.');
    }
    else {
      $message = $this->t('It cannot be removed by Package Manager.');
    }
    $messages[] = $this->createErrorMessage($message, 'package-manager-faq-composer-patches-installed-or-removed');
  }
  // If the patcher is not listed in the runtime or dev dependencies, that's
  // an error as well.
  if ($plugin_installed_in_active && !$is_active_root_requirement || $has_staged_update && $plugin_installed_in_stage && !$is_stage_root_requirement) {
    $messages[] = $this->createErrorMessage($this->t('It must be a root dependency.'), 'package-manager-faq-composer-patches-not-a-root-dependency');
  }
  // If the plugin is misconfigured in either the active or stage directories,
  // flag an error.
  if ($plugin_installed_in_active && !$active_configuration_ok || $has_staged_update && $plugin_installed_in_stage && !$stage_configuration_ok) {
    $messages[] = $this->t('The <code>composer-exit-on-patch-failure</code> key is not set to <code>true</code> in the <code>extra</code> section of <code>composer.json</code>.');
  }
  if ($messages) {
    $summary = $this->t("Problems detected related to the Composer plugin <code>@plugin</code>.", [
      '@plugin' => static::PLUGIN_NAME,
    ]);
    $event->addError($messages, $summary);
  }
}

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