Skip to content

Commit

Permalink
condition optimized
Browse files Browse the repository at this point in the history
  • Loading branch information
klimov-paul committed Dec 23, 2017
1 parent 75d962a commit 921310f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 13 deletions.
14 changes: 8 additions & 6 deletions framework/base/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -465,7 +465,7 @@ public function hasEventHandlers($name)
$this->ensureBehaviors();

foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (StringHelper::matchWildcard($wildcard, $name) && !empty($handlers)) {
if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
return true;
}
}
Expand Down Expand Up @@ -582,6 +582,10 @@ public function off($name, $handler = null)
}
if ($removed) {
$this->_eventWildcards[$name] = array_values($this->_eventWildcards[$name]);
// remove empty wildcards to save future redundant regex checks :
if (empty($this->_eventWildcards[$name])) {
unset($this->_eventWildcards[$name]);
}
}

return $removed;
Expand All @@ -599,11 +603,9 @@ public function trigger($name, Event $event = null)
$this->ensureBehaviors();

$eventHandlers = [];
if (!empty($this->_eventWildcards)) {
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (StringHelper::matchWildcard($wildcard, $name)) {
$eventHandlers = array_merge($eventHandlers, $handlers);
}
foreach ($this->_eventWildcards as $wildcard => $handlers) {
if (StringHelper::matchWildcard($wildcard, $name)) {
$eventHandlers = array_merge($eventHandlers, $handlers);
}
}

Expand Down
20 changes: 13 additions & 7 deletions framework/base/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

namespace yii\base;

use yii\helpers\StringHelper;

/**
Expand Down Expand Up @@ -171,6 +172,13 @@ public static function off($class, $name, $handler = null)
}
if ($removed) {
self::$_eventWildcards[$name][$class] = array_values(self::$_eventWildcards[$name][$class]);
// remove empty wildcards to save future redundant regex checks :
if (empty(self::$_eventWildcards[$name][$class])) {
unset(self::$_eventWildcards[$name][$class]);
if (empty(self::$_eventWildcards[$name])) {
unset(self::$_eventWildcards[$name]);
}
}
}

return $removed;
Expand Down Expand Up @@ -198,7 +206,7 @@ public static function offAll()
*/
public static function hasHandlers($class, $name)
{
if (empty(self::$_events[$name]) && empty(self::$_eventWildcards)) {
if (empty(self::$_eventWildcards) && empty(self::$_events[$name])) {
return false;
}

Expand Down Expand Up @@ -252,13 +260,11 @@ class_implements($class, true)
public static function trigger($class, $name, $event = null)
{
$wildcardEventHandlers = [];
if (!empty(self::$_eventWildcards)) {
foreach (self::$_eventWildcards as $nameWildcard => $classHandlers) {
if (!StringHelper::matchWildcard($nameWildcard, $name)) {
continue;
}
$wildcardEventHandlers = array_merge($wildcardEventHandlers, $classHandlers);
foreach (self::$_eventWildcards as $nameWildcard => $classHandlers) {
if (!StringHelper::matchWildcard($nameWildcard, $name)) {
continue;
}
$wildcardEventHandlers = array_merge($wildcardEventHandlers, $classHandlers);
}

if (empty(self::$_events[$name]) && empty($wildcardEventHandlers)) {
Expand Down

0 comments on commit 921310f

Please sign in to comment.