1<?php
2/**
3 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
4 * Copyright (c) Cake Software Foundation, Inc. (https://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. (https://cakefoundation.org)
11 * @link		  https://cakephp.org CakePHP(tm) Project
12 * @package		  Cake.Observer
13 * @since		  CakePHP(tm) v 2.1
14 * @license       https://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 */
24class 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	public function __construct($name, $subject = null, $data = null) {
76		$this->_name = $name;
77		$this->data = $data;
78		$this->_subject = $subject;
79	}
80
81/**
82 * Dynamically returns the name and subject if accessed directly
83 *
84 * @param string $attribute Attribute name.
85 * @return mixed
86 */
87	public function __get($attribute) {
88		if ($attribute === 'name' || $attribute === 'subject') {
89			return $this->{$attribute}();
90		}
91	}
92
93/**
94 * Returns the name of this event. This is usually used as the event identifier
95 *
96 * @return string
97 */
98	public function name() {
99		return $this->_name;
100	}
101
102/**
103 * Returns the subject of this event
104 *
105 * @return object
106 */
107	public function subject() {
108		return $this->_subject;
109	}
110
111/**
112 * Stops the event from being used anymore
113 *
114 * @return bool
115 */
116	public function stopPropagation() {
117		return $this->_stopped = true;
118	}
119
120/**
121 * Check if the event is stopped
122 *
123 * @return bool True if the event is stopped
124 */
125	public function isStopped() {
126		return $this->_stopped;
127	}
128
129}
130