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

Commit

Permalink
Merge branch 'master' of git://git.zendframework.com/zf
Browse files Browse the repository at this point in the history
  • Loading branch information
Shahar Evron committed Oct 6, 2011
8 parents ab12f03 + 4579f15 + c86ed1e + 16b8f20 + 83086b6 + a81c8a5 + a85d989 + 55ef06c commit a2d63b1
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 8 deletions.
58 changes: 50 additions & 8 deletions src/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ class EventManager implements EventCollection
protected $eventClass = 'Zend\EventManager\Event';

/**
* Identifier, used to pull static signals from StaticEventManager
* @var null|string
* Identifiers, used to pull static signals from StaticEventManager
* @var array
*/
protected $identifier;
protected $identifiers = array();

/**
* Static connections
Expand All @@ -69,15 +69,15 @@ class EventManager implements EventCollection
/**
* Constructor
*
* Allows optionally specifying an identifier to use to pull signals from a
* Allows optionally specifying identifier(s) to use to pull signals from a
* StaticEventManager.
*
* @param null|string|int $identifier
* @param null|string|int|array|Traversable $identifiers
* @return void
*/
public function __construct($identifier = null)
public function __construct($identifiers = null)
{
$this->identifier = $identifier;
$this->setIdentifiers($identifiers);
}

/**
Expand Down Expand Up @@ -121,6 +121,48 @@ public function getStaticConnections()
return $this->staticConnections;
}

/**
* Get the identifier(s) for this EventManager
*
* @return array
*/
public function getIdentifiers()
{
return $this->identifiers;
}

/**
* Set the identifiers (overrides any currently set identifiers)
*
* @param string|int|array|Traversable $identifiers
* @return ModuleManager
*/
public function setIdentifiers($identifiers)
{
if (is_array($identifiers) || $identifiers instanceof \Traversable) {
$this->identifiers = array_unique((array) $identifiers);
} elseif ($identifiers !== null) {
$this->identifiers = array($identifiers);
}
return $this;
}

/**
* Add some identifier(s) (appends to any currently set identifiers)
*
* @param string|int|array|Traversable $identifiers
* @return ModuleManager
*/
public function addIdentifiers($identifiers)
{
if (is_array($identifiers) || $identifiers instanceof \Traversable) {
$this->identifiers = array_unique($this->identifiers + (array) $identifiers);
} elseif ($identifiers !== null) {
$this->identifiers = array_unique($this->identifiers + array($identifiers));
}
return $this;
}

/**
* Trigger all listeners for a given event
*
Expand Down Expand Up @@ -405,7 +447,7 @@ protected function getStaticListeners($event)
return array();
}

$identifiers = (array) $this->identifier;
$identifiers = $this->getIdentifiers();
$staticListeners = array();

foreach ($identifiers as $id) {
Expand Down
31 changes: 31 additions & 0 deletions test/EventManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,4 +461,35 @@ public function testWeakRefsAreHonoredWhenTriggering()
$message = $result->last();
$this->assertNull($message);
}

public function testDuplicateIdentifiersAreNotRegistered()
{
$events = new EventManager(array(__CLASS__, get_class($this)));
$identifiers = $events->getIdentifiers();
$this->assertSame(count($identifiers), 1);
$this->assertSame($identifiers[0], __CLASS__);
$events->addIdentifiers(__CLASS__);
$this->assertSame(count($identifiers), 1);
$this->assertSame($identifiers[0], __CLASS__);
}

public function testIdentifierGetterSettersWorkWithArrays()
{
$identifiers = array('foo', 'bar');
$this->assertInstanceOf('Zend\EventManager\EventManager', $this->events->setIdentifiers($identifiers));
$this->assertSame($this->events->getIdentifiers(), $identifiers);
$identifiers[] = 'baz';
$this->assertInstanceOf('Zend\EventManager\EventManager', $this->events->addIdentifiers($identifiers));
$this->assertSame($this->events->getIdentifiers(), $identifiers);
}

public function testIdentifierGetterSettersWorkWithTraversables()
{
$identifiers = new \ArrayIterator(array('foo', 'bar'));
$this->assertInstanceOf('Zend\EventManager\EventManager', $this->events->setIdentifiers($identifiers));
$this->assertSame($this->events->getIdentifiers(), (array) $identifiers);
$identifiers = new \ArrayIterator(array('foo', 'bar', 'baz'));
$this->assertInstanceOf('Zend\EventManager\EventManager', $this->events->addIdentifiers($identifiers));
$this->assertSame($this->events->getIdentifiers(), (array) $identifiers);
}
}

0 comments on commit a2d63b1

Please sign in to comment.