Learning yii2.0 Framework Read code (14)

Source: Internet
Author: User

Components (Component)

Event Events (refresher)

<?phpnamespace yii\base;//the event is the base class for all event classes. It encapsulates the parameters associated with the event. If an event handler set [[Proceed]] is true, the rest of the uninvoked handlers will no longer be referred to as processing events. Additionally, when you add an event handler, additional data may be passed and the event handler can be called through the [data] property. classEventextends Object{    /** * @var string the event name.     This was set by [[Component::trigger ()]] and [[Trigger ()]].     * Event handlers may use the This property to check the what Event it is handling. * Name of the event*/     Public $name; /** * @var Object The sender of this event.     If not set, this property would be * Set as the object whose ' Trigger () "method is called.     * Also be a ' null ' if this event is a * Class-level the event which is triggered in a static context. * The object that triggered the event*/     Public $sender; /** * @var Boolean whether the event is handled.     Defaults to False. * When a handler sets this to be true, the event processing'll stop and * ignore the rest of the uninvoked event Han     Dlers. * Log if the event has been processed, and when handled is set to true, execution to this event will stop and ignore the remaining event*/     Public $handled=false;  Public $data; //Store all the event, because it is a static property, all event objects/classes share this piece of data         Private Static $_events= []; //attach an event handler to a class-level event that, when a class-level event is triggered, an event handler, that class invokes the parent class//Adds an event to a class     Public Static functionOn$class,$name,$handler,$data=NULL,$append=true)    {        //remove the leftmost slash of class        $class=LTrim($class, ‘\\‘); //If Append is true, place the last of the array whose name is $name in $_events, or put it first        if($append||Empty(Self::$_events[$name][$class]) { self::$_events[$name][$class][] = [$handler,$data]; } Else {            Array_unshift(Self::$_events[$name][$class], [$handler,$data]); }    }    //remove an event from a class     Public Static functionOff$class,$name,$handler=NULL)    {        $class=LTrim($class, ‘\\‘); if(Empty(Self::$_events[$name][$class])) {            //The event does not exist            return false; }        if($handler===NULL) {            //If handler is empty, the event is removed directly under the class, that is, the event of the name is removed.            unset(Self::$_events[$name][$class]); return true; } Else {            $removed=false; //If the $handler is not empty, the loop $_events finds the corresponding handler, removing only the array of handler and data            foreach(Self::$_events[$name][$class] as $i=$event) {                if($event[0] = = =$handler) {                    unset(Self::$_events[$name][$class][$i]); $removed=true; }            }            if($removed) {                //after removal, make the array into a natural array againSelf::$_events[$name][$class] =array_values(Self::$_events[$name][$class]); }            return $removed; }    }    /** * Returns A value indicating whether there is any handler attached to the specified Class-level event. * Note that this method would also check all parent classes to see if there are any handler attached * to the named even T. * detects if a class or object has an event * @param string|object $class The object or the fully qualified class name specifying the     Class-level event.     * @param string $name the event name.     * @return Boolean whether there is a handler attached to the event. */     Public Static functionHashandlers ($class,$name)    {        if(Empty(Self::$_events[$name])) {            //does not exist, returns directly            return false; }        if(Is_object($class)) {            //if it is an object, get its class name            $class=Get_class($class); } Else {            //if it is a class name, remove the leftmost slash of class            $class=LTrim($class, ‘\\‘); }        //if it is not found in the class, go to the parent class and find it until you find it or have no parent.         Do {            if(!Empty(Self::$_events[$name][$class])) {                return true; }        }  while(($class=Get_parent_class($class)) !==false); return false; }    /** * Triggers a class-level event. * This method would cause invocation of event handlers that is attached to the named event * for the specified class A     ND all its parent classes.  * Triggering an event of a class or Object * @param string|object $class The object or the fully qualified class name specifying the Class-level     Event.     * @param string $name the event name. * @param event $event the event parameter.     If not set, a default [[Event]] object would be created. */     Public Static functionTrigger$class,$name,$event=NULL)    {        if(Empty(Self::$_events[$name])) {            return; }        if($event===NULL) {            //event does not exist, create an event object            $event=New Static; }        //sets the properties of the event object, which is not handled by default        $event->handled =false; $event->name =$name; if(Is_object($class)) {            if($event->sender = = =NULL) {                //if $class is an object and sender is empty, the $class is assigned to sender, that is, $class is the object that triggered the event                $event->sender =$class; }            $class=Get_class($class); } Else {            $class=LTrim($class, ‘\\‘); }        //Loop the $_event of a class until it encounters $event->handled is true or has no parent class         Do {            if(!Empty(Self::$_events[$name][$class])) {                foreach(Self::$_events[$name][$class] as $handler) {                    //assign a parameter to the event object's Data property                    $event->data =$handler[1]; //call $handler method//In the method, you can use $this->data to the corresponding parameters//can also be set in which $this->handled Value that interrupts the triggering of subsequent events                    Call_user_func($handler[0],$event); //When a handled is set to true, it stops when it executes to this event and ignores the remaining events                    if($event-handled) {                        return; }                }            }        }  while(($class=Get_parent_class($class)) !==false); }}

Learning yii2.0 Framework Read code (14)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.