Skip to content

Commit

Permalink
BUGFIX: Make DefaultEventPublisherFactory return the same instance
Browse files Browse the repository at this point in the history
Fixes `DefaultEventPublisherFactory::create()` to return the same
`EventPublisherInterface` instance for a given `$eventStoreIdentifier`.

This allows for injecting Event Publishers into 3rd party code with
some custom `Objects.yaml` configuration:

```yaml
Some\Vendor\Some\Class:
  properties:
    'eventPublisher':
      object:
        factoryObjectName: Neos\EventSourcing\EventPublisher\DefaultEventPublisherFactory
        arguments:
          1:
            value: 'Some-EventStore-Id'
```
  • Loading branch information
bwaidelich committed Feb 3, 2020
1 parent 732538d commit 00a803c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Classes/EventPublisher/DefaultEventPublisherFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,24 @@ final class DefaultEventPublisherFactory implements EventPublisherFactoryInterfa
*/
private $mappingProvider;

/**
* A list of all initialized Event Publisher instances, indexed by the "Event Store identifier"
*
* @var EventPublisherInterface[]
*/
private $eventPublisherInstances;

public function __construct(DefaultEventToListenerMappingProvider $mappingProvider)
{
$this->mappingProvider = $mappingProvider;
}

public function create(string $eventStoreIdentifier): EventPublisherInterface
{
$mappings = $this->mappingProvider->getMappingsForEventStore($eventStoreIdentifier);
return DeferEventPublisher::forPublisher(new JobQueueEventPublisher($eventStoreIdentifier, $mappings));
if (!isset($this->eventPublisherInstances[$eventStoreIdentifier])) {
$mappings = $this->mappingProvider->getMappingsForEventStore($eventStoreIdentifier);
$this->eventPublisherInstances[$eventStoreIdentifier] = DeferEventPublisher::forPublisher(new JobQueueEventPublisher($eventStoreIdentifier, $mappings));
}
return $this->eventPublisherInstances[$eventStoreIdentifier];
}
}

0 comments on commit 00a803c

Please sign in to comment.