Skip to content

Commit 4903beb

Browse files
authored
Merge pull request #3 from sanya-misharin/feature/support-symfony-6
Remove laravel collections lib and add symfony 6 version to composer
2 parents fc05b50 + 967d5fb commit 4903beb

File tree

2 files changed

+64
-39
lines changed

2 files changed

+64
-39
lines changed

composer.json

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@
1212
],
1313
"require": {
1414
"php": "^7.4|^8.0",
15-
"symfony/config": "^4.0|^5.0",
16-
"symfony/dependency-injection": "^4.0|^5.0",
17-
"symfony/http-kernel": "^4.0|^5.0",
18-
"symfony/event-dispatcher": "^4.0|^5.0",
19-
"symfony/doctrine-bridge": "^4.0|^5.0",
15+
"symfony/config": "^4.0|^5.0|^6.0",
16+
"symfony/dependency-injection": "^4.0|^5.0|^6.0",
17+
"symfony/http-kernel": "^4.0|^5.0|^6.0",
18+
"symfony/event-dispatcher": "^4.0|^5.0|^6.0",
19+
"symfony/doctrine-bridge": "^4.0|^5.0|^6.0",
2020
"doctrine/orm": "^2.0",
21-
"cvek/collection": "^2.0",
22-
"symfony/messenger": "^4.0|^5.0"
21+
"symfony/messenger": "^4.0|^5.0|^6.0"
2322
},
2423
"require-dev": {
2524
"roave/security-advisories": "dev-master"

src/EventDispatch/Listener/DomainEventsSubscriber.php

Lines changed: 58 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
use Doctrine\ORM\Events;
2626
use Symfony\Component\Messenger\MessageBusInterface;
2727
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
28-
use Tightenco\Collect\Support\Collection;
2928

3029
final class DomainEventsSubscriber implements EventSubscriber
3130
{
@@ -61,13 +60,18 @@ public function preFlush(PreFlushEventArgs $eventArgs): void
6160
if ($this->preFlushAlreadyInvoked) {
6261
return;
6362
}
63+
6464
$this->preFlushAlreadyInvoked = true;
65+
$events = [];
6566

66-
$this->fillEntities($eventArgs)
67-
->flatMap(static function (RaiseEventsInterface $entity) {
68-
return $entity->popEvents();
69-
})
70-
->each(function (DomainEventInterface $event) {
67+
foreach ($this->fillEntities($eventArgs) as $entity) {
68+
if ($entity instanceof RaiseEventsInterface) {
69+
$events = array_merge($events, $entity->popEvents());
70+
}
71+
}
72+
73+
foreach ($events as $event) {
74+
if ($event instanceof DomainEventInterface) {
7175
if (!$event instanceof AbstractSyncDomainEvent) {
7276
return;
7377
}
@@ -77,8 +81,8 @@ public function preFlush(PreFlushEventArgs $eventArgs): void
7781
: \get_class($event);
7882

7983
$this->eventDispatcher->dispatch($event->setLifecycleEvent(Events::preFlush), $eventName);
80-
})
81-
;
84+
}
85+
}
8286

8387
$this->preFlushAlreadyInvoked = false;
8488
}
@@ -88,26 +92,35 @@ public function onFlush(OnFlushEventArgs $eventArgs): void
8892
if ($this->onFlushAlreadyInvoked) {
8993
return;
9094
}
95+
9196
$this->onFlushAlreadyInvoked = true;
97+
$events = [];
98+
99+
foreach ($this->fillEntities($eventArgs) as $entity) {
100+
if ($entity instanceof RaiseEventsInterface) {
101+
$events = array_merge($events, $entity->popEvents());
102+
}
103+
}
92104

93-
$this->fillEntities($eventArgs)
94-
->flatMap(static function (RaiseEventsInterface $entity) {
95-
return $entity->popEvents();
96-
})
97-
->each(function (DomainEventInterface $event) {
105+
foreach ($events as $event) {
106+
if ($event instanceof DomainEventInterface) {
98107
$eventName = $event instanceof CustomDomainEventInterface
99108
? $event->getEventName()
100109
: \get_class($event);
101110

102111
if ($event instanceof AbstractAsyncDomainEvent) {
103112
$this->bus->dispatch($event->setLifecycleEvent(Events::onFlush));
104-
} else if ($event instanceof AbstractSyncDomainEvent) {
105-
$this->eventDispatcher->dispatch($event->setLifecycleEvent(Events::onFlush) , $eventName);
106-
} else if (!$event->isAlreadyDispatched()) {
107-
$this->bus->dispatch($event->setDispatched());
113+
} else {
114+
if ($event instanceof AbstractSyncDomainEvent) {
115+
$this->eventDispatcher->dispatch($event->setLifecycleEvent(Events::onFlush), $eventName);
116+
} else {
117+
if (!$event->isAlreadyDispatched()) {
118+
$this->bus->dispatch($event->setDispatched());
119+
}
120+
}
108121
}
109-
})
110-
;
122+
}
123+
}
111124

112125
$this->onFlushAlreadyInvoked = false;
113126
}
@@ -117,22 +130,27 @@ public function postFlush(PostFlushEventArgs $eventArgs): void
117130
if ($this->postFlushAlreadyInvoked) {
118131
return;
119132
}
133+
120134
$this->postFlushAlreadyInvoked = true;
135+
$events = [];
121136

