Behavior.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @link https://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license https://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. * @template T of Component
  22. *
  23. * @phpstan-property T|null $owner
  24. * @psalm-property T|null $owner
  25. */
  26. class Behavior extends BaseObject
  27. {
  28. /**
  29. * @var Component|null the owner of this behavior
  30. *
  31. * @phpstan-var T|null
  32. * @psalm-var T|null
  33. */
  34. public $owner;
  35. /**
  36. * @var array Attached events handlers
  37. */
  38. private $_attachedEvents = [];
  39. /**
  40. * Declares event handlers for the [[owner]]'s events.
  41. *
  42. * Child classes may override this method to declare what PHP callbacks should
  43. * be attached to the events of the [[owner]] component.
  44. *
  45. * The callbacks will be attached to the [[owner]]'s events when the behavior is
  46. * attached to the owner; and they will be detached from the events when
  47. * the behavior is detached from the component.
  48. *
  49. * The callbacks can be any of the following:
  50. *
  51. * - method in this behavior: `'handleClick'`, equivalent to `[$this, 'handleClick']`
  52. * - object method: `[$object, 'handleClick']`
  53. * - static method: `['Page', 'handleClick']`
  54. * - anonymous function: `function ($event) { ... }`
  55. *
  56. * The following is an example:
  57. *
  58. * ```php
  59. * [
  60. * Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
  61. * Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
  62. * ]
  63. * ```
  64. *
  65. * @return array events (array keys) and the corresponding event handler methods (array values).
  66. */
  67. public function events()
  68. {
  69. return [];
  70. }
  71. /**
  72. * Attaches the behavior object to the component.
  73. * The default implementation will set the [[owner]] property
  74. * and attach event handlers as declared in [[events]].
  75. * Make sure you call the parent implementation if you override this method.
  76. * @param Component $owner the component that this behavior is to be attached to.
  77. */
  78. public function attach($owner)
  79. {
  80. $this->owner = $owner;
  81. foreach ($this->events() as $event => $handler) {
  82. $this->_attachedEvents[$event] = $handler;
  83. $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);
  84. }
  85. }
  86. /**
  87. * Detaches the behavior object from the component.
  88. * The default implementation will unset the [[owner]] property
  89. * and detach event handlers declared in [[events]].
  90. * Make sure you call the parent implementation if you override this method.
  91. */
  92. public function detach()
  93. {
  94. if ($this->owner) {
  95. foreach ($this->_attachedEvents as $event => $handler) {
  96. $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
  97. }
  98. $this->_attachedEvents = [];
  99. $this->owner = null;
  100. }
  101. }
  102. }