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: /**
18: * Represents the transport class of events across the system. It receives a name, subject and an optional
19: * payload. The name can be any string that uniquely identifies the event across the application, while the subject
20: * represents the object that the event applies to.
21: *
22: * @package Cake.Event
23: */
24: class CakeEvent {
25:
26: /**
27: * Name of the event
28: *
29: * @var string
30: */
31: protected $_name = null;
32:
33: /**
34: * The object this event applies to (usually the same object that generates the event)
35: *
36: * @var object
37: */
38: protected $_subject;
39:
40: /**
41: * Custom data for the method that receives the event
42: *
43: * @var mixed
44: */
45: public $data = null;
46:
47: /**
48: * Property used to retain the result value of the event listeners
49: *
50: * @var mixed
51: */
52: public $result = null;
53:
54: /**
55: * Flags an event as stopped or not, default is false
56: *
57: * @var bool
58: */
59: protected $_stopped = false;
60:
61: /**
62: * Constructor
63: *
64: * @param string $name Name of the event
65: * @param object $subject the object that this event applies to (usually the object that is generating the event)
66: * @param mixed $data any value you wish to be transported with this event to it can be read by listeners
67: *
68: * ## Examples of usage:
69: *
70: * ```
71: * $event = new CakeEvent('Order.afterBuy', $this, array('buyer' => $userData));
72: * $event = new CakeEvent('User.afterRegister', $UserModel);
73: * ```
74: *
75: */
76: public function __construct($name, $subject = null, $data = null) {
77: $this->_name = $name;
78: $this->data = $data;
79: $this->_subject = $subject;
80: }
81:
82: /**
83: * Dynamically returns the name and subject if accessed directly
84: *
85: * @param string $attribute Attribute name.
86: * @return mixed
87: */
88: public function __get($attribute) {
89: if ($attribute === 'name' || $attribute === 'subject') {
90: return $this->{$attribute}();
91: }
92: }
93:
94: /**
95: * Returns the name of this event. This is usually used as the event identifier
96: *
97: * @return string
98: */
99: public function name() {
100: return $this->_name;
101: }
102:
103: /**
104: * Returns the subject of this event
105: *
106: * @return string
107: */
108: public function subject() {
109: return $this->_subject;
110: }
111:
112: /**
113: * Stops the event from being used anymore
114: *
115: * @return void
116: */
117: public function stopPropagation() {
118: return $this->_stopped = true;
119: }
120:
121: /**
122: * Check if the event is stopped
123: *
124: * @return bool True if the event is stopped
125: */
126: public function isStopped() {
127: return $this->_stopped;
128: }
129:
130: }
131: