/** * 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 // 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 // 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 // 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[] = $response;// otherwise } array_pop($this->firing);// pop it return $halt ? null : $responses; }
时间: 2024-11-06 03:53:40