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

Commit

Permalink
Merge branch 'deprecation/zendframework/zendframework#6613-deprecate-…
Browse files Browse the repository at this point in the history
…eventmanager-triggeruntil' into develop

Close zendframework/zendframework#6613
Close zendframework/zendframework#4122
  • Loading branch information
Ocramius committed Dec 5, 2014
168 parents b5c8751 + 38470d9 + 751bf7e + 330b204 + e52149f + 946aba8 + e68a45a + e3292c5 + 1dfee57 + 55843e2 + affa379 + a196779 + 3d3d017 + 6fa20e6 + b20a39d + 226510f + 6d779b2 + ae8a2d3 + b2b7d74 + d748fa1 + dab45d1 + cdb0e25 + 55ca933 + 58174bc + 1804746 + 67c0075 + e5983dd + a8b3425 + a2a193a + 33d1c27 + 1d5cfa1 + 460ae0b + c577f06 + db33b2e + fea371a + c93bbae + a4ff530 + cbd4621 + 4b4dffb + 00380d2 + 3895f9f + 6349844 + b6905a7 + b7735a0 + 8312aef + 7be4333 + 8625190 + 4dd38f5 + e02bd81 + 87c797f + 73208bf + 0272cd1 + 1fcbea4 + 197a891 + 5859e98 + d34ef1c + e06dc2f + 4907ff6 + fd0e0ba + 891fbf7 + 83d5b01 + 4579f15 + c86ed1e + 16b8f20 + 83086b6 + a81c8a5 + a85d989 + 55ef06c + a2d63b1 + 8210d1d + 0a08fc0 + 7d37340 + ab0ab2a + b9e0b9b + e1d4bda + e251276 + 161ba87 + f10c77c + 58cc6ef + cecfbb0 + c165fb8 + 1febb29 + 3325e48 + 8b68fab + 43c1b37 + ec52465 + 2904103 + e4d7a5a + be7b13f + f8dd43d + b1b3322 + 93ccc75 + 2d494af + 25e9a69 + 3f70aa8 + 598d6ed + 8161582 + c01dc55 + 98634fe + 0c55546 + 1c83b8f + 815d8cc + 19c90c8 + 5dbf1b9 + 4a927fd + 4a648ec + d0636ac + 840f139 + aafb3ee + 8600965 + b26220b + 860ba0d + 759cd42 + eebf7bc + 6546e0d + 11d36d3 + 4a1db1e + 04eebff + 892dcff + b878b24 + 2db85a1 + 97242e6 + 9ee6b4d + 13fabed + 8fe32d7 + 035474f + 918b5da + 8d65fb0 + 1f16b54 + 282eead + cb64f10 + 491c672 + e10e997 + 93858ca + d41e72e + 9af5add + 309bc00 + 984be73 + be29716 + 41232e8 + 1b6d07e + 8cd4674 + ee1d485 + e5aa383 + fc3991f + ba6e7b6 + 90ecccb + b1db52f + ef24a14 + ae89a5d + 2963b07 + 5174cf5 + ce1dffc + 0c36811 + 486ca02 + f995ad2 + c4edf83 + f7ed875 + 372eb58 + c450aa4 + b5dc21d + 4c23096 + eaee668 + acb0cc0 + 39b294f + 2e55551 + 0bb600c + a846d4b commit 9d357f9
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 53 deletions.
48 changes: 12 additions & 36 deletions src/EventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,10 @@ public function addIdentifiers($identifiers)
/**
* Trigger all listeners for a given event
*
* Can emulate triggerUntil() if the last argument provided is a callback.
*
* @param string $event
* @param string|object $target Object calling emit, or symbol describing target (such as static method name)
* @param array|ArrayAccess $argv Array of arguments; typically, should be associative
* @param null|callable $callback
* @param string $event
* @param string|object $target Object calling emit, or symbol describing target (such as static method name)
* @param array|ArrayAccess $argv Array of arguments; typically, should be associative
* @param null|callable $callback Trigger listeners until return value of this callback evaluate to true
* @return ResponseCollection All listener return values
* @throws Exception\InvalidCallbackException
*/
Expand Down Expand Up @@ -219,37 +217,16 @@ public function trigger($event, $target = null, $argv = array(), $callback = nul
* @param array|ArrayAccess $argv Array of arguments; typically, should be associative
* @param callable $callback
* @return ResponseCollection
* @deprecated Please use trigger()
* @throws Exception\InvalidCallbackException if invalid callable provided
*/
public function triggerUntil($event, $target, $argv = null, $callback = null)
{
if ($event instanceof EventInterface) {
$e = $event;
$event = $e->getName();
$callback = $target;
} elseif ($target instanceof EventInterface) {
$e = $target;
$e->setName($event);
$callback = $argv;
} elseif ($argv instanceof EventInterface) {
$e = $argv;
$e->setName($event);
$e->setTarget($target);
} else {
$e = new $this->eventClass();
$e->setName($event);
$e->setTarget($target);
$e->setParams($argv);
}

if (!is_callable($callback)) {
throw new Exception\InvalidCallbackException('Invalid callback provided');
}

// Initial value of stop propagation flag should be false
$e->stopPropagation(false);

return $this->triggerListeners($event, $e, $callback);
trigger_error(
'This method is deprecated and will be removed in the future. Please use trigger() instead.',
E_USER_DEPRECATED
);
return $this->trigger($event, $target, $argv, $callback);
}

/**
Expand Down Expand Up @@ -416,7 +393,7 @@ public function clearListeners($event)
*
* Use this method if you want to be able to modify arguments from within a
* listener. It returns an ArrayObject of the arguments, which may then be
* passed to trigger() or triggerUntil().
* passed to trigger().
*
* @param array $args
* @return ArrayObject
Expand All @@ -429,8 +406,7 @@ public function prepareArgs(array $args)
/**
* Trigger listeners
*
* Actual functionality for triggering listeners, to which both trigger() and triggerUntil()
* delegate.
* Actual functionality for triggering listeners, to which trigger() delegate.
*
* @param string $event Event name
* @param EventInterface $e
Expand Down
4 changes: 2 additions & 2 deletions src/EventManagerInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ interface EventManagerInterface extends SharedEventManagerAwareInterface
* - Passing event name and Event object only
* - Passing event name, target, and Event object
* - Passing event name, target, and array|ArrayAccess of arguments
*
* Can emulate triggerUntil() if the last argument provided is a callback.
* - Passing event name, target, array|ArrayAccess of arguments, and callback
*
* @param string $event
* @param object|string $target
Expand All @@ -50,6 +49,7 @@ public function trigger($event, $target = null, $argv = array(), $callback = nul
* @param array|object $argv
* @param callable $callback
* @return ResponseCollection
* @deprecated Please use trigger()
*/
public function triggerUntil($event, $target, $argv = null, $callback = null);

Expand Down
16 changes: 11 additions & 5 deletions src/GlobalEventManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ public static function getEventCollection()
/**
* Trigger an event
*
* @param string $event
* @param string $event
* @param object|string $context
* @param array|object $argv
* @param array|object $argv
* @param null|callable $callback
* @return ResponseCollection
*/
public static function trigger($event, $context, $argv = array())
public static function trigger($event, $context, $argv = array(), $callback = null)
{
return static::getEventCollection()->trigger($event, $context, $argv);
return static::getEventCollection()->trigger($event, $context, $argv, $callback);
}

/**
Expand All @@ -71,10 +72,15 @@ public static function trigger($event, $context, $argv = array())
* @param array|object $argv
* @param callable $callback
* @return ResponseCollection
* @deprecated Please use trigger()
*/
public static function triggerUntil($event, $context, $argv, $callback)
{
return static::getEventCollection()->triggerUntil($event, $context, $argv, $callback);
trigger_error(
'This method is deprecated and will be removed in the future. Please use trigger() instead.',
E_USER_DEPRECATED
);
return static::trigger($event, $context, $argv, $callback);
}

/**
Expand Down
29 changes: 21 additions & 8 deletions test/EventManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function testTriggerUntilShouldReturnAsSoonAsCallbackReturnsTrue()
$search = $e->getParam('search', '?');
return strstr($string, $search);
});
$responses = $this->events->triggerUntil(
$responses = $this->events->trigger(
'foo.bar',
$this,
array('string' => 'foo', 'search' => 'f'),
Expand Down Expand Up @@ -205,7 +205,7 @@ public function testTriggerUntilShouldMarkResponseCollectionStoppedWhenCondition
$this->events->attach('foo.bar', function () { return 'nada'; }, 3);
$this->events->attach('foo.bar', function () { return 'found'; }, 2);
$this->events->attach('foo.bar', function () { return 'zero'; }, 1);
$responses = $this->events->triggerUntil('foo.bar', $this, array(), function ($result) {
$responses = $this->events->trigger('foo.bar', $this, array(), function ($result) {
return ($result === 'found');
});
$this->assertTrue($responses instanceof ResponseCollection);
Expand All @@ -221,7 +221,7 @@ public function testTriggerUntilShouldMarkResponseCollectionStoppedWhenCondition
$this->events->attach('foo.bar', function () { return 'nada'; });
$this->events->attach('foo.bar', function () { return 'zero'; });
$this->events->attach('foo.bar', function () { return 'found'; });
$responses = $this->events->triggerUntil('foo.bar', $this, array(), function ($result) {
$responses = $this->events->trigger('foo.bar', $this, array(), function ($result) {
return ($result === 'found');
});
$this->assertTrue($responses instanceof ResponseCollection);
Expand All @@ -235,7 +235,7 @@ public function testResponseCollectionIsNotStoppedWhenNoCallbackMatchedByTrigger
$this->events->attach('foo.bar', function () { return 'nada'; }, 3);
$this->events->attach('foo.bar', function () { return 'found'; }, 2);
$this->events->attach('foo.bar', function () { return 'zero'; }, 1);
$responses = $this->events->triggerUntil('foo.bar', $this, array(), function ($result) {
$responses = $this->events->trigger('foo.bar', $this, array(), function ($result) {
return ($result === 'never found');
});
$this->assertTrue($responses instanceof ResponseCollection);
Expand Down Expand Up @@ -465,7 +465,7 @@ public function testCanPassEventObjectAndCallbackAsSoleArgumentsToTriggerUntil()
$this->events->attach(__FUNCTION__, function ($e) {
return $e;
});
$responses = $this->events->triggerUntil($event, function ($r) {
$responses = $this->events->trigger($event, function ($r) {
return ($r instanceof EventInterface);
});
$this->assertTrue($responses->stopped());
Expand All @@ -480,7 +480,7 @@ public function testCanPassEventNameAndEventObjectAndCallbackAsSoleArgumentsToTr
$this->events->attach(__FUNCTION__, function ($e) {
return $e;
});
$responses = $this->events->triggerUntil(__FUNCTION__, $event, function ($r) {
$responses = $this->events->trigger(__FUNCTION__, $event, function ($r) {
return ($r instanceof EventInterface);
});
$this->assertTrue($responses->stopped());
Expand All @@ -495,7 +495,7 @@ public function testCanPassEventObjectAsArgvToTriggerUntil()
$this->events->attach(__FUNCTION__, function ($e) {
return $e;
});
$responses = $this->events->triggerUntil(__FUNCTION__, $this, $event, function ($r) {
$responses = $this->events->trigger(__FUNCTION__, $this, $event, function ($r) {
return ($r instanceof EventInterface);
});
$this->assertTrue($responses->stopped());
Expand Down Expand Up @@ -665,9 +665,22 @@ public function testTriggerUntilSetsStopPropagationFlagToFalse()
};
$event = new Event();
$event->stopPropagation(true);
$this->events->triggerUntil('foo', $event, $criteria);
$this->events->trigger('foo', $event, $criteria);

$this->assertFalse($marker->propagationIsStopped);
$this->assertFalse($event->propagationIsStopped());
}

public function testTriggerUntilDeprecated()
{
$deprecated = null;
set_error_handler(function () use (&$deprecated) {
$deprecated = true;
}, E_USER_DEPRECATED);

$this->events->triggerUntil('foo.bar', $this, array('foo' => 'bar'), function () {});
restore_error_handler();

$this->assertTrue($deprecated, 'EventManager::triggerUntil not marked as E_USER_DEPRECATED');
}
}
18 changes: 16 additions & 2 deletions test/GlobalEventManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function testPassingNullValueForEventCollectionResetsInstance()

public function testProxiesAllStaticOperationsToEventCollectionInstance()
{
$test = new \stdClass();
$test = new \stdClass();
$listener = GlobalEventManager::attach('foo.bar', function ($e) use ($test) {
$test->event = $e->getName();
$test->target = $e->getTarget();
Expand All @@ -54,9 +54,10 @@ public function testProxiesAllStaticOperationsToEventCollectionInstance()
$this->assertEquals('foo.bar', $test->event);
$this->assertEquals(array('foo' => 'bar'), $test->params);

$results = GlobalEventManager::triggerUntil('foo.bar', $this, array('baz' => 'bat'), function ($r) {
$results = GlobalEventManager::trigger('foo.bar', $this, array('baz' => 'bat'), function ($r) {
return is_array($r);
});

$this->assertTrue($results->stopped());
$this->assertEquals(array('baz' => 'bat'), $test->params);
$this->assertEquals(array('baz' => 'bat'), $results->last());
Expand All @@ -83,4 +84,17 @@ public function testProxiesAllStaticOperationsToEventCollectionInstance()
$events = GlobalEventManager::getEvents();
$this->assertEquals(array(), $events);
}

public function testTriggerUntilDeprecated()
{
$deprecated = null;
set_error_handler(function () use (&$deprecated) {
$deprecated = true;
}, E_USER_DEPRECATED);

GlobalEventManager::triggerUntil('foo.bar', $this, array('foo' => 'bar'), function () {});
restore_error_handler();

$this->assertTrue($deprecated, 'GlobalEventManager::triggerUntil not marked as E_USER_DEPRECATED');
}
}

0 comments on commit 9d357f9

Please sign in to comment.