Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Basic support for aggregates in SharedEventManager according to feedback... #3084

Closed
wants to merge 1 commit into from

2 participants

@stefankleff

Replaces #2819

@weierophinney weierophinney commented on the diff
...ary/Zend/EventManager/SharedEventManagerInterface.php
@@ -67,4 +67,21 @@ public function getEvents($id);
@weierophinney Owner

Changing interfaces is a BC break.

The way to get around this is to introduce a new interface -- let's say, "SharedListenerAggregateAwareInterface" -- with the new methods. The default implementation can then implement that as well as the SharedEventManagerInterface.

(If I merge today, I'll do that work; if not, I'll let you know.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@weierophinney weierophinney referenced this pull request from a commit
@weierophinney weierophinney [#3084] Split new interface methods into separate interface
- To prevent a BC break (uncovered by running the unit tests), moved new
  attachAggregate() and detachAggregate() methods into a separate interface; the
  SharedEventManager implementation now also implements this new interface.
0482078
@weierophinney

Merged, with the changes mentioned in feedback. Which, by the way, were caught by Travis -- that's what the CI failures were. :)

@ghost Unknown referenced this pull request from a commit
@weierophinney weierophinney [#3084] Split new interface methods into separate interface
- To prevent a BC break (uncovered by running the unit tests), moved new
  attachAggregate() and detachAggregate() methods into a separate interface; the
  SharedEventManager implementation now also implements this new interface.
a181500
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-eventmanager
@weierophinney weierophinney [zendframework/zf2#3084] Split new interface methods into separate in…
…terface

- To prevent a BC break (uncovered by running the unit tests), moved new
  attachAggregate() and detachAggregate() methods into a separate interface; the
  SharedEventManager implementation now also implements this new interface.
b235040
@weierophinney weierophinney referenced this pull request from a commit in zendframework/zend-eventmanager
@weierophinney weierophinney Merge branch 'feature/events-shared-aggregates' into develop d41e72e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
30 library/Zend/EventManager/SharedEventManager.php
@@ -78,6 +78,22 @@ public function attach($id, $event, $callback, $priority = 1)
}
/**
+ * Attach a listener aggregate
+ *
+ * Listener aggregates accept an EventManagerInterface instance, and call attachShared()
+ * one or more times, typically to attach to multiple events using local
+ * methods.
+ *
+ * @param SharedListenerAggregateInterface $aggregate
+ * @param int $priority If provided, a suggested priority for the aggregate to use
+ * @return mixed return value of {@link ListenerAggregateInterface::attachShared()}
+ */
+ public function attachAggregate(SharedListenerAggregateInterface $aggregate, $priority = 1)
+ {
+ return $aggregate->attachShared($this, $priority);
+ }
+
+ /**
* Detach a listener from an event offered by a given resource
*
* @param string|int $id
@@ -93,6 +109,20 @@ public function detach($id, CallbackHandler $listener)
}
/**
+ * Detach a listener aggregate
+ *
+ * Listener aggregates accept an SharedEventManagerInterface instance, and call detachShared()
+ * of all previously attached listeners.
+ *
+ * @param SharedListenerAggregateInterface $aggregate
+ * @return mixed return value of {@link SharedListenerAggregateInterface::detachShared()}
+ */
+ public function detachAggregate(SharedListenerAggregateInterface $aggregate)
+ {
+ return $aggregate->detachShared($this);
+ }
+
+ /**
* Retrieve all registered events for a given resource
*
* @param string|int $id
View
17 library/Zend/EventManager/SharedEventManagerInterface.php
@@ -67,4 +67,21 @@ public function getEvents($id);
*/
public function clearListeners($id, $event = null);
+ /**
+ * Attach a listener aggregate
+ *
+ * @param SharedListenerAggregateInterface $aggregate
+ * @param int $priority If provided, a suggested priority for the aggregate to use
+ * @return mixed return value of {@link SharedListenerAggregateInterface::attachShared()}
+ */
+ public function attachAggregate(SharedListenerAggregateInterface $aggregate, $priority = 1);
+
+ /**
+ * Detach a listener aggregate
+ *
+ * @param SharedListenerAggregateInterface $aggregate
+ * @return mixed return value of {@link SharedListenerAggregateInterface::detachShared()}
+ */
+ public function detachAggregate(SharedListenerAggregateInterface $aggregate);
+
}
View
42 library/Zend/EventManager/SharedListenerAggregateInterface.php
@@ -0,0 +1,42 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_EventManager
+ */
+
+namespace Zend\EventManager;
+
+/**
+ * Interface for self-registering event listeners.
+ *
+ * Classes implementing this interface may be registered by name or instance
+ * with an SharedEventManager, without an event name. The {@link attach()} method will
+ * then be called with the current SharedEventManager instance, allowing the class to
+ * wire up one or more listeners.
+ *
+ * @category Zend
+ * @package Zend_EventManager
+ */
+interface SharedListenerAggregateInterface
+{
+ /**
+ * Attach one or more listeners
+ *
+ * Implementors may add an optional $priority argument; the SharedEventManager
+ * implementation will pass this to the aggregate.
+ *
+ * @param SharedEventManagerInterface $events
+ */
+ public function attachShared(SharedEventManagerInterface $events);
+
+ /**
+ * Detach all previously attached listeners
+ *
+ * @param SharedEventManagerInterface $events
+ */
+ public function detachShared(SharedEventManagerInterface $events);
+}
View
24 tests/ZendTest/EventManager/StaticEventManagerTest.php
@@ -307,4 +307,28 @@ public function testListenersAttachedToAnyIdentifierProvidedToEventManagerOrWild
$manager->trigger('bar', $this, array());
$this->assertEquals(4, $test->triggered);
}
+
+ public function testCanAttachListenerAggregate()
+ {
+ $staticManager = StaticEventManager::getInstance();
+ $aggregate = new TestAsset\SharedMockAggregate('bazinga');
+ $staticManager->attachAggregate($aggregate);
+
+ $events = $staticManager->getEvents('bazinga');
+ $this->assertCount(2, $events);
+ }
+
+ public function testCanDetachListenerAggregate()
+ {
+ $staticManager = StaticEventManager::getInstance();
+ $aggregate = new TestAsset\SharedMockAggregate('bazinga');
+
+ $staticManager->attachAggregate($aggregate);
+ $events = $staticManager->getEvents('bazinga');
+ $this->assertCount(2, $events);
+
+ $staticManager->detachAggregate($aggregate);
+ $events = $staticManager->getEvents('bazinga');
+ $this->assertCount(0, $events);
+ }
}
View
66 tests/ZendTest/EventManager/TestAsset/SharedMockAggregate.php
@@ -0,0 +1,66 @@
+<?php
+/**
+ * Zend Framework (http://framework.zend.com/)
+ *
+ * @link http://github.com/zendframework/zf2 for the canonical source repository
+ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
+ * @license http://framework.zend.com/license/new-bsd New BSD License
+ * @package Zend_EventManager
+ */
+
+namespace ZendTest\EventManager\TestAsset;
+
+use Zend\EventManager\SharedEventManagerInterface;
+use Zend\EventManager\SharedListenerAggregateInterface;
+
+/**
+ * @category Zend
+ * @package Zend_EventManager
+ * @subpackage UnitTests
+ * @group Zend_EventManager
+ */
+class SharedMockAggregate implements SharedListenerAggregateInterface
+{
+
+ protected $identity;
+
+ public function __construct($identity)
+ {
+ $this->identity = $identity;
+ }
+
+ protected $listeners = array();
+ public $priority;
+
+ public function attachShared(SharedEventManagerInterface $events, $priority = null)
+ {
+ $this->priority = $priority;
+
+ $listeners = array();
+ $listeners[] = $events->attach($this->identity, 'foo.bar', array( $this, 'fooBar' ));
+ $listeners[] = $events->attach($this->identity, 'foo.baz', array( $this, 'fooBaz' ));
+
+ $this->listeners[ \spl_object_hash($events) ] = $listeners;
+
+ return __METHOD__;
+ }
+
+ public function detachShared(SharedEventManagerInterface $events)
+ {
+ foreach ($this->listeners[ \spl_object_hash($events) ] as $listener) {
+ $events->detach($this->identity, $listener);
+ }
+
+ return __METHOD__;
+ }
+
+ public function fooBar()
+ {
+ return __METHOD__;
+ }
+
+ public function fooBaz()
+ {
+ return __METHOD__;
+ }
+}
Something went wrong with that request. Please try again.