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

Commit

Permalink
[zendframework/zendframework#1] allow specifying priority to strategy…
Browse files Browse the repository at this point in the history
… listeners

- Allow accepting an optional $priority argument to attach on all view
  strategy listener aggregates
  • Loading branch information
weierophinney committed Feb 28, 2012
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 9 deletions.
12 changes: 9 additions & 3 deletions src/Strategy/FeedStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,16 @@ public function __construct(FeedRenderer $renderer)
* @param EventCollection $events
* @return void
*/
public function attach(EventCollection $events)
public function attach(EventCollection $events, $priority = null)
{
$this->listeners[] = $events->attach('renderer', array($this, 'selectRenderer'));
$this->listeners[] = $events->attach('response', array($this, 'injectResponse'));
if (null === $priority) {
$this->listeners[] = $events->attach('renderer', array($this, 'selectRenderer'));
$this->listeners[] = $events->attach('response', array($this, 'injectResponse'));
return;
}

$this->listeners[] = $events->attach('renderer', array($this, 'selectRenderer'), $priority);
$this->listeners[] = $events->attach('response', array($this, 'injectResponse'), $priority);
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/Strategy/JsonStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,16 @@ public function __construct(JsonRenderer $renderer)
* @param EventCollection $events
* @return void
*/
public function attach(EventCollection $events)
public function attach(EventCollection $events, $priority = null)
{
$this->listeners[] = $events->attach('renderer', array($this, 'selectRenderer'));
$this->listeners[] = $events->attach('response', array($this, 'injectResponse'));
if (null === $priority) {
$this->listeners[] = $events->attach('renderer', array($this, 'selectRenderer'));
$this->listeners[] = $events->attach('response', array($this, 'injectResponse'));
return;
}

$this->listeners[] = $events->attach('renderer', array($this, 'selectRenderer'), $priority);
$this->listeners[] = $events->attach('response', array($this, 'injectResponse'), $priority);
}

/**
Expand Down
12 changes: 9 additions & 3 deletions src/Strategy/PhpRendererStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,16 @@ public function getContentPlaceholders()
* @param EventCollection $events
* @return void
*/
public function attach(EventCollection $events)
public function attach(EventCollection $events, $priority = null)
{
$this->listeners[] = $events->attach('renderer', array($this, 'selectRenderer'));
$this->listeners[] = $events->attach('response', array($this, 'injectResponse'));
if (null === $priority) {
$this->listeners[] = $events->attach('renderer', array($this, 'selectRenderer'));
$this->listeners[] = $events->attach('response', array($this, 'injectResponse'));
return;
}

$this->listeners[] = $events->attach('renderer', array($this, 'selectRenderer'), $priority);
$this->listeners[] = $events->attach('response', array($this, 'injectResponse'), $priority);
}

/**
Expand Down
23 changes: 23 additions & 0 deletions test/Strategy/FeedStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,29 @@ public function testAttachesListenersAtExpectedPriorities()
}
}

public function testCanAttachListenersAtSpecifiedPriority()
{
$events = new EventManager();
$events->attachAggregate($this->strategy, 100);

foreach (array('renderer' => 'selectRenderer', 'response' => 'injectResponse') as $event => $method) {
$listeners = $events->getListeners($event);
$expectedCallback = array($this->strategy, $method);
$expectedPriority = 100;
$found = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
if ($listener->getMetadatum('priority') == $expectedPriority) {
$found = true;
break;
}
}
}
$this->assertTrue($found, 'Listener not found');
}
}

public function testDetachesListeners()
{
$events = new EventManager();
Expand Down
23 changes: 23 additions & 0 deletions test/Strategy/JsonStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,29 @@ public function testAttachesListenersAtExpectedPriorities()
$this->assertTrue($found, 'Listener not found');
}
}

public function testCanAttachListenersAtSpecifiedPriority()
{
$events = new EventManager();
$events->attachAggregate($this->strategy, 1000);

foreach (array('renderer' => 'selectRenderer', 'response' => 'injectResponse') as $event => $method) {
$listeners = $events->getListeners($event);
$expectedCallback = array($this->strategy, $method);
$expectedPriority = 1000;
$found = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
if ($listener->getMetadatum('priority') == $expectedPriority) {
$found = true;
break;
}
}
}
$this->assertTrue($found, 'Listener not found');
}
}

public function testDetachesListeners()
{
Expand Down
23 changes: 23 additions & 0 deletions test/Strategy/PhpRendererStrategyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,29 @@ public function testAttachesListenersAtExpectedPriorities()
$this->assertTrue($found, 'Listener not found');
}
}

public function testCanAttachListenersAtSpecifiedPriority()
{
$events = new EventManager();
$events->attachAggregate($this->strategy, 100);

foreach (array('renderer' => 'selectRenderer', 'response' => 'injectResponse') as $event => $method) {
$listeners = $events->getListeners($event);
$expectedCallback = array($this->strategy, $method);
$expectedPriority = 100;
$found = false;
foreach ($listeners as $listener) {
$callback = $listener->getCallback();
if ($callback === $expectedCallback) {
if ($listener->getMetadatum('priority') == $expectedPriority) {
$found = true;
break;
}
}
}
$this->assertTrue($found, 'Listener not found');
}
}

public function testDetachesListeners()
{
Expand Down

0 comments on commit 43dd724

Please sign in to comment.