mirror of
				https://github.com/yiisoft/yii2.git
				synced 2025-10-31 18:47:33 +08:00 
			
		
		
		
	Fix #19108: Optimize Component::hasEventHandlers() and Component::trigger()
				
					
				
			This commit is contained in:
		| @ -35,6 +35,7 @@ Yii Framework 2 Change Log | |||||||
| - Bug #19031: Fix displaying console help for parameters with declared types (WinterSilence) | - Bug #19031: Fix displaying console help for parameters with declared types (WinterSilence) | ||||||
| - Bug #19030: Add DI container usage to `yii\base\Widget::end()` (papppeter) | - Bug #19030: Add DI container usage to `yii\base\Widget::end()` (papppeter) | ||||||
| - Bug #19096: Fix `Request::getIsConsoleRequest()` may return erroneously when testing a Web application in Codeception (WinterSilence) | - Bug #19096: Fix `Request::getIsConsoleRequest()` may return erroneously when testing a Web application in Codeception (WinterSilence) | ||||||
|  | - Enh #19108: Optimize `Component::hasEventHandlers()` and `Component::trigger()` (WinterSilence) | ||||||
|  |  | ||||||
|  |  | ||||||
| 2.0.43 August 09, 2021 | 2.0.43 August 09, 2021 | ||||||
|  | |||||||
| @ -465,13 +465,17 @@ class Component extends BaseObject | |||||||
|     { |     { | ||||||
|         $this->ensureBehaviors(); |         $this->ensureBehaviors(); | ||||||
|  |  | ||||||
|  |         if (!empty($this->_events[$name])) { | ||||||
|  |             return true; | ||||||
|  |         } | ||||||
|  |  | ||||||
|         foreach ($this->_eventWildcards as $wildcard => $handlers) { |         foreach ($this->_eventWildcards as $wildcard => $handlers) { | ||||||
|             if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) { |             if (!empty($handlers) && StringHelper::matchWildcard($wildcard, $name)) { | ||||||
|                 return true; |                 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. |      * 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 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) |     public function trigger($name, Event $event = null) | ||||||
|     { |     { | ||||||
| @ -606,15 +612,15 @@ class Component extends BaseObject | |||||||
|         $eventHandlers = []; |         $eventHandlers = []; | ||||||
|         foreach ($this->_eventWildcards as $wildcard => $handlers) { |         foreach ($this->_eventWildcards as $wildcard => $handlers) { | ||||||
|             if (StringHelper::matchWildcard($wildcard, $name)) { |             if (StringHelper::matchWildcard($wildcard, $name)) { | ||||||
|                 $eventHandlers = array_merge($eventHandlers, $handlers); |                 $eventHandlers[] = $handlers; | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (!empty($this->_events[$name])) { |         if (!empty($this->_events[$name])) { | ||||||
|             $eventHandlers = array_merge($eventHandlers, $this->_events[$name]); |             $eventHandlers[] = $this->_events[$name]; | ||||||
|         } |         } | ||||||
|  |  | ||||||
|         if (!empty($eventHandlers)) { |         if (!empty($eventHandlers)) { | ||||||
|  |             $eventHandlers = call_user_func_array('array_merge', $eventHandlers); | ||||||
|             if ($event === null) { |             if ($event === null) { | ||||||
|                 $event = new Event(); |                 $event = new Event(); | ||||||
|             } |             } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Anton
					Anton