Permalink
Browse files

Merge branch 'feature/events-shared-aggregates' into develop

Close #3084
  • Loading branch information...
2 parents 493dadc + a181500 commit 67cd670ec865bb51502f11dcc45407b9dc04479f @weierophinney weierophinney committed Dec 11, 2012
@@ -0,0 +1,38 @@
+<?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 allowing attachment of shared aggregate listeners.
+ *
+ * @category Zend
+ * @package Zend_EventManager
+ */
+interface SharedEventAggregateAwareInterface
+{
+ /**
+ * 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);
+}
@@ -23,7 +23,9 @@
* @category Zend
* @package Zend_EventManager
*/
-class SharedEventManager implements SharedEventManagerInterface
+class SharedEventManager implements
+ SharedEventAggregateAwareInterface,
+ SharedEventManagerInterface
{
/**
* Identifiers with event connections
@@ -78,6 +80,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 +111,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
@@ -66,5 +66,4 @@ public function getEvents($id);
* @return bool
*/
public function clearListeners($id, $event = null);
-
}
@@ -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);
+}
@@ -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);
+ }
}
@@ -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__;
+ }
+}

0 comments on commit 67cd670

Please sign in to comment.