Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the EventDispatcher event distribution component like

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In view of what the EventDispatcher event distribution component is, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

Consider that now you want to provide a plug-in system for your project, where plug-ins can add methods or do something before or after some methods are executed without interfering with other plug-ins. To implement this system, simple single inheritance is not a good way, even if multiple inheritance is possible in PHP, it also has inherent shortcomings.

Symfony EventDispatcher implements the mediator pattern in a simple and efficient way, and the event dispatcher is the intermediary that makes the system and plug-ins not coupled, which makes the above plug-in system possible, and it will make your project more scalable.

System analysis

Event storage

The above diagram is derived from an analysis of the source code of Symfony EventDispatcher components, and you can see how events are stored in the system.

The event is stored twice to add the concept of priority priority. When it is stored, it is put into the structure above, and when it is taken out, it is taken out from the structure at the bottom of the figure. The same event name can be given different priority. The higher the priority is, the higher the event priority is. When the priority is the same, the event inserted first is triggered first.

Sorted events (the structure below above) are not built when events are inserted, but sequenced events are generated when events are taken out, and when a new event is inserted or deleted from the same event name, the corresponding sorted event name is deleted and rebuilt later when used.

When the event is executed, it gets the sorted linster list of the corresponding event names and executes them in order.

Event execution

As shown in the figure above, when triggering a certain time, if multiple trigger actions are monitored under the event name, they will be triggered in the order of priority and registration. The trigger action is generally an executable "instance" (whether it is a class or a function, it must be called through call_user_func_array), and three parameters can be passed in. The first parameter (must) is an Event instance. The second is the name of the triggered event, and the third is the instance of the event dispatcher. The first parameter controls whether the event continues to be passed between all triggered actions under the event name. For example, Event.propagationStopped is set to true in the above linstener_2. After the linstener_2 is executed, the event will stop propagating, and the subsequent actions of the linstener_2 will not be triggered.

In addition, other necessary information can be saved in the Event instance so that when linstener triggers execution, additional information can be obtained.

Event subscriber

The event subscriber (Event subscriber) tells the dispathcer instance that all events he wants to subscribe to do not have to be registered through the dispathcer instance one by one. The event subscriber is a PHP class that can tell dispathcer the specific events he wants to subscribe to.

Benefits:

Events of concern do not need to be registered one by one.

Unfollow events do not have to remove registrations one by one.

The events that subscribers pay attention to within the subscriber are a whole, either all or none.

Example

Common chestnut

Include "vendor/autoload.php"; use Symfony\ Component\ EventDispatcher\ EventDispatcher;use Symfony\ Component\ EventDispatcher\ Event;class UserEvent extends Event {public function name () {return "Cartman";} public function age () {return "24";}} $dispatcher = new EventDispatcher (); $dispatcher- > addListener ("user.name", function ($event, $eventName, $dispatcher) {echo "My name is Cartman\ n";}) $dispatcher- > addListener ("user.name", function ($event, $eventName, $dispatcher) {echo "My name is {$event- > name ()} from Event instance\ n";}, 10); $dispatcher- > addListener ("user.age", function ($event, $eventName, $dispatcher) {echo "My age is 24\ n";}, 10); $dispatcher- > addListener ("user.age", function ($event, $eventName, $dispatcher) {echo "My age is {$event- > age ()} from Event instance\ n";},-10) $dispatcher- > dispatch ("user.name", new UserEvent ()); $dispatcher- > dispatch ("user.age", new UserEvent ())

The output of the above example

My name is Cartman from Event instanceMy name is CartmanMy age is 24My age is 24 from Event instance

Event subscriber Chestnut

Register events through Subscriber

Include "vendor/autoload.php"; use Symfony\ Component\ EventDispatcher\ EventDispatcher;use Symfony\ Component\ EventDispatcher\ Event;use Symfony\ Component\ EventDispatcher\ EventSubscriberInterface;class BookEvent extends Event {public $name = self::class;} class BookSubscriber implements EventSubscriberInterface {public static function getSubscribedEvents () {return ["chinese.name" = > "chineseNameShow", "english.name" = > [["englishNameShow",-10], ["englishNameAFter", 10],], "math.name" = > ["mathNameShow", 100]] } public function chineseNameShow (Event $event) {echo "I am a Chinese book\ n";} public function englishNameShow (Event $event) {echo "I am an English book\ n";} public function englishNameAFter (Event $event) {echo "I am an English book [from Event instance {$event- > name}]\ n"; public function mathNameShow (Event $event) {echo "I am a displayed math book\ n";} $dispatcher = new EventDispatcher (); $subscriber = new BookSubscriber () $dispatcher- > addSubscriber ($subscriber); $dispatcher- > dispatch ("english.name", new BookEvent ()); $dispatcher- > dispatch ("chinese.name"); $dispatcher- > removeSubscriber ($subscriber); $dispatcher- > dispatch ("math.name")

Output as content:

I am the English book [from Event instance BookEvent] after the presentation. I am an English book and I am a Chinese book. I hope the answers to the questions about the EventDispatcher event distribution assembly are shared here. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel for more related knowledge.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report