diff --git a/package_manager/src/Event/StageEvent.php b/package_manager/src/Event/StageEvent.php index e3fa57ee8c254d5a0d6266c2512b08261bd6cb83..a222fbf648e42da50e3f55f102c8b46d1c014227 100644 --- a/package_manager/src/Event/StageEvent.php +++ b/package_manager/src/Event/StageEvent.php @@ -2,6 +2,7 @@ namespace Drupal\package_manager\Event; +use Drupal\package_manager\Stage; use Drupal\package_manager\ValidationResult; use Symfony\Contracts\EventDispatcher\Event; @@ -17,6 +18,33 @@ abstract class StageEvent extends Event { */ protected $results = []; + /** + * The stage which fired this event. + * + * @var \Drupal\package_manager\Stage + */ + protected $stage; + + /** + * Constructs a StageEvent object. + * + * @param \Drupal\package_manager\Stage $stage + * The stage which fired this event. + */ + public function __construct(Stage $stage) { + $this->stage = $stage; + } + + /** + * Returns the stage which fired this event. + * + * @return \Drupal\package_manager\Stage + * The stage which fired this event. + */ + public function getStage(): Stage { + return $this->stage; + } + /** * Gets the validation results. * diff --git a/package_manager/src/Stage.php b/package_manager/src/Stage.php index 562ef8f9274ceb16bf81c5117deb052e82bbc4ac..aef260e9200c0274fa185098e19f4c19fcd8ae77 100644 --- a/package_manager/src/Stage.php +++ b/package_manager/src/Stage.php @@ -102,11 +102,11 @@ class Stage { $active_dir = $this->pathLocator->getActiveDirectory(); $stage_dir = $this->pathLocator->getStageDirectory(); - $event = new PreCreateEvent(); + $event = new PreCreateEvent($this); $this->dispatch($event); $this->beginner->begin($active_dir, $stage_dir, $event->getExcludedPaths()); - $this->dispatch(new PostCreateEvent()); + $this->dispatch(new PostCreateEvent($this)); } /** @@ -119,9 +119,9 @@ class Stage { $command = array_merge(['require'], $constraints); $command[] = '--update-with-all-dependencies'; - $this->dispatch(new PreRequireEvent()); + $this->dispatch(new PreRequireEvent($this)); $this->stager->stage($command, $this->pathLocator->getStageDirectory()); - $this->dispatch(new PostRequireEvent()); + $this->dispatch(new PostRequireEvent($this)); } /** @@ -131,23 +131,23 @@ class Stage { $active_dir = $this->pathLocator->getActiveDirectory(); $stage_dir = $this->pathLocator->getStageDirectory(); - $event = new PreApplyEvent(); + $event = new PreApplyEvent($this); $this->dispatch($event); $this->committer->commit($stage_dir, $active_dir, $event->getExcludedPaths()); - $this->dispatch(new PostApplyEvent()); + $this->dispatch(new PostApplyEvent($this)); } /** * Deletes the staging area. */ public function destroy(): void { - $this->dispatch(new PreDestroyEvent()); + $this->dispatch(new PreDestroyEvent($this)); $stage_dir = $this->pathLocator->getStageDirectory(); if (is_dir($stage_dir)) { $this->cleaner->clean($stage_dir); } - $this->dispatch(new PostDestroyEvent()); + $this->dispatch(new PostDestroyEvent($this)); } /** diff --git a/package_manager/tests/src/Kernel/StageEventsTest.php b/package_manager/tests/src/Kernel/StageEventsTest.php index 8124fb63e29098d8e449e6d9308686985b402937..11cdf2071fe24e2ffce15848bc3abbe73ed574c6 100644 --- a/package_manager/tests/src/Kernel/StageEventsTest.php +++ b/package_manager/tests/src/Kernel/StageEventsTest.php @@ -19,6 +19,8 @@ use Symfony\Component\EventDispatcher\EventSubscriberInterface; /** * Tests that the staging area fires events during its lifecycle. * + * @covers \Drupal\package_manager\Event\StageEvent + * * @group package_manager */ class StageEventsTest extends KernelTestBase implements EventSubscriberInterface { @@ -63,6 +65,9 @@ class StageEventsTest extends KernelTestBase implements EventSubscriberInterface public function handleEvent(StageEvent $event): void { array_push($this->events, get_class($event)); + // The event should have a reference to the stage which fired it. + $this->assertSame($event->getStage(), $this->container->get('package_manager.stage')); + // Adding a warning to the event, should not trigger an exception. $result = ValidationResult::createWarning([ 'This is a public service announcement, this is only a test.',