Skip to content
Snippets Groups Projects
InstalledVersionValidator.php 1.74 KiB
Newer Older
<?php

namespace Drupal\automatic_updates\Validator;

use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\ProjectInfo;
use Drupal\automatic_updates\Updater;
use Drupal\Core\Extension\ExtensionVersion;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\package_manager\Event\PreCreateEvent;
use Drupal\package_manager\Event\PreOperationStageEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

/**
 * Validates that the site can update from the installed version of Drupal.
 */
class InstalledVersionValidator implements EventSubscriberInterface {

  use StringTranslationTrait;

  /**
   * Checks that the site can update from the installed version of Drupal.
   *
   * @param \Drupal\package_manager\Event\PreOperationStageEvent $event
   *   The event object.
   */
  public function checkInstalledVersion(PreOperationStageEvent $event): void {
    // This check only works with Automatic Updates.
    if (!$event->getStage() instanceof Updater) {
      return;
    }

    $installed_version = (new ProjectInfo('drupal'))->getInstalledVersion();
    $extra = ExtensionVersion::createFromVersionString($installed_version)
      ->getVersionExtra();

    if ($extra === 'dev') {
      $message = $this->t('Drupal cannot be automatically updated from the installed version, @installed_version, because automatic updates from a dev version to any other version are not supported.', [
        '@installed_version' => $installed_version,
      ]);
      $event->addError([$message]);
    }
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    return [
      ReadinessCheckEvent::class => 'checkInstalledVersion',
      PreCreateEvent::class => 'checkInstalledVersion',
    ];
  }

}