Behavior.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\base;
  8. /**
  9. * Behavior is the base class for all behavior classes.
  10. *
  11. * A behavior can be used to enhance the functionality of an existing component without modifying its code.
  12. * In particular, it can "inject" its own methods and properties into the component
  13. * and make them directly accessible via the component. It can also respond to the events triggered in the component
  14. * and thus intercept the normal code execution.
  15. *
  16. * For more details and usage information on Behavior, see the [guide article on behaviors](guide:concept-behaviors).
  17. *
  18. * @author Qiang Xue <qiang.xue@gmail.com>
  19. * @since 2.0
  20. */
  21. class Behavior extends BaseObject
  22. {
  23. /**
  24. * @var Component|null the owner of this behavior
  25. */
  26. public $owner;
  27. /**
  28. * @var array Attached events handlers
  29. */
  30. private $_attachedEvents = [];
  31. /**
  32. * Declares event handlers for the [[owner]]'s events.
  33. *
  34. * Child classes may override this method to declare what PHP callbacks should
  35. * be attached to the events of the [[owner]] component.
  36. *
  37. * The callbacks will be attached to the [[owner]]'s events when the behavior is
  38. * attached to the owner; and they will be detached from the events when
  39. * the behavior is detached from the component.
  40. *
  41. * The callbacks can be any of the following:
  42. *
  43. * - method in this behavior: `'handleClick'`, equivalent to `[$this, 'handleClick']`
  44. * - object method: `[$object, 'handleClick']`
  45. * - static method: `['Page', 'handleClick']`
  46. * - anonymous function: `function ($event) { ... }`
  47. *
  48. * The following is an example:
  49. *
  50. * ```php
  51. * [
  52. * Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
  53. * Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
  54. * ]
  55. * ```
  56. *
  57. * @return array events (array keys) and the corresponding event handler methods (array values).
  58. */
  59. public function events()
  60. {
  61. return [];
  62. }
  63. /**
  64. * Attaches the behavior object to the component.
  65. * The default implementation will set the [[owner]] property
  66. * and attach event handlers as declared in [[events]].
  67. * Make sure you call the parent implementation if you override this method.
  68. * @param Component $owner the component that this behavior is to be attached to.
  69. */
  70. public function attach($owner)
  71. {
  72. $this->owner = $owner;
  73. foreach ($this->events() as $event => $handler) {
  74. $this->_attachedEvents[$event] = $handler;
  75. $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);
  76. }
  77. }
  78. /**
  79. * Detaches the behavior object from the component.
  80. * The default implementation will unset the [[owner]] property
  81. * and detach event handlers declared in [[events]].
  82. * Make sure you call the parent implementation if you override this method.
  83. */
  84. public function detach()
  85. {
  86. if ($this->owner) {
  87. foreach ($this->_attachedEvents as $event => $handler) {
  88. $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
  89. }
  90. $this->_attachedEvents = [];
  91. $this->owner = null;
  92. }
  93. }
  94. }