Skip to content
Snippets Groups Projects
Unverified Commit c8871e5d authored by Ted Bowman's avatar Ted Bowman
Browse files

use event classes instead of event names

parent 55cb5675
No related branches found
No related tags found
No related merge requests found
Showing
with 77 additions and 122 deletions
<?php
namespace Drupal\automatic_updates;
/**
* Defines events for the automatic_updates module.
*
* These events allow listeners to validate updates at various points in the
* update process. Listeners to these events should add validation results via
* \Drupal\automatic_updates\Event\UpdateEvent::addValidationResult() if
* necessary. Only error level validation results will stop an update from
* continuing.
*
* @see \Drupal\automatic_updates\Event\UpdateEvent
* @see \Drupal\automatic_updates\Validation\ValidationResult
*/
final class AutomaticUpdatesEvents {
/**
* Name of the event fired when checking if the site could perform an update.
*
* An update is not actually being started when this event is being fired. It
* should be used to notify site admins if the site is in a state which will
* not allow automatic updates to succeed.
*
* This event should only be dispatched from ReadinessValidationManager to
* allow caching of the results.
*
* @Event
*
* @see \Drupal\automatic_updates\Validation\ReadinessValidationManager
*
* @var string
*/
const READINESS_CHECK = 'automatic_updates.readiness_check';
/**
* Name of the event fired when an automatic update is starting.
*
* This event is fired before any files are staged. Validation results added
* by subscribers are not cached.
*
* @Event
*
* @var string
*/
const PRE_START = 'automatic_updates.pre_start';
/**
* Name of the event fired when an automatic update is about to be committed.
*
* Validation results added by subscribers are not cached.
*
* @Event
*
* @var string
*/
const PRE_COMMIT = 'automatic_updates.pre_commit';
/**
* Name of the event fired when a staged update has been committed.
*
* @Event
*
* @var string
*/
const POST_COMMIT = 'automatic_updates.post_commit';
}
......@@ -2,7 +2,6 @@
namespace Drupal\automatic_updates\Event;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Core\StreamWrapper\LocalStream;
use Drupal\Core\StreamWrapper\StreamWrapperManagerInterface;
......@@ -158,8 +157,8 @@ class ExcludedPathsSubscriber implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::PRE_START => 'preStart',
AutomaticUpdatesEvents::PRE_COMMIT => 'preCommit',
PreStartEvent::class => 'preStart',
PreCommitEvent::class => 'preCommit',
];
}
......
<?php
namespace Drupal\automatic_updates\Event;
/**
* Event fired when a staged update has been committed.
*/
class PostCommitEvent extends UpdateEvent {
}
......@@ -6,6 +6,8 @@ use Drupal\package_manager\ComposerUtility;
/**
* Event fired before staged changes are copied into the active site.
*
* Validation results added by subscribers are not cached.
*/
class PreCommitEvent extends UpdateEvent {
......
......@@ -6,6 +6,9 @@ use Drupal\package_manager\ComposerUtility;
/**
* Event fired before an update begins.
*
* This event is fired before any files are staged. Validation results added
* by subscribers are not cached.
*/
class PreStartEvent extends UpdateEvent {
......
......@@ -6,6 +6,15 @@ use Drupal\package_manager\ComposerUtility;
/**
* Event fired when checking if the site could perform an update.
*
* An update is not actually being started when this event is being fired. It
* should be used to notify site admins if the site is in a state which will
* not allow automatic updates to succeed.
*
* This event should only be dispatched from ReadinessValidationManager to
* allow caching of the results.
*
* @see \Drupal\automatic_updates\Validation\ReadinessValidationManager
*/
class ReadinessCheckEvent extends UpdateEvent {
......
......@@ -9,11 +9,12 @@ use Drupal\package_manager\ComposerUtility;
/**
* Event fired when a site is updating.
*
* Subscribers to this event should call ::addValidationResult().
*
* @see \Drupal\automatic_updates\AutomaticUpdatesEvents
* These events allow listeners to validate updates at various points in the
* update process. Listeners to these events should add validation results via
* ::addValidationResult() if necessary. Only error level validation results
* will stop an update from continuing.
*/
class UpdateEvent extends Event {
abstract class UpdateEvent extends Event {
/**
* The validation results.
......
......@@ -2,7 +2,6 @@
namespace Drupal\automatic_updates\Event;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\update\UpdateManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
......@@ -41,7 +40,7 @@ class UpdateRefreshSubscriber implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::POST_COMMIT => ['clearData', 1000],
PostCommitEvent::class => ['clearData', 1000],
];
}
......
......@@ -2,6 +2,7 @@
namespace Drupal\automatic_updates;
use Drupal\automatic_updates\Event\PostCommitEvent;
use Drupal\automatic_updates\Event\PreCommitEvent;
use Drupal\automatic_updates\Event\PreStartEvent;
use Drupal\automatic_updates\Event\UpdateEvent;
......@@ -117,7 +118,7 @@ class Updater {
}
$stage_key = $this->createActiveStage($packages);
/** @var \Drupal\automatic_updates\Event\PreStartEvent $event */
$event = $this->dispatchUpdateEvent(new PreStartEvent($composer, $packages), AutomaticUpdatesEvents::PRE_START);
$event = $this->dispatchUpdateEvent(new PreStartEvent($composer, $packages));
$this->stage->create($this->getExclusions($event));
return $stage_key;
}
......@@ -157,9 +158,9 @@ class Updater {
$stage_composer = ComposerUtility::createForDirectory($stage_dir);
/** @var \Drupal\automatic_updates\Event\PreCommitEvent $event */
$event = $this->dispatchUpdateEvent(new PreCommitEvent($active_composer, $stage_composer), AutomaticUpdatesEvents::PRE_COMMIT);
$event = $this->dispatchUpdateEvent(new PreCommitEvent($active_composer, $stage_composer));
$this->stage->apply($this->getExclusions($event));
$this->dispatchUpdateEvent(new UpdateEvent($active_composer), AutomaticUpdatesEvents::POST_COMMIT);
$this->dispatchUpdateEvent(new PostCommitEvent($active_composer));
}
/**
......@@ -201,8 +202,6 @@ class Updater {
*
* @param \Drupal\automatic_updates\Event\UpdateEvent $event
* The update event.
* @param string $event_name
* The name of the event to dispatch.
*
* @return \Drupal\automatic_updates\Event\UpdateEvent
* The event object.
......@@ -210,8 +209,8 @@ class Updater {
* @throws \Drupal\automatic_updates\Exception\UpdateException
* If any of the event subscribers adds a validation error.
*/
public function dispatchUpdateEvent(UpdateEvent $event, string $event_name): UpdateEvent {
$this->eventDispatcher->dispatch($event, $event_name);
public function dispatchUpdateEvent(UpdateEvent $event): UpdateEvent {
$this->eventDispatcher->dispatch($event);
if ($checker_results = $event->getResults(SystemManager::REQUIREMENT_ERROR)) {
throw new UpdateException($checker_results,
"Unable to complete the update because of errors.");
......
......@@ -2,7 +2,6 @@
namespace Drupal\automatic_updates\Validation;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\UpdateRecommender;
use Drupal\Component\Datetime\TimeInterface;
......@@ -93,13 +92,13 @@ class ReadinessValidationManager {
$package_versions = [];
}
$event = new ReadinessCheckEvent($composer, $package_versions);
$this->eventDispatcher->dispatch($event, AutomaticUpdatesEvents::READINESS_CHECK);
$this->eventDispatcher->dispatch($event);
$results = $event->getResults();
$this->keyValueExpirable->setWithExpire(
'readiness_validation_last_run',
[
'results' => $results,
'listeners' => $this->getListenersAsString(AutomaticUpdatesEvents::READINESS_CHECK),
'listeners' => $this->getListenersAsString(ReadinessCheckEvent::class),
],
$this->resultsTimeToLive * 60 * 60
);
......@@ -180,7 +179,7 @@ class ReadinessValidationManager {
$last_run = $this->keyValueExpirable->get('readiness_validation_last_run');
// If the listeners have not changed return the results.
if ($last_run && $last_run['listeners'] === $this->getListenersAsString(AutomaticUpdatesEvents::READINESS_CHECK)) {
if ($last_run && $last_run['listeners'] === $this->getListenersAsString(ReadinessCheckEvent::class)) {
return $last_run['results'];
}
return NULL;
......
......@@ -2,7 +2,7 @@
namespace Drupal\automatic_updates\Validator;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\Core\Extension\ExtensionVersion;
......@@ -91,7 +91,7 @@ class ComposerExecutableValidator implements EventSubscriberInterface, ProcessOu
*/
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::READINESS_CHECK => 'checkForComposerExecutable',
ReadinessCheckEvent::class => 'checkForComposerExecutable',
];
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\automatic_updates\Validator;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\Core\StringTranslation\StringTranslationTrait;
......@@ -42,7 +41,7 @@ class CoreComposerValidator implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::READINESS_CHECK => ['checkCoreRequirements', 1000],
ReadinessCheckEvent::class => ['checkCoreRequirements', 1000],
];
}
......
......@@ -2,7 +2,7 @@
namespace Drupal\automatic_updates\Validator;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\Component\FileSystem\FileSystem;
......@@ -165,7 +165,7 @@ class DiskSpaceValidator implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::READINESS_CHECK => 'checkDiskSpace',
ReadinessCheckEvent::class => 'checkDiskSpace',
];
}
......
......@@ -2,7 +2,8 @@
namespace Drupal\automatic_updates\Validator;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\PreStartEvent;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\Core\StringTranslation\StringTranslationTrait;
......@@ -76,8 +77,8 @@ class PendingUpdatesValidator implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::PRE_START => 'checkPendingUpdates',
AutomaticUpdatesEvents::READINESS_CHECK => 'checkPendingUpdates',
PreStartEvent::class => 'checkPendingUpdates',
ReadinessCheckEvent::class => 'checkPendingUpdates',
];
}
......
......@@ -2,7 +2,6 @@
namespace Drupal\automatic_updates\Validator;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\PreCommitEvent;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\Core\StringTranslation\StringTranslationTrait;
......@@ -124,7 +123,7 @@ final class StagedProjectsValidator implements EventSubscriberInterface {
* {@inheritdoc}
*/
public static function getSubscribedEvents() {
$events[AutomaticUpdatesEvents::PRE_COMMIT][] = ['validateStagedProjects'];
$events[PreCommitEvent::class][] = ['validateStagedProjects'];
return $events;
}
......
......@@ -3,7 +3,7 @@
namespace Drupal\automatic_updates\Validator;
use Composer\Semver\Semver;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\PreStartEvent;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\automatic_updates\Validation\ValidationResult;
......@@ -101,8 +101,8 @@ class UpdateVersionValidator implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::PRE_START => 'checkUpdateVersion',
AutomaticUpdatesEvents::READINESS_CHECK => 'checkReadinessUpdateVersion',
PreStartEvent::class => 'checkUpdateVersion',
ReadinessCheckEvent::class => 'checkReadinessUpdateVersion',
];
}
......
......@@ -2,7 +2,8 @@
namespace Drupal\automatic_updates\Validator;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\PreStartEvent;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\Core\StringTranslation\StringTranslationTrait;
......@@ -83,8 +84,8 @@ class WritableFileSystemValidator implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
return [
AutomaticUpdatesEvents::READINESS_CHECK => 'checkPermissions',
AutomaticUpdatesEvents::PRE_START => 'checkPermissions',
ReadinessCheckEvent::class => 'checkPermissions',
PreStartEvent::class => 'checkPermissions',
];
}
......
......@@ -2,7 +2,9 @@
namespace Drupal\automatic_updates_test\ReadinessChecker;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\PreCommitEvent;
use Drupal\automatic_updates\Event\PreStartEvent;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates\Event\UpdateEvent;
use Drupal\Core\State\StateInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
......@@ -45,9 +47,9 @@ class TestChecker1 implements EventSubscriberInterface {
* stored results.
* @param string $event_name
* (optional )The event name. Defaults to
* AutomaticUpdatesEvents::READINESS_CHECK.
* ReadinessCheckEvent::class.
*/
public static function setTestResult($checker_results, string $event_name = AutomaticUpdatesEvents::READINESS_CHECK): void {
public static function setTestResult($checker_results, string $event_name = ReadinessCheckEvent::class): void {
$key = static::STATE_KEY . ".$event_name";
if (isset($checker_results)) {
......@@ -83,7 +85,7 @@ class TestChecker1 implements EventSubscriberInterface {
* The update event.
*/
public function runPreChecks(UpdateEvent $event): void {
$this->addResults($event, static::STATE_KEY . "." . AutomaticUpdatesEvents::READINESS_CHECK);
$this->addResults($event, static::STATE_KEY . "." . ReadinessCheckEvent::class);
}
/**
......@@ -93,7 +95,7 @@ class TestChecker1 implements EventSubscriberInterface {
* The update event.
*/
public function runPreCommitChecks(UpdateEvent $event): void {
$this->addResults($event, static::STATE_KEY . "." . AutomaticUpdatesEvents::PRE_COMMIT);
$this->addResults($event, static::STATE_KEY . "." . PreCommitEvent::class);
}
/**
......@@ -103,7 +105,7 @@ class TestChecker1 implements EventSubscriberInterface {
* The update event.
*/
public function runStartChecks(UpdateEvent $event): void {
$this->addResults($event, static::STATE_KEY . "." . AutomaticUpdatesEvents::PRE_START);
$this->addResults($event, static::STATE_KEY . "." . PreStartEvent::class);
}
/**
......@@ -111,9 +113,9 @@ class TestChecker1 implements EventSubscriberInterface {
*/
public static function getSubscribedEvents() {
$priority = defined('AUTOMATIC_UPDATES_TEST_SET_PRIORITY') ? AUTOMATIC_UPDATES_TEST_SET_PRIORITY : 5;
$events[AutomaticUpdatesEvents::READINESS_CHECK][] = ['runPreChecks', $priority];
$events[AutomaticUpdatesEvents::PRE_START][] = ['runStartChecks', $priority];
$events[AutomaticUpdatesEvents::PRE_COMMIT][] = ['runPreCommitChecks', $priority];
$events[ReadinessCheckEvent::class][] = ['runPreChecks', $priority];
$events[PreStartEvent::class][] = ['runStartChecks', $priority];
$events[PreCommitEvent::class][] = ['runPreCommitChecks', $priority];
return $events;
}
......
......@@ -2,7 +2,8 @@
namespace Drupal\automatic_updates_test2\ReadinessChecker;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\PreStartEvent;
use Drupal\automatic_updates\Event\ReadinessCheckEvent;
use Drupal\automatic_updates_test\ReadinessChecker\TestChecker1;
/**
......@@ -13,8 +14,8 @@ class TestChecker2 extends TestChecker1 {
protected const STATE_KEY = 'automatic_updates_test2.checker_results';
public static function getSubscribedEvents() {
$events[AutomaticUpdatesEvents::READINESS_CHECK][] = ['runPreChecks', 4];
$events[AutomaticUpdatesEvents::PRE_START][] = ['runStartChecks', 4];
$events[ReadinessCheckEvent::class][] = ['runPreChecks', 4];
$events[PreStartEvent::class][] = ['runStartChecks', 4];
return $events;
}
......
......@@ -2,7 +2,7 @@
namespace Drupal\Tests\automatic_updates\Functional;
use Drupal\automatic_updates\AutomaticUpdatesEvents;
use Drupal\automatic_updates\Event\PreStartEvent;
use Drupal\automatic_updates\Exception\UpdateException;
use Drupal\automatic_updates\Validation\ValidationResult;
use Drupal\automatic_updates_test\ReadinessChecker\TestChecker1;
......@@ -177,7 +177,7 @@ class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase {
// Repackage the validation error as an exception, so we can test what
// happens if a validator throws once the update has started.
$error = new UpdateException($expected_results, 'The update exploded.');
TestChecker1::setTestResult($error, AutomaticUpdatesEvents::PRE_START);
TestChecker1::setTestResult($error, PreStartEvent::class);
$session->reload();
$assert_session->pageTextNotContains(static::$errorsExplanation);
$assert_session->pageTextNotContains(static::$warningsExplanation);
......@@ -193,7 +193,7 @@ class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase {
// If a validator flags an error, but doesn't throw, the update should still
// be halted.
TestChecker1::setTestResult($expected_results, AutomaticUpdatesEvents::PRE_START);
TestChecker1::setTestResult($expected_results, PreStartEvent::class);
$this->deleteStagedUpdate();
$page->pressButton('Update');
$this->checkForMetaRefresh();
......@@ -206,7 +206,7 @@ class UpdaterFormTest extends AutomaticUpdatesFunctionalTestBase {
// If a validator flags a warning, but doesn't throw, the update should
// continue.
$expected_results = $this->testResults['checker_1']['1 warning'];
TestChecker1::setTestResult($expected_results, AutomaticUpdatesEvents::PRE_START);
TestChecker1::setTestResult($expected_results, PreStartEvent::class);
$session->reload();
$this->deleteStagedUpdate();
$page->pressButton('Update');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment