diff --git a/src/Strategy/FeedStrategy.php b/src/Strategy/FeedStrategy.php index 3ceb96b8..c4c87237 100644 --- a/src/Strategy/FeedStrategy.php +++ b/src/Strategy/FeedStrategy.php @@ -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); } /** diff --git a/src/Strategy/JsonStrategy.php b/src/Strategy/JsonStrategy.php index aa10d596..4c17e7ef 100644 --- a/src/Strategy/JsonStrategy.php +++ b/src/Strategy/JsonStrategy.php @@ -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); } /** diff --git a/src/Strategy/PhpRendererStrategy.php b/src/Strategy/PhpRendererStrategy.php index 0c957d5a..539bfc44 100644 --- a/src/Strategy/PhpRendererStrategy.php +++ b/src/Strategy/PhpRendererStrategy.php @@ -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); } /** diff --git a/test/Strategy/FeedStrategyTest.php b/test/Strategy/FeedStrategyTest.php index 1383fffe..8c892d14 100644 --- a/test/Strategy/FeedStrategyTest.php +++ b/test/Strategy/FeedStrategyTest.php @@ -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(); diff --git a/test/Strategy/JsonStrategyTest.php b/test/Strategy/JsonStrategyTest.php index a4c890b8..661f5d47 100644 --- a/test/Strategy/JsonStrategyTest.php +++ b/test/Strategy/JsonStrategyTest.php @@ -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() { diff --git a/test/Strategy/PhpRendererStrategyTest.php b/test/Strategy/PhpRendererStrategyTest.php index 54632a59..047fe2f6 100644 --- a/test/Strategy/PhpRendererStrategyTest.php +++ b/test/Strategy/PhpRendererStrategyTest.php @@ -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() {