Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @since 2.0
  18. */
  19. class Behavior extends Object
  20. {
  21. /**
  22. * @var Component the owner of this behavior
  23. */
  24. public $owner;
  25. /**
  26. * Declares event handlers for the [[owner]]'s events.
  27. *
  28. * Child classes may override this method to declare what PHP callbacks should
  29. * be attached to the events of the [[owner]] component.
  30. *
  31. * The callbacks will be attached to the [[owner]]'s events when the behavior is
  32. * attached to the owner; and they will be detached from the events when
  33. * the behavior is detached from the component.
  34. *
  35. * The callbacks can be any of the following:
  36. *
  37. * - method in this behavior: `'handleClick'`, equivalent to `[$this, 'handleClick']`
  38. * - object method: `[$object, 'handleClick']`
  39. * - static method: `['Page', 'handleClick']`
  40. * - anonymous function: `function ($event) { ... }`
  41. *
  42. * The following is an example:
  43. *
  44. * ```php
  45. * [
  46. * Model::EVENT_BEFORE_VALIDATE => 'myBeforeValidate',
  47. * Model::EVENT_AFTER_VALIDATE => 'myAfterValidate',
  48. * ]
  49. * ```
  50. *
  51. * @return array events (array keys) and the corresponding event handler methods (array values).
  52. */
  53. public function events()
  54. {
  55. return [];
  56. }
  57. /**
  58. * Attaches the behavior object to the component.
  59. * The default implementation will set the [[owner]] property
  60. * and attach event handlers as declared in [[events]].
  61. * Make sure you call the parent implementation if you override this method.
  62. * @param Component $owner the component that this behavior is to be attached to.
  63. */
  64. public function attach($owner)
  65. {
  66. $this->owner = $owner;
  67. foreach ($this->events() as $event => $handler) {
  68. $owner->on($event, is_string($handler) ? [$this, $handler] : $handler);
  69. }
  70. }
  71. /**
  72. * Detaches the behavior object from the component.
  73. * The default implementation will unset the [[owner]] property
  74. * and detach event handlers declared in [[events]].
  75. * Make sure you call the parent implementation if you override this method.
  76. */
  77. public function detach()
  78. {
  79. if ($this->owner) {
  80. foreach ($this->events() as $event => $handler) {
  81. $this->owner->off($event, is_string($handler) ? [$this, $handler] : $handler);
  82. }
  83. $this->owner = null;
  84. }
  85. }
  86. }