You are here

class UpdateVersionValidator in Automatic Updates 8.2

Validates that core updates are within a supported version range.

Hierarchy

Expanded class hierarchy of UpdateVersionValidator

1 string reference to 'UpdateVersionValidator'
automatic_updates.services.yml in ./automatic_updates.services.yml
automatic_updates.services.yml
1 service uses UpdateVersionValidator
automatic_updates.update_version_validator in ./automatic_updates.services.yml
Drupal\automatic_updates\Validator\UpdateVersionValidator

File

src/Validator/UpdateVersionValidator.php, line 17

Namespace

Drupal\automatic_updates\Validator
View source
class UpdateVersionValidator implements EventSubscriberInterface {
  use StringTranslationTrait;

  /**
   * Constructs a UpdateVersionValidation object.
   *
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   The translation service.
   */
  public function __construct(TranslationInterface $translation) {
    $this
      ->setStringTranslation($translation);
  }

  /**
   * Returns the running core version, according to the Update module.
   *
   * @return string
   *   The running core version as known to the Update module.
   */
  protected function getCoreVersion() : string {

    // We need to call these functions separately, because
    // update_get_available() will include the file that contains
    // update_calculate_project_data().
    $available_updates = update_get_available();
    $available_updates = update_calculate_project_data($available_updates);
    return $available_updates['drupal']['existing_version'];
  }

  /**
   * Validates that core is not being updated to another minor or major version.
   *
   * @param \Drupal\automatic_updates\Event\PreStartEvent|\Drupal\automatic_updates\Event\ReadinessCheckEvent $event
   *   The event object.
   */
  public function checkUpdateVersion(UpdateEvent $event) : void {
    $from_version = ExtensionVersion::createFromVersionString($this
      ->getCoreVersion());
    $core_package_names = $event
      ->getActiveComposer()
      ->getCorePackageNames();

    // All the core packages will be updated to the same version, so it doesn't
    // matter which specific package we're looking at.
    $core_package_name = reset($core_package_names);
    $to_version = ExtensionVersion::createFromVersionString($event
      ->getPackageVersions()[$core_package_name]);
    if ($from_version
      ->getMajorVersion() !== $to_version
      ->getMajorVersion()) {
      $error = ValidationResult::createError([
        $this
          ->t('Updating from one major version to another is not supported.'),
      ]);
      $event
        ->addValidationResult($error);
    }
    elseif ($from_version
      ->getMinorVersion() !== $to_version
      ->getMinorVersion()) {
      $error = ValidationResult::createError([
        $this
          ->t('Updating from one minor version to another is not supported.'),
      ]);
      $event
        ->addValidationResult($error);
    }
  }

  /**
   * Validates readiness check event.
   *
   * @param \Drupal\automatic_updates\Event\ReadinessCheckEvent $event
   *   The readiness check event object.
   */
  public function checkReadinessUpdateVersion(ReadinessCheckEvent $event) : void {

    // During readiness checks, we might not know the desired package versions,
    // which means there's nothing to validate.
    if ($event
      ->getPackageVersions()) {
      $this
        ->checkUpdateVersion($event);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      AutomaticUpdatesEvents::PRE_START => 'checkUpdateVersion',
      AutomaticUpdatesEvents::READINESS_CHECK => 'checkReadinessUpdateVersion',
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UpdateVersionValidator::checkReadinessUpdateVersion public function Validates readiness check event.
UpdateVersionValidator::checkUpdateVersion public function Validates that core is not being updated to another minor or major version.
UpdateVersionValidator::getCoreVersion protected function Returns the running core version, according to the Update module.
UpdateVersionValidator::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
UpdateVersionValidator::__construct public function Constructs a UpdateVersionValidation object.