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