summaryrefslogtreecommitdiff
path: root/framework/Base/TBehavior.php
diff options
context:
space:
mode:
authorrojaro <>2009-07-13 14:12:10 +0000
committerrojaro <>2009-07-13 14:12:10 +0000
commit2ecfe49532dbfc53d75a32e68df643b7af13dc1f (patch)
tree6d0a0ca67e1e0cdd1c2ff9403bfb760abb87d61c /framework/Base/TBehavior.php
parent78af2b4630ccde4b03659a259f739f83ae0de9ec (diff)
- Models, Behaviors ...
Diffstat (limited to 'framework/Base/TBehavior.php')
-rw-r--r--framework/Base/TBehavior.php120
1 files changed, 120 insertions, 0 deletions
diff --git a/framework/Base/TBehavior.php b/framework/Base/TBehavior.php
new file mode 100644
index 00000000..b2833140
--- /dev/null
+++ b/framework/Base/TBehavior.php
@@ -0,0 +1,120 @@
+<?php
+/**
+ * CBehavior class file.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @link http://www.yiiframework.com/
+ * @copyright Copyright &copy; 2008-2009 Yii Software LLC
+ * @license http://www.yiiframework.com/license/
+ */
+
+/**
+ * CBehavior is a convenient base class for behavior classes.
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: CBehavior.php 564 2009-01-21 22:07:10Z qiang.xue $
+ * @package system.base
+ * @since 1.0.2
+ */
+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 CComponent 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 CComponent 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 CComponent 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;
+ }
+}
+
+/**
+ * IBehavior interfaces is implemented by all behavior classes.
+ *
+ * A behavior is a way to enhance a component with additional methods that
+ * are defined in the behavior class and not available in the component class.
+ *
+ * @author Qiang Xue <qiang.xue@gmail.com>
+ * @version $Id: interfaces.php 956 2009-04-21 15:16:03Z qiang.xue@gmail.com $
+ * @package system.base
+ * @since 1.0.2
+ */
+interface IBehavior
+{
+ /**
+ * Attaches the behavior object to the component.
+ * @param CComponent the component that this behavior is to be attached to.
+ */
+ public function attach($component);
+ /**
+ * Detaches the behavior object from the component.
+ * @param CComponent the component that this behavior is to be detached from.
+ */
+ public function detach($component);
+ /**
+ * @return boolean whether this behavior is enabled
+ */
+ public function getEnabled();
+ /**
+ * @param boolean whether this behavior is enabled
+ */
+ public function setEnabled($value);
+} \ No newline at end of file