Skip to content

Commit

Permalink
- Simplified DoctrineDomainEventDispatcher
Browse files Browse the repository at this point in the history
- Updated description
  • Loading branch information
maryo committed Oct 23, 2016
1 parent 860ab19 commit 81c4f4d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 43 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
![PHP7](https://img.shields.io/badge/php-7-6B7EB9.svg)
[![License](https://poser.pugx.org/vanio/doctrine-domain-events/license)](https://github.com/vaniocz/doctrine-domain-events/blob/master/LICENSE)

Very lightweight extension for Doctrine2 ORM allowing usage of domain events together with an ability to call flush
inside it's listeners. This library is inspired in Beberlei's
Very lightweight extension for Doctrine2 ORM allowing usage of domain events together with an ability to flush inside
it's listeners. This library is inspired in Beberlei's
[Doctrine and Domain Events article](http://www.whitewashing.de/2013/07/24/doctrine_and_domainevents.html).

# Installation
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vanio/doctrine-domain-events",
"description": "An extension for Doctrine2 ORM allowing usage of domain events and an ability to call flush inside events triggered by flush.",
"description": "An extension for Doctrine2 ORM allowing usage of domain events and an ability to flush inside it's listeners.",
"keywords": ["doctrine", "doctrine2", "domain events"],
"homepage": "https://github.com/vaniocz/doctrine-domain-events",
"license": "MIT",
Expand Down
67 changes: 27 additions & 40 deletions src/DoctrineDomainEventDispatcher.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Vanio\DoctrineDomainEvents;

use Doctrine\Common\EventArgs;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Event\LifecycleEventArgs;
Expand All @@ -14,43 +15,11 @@ class DoctrineDomainEventDispatcher implements EventSubscriber
private $entityManager;

/** @var EventProvider[] */
private $eventProviders;
private $eventProviders = [];

public function postFlush(PostFlushEventArgs $event)
{
$this->entityManager = $event->getEntityManager();
$this->eventProviders = [];
$this->dispatchEventsOnTransactionEnd();
}

public function postRemove(LifecycleEventArgs $event)
{
$this->entityManager = $event->getEntityManager();
$this->keepEventProviders($event->getEntity());
$this->dispatchEventsOnTransactionEnd();
}

public function getSubscribedEvents(): array
{
return [Events::postFlush, Events::postRemove];
}

/**
* @param object $entity
*/
private function keepEventProviders($entity)
{
if ($entity instanceof EventProvider) {
$this->eventProviders[] = $entity;
}
}

private function dispatchEventsOnTransactionEnd()
{
if (!$this->isTransactionEnd()) {
return;
}

$eventsByOrder = [];

foreach ($this->entityManager->getUnitOfWork()->getIdentityMap() as $entities) {
Expand All @@ -65,23 +34,41 @@ private function dispatchEventsOnTransactionEnd()
}
}

if (!$eventsByOrder) {
return;
}

$this->clearChangeSets();
ksort($eventsByOrder);

foreach ($eventsByOrder as $events) {
foreach ($events as $event) {
$this->entityManager->getEventManager()->dispatchEvent($event->name(), $event);
$this->dispatchEvent($event->name(), $event);
}
}

$this->eventProviders = [];
}

public function postRemove(LifecycleEventArgs $event)
{
$this->keepEventProviders($event->getEntity());
}

public function getSubscribedEvents(): array
{
return [Events::postFlush, Events::postRemove];
}

/**
* @param object $entity
*/
private function keepEventProviders($entity)
{
if ($entity instanceof EventProvider) {
$this->eventProviders[] = $entity;
}
}

private function isTransactionEnd(): bool
private function dispatchEvent(string $eventName, EventArgs $event = null)
{
return !$this->entityManager->getUnitOfWork()->getScheduledEntityDeletions();
$this->entityManager->getEventManager()->dispatchEvent($eventName, $event);
}

/**
Expand Down

0 comments on commit 81c4f4d

Please sign in to comment.