CakeEvent.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @package Cake.Observer
  13. * @since CakePHP(tm) v 2.1
  14. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  15. */
  16. /**
  17. * Represents the transport class of events across the system. It receives a name, subject and an optional
  18. * payload. The name can be any string that uniquely identifies the event across the application, while the subject
  19. * represents the object that the event applies to.
  20. *
  21. * @package Cake.Event
  22. */
  23. class CakeEvent {
  24. /**
  25. * Name of the event
  26. *
  27. * @var string
  28. */
  29. protected $_name = null;
  30. /**
  31. * The object this event applies to (usually the same object that generates the event)
  32. *
  33. * @var object
  34. */
  35. protected $_subject;
  36. /**
  37. * Custom data for the method that receives the event
  38. *
  39. * @var mixed
  40. */
  41. public $data = null;
  42. /**
  43. * Property used to retain the result value of the event listeners
  44. *
  45. * @var mixed
  46. */
  47. public $result = null;
  48. /**
  49. * Flags an event as stopped or not, default is false
  50. *
  51. * @var bool
  52. */
  53. protected $_stopped = false;
  54. /**
  55. * Constructor
  56. *
  57. * @param string $name Name of the event
  58. * @param object $subject the object that this event applies to (usually the object that is generating the event)
  59. * @param mixed $data any value you wish to be transported with this event to it can be read by listeners
  60. *
  61. * ## Examples of usage:
  62. *
  63. * {{{
  64. * $event = new CakeEvent('Order.afterBuy', $this, array('buyer' => $userData));
  65. * $event = new CakeEvent('User.afterRegister', $UserModel);
  66. * }}}
  67. *
  68. */
  69. public function __construct($name, $subject = null, $data = null) {
  70. $this->_name = $name;
  71. $this->data = $data;
  72. $this->_subject = $subject;
  73. }
  74. /**
  75. * Dynamically returns the name and subject if accessed directly
  76. *
  77. * @param string $attribute Attribute name.
  78. * @return mixed
  79. */
  80. public function __get($attribute) {
  81. if ($attribute === 'name' || $attribute === 'subject') {
  82. return $this->{$attribute}();
  83. }
  84. }
  85. /**
  86. * Returns the name of this event. This is usually used as the event identifier
  87. *
  88. * @return string
  89. */
  90. public function name() {
  91. return $this->_name;
  92. }
  93. /**
  94. * Returns the subject of this event
  95. *
  96. * @return string
  97. */
  98. public function subject() {
  99. return $this->_subject;
  100. }
  101. /**
  102. * Stops the event from being used anymore
  103. *
  104. * @return void
  105. */
  106. public function stopPropagation() {
  107. return $this->_stopped = true;
  108. }
  109. /**
  110. * Check if the event is stopped
  111. *
  112. * @return bool True if the event is stopped
  113. */
  114. public function isStopped() {
  115. return $this->_stopped;
  116. }
  117. }