From 3fdbb2cbfe778fcf48c61bb9fe40d4ac85f8004f Mon Sep 17 00:00:00 2001 From: Fabio Bas Date: Thu, 19 Sep 2013 23:07:57 +0200 Subject: Committed TComponent patch by javalizard TComponent Update: Behaviors, Class Behaviors, fx global events, and dy one to one events. This patchset was currently available only on svn trunk/ --- framework/Util/TBehavior.php | 87 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 framework/Util/TBehavior.php (limited to 'framework/Util/TBehavior.php') diff --git a/framework/Util/TBehavior.php b/framework/Util/TBehavior.php new file mode 100644 index 00000000..951a758f --- /dev/null +++ b/framework/Util/TBehavior.php @@ -0,0 +1,87 @@ + + * @link http://www.yiiframework.com/ + * @copyright Copyright © 2008-2009 Yii Software LLC + * @license http://www.yiiframework.com/license/ + */ + +/** + * TBehavior is a convenient base class for behavior classes. + * @author Qiang Xue + * @version $Id: TBehaviour.php 3211 2012-10-31 02:35:01Z javalizard@gmail.com $ + * @package System.Util + * @since 3.2.3 + */ +class TBehavior extends TComponent implements IBehavior +{ + private $_enabled; + private $_owner; + + /** + * Declares events and the corresponding event handler methods. + * The events are defined by the {@link owner} component, while the handler + * methods by the behavior class. The handlers will be attached to the corresponding + * events when the behavior is attached to the {@link owner} component; and they + * will be detached from the events when the behavior is detached from the component. + * @return array events (array keys) and the corresponding event handler methods (array values). + */ + public function events() + { + return array(); + } + + /** + * Attaches the behavior object to the component. + * The default implementation will set the {@link owner} property + * and attach event handlers as declared in {@link events}. + * Make sure you call the parent implementation if you override this method. + * @param TComponent the component that this behavior is to be attached to. + */ + public function attach($owner) + { + $this->_owner=$owner; + foreach($this->events() as $event=>$handler) + $owner->attachEventHandler($event,array($this,$handler)); + } + + /** + * Detaches the behavior object from the component. + * The default implementation will unset the {@link owner} property + * and detach event handlers declared in {@link events}. + * Make sure you call the parent implementation if you override this method. + * @param TComponent the component that this behavior is to be detached from. + */ + public function detach($owner) + { + foreach($this->events() as $event=>$handler) + $owner->detachEventHandler($event,array($this,$handler)); + $this->_owner=null; + } + + /** + * @return TComponent the owner component that this behavior is attached to. + */ + public function getOwner() + { + return $this->_owner; + } + + /** + * @return boolean whether this behavior is enabled + */ + public function getEnabled() + { + return $this->_enabled; + } + + /** + * @param boolean whether this behavior is enabled + */ + public function setEnabled($value) + { + $this->_enabled=$value; + } +} \ No newline at end of file -- cgit v1.2.3