mirror of
https://github.com/yiisoft/yii2.git
synced 2025-10-30 10:08:08 +08:00
Fix #19108: Optimize Component::hasEventHandlers() and Component::trigger()
This commit is contained in:
@ -465,13 +465,17 @@ class Component extends BaseObject
|
||||
{
|
||||
$this->ensureBehaviors();
|
||||
|
||||
if (!empty($this->_events[$name])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
foreach ($this->_eventWildcards as $wildcard => $handlers) {
|
||||
if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return !empty($this->_events[$name]) || Event::hasHandlers($this, $name);
|
||||
return Event::hasHandlers($this, $name);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -594,10 +598,12 @@ class Component extends BaseObject
|
||||
|
||||
/**
|
||||
* Triggers an event.
|
||||
* This method represents the happening of an event. It invokes
|
||||
* all attached handlers for the event including class-level handlers.
|
||||
*
|
||||
* This method represents the happening of an event. It invokes all attached handlers for the event
|
||||
* including class-level handlers.
|
||||
*
|
||||
* @param string $name the event name
|
||||
* @param Event $event the event parameter. If not set, a default [[Event]] object will be created.
|
||||
* @param Event|null $event the event instance. If not set, a default [[Event]] object will be created.
|
||||
*/
|
||||
public function trigger($name, Event $event = null)
|
||||
{
|
||||
@ -606,15 +612,15 @@ class Component extends BaseObject
|
||||
$eventHandlers = [];
|
||||
foreach ($this->_eventWildcards as $wildcard => $handlers) {
|
||||
if (StringHelper::matchWildcard($wildcard, $name)) {
|
||||
$eventHandlers = array_merge($eventHandlers, $handlers);
|
||||
$eventHandlers[] = $handlers;
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($this->_events[$name])) {
|
||||
$eventHandlers = array_merge($eventHandlers, $this->_events[$name]);
|
||||
$eventHandlers[] = $this->_events[$name];
|
||||
}
|
||||
|
||||
if (!empty($eventHandlers)) {
|
||||
$eventHandlers = call_user_func_array('array_merge', $eventHandlers);
|
||||
if ($event === null) {
|
||||
$event = new Event();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user