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

Commit

Permalink
Merge branch 'develop' of https://github.com/zendframework/zf2 into h…
Browse files Browse the repository at this point in the history
…otfix/zend-session-global-session-storage
  • Loading branch information
Show file tree
Hide file tree
Showing 8 changed files with 271 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/EventManagerAwareTrait.php
@@ -0,0 +1,22 @@
<?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;

use \Zend\EventManager\ProvidesEvents;

/**
* @category Zend
* @package Zend_EventManager
*/
trait EventManagerAwareTrait
{
use ProvidesEvents;
}
38 changes: 38 additions & 0 deletions src/SharedEventAggregateAwareInterface.php
@@ -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);
}
34 changes: 33 additions & 1 deletion src/SharedEventManager.php
Expand Up @@ -23,7 +23,9 @@
* @category Zend
* @package Zend_EventManager
*/
class SharedEventManager implements SharedEventManagerInterface
class SharedEventManager implements
SharedEventAggregateAwareInterface,
SharedEventManagerInterface
{
/**
* Identifiers with event connections
Expand Down Expand Up @@ -77,6 +79,22 @@ public function attach($id, $event, $callback, $priority = 1)
return $listeners[0];
}

/**
* 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
*
Expand All @@ -92,6 +110,20 @@ public function detach($id, CallbackHandler $listener)
return $this->identifiers[$id]->detach($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
*
Expand Down
1 change: 0 additions & 1 deletion src/SharedEventManagerInterface.php
Expand Up @@ -66,5 +66,4 @@ public function getEvents($id);
* @return bool
*/
public function clearListeners($id, $event = null);

}
42 changes: 42 additions & 0 deletions src/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);
}
46 changes: 46 additions & 0 deletions test/EventManagerAwareTraitTest.php
@@ -0,0 +1,46 @@
<?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;

use \PHPUnit_Framework_TestCase as TestCase;
use \Zend\EventManager\EventManager;

/**
* @requires PHP 5.4
*/
class EventManagerAwareTraitTest extends TestCase
{
public function testSetEventManager()
{
$object = $this->getObjectForTrait('\Zend\EventManager\EventManagerAwareTrait');

$this->assertAttributeEquals(null, 'events', $object);

$eventManager = new EventManager;

$object->setEventManager($eventManager);

$this->assertAttributeEquals($eventManager, 'events', $object);
}

public function testGetEventManager()
{
$object = $this->getObjectForTrait('\Zend\EventManager\EventManagerAwareTrait');

$this->assertInstanceOf('\Zend\EventManager\EventManagerInterface', $object->getEventManager());

$eventManager = new EventManager;

$object->setEventManager($eventManager);

$this->assertSame($eventManager, $object->getEventManager());
}
}
24 changes: 24 additions & 0 deletions test/StaticEventManagerTest.php
Expand Up @@ -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);
}
}
66 changes: 66 additions & 0 deletions test/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__;
}
}

0 comments on commit ef24a14

Please sign in to comment.