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