Fixes #16377: Fixed yii\base\Event:off() undefined index error when event handler does not match

This commit is contained in:
Razvan Grigore
2018-06-12 08:50:43 +02:00
committed by Alexander Makarov
parent 858d3531ed
commit ca3c8da503
3 changed files with 23 additions and 12 deletions

View File

@ -34,6 +34,7 @@ Yii Framework 2 Change Log
- Bug #16301: Fixed `yii\web\User::setIdentity()` to clear access check cache while setting identity object to `null` (Izumi-kun) - Bug #16301: Fixed `yii\web\User::setIdentity()` to clear access check cache while setting identity object to `null` (Izumi-kun)
- Bug #16322: Fixed strings were not were not compared using timing attack resistant approach while CSRF token validation (samdark, Felix Wiedemann) - Bug #16322: Fixed strings were not were not compared using timing attack resistant approach while CSRF token validation (samdark, Felix Wiedemann)
- Chg #16192: `yii\db\Command::logQuery()` is now protected (drlibra) - Chg #16192: `yii\db\Command::logQuery()` is now protected (drlibra)
- Bug #16377: Fixed `yii\base\Event:off()` undefined index error when event handler does not match (razvanphp)
2.0.15.1 March 21, 2018 2.0.15.1 March 21, 2018
----------------------- -----------------------

View File

@ -164,19 +164,21 @@ class Event extends BaseObject
// wildcard event names // wildcard event names
$removed = false; $removed = false;
foreach (self::$_eventWildcards[$name][$class] as $i => $event) { if (isset(self::$_eventWildcards[$name][$class])) {
if ($event[0] === $handler) { foreach (self::$_eventWildcards[$name][$class] as $i => $event) {
unset(self::$_eventWildcards[$name][$class][$i]); if ($event[0] === $handler) {
$removed = true; unset(self::$_eventWildcards[$name][$class][$i]);
$removed = true;
}
} }
} if ($removed) {
if ($removed) { self::$_eventWildcards[$name][$class] = array_values(self::$_eventWildcards[$name][$class]);
self::$_eventWildcards[$name][$class] = array_values(self::$_eventWildcards[$name][$class]); // remove empty wildcards to save future redundant regex checks :
// remove empty wildcards to save future redundant regex checks : if (empty(self::$_eventWildcards[$name][$class])) {
if (empty(self::$_eventWildcards[$name][$class])) { unset(self::$_eventWildcards[$name][$class]);
unset(self::$_eventWildcards[$name][$class]); if (empty(self::$_eventWildcards[$name])) {
if (empty(self::$_eventWildcards[$name])) { unset(self::$_eventWildcards[$name]);
unset(self::$_eventWildcards[$name]); }
} }
} }
} }

View File

@ -91,6 +91,14 @@ class EventTest extends TestCase
$this->assertTrue(Event::hasHandlers('yiiunit\framework\base\SomeInterface', SomeInterface::EVENT_SUPER_EVENT)); $this->assertTrue(Event::hasHandlers('yiiunit\framework\base\SomeInterface', SomeInterface::EVENT_SUPER_EVENT));
} }
public function testOffUnmatchedHandler()
{
$this->assertFalse(Event::hasHandlers(Post::className(), 'afterSave'));
Event::on(Post::className(), 'afterSave', [$this, 'bla-bla']);
$this->assertFalse(Event::off(Post::className(), 'afterSave', [$this, 'bla-bla-bla']));
$this->assertTrue(Event::off(Post::className(), 'afterSave', [$this, 'bla-bla']));
}
/** /**
* @depends testOn * @depends testOn
* @depends testHasHandlers * @depends testHasHandlers