* @link http://www.yiiframework.com/ * @copyright Copyright © 2008 Yii Software LLC * @license http://www.yiiframework.com/license/ */ /** * CComponent is the base class for all components. * * CComponent implements the protocol of defining, using properties and events. * * A property is defined by a getter method, and/or a setter method. * Properties can be accessed in the way like accessing normal object members. * Reading or writing a property will cause the invocation of the corresponding * getter or setter method, e.g., *
* $a=$component->text; // equivalent to $a=$component->getText();
* $component->text='abc'; // equivalent to $component->setText('abc');
*
* The signatures of getter and setter methods are as follows,
*
* // getter, defines a readable property 'text'
* public function getText() { ... }
* // setter, defines a writable property 'text' with $value to be set to the property
* public function setText($value) { ... }
*
*
* An event is defined by the presence of a method whose name starts with 'on'.
* The event name is the method name. When an event is raised, functions
* (called event handlers) attached to the event will be invoked automatically.
*
* An event can be raised by calling {@link raiseEvent} method, upon which
* the attached event handlers will be invoked automatically in the order they
* are attached to the event. Event handlers must have the following signature,
*
* function eventHandler($event) { ... }
*
* where $event includes parameters associated with the event.
*
* To attach an event handler to an event, see {@link attachEventHandler}.
* You can also use the following syntax:
* * $component->onClick=$callback; // or $component->onClick->add($callback); ** where $callback refers to a valid PHP callback. Below we show some callback examples: *
* 'handleOnClick' // handleOnClick() is a global function
* array($object,'handleOnClick') // using $object->handleOnClick()
* array('Page','handleOnClick') // using Page::handleOnClick()
*
*
* To raise an event, use {@link raiseEvent}. The on-method defining an event is
* commonly written like the following:
*
* public function onClick($event)
* {
* $this->raiseEvent('onClick',$event);
* }
*
* where $event is an instance of {@link CEvent} or its child class.
* One can then raise the event by calling the on-method instead of {@link raiseEvent} directly.
*
* Both property names and event names are case-insensitive.
*
* @author Qiang Xue * $value=$component->propertyName; * $handlers=$component->eventName; ** @param string the property name or event name * @return mixed the property value or event handlers attached to the event * @throws CException if the property or event is not defined * @see __set */ public function __get($name) { $getter='get'.$name; if(method_exists($this,$getter)) return $this->$getter(); else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name)) { // duplicating getEventHandlers() here for performance $name=strtolower($name); if(!isset($this->_e[$name])) $this->_e[$name]=new CList; return $this->_e[$name]; } else throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.', array('{class}'=>get_class($this), '{property}'=>$name))); } /** * Sets value of a component property. * Do not call this method. This is a PHP magic method that we override * to allow using the following syntax to set a property or attach an event handler *
* $this->propertyName=$value; * $this->eventName=$callback; ** @param string the property name or the event name * @param mixed the property value or callback * @throws CException if the property/event is not defined or the property is read only. * @see __get */ public function __set($name,$value) { $setter='set'.$name; if(method_exists($this,$setter)) $this->$setter($value); else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name)) { // duplicating getEventHandlers() here for performance $name=strtolower($name); if(!isset($this->_e[$name])) $this->_e[$name]=new CList; $this->_e[$name]->add($value); } else if(method_exists($this,'get'.$name)) throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.', array('{class}'=>get_class($this), '{property}'=>$name))); else throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.', array('{class}'=>get_class($this), '{property}'=>$name))); } /** * Determines whether a property is defined. * A property is defined if there is a getter or setter method * defined in the class. Note, property names are case-insensitive. * @param string the property name * @return boolean whether the property is defined * @see canGetProperty * @see canSetProperty */ public function hasProperty($name) { return method_exists($this,'get'.$name) || method_exists($this,'set'.$name); } /** * Determines whether a property can be read. * A property can be read if the class has a getter method * for the property name. Note, property name is case-insensitive. * @param string the property name * @return boolean whether the property can be read * @see canSetProperty */ public function canGetProperty($name) { return method_exists($this,'get'.$name); } /** * Determines whether a property can be set. * A property can be written if the class has a setter method * for the property name. Note, property name is case-insensitive. * @param string the property name * @return boolean whether the property can be written * @see canGetProperty */ public function canSetProperty($name) { return method_exists($this,'set'.$name); } /** * Determines whether an event is defined. * An event is defined if the class has a method named like 'onXXX'. * Note, event name is case-insensitive. * @param string the event name * @return boolean whether an event is defined */ public function hasEvent($name) { return !strncasecmp($name,'on',2) && method_exists($this,$name); } /** * Checks whether the named event has attached handlers. * @param string the event name * @return boolean whether an event has been attached one or several handlers */ public function hasEventHandler($name) { $name=strtolower($name); return isset($this->_e[$name]) && $this->_e[$name]->getCount()>0; } /** * Returns the list of attached event handlers for an event. * @param string the event name * @return CList list of attached event handlers for the event * @throws CException if the event is not defined */ public function getEventHandlers($name) { if($this->hasEvent($name)) { $name=strtolower($name); if(!isset($this->_e[$name])) $this->_e[$name]=new CList; return $this->_e[$name]; } else throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.', array('{class}'=>get_class($this), '{event}'=>$name))); } /** * Attaches an event handler to an event. * * An event handler must be a valid PHP callback, i.e., a string referring to * a global function name, or an array containing two elements with * the first element being an object and the second element a method name * of the object. * * An event handler must be defined with the following signature, *
* function handlerName($event) {}
*
* where $event includes parameters associated with the event.
*
* This is a convenient method of attaching a handler to an event.
* It is equivalent to the following code:
* * $component->getEventHandlers($eventName)->add($eventHandler); ** * Using {@link getEventHandlers}, one can also specify the excution order * of multiple handlers attaching to the same event. For example: *
* $component->getEventHandlers($eventName)->insertAt(0,$eventHandler); ** makes the handler to be invoked first. * * @param string the event name * @param callback the event handler * @throws CException if the event is not defined * @see detachEventHandler */ public function attachEventHandler($name,$handler) { $this->getEventHandlers($name)->add($handler); } /** * Detaches an existing event handler. * This method is the opposite of {@link attachEventHandler}. * @param string event name * @param callback the event handler to be removed * @return boolean if the detachment process is successful * @see attachEventHandler */ public function detachEventHandler($name,$handler) { if($this->hasEventHandler($name)) { try { $this->getEventHandlers($name)->remove($handler); return true; } catch(Exception $e) { } } return false; } /** * Raises an event. * This method represents the happening of an event. It invokes * all attached handlers for the event. * @param string the event name * @param CEvent the event parameter * @throws CException if the event is undefined or an event handler is invalid. */ public function raiseEvent($name,$event) { $name=strtolower($name); if(isset($this->_e[$name])) { foreach($this->_e[$name] as $handler) { if(is_string($handler)) call_user_func($handler,$event); else if(is_callable($handler,true)) { // an array: 0 - object, 1 - method name list($object,$method)=$handler; if(is_string($object)) // static method call call_user_func($handler,$event); else if(method_exists($object,$method)) $object->$method($event); else throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".', array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>$handler[1]))); } else throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".', array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>gettype($handler)))); // stop further handling if param.handled is set true if(($event instanceof CEvent) && $event->handled) return; } } else if(!$this->hasEvent($name)) throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.', array('{class}'=>get_class($this), '{event}'=>$name))); } } /** * CEvent is the base class for all event classes. * * It encapsulates the parameters associated with an event. * The {@link sender} property describes who raises the event. * And the {@link handled} property indicates if the event is handled. * If an event handler sets {@link handled} to true, those handlers * that are not invoked yet will not be invoked anymore. * * @author Qiang Xue
* class TextAlign extends CEnumerable
* {
* const Left='Left';
* const Right='Right';
* }
*
* Then, one can use the enumerable values such as TextAlign::Left and
* TextAlign::Right.
*
* @author Qiang Xue
* public function setPropertyName($value)
* {
* $value=CPropertyValue::ensureBoolean($value);
* // $value is now of boolean type
* }
*
*
* Properties can be of the following types with specific type conversion rules:
*