2011-12-13 04:52:40 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
2013-02-08 11:59:49 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-12-13 04:52:40 +00:00
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
2013-02-08 12:22:51 +00:00
|
|
|
* For full copyright and license information, please see the LICENSE.txt
|
2011-12-13 04:52:40 +00:00
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
2013-02-08 11:59:49 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
2011-12-13 04:52:40 +00:00
|
|
|
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
|
* @package Cake.Observer
|
|
|
|
* @since CakePHP(tm) v 2.1
|
2013-05-30 22:11:14 +00:00
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2013-03-29 10:16:20 +00:00
|
|
|
* Represents the transport class of events across the system. It receives a name, subject and an optional
|
2011-12-13 04:52:40 +00:00
|
|
|
* payload. The name can be any string that uniquely identifies the event across the application, while the subject
|
2012-07-27 02:30:17 +00:00
|
|
|
* represents the object that the event applies to.
|
2011-12-13 04:52:40 +00:00
|
|
|
*
|
|
|
|
* @package Cake.Event
|
|
|
|
*/
|
|
|
|
class CakeEvent {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Name of the event
|
2012-07-18 01:55:29 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var string
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
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
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var mixed
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
public $data = null;
|
|
|
|
|
2011-12-26 02:22:12 +00:00
|
|
|
/**
|
|
|
|
* Property used to retain the result value of the event listeners
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var mixed
|
2011-12-26 02:22:12 +00:00
|
|
|
*/
|
|
|
|
public $result = null;
|
2011-12-13 04:52:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Flags an event as stopped or not, default is false
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
protected $_stopped = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param string $name Name of the event
|
|
|
|
* @param object $subject the object that this event applies to (usually the object that is generating the event)
|
|
|
|
* @param mixed $data any value you wish to be transported with this event to it can be read by listeners
|
|
|
|
*
|
|
|
|
* ## Examples of usage:
|
|
|
|
*
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-12-13 04:52:40 +00:00
|
|
|
* $event = new CakeEvent('Order.afterBuy', $this, array('buyer' => $userData));
|
|
|
|
* $event = new CakeEvent('User.afterRegister', $UserModel);
|
2015-01-09 12:47:25 +00:00
|
|
|
* ```
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
public function __construct($name, $subject = null, $data = null) {
|
|
|
|
$this->_name = $name;
|
|
|
|
$this->data = $data;
|
|
|
|
$this->_subject = $subject;
|
|
|
|
}
|
|
|
|
|
2011-12-25 00:27:59 +00:00
|
|
|
/**
|
|
|
|
* Dynamically returns the name and subject if accessed directly
|
|
|
|
*
|
2014-06-02 12:57:11 +00:00
|
|
|
* @param string $attribute Attribute name.
|
2011-12-25 00:27:59 +00:00
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function __get($attribute) {
|
|
|
|
if ($attribute === 'name' || $attribute === 'subject') {
|
|
|
|
return $this->{$attribute}();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-13 04:52:40 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*
|
2016-01-28 21:03:24 +00:00
|
|
|
* @return object
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
public function subject() {
|
|
|
|
return $this->_subject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stops the event from being used anymore
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function stopPropagation() {
|
|
|
|
return $this->_stopped = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if the event is stopped
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @return bool True if the event is stopped
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
public function isStopped() {
|
|
|
|
return $this->_stopped;
|
|
|
|
}
|
|
|
|
|
2012-03-04 00:55:18 +00:00
|
|
|
}
|