CBehavior.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * CBehavior class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CBehavior is a convenient base class for behavior classes.
  12. *
  13. * @property CComponent $owner The owner component that this behavior is attached to.
  14. * @property boolean $enabled Whether this behavior is enabled.
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @package system.base
  18. */
  19. class CBehavior extends CComponent implements IBehavior
  20. {
  21. private $_enabled=false;
  22. private $_owner;
  23. /**
  24. * Declares events and the corresponding event handler methods.
  25. * The events are defined by the {@link owner} component, while the handler
  26. * methods by the behavior class. The handlers will be attached to the corresponding
  27. * events when the behavior is attached to the {@link owner} component; and they
  28. * will be detached from the events when the behavior is detached from the component.
  29. * Make sure you've declared handler method as public.
  30. * @return array events (array keys) and the corresponding event handler methods (array values).
  31. */
  32. public function events()
  33. {
  34. return array();
  35. }
  36. /**
  37. * Attaches the behavior object to the component.
  38. * The default implementation will set the {@link owner} property
  39. * and attach event handlers as declared in {@link events}.
  40. * This method will also set {@link enabled} to true.
  41. * Make sure you've declared handler as public and call the parent implementation if you override this method.
  42. * @param CComponent $owner the component that this behavior is to be attached to.
  43. */
  44. public function attach($owner)
  45. {
  46. $this->_enabled=true;
  47. $this->_owner=$owner;
  48. $this->_attachEventHandlers();
  49. }
  50. /**
  51. * Detaches the behavior object from the component.
  52. * The default implementation will unset the {@link owner} property
  53. * and detach event handlers declared in {@link events}.
  54. * This method will also set {@link enabled} to false.
  55. * Make sure you call the parent implementation if you override this method.
  56. * @param CComponent $owner the component that this behavior is to be detached from.
  57. */
  58. public function detach($owner)
  59. {
  60. foreach($this->events() as $event=>$handler)
  61. $owner->detachEventHandler($event,array($this,$handler));
  62. $this->_owner=null;
  63. $this->_enabled=false;
  64. }
  65. /**
  66. * @return CComponent the owner component that this behavior is attached to.
  67. */
  68. public function getOwner()
  69. {
  70. return $this->_owner;
  71. }
  72. /**
  73. * @return boolean whether this behavior is enabled
  74. */
  75. public function getEnabled()
  76. {
  77. return $this->_enabled;
  78. }
  79. /**
  80. * @param boolean $value whether this behavior is enabled
  81. */
  82. public function setEnabled($value)
  83. {
  84. $value=(bool)$value;
  85. if($this->_enabled!=$value && $this->_owner)
  86. {
  87. if($value)
  88. $this->_attachEventHandlers();
  89. else
  90. {
  91. foreach($this->events() as $event=>$handler)
  92. $this->_owner->detachEventHandler($event,array($this,$handler));
  93. }
  94. }
  95. $this->_enabled=$value;
  96. }
  97. private function _attachEventHandlers()
  98. {
  99. $class=new ReflectionClass($this);
  100. foreach($this->events() as $event=>$handler)
  101. {
  102. if($class->getMethod($handler)->isPublic())
  103. $this->_owner->attachEventHandler($event,array($this,$handler));
  104. }
  105. }
  106. }