|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Zend Framework (http://framework.zend.com/) |
| 4 | + * |
| 5 | + * @link http://github.com/zendframework/zf2 for the canonical source repository |
| 6 | + * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) |
| 7 | + * @license http://framework.zend.com/license/new-bsd New BSD License |
| 8 | + */ |
| 9 | + |
| 10 | +namespace ZendTest\Cache; |
| 11 | + |
| 12 | +use ReflectionProperty; |
| 13 | +use Zend\EventManager\EventManager; |
| 14 | + |
| 15 | +/** |
| 16 | + * Offer methods for introspecting event manager events and listeners. |
| 17 | + */ |
| 18 | +trait EventManagerIntrospectionTrait |
| 19 | +{ |
| 20 | + /** |
| 21 | + * Retrieve a list of event names from an event manager. |
| 22 | + * |
| 23 | + * @param EventManager $events |
| 24 | + * @return string[] |
| 25 | + */ |
| 26 | + private function getEventsFromEventManager(EventManager $events) |
| 27 | + { |
| 28 | + $r = new ReflectionProperty($events, 'events'); |
| 29 | + $r->setAccessible(true); |
| 30 | + $listeners = $r->getValue($events); |
| 31 | + return array_keys($listeners); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Retrieve an interable list of listeners for an event. |
| 36 | + * |
| 37 | + * Given an event and an event manager, returns an iterator with the |
| 38 | + * listeners for that event, in priority order. |
| 39 | + * |
| 40 | + * If $withPriority is true, the key values will be the priority at which |
| 41 | + * the given listener is attached. |
| 42 | + * |
| 43 | + * Do not pass $withPriority if you want to cast the iterator to an array, |
| 44 | + * as many listeners will likely have the same priority, and thus casting |
| 45 | + * will collapse to the last added. |
| 46 | + * |
| 47 | + * @param string $event |
| 48 | + * @param EventManager $events |
| 49 | + * @param bool $withPriority |
| 50 | + * @return \Traversable |
| 51 | + */ |
| 52 | + private function getListenersForEvent($event, EventManager $events, $withPriority = false) |
| 53 | + { |
| 54 | + $r = new ReflectionProperty($events, 'events'); |
| 55 | + $r->setAccessible(true); |
| 56 | + $listeners = $r->getValue($events); |
| 57 | + |
| 58 | + if (! isset($listeners[$event])) { |
| 59 | + return $this->traverseListeners([]); |
| 60 | + } |
| 61 | + |
| 62 | + return $this->traverseListeners($listeners[$event], $withPriority); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Assert that a given listener exists at the specified priority. |
| 67 | + * |
| 68 | + * @param callable $expectedListener |
| 69 | + * @param int $expectedPriority |
| 70 | + * @param string $event |
| 71 | + * @param EventManager $events |
| 72 | + * @param string $message Failure message to use, if any. |
| 73 | + */ |
| 74 | + private function assertListenerAtPriority( |
| 75 | + callable $expectedListener, |
| 76 | + $expectedPriority, |
| 77 | + $event, |
| 78 | + EventManager $events, |
| 79 | + $message = '' |
| 80 | + ) { |
| 81 | + $message = $message ?: sprintf( |
| 82 | + 'Listener not found for event "%s" and priority %d', |
| 83 | + $event, |
| 84 | + $expectedPriority |
| 85 | + ); |
| 86 | + $listeners = $this->getListenersForEvent($event, $events, true); |
| 87 | + $found = false; |
| 88 | + foreach ($listeners as $priority => $listener) { |
| 89 | + if ($listener === $expectedListener |
| 90 | + && $priority === $expectedPriority |
| 91 | + ) { |
| 92 | + $found = true; |
| 93 | + break; |
| 94 | + } |
| 95 | + } |
| 96 | + $this->assertTrue($found, $message); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns an indexed array of listeners for an event. |
| 101 | + * |
| 102 | + * Returns an indexed array of listeners for an event, in priority order. |
| 103 | + * Priority values will not be included; use this only for testing if |
| 104 | + * specific listeners are present, or for a count of listeners. |
| 105 | + * |
| 106 | + * @param string $event |
| 107 | + * @param EventManager $events |
| 108 | + * @return callable[] |
| 109 | + */ |
| 110 | + private function getArrayOfListenersForEvent($event, EventManager $events) |
| 111 | + { |
| 112 | + return iterator_to_array($this->getListenersForEvent($event, $events)); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Generator for traversing listeners in priority order. |
| 117 | + * |
| 118 | + * @param array $listeners |
| 119 | + * @param bool $withPriority When true, yields priority as key. |
| 120 | + */ |
| 121 | + public function traverseListeners(array $queue, $withPriority = false) |
| 122 | + { |
| 123 | + krsort($queue, SORT_NUMERIC); |
| 124 | + |
| 125 | + foreach ($queue as $priority => $listeners) { |
| 126 | + $priority = (int) $priority; |
| 127 | + foreach ($listeners as $listener) { |
| 128 | + if ($withPriority) { |
| 129 | + yield $priority => $listener; |
| 130 | + } else { |
| 131 | + yield $listener; |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments