Skip to content

Commit ec2cee0

Browse files
authored
feat: Use destruction events at a later runtime
Fixes #444
1 parent 183605b commit ec2cee0

File tree

6 files changed

+74
-23
lines changed

6 files changed

+74
-23
lines changed

modules/next/next.module

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,31 +69,21 @@ function next_next_site_preview_alter(array &$preview, array $context) {
6969
*/
7070
function next_entity_insert(EntityInterface $entity) {
7171
$event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::INSERT_ACTION);
72-
drupal_register_shutdown_function('_next_dispatch_entity_action_event', $event);
72+
\Drupal::service('next.entity_action_event_dispatcher')->addEvent($event);
7373
}
7474

7575
/**
7676
* Implements hook_entity_update().
7777
*/
7878
function next_entity_update(EntityInterface $entity) {
7979
$event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::UPDATE_ACTION);
80-
drupal_register_shutdown_function('_next_dispatch_entity_action_event', $event);
80+
\Drupal::service('next.entity_action_event_dispatcher')->addEvent($event);
8181
}
8282

8383
/**
8484
* Implements hook_entity_predelete().
8585
*/
8686
function next_entity_predelete(EntityInterface $entity) {
8787
$event = EntityActionEvent::createFromEntity($entity, EntityActionEventInterface::DELETE_ACTION);
88-
drupal_register_shutdown_function('_next_dispatch_entity_action_event', $event);
89-
}
90-
91-
/**
92-
* Helper to dispatch an entity action event.
93-
*
94-
* @param \Drupal\next\Event\EntityActionEventInterface $event
95-
* The entity action event.
96-
*/
97-
function _next_dispatch_entity_action_event(EntityActionEventInterface $event) {
98-
\Drupal::service('event_dispatcher')->dispatch($event, EntityEvents::ENTITY_ACTION);
88+
\Drupal::service('next.entity_action_event_dispatcher')->addEvent($event);
9989
}

