/** * fire an event and call the listeners. * * @param string|object $event * @param mixed $ payload * @param bool $halt * @return array|null */ // public function fire ($event, $payload = [], $halt = false) {// fire means to start // call listeners // when the given "Event" is actually an object we will assume[like] it is an event &nBsp; // object and use the class as the event name and this event itself as the // payload to the handler, which makes object based events quite simple. if (Is_object ($event)) {// determine is a object list ($payload, $event) = [[$event], get_class ($event)]; }// set this as list function // now i love this function $responses = [];// prepare listener // if an array is not given to us as the payload, we will turn It into one so // we can easily use call_user_func_array on the listeners, passing in the // payload to each of them so that they receive each of these arguments. if (! is_array ($payload)) { $payload = [$payload];// be array }// if it is a array $this firing[] = $event;// set this firing listener if (iSset ($payload [0]) && $payload [0] instanceof shouldbroadcast] { $this->broadcastevent ($payload [0]); } foreach ($this Getlisteners ($event) as $listener) { // use a function to return big array and list loop it $response = call _user_func_array ($listener, $payload);// cal_user_func_array// get response // If a response is returned from the listener and event halting is enabled &Nbsp; // we will just return this response, and not call the rest of the event // listeners. Otherwise we will add the response on the response list. if (! is_null ($response) && $halt) { array_pop ($this->firing);// if null array_pop it return $response;// return it }// add response on the response list &nbSp; // if a boolean false is returned from a listener, we will stop propagating // the event to any further listeners down in the chain, else we keep on // looping through the listeners and firing every one in our sequence. if ($response === false) { break; }// if response is null $responses []&NBsp;= $response;// otherwise } array_pop ($this->firing);// pop it return $halt ? null : $responses; }
This article is from the "Focus on PHP" blog, please be sure to keep this source http://jingshanls.blog.51cto.com/3357095/1791598
[Li Jingshan php] every day laravel-20160905| Dispatcher-5