Event.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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 (deprecated) Name of the event
  22. * @property object $subject (deprecated) The object this event applies to
  23. * @property mixed $result (deprecated) Property used to retain the result value of the event listeners
  24. * @property array $data (deprecated) Custom data for the method that receives the event
  25. */
  26. class Event
  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. public $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|\ArrayAccess|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. $this->_name = $name;
  75. $this->_data = (array)$data;
  76. $this->_subject = $subject;
  77. }
  78. /**
  79. * Provides read-only access for the name and subject properties.
  80. *
  81. * @param string $attribute Attribute name.
  82. * @return mixed
  83. * @deprecated Public properties will be removed.
  84. */
  85. public function __get($attribute)
  86. {
  87. if ($attribute === 'name' || $attribute === 'subject') {
  88. return $this->{$attribute}();
  89. }
  90. if ($attribute === 'data') {
  91. return $this->_data;
  92. }
  93. if ($attribute === 'result') {
  94. return $this->result;
  95. }
  96. }
  97. /**
  98. * Provides backward compatibility for write access to data and result properties.
  99. *
  100. * @param string $attribute Attribute name.
  101. * @param mixed $value The value to set.
  102. * @return void
  103. * @deprecated Public properties will be removed.
  104. */
  105. public function __set($attribute, $value)
  106. {
  107. if ($attribute === 'data') {
  108. $this->_data = (array)$value;
  109. }
  110. if ($attribute === 'result') {
  111. $this->result = $value;
  112. }
  113. }
  114. /**
  115. * Returns the name of this event. This is usually used as the event identifier
  116. *
  117. * @return string
  118. * @deprecated 3.4.0 use getName() instead.
  119. */
  120. public function name()
  121. {
  122. return $this->_name;
  123. }
  124. /**
  125. * Returns the name of this event. This is usually used as the event identifier
  126. *
  127. * @return string
  128. */
  129. public function getName()
  130. {
  131. return $this->_name;
  132. }
  133. /**
  134. * Returns the subject of this event
  135. *
  136. * @return object
  137. * @deprecated 3.4.0 use getSubject() instead.
  138. */
  139. public function subject()
  140. {
  141. return $this->_subject;
  142. }
  143. /**
  144. * Returns the subject of this event
  145. *
  146. * @return object
  147. */
  148. public function getSubject()
  149. {
  150. return $this->_subject;
  151. }
  152. /**
  153. * Stops the event from being used anymore
  154. *
  155. * @return void
  156. */
  157. public function stopPropagation()
  158. {
  159. $this->_stopped = true;
  160. }
  161. /**
  162. * Check if the event is stopped
  163. *
  164. * @return bool True if the event is stopped
  165. */
  166. public function isStopped()
  167. {
  168. return $this->_stopped;
  169. }
  170. /**
  171. * The result value of the event listeners
  172. *
  173. * @return mixed
  174. * @deprecated 3.4.0 use getResult() instead.
  175. */
  176. public function result()
  177. {
  178. return $this->result;
  179. }
  180. /**
  181. * The result value of the event listeners
  182. *
  183. * @return mixed
  184. */
  185. public function getResult()
  186. {
  187. return $this->result;
  188. }
  189. /**
  190. * Listeners can attach a result value to the event.
  191. *
  192. * @param mixed $value The value to set.
  193. * @return $this
  194. */
  195. public function setResult($value = null)
  196. {
  197. $this->result = $value;
  198. return $this;
  199. }
  200. /**
  201. * Access the event data/payload.
  202. *
  203. * @param string|null $key The data payload element to return, or null to return all data.
  204. * @return array|mixed|null The data payload if $key is null, or the data value for the given $key. If the $key does not
  205. * exist a null value is returned.
  206. * @deprecated 3.4.0 use getData() instead.
  207. */
  208. public function data($key = null)
  209. {
  210. return $this->getData($key);
  211. }
  212. /**
  213. * Access the event data/payload.
  214. *
  215. * @param string|null $key The data payload element to return, or null to return all data.
  216. * @return array|mixed|null The data payload if $key is null, or the data value for the given $key. If the $key does not
  217. * exist a null value is returned.
  218. */
  219. public function getData($key = null)
  220. {
  221. if ($key !== null) {
  222. return isset($this->_data[$key]) ? $this->_data[$key] : null;
  223. }
  224. return (array)$this->_data;
  225. }
  226. /**
  227. * Assigns a value to the data/payload of this event.
  228. *
  229. * @param array|string $key An array will replace all payload data, and a key will set just that array item.
  230. * @param mixed $value The value to set.
  231. * @return $this
  232. */
  233. public function setData($key, $value = null)
  234. {
  235. if (is_array($key)) {
  236. $this->_data = $key;
  237. } else {
  238. $this->_data[$key] = $value;
  239. }
  240. return $this;
  241. }
  242. }