122-
$this->fillEntities($eventArgs)
123-
->flatMap(static function (RaiseEventsInterface $entity) {
124-
return $entity->popEvents();
125-
})
126-
->each(function (DomainEventInterface $event) {
137+
foreach ($this->fillEntities($eventArgs) as $entity) {
138+
if ($entity instanceof RaiseEventsInterface) {
139+
$events = array_merge($events, $entity->popEvents());
140+
}
141+
}
142+
143+
foreach ($events as $event) {
144+
if ($event instanceof DomainEventInterface) {
127145
$eventName = $event instanceof CustomDomainEventInterface
128146
? $event->getEventName()
129147
: \get_class($event);
130148

131149
if ($event instanceof AbstractSyncDomainEvent) {
132150
$this->eventDispatcher->dispatch($event->setLifecycleEvent(Events::postFlush), $eventName);
133151
}
134-
})
135-
;
152+
}
153+
}
136154

137155
$this->postFlushAlreadyInvoked = false;
138156
}
@@ -142,23 +160,31 @@ public function postFlush(PostFlushEventArgs $eventArgs): void
142160
*
143161
* @return RaiseEventsInterface[]
144162
*/
145-
private function fillEntities(EventArgs $eventArgs): Collection
163+
private function fillEntities(EventArgs $eventArgs): array
146164
{
147-
$domainEventsEntities = new Collection();
148-
foreach ($eventArgs->getEntityManager()->getUnitOfWork()->getIdentityMap() as $class => $entities) {
165+
$domainEventsEntities = [];
166+
foreach (
167+
$eventArgs->getEntityManager()
168+
->getUnitOfWork()
169+
->getIdentityMap() as $class => $entities
170+
) {
149171
if (!\in_array(RaiseEventsInterface::class, \class_implements($class), true)) {
150172
continue;
151173
}
152174

153-
$domainEventsEntities = $domainEventsEntities->merge($entities);
175+
$domainEventsEntities = array_merge($domainEventsEntities, $entities);
154176
}
155177

156-
foreach ($eventArgs->getEntityManager()->getUnitOfWork()->getScheduledEntityDeletions() as $entityToDelete) {
178+
foreach (
179+
$eventArgs->getEntityManager()
180+
->getUnitOfWork()
181+
->getScheduledEntityDeletions() as $entityToDelete
182+
) {
157183
if (!$entityToDelete instanceof RaiseEventsInterface) {
158184
continue;
159185
}
160186

161-
$domainEventsEntities->add($entityToDelete);
187+
$domainEventsEntities[] = $entityToDelete;
162188
}
163189

164190
return $domainEventsEntities;

0 commit comments

Comments
 (0)