This repository has been archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
[WIP] Refactor with new service and event manager #22
Merged
weierophinney
merged 2 commits into
zendframework:develop
from
ezimuel:feature/refactor-service-event-manager
Oct 29, 2015
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev
Previous commit
First code refactor for new zend service and event manager
- Loading branch information
commit cec6ebbd420e0455fc0aeaacf91935a96a5f351b
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
<?php | ||
/** | ||
* Zend Framework (http://framework.zend.com/) | ||
* | ||
* @link http://github.com/zendframework/zf2 for the canonical source repository | ||
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
*/ | ||
|
||
namespace ZendTest\Cache; | ||
|
||
use ReflectionProperty; | ||
use Zend\EventManager\EventManager; | ||
|
||
/** | ||
* Offer methods for introspecting event manager events and listeners. | ||
*/ | ||
trait EventManagerIntrospectionTrait | ||
{ | ||
/** | ||
* Retrieve a list of event names from an event manager. | ||
* | ||
* @param EventManager $events | ||
* @return string[] | ||
*/ | ||
private function getEventsFromEventManager(EventManager $events) | ||
{ | ||
$r = new ReflectionProperty($events, 'events'); | ||
$r->setAccessible(true); | ||
$listeners = $r->getValue($events); | ||
return array_keys($listeners); | ||
} | ||
|
||
/** | ||
* Retrieve an interable list of listeners for an event. | ||
* | ||
* Given an event and an event manager, returns an iterator with the | ||
* listeners for that event, in priority order. | ||
* | ||
* If $withPriority is true, the key values will be the priority at which | ||
* the given listener is attached. | ||
* | ||
* Do not pass $withPriority if you want to cast the iterator to an array, | ||
* as many listeners will likely have the same priority, and thus casting | ||
* will collapse to the last added. | ||
* | ||
* @param string $event | ||
* @param EventManager $events | ||
* @param bool $withPriority | ||
* @return \Traversable | ||
*/ | ||
private function getListenersForEvent($event, EventManager $events, $withPriority = false) | ||
{ | ||
$r = new ReflectionProperty($events, 'events'); | ||
$r->setAccessible(true); | ||
$listeners = $r->getValue($events); | ||
|
||
if (! isset($listeners[$event])) { | ||
return $this->traverseListeners([]); | ||
} | ||
|
||
return $this->traverseListeners($listeners[$event], $withPriority); | ||
} | ||
|
||
/** | ||
* Assert that a given listener exists at the specified priority. | ||
* | ||
* @param callable $expectedListener | ||
* @param int $expectedPriority | ||
* @param string $event | ||
* @param EventManager $events | ||
* @param string $message Failure message to use, if any. | ||
*/ | ||
private function assertListenerAtPriority( | ||
callable $expectedListener, | ||
$expectedPriority, | ||
$event, | ||
EventManager $events, | ||
$message = '' | ||
) { | ||
$message = $message ?: sprintf( | ||
'Listener not found for event "%s" and priority %d', | ||
$event, | ||
$expectedPriority | ||
); | ||
$listeners = $this->getListenersForEvent($event, $events, true); | ||
$found = false; | ||
foreach ($listeners as $priority => $listener) { | ||
if ($listener === $expectedListener | ||
&& $priority === $expectedPriority | ||
) { | ||
$found = true; | ||
break; | ||
} | ||
} | ||
$this->assertTrue($found, $message); | ||
} | ||
|
||
/** | ||
* Returns an indexed array of listeners for an event. | ||
* | ||
* Returns an indexed array of listeners for an event, in priority order. | ||
* Priority values will not be included; use this only for testing if | ||
* specific listeners are present, or for a count of listeners. | ||
* | ||
* @param string $event | ||
* @param EventManager $events | ||
* @return callable[] | ||
*/ | ||
private function getArrayOfListenersForEvent($event, EventManager $events) | ||
{ | ||
return iterator_to_array($this->getListenersForEvent($event, $events)); | ||
} | ||
|
||
/** | ||
* Generator for traversing listeners in priority order. | ||
* | ||
* @param array $listeners | ||
* @param bool $withPriority When true, yields priority as key. | ||
*/ | ||
public function traverseListeners(array $queue, $withPriority = false) | ||
{ | ||
krsort($queue, SORT_NUMERIC); | ||
|
||
foreach ($queue as $priority => $listeners) { | ||
$priority = (int) $priority; | ||
foreach ($listeners as $listener) { | ||
if ($withPriority) { | ||
yield $priority => $listener; | ||
} else { | ||
yield $listener; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ugh - found another slight BC break in SM v3: this becomes
$sharedByDefault
(noteshared
vsshare
). That change fixes a few errors.