Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit 56f5641

Browse files
committed
feat: update EventManager to use listener providers
- Adds `Zend\EventManager\EventDispatcherInterface` as a forwards compatibility shim for PSR-14. - Adds `Zend\EventManager\SharedEventManager\SharedEventManagerDecorator`, which decorates generic `SharedEventManagerInterface` instances as listener providers. - Modifies `PrioritizedAggregateListenerProvider` to accept an optional `ListenerProviderInterface $default` argument. This allows non-prioritized `SharedEventManagerInterface` instances (such as the `SharedEventManagerDecorator` in the previous item) to be fallback providers. - Modifies `Zend\EventManager\EventManager` as follows: - It now implements `EventDispatcherInterface` - It now composes a `$provider` property, and an optional `$prioritizedProvider` property. If you instantiate it per previous versions, it creates a `PrioritizedListenerProvider` instance and assigns it to the `$prioritizedProvider` property. It then checks to see if a shared manager was provided, and the type provided, to either assign the `$prioritizedProvider` as the `$provider`, or a `PrioritizedAggregateListenerProvider` that composes both the `$prioritizedProvider` and shared manager instances. - It adds a static named constructor, `createUsingListenerProvider()`, which accepts a single `ListenerProviderInterface` instance. This value is assigned to `$provider`, and, if it is a `PrioritizedListenerAttachmentInterface` instance, to the `$prioritizedProvider` property as well. - Each of the listener attachment methods (attach, detach, clearListeners, *WildcardListeners) now proxy to the composed `$prioritizedProvider`, if any. If there is none, theses methods now raise an exception. - The `getListenersForEvent()` method now proxies to the underling `$provider` property. - The `triggerListeners()` method now consumes the value of `getListenersForEvent()`. - It adds the method `dispatch($event)`, which proxies to `triggerListeners()`, and returns the `$event` it was passed. The method raises an exception of `$event` is a non-object. - Each of `trigger()`, `triggerUntil`, `triggerEvent`, `triggerEventUntil`, `getIdentifiers`, `setIdentifiers`, `addIdenitifers`, `getSharedManager`, `attach`, `detach`, `attachWildcardListener`, `detachWildcardListener`, `clearListeners`, and `getListenersForEvent` have been marked deprecated. - Updates `EventListenerIntrospectionTrait` to work with the new internals of the `EventManager`. - Updates `EventManagerTest`: - updates `getListenersForEvent()` to work with the new `EventManager` internals - Removes `testAttachShouldAddEventIfItDoesNotExist` as it was irrelevant even before the changes. - Removes the `testTriggeringAnEventWithAnEmptyNameRaisesAnException` test, as this is no longer true; you can use any object as an event now. - Modifies a few tests where they were accessing internal structures that have changed, while keeping the same assertions in place. - Adds `EventManagerWithProviderTest` to demonstrate usage when creating an `EventManager` via its `createUsingListenerProvider()` method. - Updates `EventListenerIntrospectionTraitTest` to work with the new internals of the `EventManager`.
1 parent 9200b59 commit 56f5641

9 files changed

+541
-212
lines changed

TODO-PSR-14.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,22 +59,34 @@
5959
- [x] with an event, can be attached to `LazyListenerSubscriber`
6060
- [x] Constructor aggregates `LazyListener` _instances_ only
6161
- [x] raises exception when `getEvent()` returns null
62-
- [ ] Event Dispatcher implementation
63-
- [ ] Implement `PrioritizedListenerAttachmentInterface` (if BC)
64-
- [ ] Create a `PrioritizedListenerProvider` instance in the `EventManger`
65-
constructor, and have the various `attach()`, `detach()`, etc. methods
66-
proxy to it.
67-
- [ ] When triggering listeners, create a `PrioritizedAggregateListenerProvider`
62+
- [x] Adapter for SharedEventManagerInterface
63+
Since we type-hint on SharedEventManagerInterface, we need to adapt generic
64+
implementations to work as ListenerProviders.
65+
- [x] Class that adapts SharedEventManagerInterface instances to ListenerProviders
66+
- [x] Event Dispatcher implementation
67+
- [x] Implement `PrioritizedListenerAttachmentInterface` (if BC)
68+
- [x] Implement `ListenerProviderInterface` (if BC)
69+
- [x] Create a `PrioritizedListenerProvider` instance in the `EventManger`
70+
constructor
71+
- [x] Decorate it in a `PrioritizedAggregateListenerProvider`
72+
- [x] Have the various `attach()`, `detach()`, etc. methods proxy to it.
73+
- [x] Adapt any provided `SharedEventManagerInterface` instance, and add it
74+
to the `PrioritizedAggregateListenerProvider`
75+
- [x] Create a named constructor that accepts a listener provider and which
76+
then uses it internally.
77+
- [x] If the instance is a `PrioritizedListenerAttachmentInterface`
78+
instance, allow the attach/detach/clear methods to proxy to it.
79+
- [x] When triggering listeners, create a `PrioritizedAggregateListenerProvider`
6880
with the composed `PrioritizedListenerProvider` and `SharedListenerProvider` /
6981
`PrioritizedIdentifierListenerProvider` implementations, in that order.
70-
- [ ] Replace logic of `triggerListeners()` to just call
82+
- [x] Replace logic of `triggerListeners()` to just call
7183
`getListenersForEvent()` on the provider. It can continue to aggregate the
7284
responses in a `ResponseCollection`
73-
- [ ] `triggerListeners()` no longer needs to type-hint its first argument
74-
- [ ] Create a `dispatch()` method
75-
- [ ] Method will act like `triggerEvent()`, except
76-
- [ ] it will return the event itself
77-
- [ ] it will need to validate that it received an object before calling
85+
- [x] `triggerListeners()` no longer needs to type-hint its first argument
86+
- [x] Create a `dispatch()` method
87+
- [x] Method will act like `triggerEvent()`, except
88+
- [x] it will return the event itself
89+
- [x] it will need to validate that it received an object before calling
7890
`triggerListeners`
7991
- [ ] Additional utilities
8092
- [ ] `EventDispatchingInterface` with a `getEventDispatcher()` method

src/EventDispatcherInterface.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* @see https://github.com/zendframework/zend-eventmanager for the canonical source repository
4+
* @copyright Copyright (c) 2019 Zend Technologies USA Inc. (https://www.zend.com)
5+
* @license https://github.com/zendframework/zend-eventmanager/blob/master/LICENSE.md New BSD License
6+
*/
7+
8+
namespace Zend\EventManager;
9+
10+
/**
11+
* Forwards-compatibility shim for PSR-14.
12+
*
13+
* @deprecated Will be removed with version 4, at which time users can instead
14+
* implement or typehint against the PSR-14 Psr\EventDispatcher\EventDispatcherInterface.
15+
*/
16+
interface EventDispatcherInterface
17+
{
18+
/**
19+
* Provide all relevant listeners with an event to process.
20+
*
21+
* @param object $event
22+
* The object to process.
23+
*
24+
* @return object
25+
* The Event that was passed, now modified by listeners.
26+
*/
27+
public function dispatch($event);
28+
}

0 commit comments

Comments
 (0)