1
0
mirror of https://github.com/guzzle/guzzle.git synced 2025-02-13 03:45:22 +01:00

[Common] [Service] Adding a method to the event manager to get the priority level that an observer was attached at. Attaching observers to requests created by client in the same order that the observers were attached to the client

This commit is contained in:
Michael Dowling 2011-03-24 11:20:38 -05:00
parent 620e13057c
commit b15cf68883
3 changed files with 33 additions and 7 deletions

View File

@ -68,10 +68,7 @@ class EventManager
$hash = spl_object_hash($observer);
$this->observers[] = $observer;
if ($priority) {
$this->priorities[$hash] = $priority;
}
$this->priorities[$hash] = $priority;
$priorities = $this->priorities;
// Sort the events by priority
@ -111,12 +108,14 @@ class EventManager
{
if ($this->observers === array($observer)) {
$this->observers = array();
$this->priorities = array();
} else {
if (count($this->observers)) {
foreach ($this->observers as $i => $o) {
if ($o === $observer) {
// Notify the observer that it is being detached
$this->notifyObserver($observer, 'event.detach');
unset($this->priorities[spl_object_hash($observer)]);
unset($this->observers[$i]);
}
}
@ -242,4 +241,23 @@ class EventManager
return $observer($this->subject, $event, $context);
}
}
/**
* Get the priority level that an observer was attached at
*
* @param object $observer Observer to get the priority level of
*
* @return int|null Returns the priortity level or NULl if not attached
*/
public function getPriority($observer)
{
if (is_object($observer)) {
$hash = spl_object_hash($observer);
if (array_key_exists($hash, $this->priorities)) {
return $this->priorities[$hash];
}
}
return null;
}
}

View File

@ -229,11 +229,13 @@ class Client extends AbstractSubject
}
// Attach client observers to the request
foreach ($this->getEventManager()->getAttached() as $observer) {
$request->getEventManager()->attach($observer);
$reqManager = $request->getEventManager();
$manager = $this->getEventManager();
foreach ($manager->getAttached() as $observer) {
$reqManager->attach($observer, $manager->getPriority($observer));
}
$this->getEventManager()->notify('request.create', $request);
$manager->notify('request.create', $request);
return $request;
}

View File

@ -22,6 +22,7 @@ class EventManagerTest extends \Guzzle\Tests\GuzzleTestCase implements Observer
* @covers Guzzle\Common\Event\EventManager::getAttached
* @covers Guzzle\Common\Event\EventManager::__construct
* @covers Guzzle\Common\Event\EventManager::getSubject
* @covers Guzzle\Common\Event\EventManager::getPriority
*/
public function testAttachesObservers()
{
@ -42,6 +43,11 @@ class EventManagerTest extends \Guzzle\Tests\GuzzleTestCase implements Observer
$this->assertType('Closure', $closure);
$this->assertEquals(array($observer, $closure), $subject->getAttached());
$this->assertEquals(0, $subject->getPriority($observer));
$this->assertEquals(-10, $subject->getPriority($closure));
$this->assertNull($subject->getPriority(new \stdClass()));
$this->assertNull($subject->getPriority('abc'));
}
/**