modules/next/next.services.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,9 @@ services:
6565
]
6666
tags:
6767
- { name: event_subscriber }
68+
69+
next.entity_action_event_dispatcher:
70+
class: Drupal\next\EventSubscriber\EntityActionEventDispatcher
71+
arguments: ['@event_dispatcher']
72+
tags:
73+
- { name: needs_destruction }
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Drupal\next\EventSubscriber;
4+
5+
use Drupal\Core\DestructableInterface;
6+
use Drupal\next\Event\EntityActionEvent;
7+
use Drupal\next\Event\EntityEvents;
8+
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
9+
10+
/**
11+
* Defines an event subscriber for dispatching entity events.
12+
*/
13+
final class EntityActionEventDispatcher implements DestructableInterface {
14+
15+
/**
16+
* The events to dispach.
17+
*
18+
* @var \Drupal\next\Event\EntityActionEvent[]
19+
*/
20+
private array $events = [];
21+
22+
/**
23+
* EntityActionEventDispatcher constructor.
24+
*/
25+
public function __construct(
26+
private EventDispatcherInterface $eventDispatcher
27+
) {
28+
}
29+
30+
/**
31+
* Adds an event to be dispatched at the end of the request.
32+
*
33+
* @param \Drupal\next\Event\EntityActionEvent $event
34+
* The event.
35+
*/
36+
public function addEvent(EntityActionEvent $event): void {
37+
$this->events[] = $event;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function destruct() {
44+
foreach ($this->events as $event) {
45+
$this->eventDispatcher->dispatch($event, EntityEvents::ENTITY_ACTION);
46+
}
47+
}
48+
49+
}

modules/next/tests/src/Kernel/Event/EntityActionEventTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Drupal\KernelTests\KernelTestBase;
88
use Drupal\node\Entity\NodeType;
99
use Drupal\Tests\node\Traits\NodeCreationTrait;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\Response;
1012

1113
/**
1214
* Tests the EntityActionEvent.
@@ -60,17 +62,17 @@ public function testEntityActionEvents() {
6062

6163
// Insert.
6264
$page->save();
63-
_drupal_shutdown_function();
65+
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
6466
$this->assertLogsContains("Event next.entity.action dispatched for entity A page and action insert.");
6567

6668
// Update.
6769
$page->set('title', 'A page updated')->save();
68-
_drupal_shutdown_function();
70+
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
6971
$this->assertLogsContains("Event next.entity.action dispatched for entity A page updated and action update.");
7072

7173
// Delete.
7274
$page->delete();
73-
_drupal_shutdown_function();
75+
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
7476
$this->assertLogsContains("Event next.entity.action dispatched for entity A page updated and action delete.");
7577
}
7678

modules/next/tests/src/Kernel/Event/EntityRevalidatedEventTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Drupal\KernelTests\KernelTestBase;
88
use Drupal\next\Entity\NextEntityTypeConfig;
99
use Drupal\Tests\node\Traits\NodeCreationTrait;
10+
use Symfony\Component\HttpFoundation\Request;
11+
use Symfony\Component\HttpFoundation\Response;
1012

1113
/**
1214
* Tests the EntityRevalidatedEvent.
@@ -70,17 +72,17 @@ public function testEntityRevalidatedEvents() {
7072

7173
// Insert.
7274
$page->save();
73-
_drupal_shutdown_function();
75+
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
7476
$this->assertLogsContains("Entity A page, action insert, revalidated 0.");
7577

7678
// Update.
7779
$page->set('title', 'A page updated')->save();
78-
_drupal_shutdown_function();
80+
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
7981
$this->assertLogsContains("Entity A page updated, action update, revalidated 0.");
8082

8183
// Delete.
8284
$page->delete();
83-
_drupal_shutdown_function();
85+
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
8486
$this->assertLogsContains("Entity A page updated, action delete, revalidated 0.");
8587
}
8688

modules/next/tests/src/Kernel/Plugin/PathRevalidatorTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
use Drupal\Tests\node\Traits\NodeCreationTrait;
99
use GuzzleHttp\ClientInterface;
1010
use Prophecy\PhpUnit\ProphecyTrait;
11+
use Symfony\Component\HttpFoundation\Request;
12+
use Symfony\Component\HttpFoundation\Response;
1113

1214
/**
1315
* Tests the path revalidator plugin.
@@ -80,13 +82,13 @@ public function testRevalidate() {
8082
$client->request('GET', $this->any())->shouldNotBeCalled();
8183
$page = $this->createNode();
8284
$page->save();
83-
_drupal_shutdown_function();
85+
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
8486

8587
$client->request('GET', 'http://blog.com/api/revalidate?slug=/node/2')->shouldBeCalled();
8688
$blog_site->setRevalidateUrl('http://blog.com/api/revalidate')->save();
8789
$page = $this->createNode();
8890
$page->save();
89-
_drupal_shutdown_function();
91+
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
9092

9193
$marketing = NextSite::create([
9294
'id' => 'marketing',
@@ -105,7 +107,7 @@ public function testRevalidate() {
105107
$client->request('GET', 'http://blog.com/api/revalidate?slug=/node/3')->shouldBeCalled();
106108
$page = $this->createNode();
107109
$page->save();
108-
_drupal_shutdown_function();
110+
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
109111

110112
$entity_type_config->setRevalidatorConfiguration('path', [
111113
'additional_paths' => "/\n/blog",
@@ -119,7 +121,7 @@ public function testRevalidate() {
119121
$client->request('GET', 'http://blog.com/api/revalidate?slug=/blog')->shouldBeCalled();
120122
$page = $this->createNode();
121123
$page->save();
122-
_drupal_shutdown_function();
124+
$this->container->get('kernel')->terminate(Request::create('/'), new Response());
123125
}
124126

125127
}

0 commit comments

Comments
 (0)