CakeEvent.php 2.9 KB

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