Event.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. * @since 2.1.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Event;
  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. * @property string $name Name of the event
  22. * @property object $subject The object this event applies to
  23. * @property mixed $result Property used to retain the result value of the event listeners
  24. * @property array $data Custom data for the method that receives the event
  25. */
  26. class Event implements EventInterface
  27. {
  28. /**
  29. * Name of the event
  30. *
  31. * @var string
  32. */
  33. protected $_name = null;
  34. /**
  35. * The object this event applies to (usually the same object that generates the event)
  36. *
  37. * @var object
  38. */
  39. protected $_subject;
  40. /**
  41. * Custom data for the method that receives the event
  42. *
  43. * @var array
  44. */
  45. protected $_data;
  46. /**
  47. * Property used to retain the result value of the event listeners
  48. *
  49. * @var mixed
  50. */
  51. protected $_result = null;
  52. /**
  53. * Flags an event as stopped or not, default is false
  54. *
  55. * @var bool
  56. */
  57. protected $_stopped = false;
  58. /**
  59. * Constructor
  60. *
  61. * ### Examples of usage:
  62. *
  63. * ```
  64. * $event = new Event('Order.afterBuy', $this, ['buyer' => $userData]);
  65. * $event = new Event('User.afterRegister', $UserModel);
  66. * ```
  67. *
  68. * @param string $name Name of the event
  69. * @param object|null $subject the object that this event applies to (usually the object that is generating the event)
  70. * @param array|null $data any value you wish to be transported with this event to it can be read by listeners
  71. */
  72. public function __construct($name, $subject = null, $data = null)
  73. {
  74. if ($data !== null && !is_array($data)) {
  75. trigger_error('Data parameter will be changing to array hint typing', E_USER_DEPRECATED);
  76. }
  77. $this->_name = $name;
  78. $this->_data = (array)$data;
  79. $this->_subject = $subject;
  80. }
  81. /**
  82. * Provides read-only access for the name and subject properties.
  83. *
  84. * @param string $attribute Attribute name.
  85. * @return mixed
  86. */
  87. public function __get($attribute)
  88. {
  89. if ($attribute === 'name' || $attribute === 'subject') {
  90. return $this->{$attribute}();
  91. }
  92. if ($attribute === 'data')
  93. {
  94. trigger_error('Public read access to data is deprecated, use data()', E_USER_DEPRECATED);
  95. return $this->_data;
  96. }
  97. if ($attribute === 'result')
  98. {
  99. trigger_error('Public read access to result is deprecated, use result()', E_USER_DEPRECATED);
  100. return $this->_result;
  101. }
  102. }
  103. /**
  104. * Provides backward compatibility for write access to data and result properties.
  105. *
  106. * @param string $attribute Attribute name.
  107. * @param mixed $value
  108. */
  109. public function __set($attribute, $value)
  110. {
  111. if($attribute === 'data')
  112. {
  113. trigger_error('Public write access to data is deprecated, use setData()', E_USER_DEPRECATED);
  114. $this->_data = (array)$value;
  115. }
  116. if ($attribute === 'result')
  117. {
  118. trigger_error('Public write access to result is deprecated, use setResult()', E_USER_DEPRECATED);
  119. $this->_result = $value;
  120. }
  121. }
  122. /**
  123. * Returns the name of this event. This is usually used as the event identifier
  124. *
  125. * @return string
  126. */
  127. public function name()
  128. {
  129. return $this->_name;
  130. }
  131. /**
  132. * Returns the subject of this event
  133. *
  134. * @return object
  135. */
  136. public function subject()
  137. {
  138. return $this->_subject;
  139. }
  140. /**
  141. * Stops the event from being used anymore
  142. *
  143. * @return void
  144. */
  145. public function stopPropagation()
  146. {
  147. $this->_stopped = true;
  148. }
  149. /**
  150. * Check if the event is stopped
  151. *
  152. * @return bool True if the event is stopped
  153. */
  154. public function isStopped()
  155. {
  156. return $this->_stopped;
  157. }
  158. /**
  159. * The result value of the event listeners
  160. *
  161. * @return mixed
  162. */
  163. public function result()
  164. {
  165. return $this->_result;
  166. }
  167. /**
  168. * @param null $value
  169. * @return $this
  170. */
  171. public function setResult($value = null)
  172. {
  173. $this->_result = $value;
  174. return $this;
  175. }
  176. /**
  177. * Access the event data/payload.
  178. *
  179. * @param string|null $key
  180. * @return array|null The data payload if $key is null, or the data value for the given $key. If the $key does not
  181. * exist a null value is returned.
  182. */
  183. public function data($key = null)
  184. {
  185. if ($key !== null) {
  186. return isset($this->_data[$key]) ? $this->_data[$key] : null;
  187. }
  188. return (array)$this->_data;
  189. }
  190. /**
  191. * @param array|string $key
  192. * @param mixed $value
  193. * @return $this
  194. */
  195. public function setData($key, $value = null)
  196. {
  197. if(is_array($key)) {
  198. $this->_data = $key;
  199. } else {
  200. $this->_data[$key] = $value;
  201. }
  202. return $this;
  203. }
  204. }