From 4b1e77b4ce7c65056c47414f5025fc6101ba163b Mon Sep 17 00:00:00 2001
From: phenaproxima <phenaproxima@205645.no-reply.drupal.org>
Date: Wed, 27 Oct 2021 13:42:13 +0000
Subject: [PATCH] Issue #3246194 by phenaproxima: All of Package Manager's
 events should carry a reference to the stage

---
 package_manager/src/Event/StageEvent.php      | 28 +++++++++++++++++++
 package_manager/src/Stage.php                 | 16 +++++------
 .../tests/src/Kernel/StageEventsTest.php      |  5 ++++
 3 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/package_manager/src/Event/StageEvent.php b/package_manager/src/Event/StageEvent.php
index e3fa57ee8c..a222fbf648 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 562ef8f927..aef260e920 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 8124fb63e2..11cdf2071f 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.',
-- 
GitLab