Newer
Older

Adam G-H
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?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',
];
}
}