Skip to content
Permalink
Browse files

adds methods for Events with deprecate warnings for data property

  • Loading branch information...
thinkingmedia
thinkingmedia committed Aug 30, 2016
1 parent e903813 commit 9ada46980142cdf466e2d1bdfe87b8ab3c25dc29
@@ -713,8 +713,8 @@ protected function _getUser()
if (!empty($result) && is_array($result)) {
$this->_authenticationProvider = $auth;
$event = $this->dispatchEvent('Auth.afterIdentify', [$result, $auth]);
if ($event->result !== null) {
$result = $event->result;
if ($event->result() !== null) {
$result = $event->result();
}
$this->storage()->write($result);
@@ -787,8 +787,8 @@ public function identify()
if (!empty($result)) {
$this->_authenticationProvider = $auth;
$event = $this->dispatchEvent('Auth.afterIdentify', [$result, $auth]);
if ($event->result !== null) {
return $event->result;
if ($event->result() !== null) {
return $event->result();
}
return $result;
@@ -495,13 +495,14 @@ protected function _loadComponents()
public function startupProcess()
{
$event = $this->dispatchEvent('Controller.initialize');
if ($event->result instanceof Response) {
return $event->result;
if ($event->result() instanceof Response) {
return $event->result();
}
$event = $this->dispatchEvent('Controller.startup');
if ($event->result instanceof Response) {
return $event->result;
if ($event->result() instanceof Response) {
return $event->result();
}
return null;
}
/**
@@ -516,9 +517,10 @@ public function startupProcess()
public function shutdownProcess()
{
$event = $this->dispatchEvent('Controller.shutdown');
if ($event->result instanceof Response) {
return $event->result;
if ($event->result() instanceof Response) {
return $event->result();
}
return null;
}
/**
@@ -540,8 +542,8 @@ public function redirect($url, $status = 302)
}
$event = $this->dispatchEvent('Controller.beforeRedirect', [$url, $response]);
if ($event->result instanceof Response) {
return $event->result;
if ($event->result() instanceof Response) {
return $event->result();
}
if ($event->isStopped()) {
return null;
@@ -600,8 +602,8 @@ public function render($view = null, $layout = null)
$this->autoRender = false;
$event = $this->dispatchEvent('Controller.beforeRender');
if ($event->result instanceof Response) {
return $event->result;
if ($event->result() instanceof Response) {
return $event->result();
}
if ($event->isStopped()) {
return $this->response;
@@ -386,6 +386,6 @@ protected function _shutdown()
];
$result = $dispatcher->dispatchEvent('Dispatcher.afterDispatch', $args);
return $result->data['response'];
return $result->data('response');
}
}
@@ -18,8 +18,13 @@
* 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.
*
* @property string $name Name of the event
* @property object $subject The object this event applies to
* @property mixed $result Property used to retain the result value of the event listeners
* @property array $data Custom data for the method that receives the event
*/
class Event
class Event implements EventInterface
{
/**
@@ -39,16 +44,16 @@ class Event
/**
* Custom data for the method that receives the event
*
* @var mixed
* @var array
*/
public $data = null;
protected $_data;
/**
* Property used to retain the result value of the event listeners
*
* @var mixed
*/
public $result = null;
protected $_result = null;
/**
* Flags an event as stopped or not, default is false
@@ -73,13 +78,17 @@ class Event
*/
public function __construct($name, $subject = null, $data = null)
{
if ($data !== null && !is_array($data)) {
trigger_error('Data parameter will be changing to array hint typing', E_USER_DEPRECATED);
}
$this->_name = $name;
$this->data = $data;
$this->_data = (array)$data;
$this->_subject = $subject;
}
/**
* Dynamically returns the name and subject if accessed directly
* Provides read-only access for the name and subject properties.
*
* @param string $attribute Attribute name.
* @return mixed
@@ -89,6 +98,36 @@ public function __get($attribute)
if ($attribute === 'name' || $attribute === 'subject') {
return $this->{$attribute}();
}
if ($attribute === 'data')
{
trigger_error('Public read access to data is deprecated, use data()', E_USER_DEPRECATED);
return $this->_data;
}
if ($attribute === 'result')
{
trigger_error('Public read access to result is deprecated, use result()', E_USER_DEPRECATED);
return $this->_result;
}
}
/**
* Provides backward compatibility for write access to data and result properties.
*
* @param string $attribute Attribute name.
* @param mixed $value
*/
public function __set($attribute, $value)
{
if($attribute === 'data')
{
trigger_error('Public write access to data is deprecated, use setData()', E_USER_DEPRECATED);
$this->_data = (array)$value;
}
if ($attribute === 'result')
{
trigger_error('Public write access to result is deprecated, use setResult()', E_USER_DEPRECATED);
$this->_result = $value;
}
}
/**
@@ -131,13 +170,53 @@ public function isStopped()
return $this->_stopped;
}
/**
* The result value of the event listeners
*
* @return mixed
*/
public function result()
{
return $this->_result;
}
/**
* @param null $value
* @return $this
*/
public function setResult($value = null)
{
$this->_result = $value;
return $this;
}
/**
* Access the event data/payload.
*
* @return array
* @param string|null $key
* @return array|null The data payload if $key is null, or the data value for the given $key. If the $key does not
* exist a null value is returned.
*/
public function data($key = null)
{
if ($key !== null) {
return isset($this->_data[$key]) ? $this->_data[$key] : null;
}
return (array)$this->_data;
}
/**
* @param array|string $key
* @param mixed $value
* @return $this
*/
public function data()
public function setData($key, $value = null)
{
return (array)$this->data;
if(is_array($key)) {
$this->_data = $key;
} else {
$this->_data[$key] = $value;
}
return $this;
}
}
@@ -0,0 +1,82 @@
<?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 3.3.2
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Event;
/**
* Represents the transport interface for events across the system.
*/
interface EventInterface
{
/**
* Access the event data/payload.
*
* @param string|null $key The string identifier for the data payload, or if null all of the payload is returned.
* @return array|null The data payload if $key is null, or the data value for the given $key. If the $key does not
* exist a null value should be returned.
*/
public function data($key = null);
/**
* Check if the event is stopped
*
* @return bool True if the event is stopped
*/
public function isStopped();
/**
* Returns the name of this event. This is usually used as the event identifier
*
* @return string
*/
public function name();
/**
* The result value of the event listeners
*
* @return mixed
*/
public function result();
/**
* Modify the event data/payload.
*
* @param array|string $key The string identifier to be modified, or an array to replace the payload.
* @param mixed $value The payload value to be modified if $key is a string, otherwise ignored.
* @return $this
*/
public function setData($key, $value = null);
/**
* Assigns a result value for the event listener. If a result has already be assigned it will be overwritten.
*
* @param mixed $value
* @return $this
*/
public function setResult($value = null);
/**
* Stops the event from being used anymore
*
* @return void
*/
public function stopPropagation();
/**
* Returns the subject of this event
*
* @return object
*/
public function subject();
}
@@ -393,7 +393,7 @@ public function dispatch($event)
$event->stopPropagation();
}
if ($result !== null) {
$event->result = $result;
$event->setResult($result);
}
}
@@ -80,15 +80,15 @@ public function dispatch(Request $request, Response $response)
}
$beforeEvent = $this->dispatchEvent('Dispatcher.beforeDispatch', compact('request', 'response'));
$request = $beforeEvent->data['request'];
if ($beforeEvent->result instanceof Response) {
return $beforeEvent->result;
$request = $beforeEvent->data('request');
if ($beforeEvent->result() instanceof Response) {
return $beforeEvent->result();
}
// Use the controller built by an beforeDispatch
// event handler if there is one.
if (isset($beforeEvent->data['controller'])) {
$controller = $beforeEvent->data['controller'];
if ($beforeEvent->data('controller') instanceof Controller) {
$controller = $beforeEvent->data('controller');
} else {
$controller = $this->factory->create($request, $response);
}
@@ -100,7 +100,7 @@ public function dispatch(Request $request, Response $response)
$afterEvent = $this->dispatchEvent('Dispatcher.afterDispatch', compact('request', 'response'));
return $afterEvent->data['response'];
return $afterEvent->data('response');
}
/**
@@ -1542,7 +1542,7 @@ protected function _processSave($entity, $options)
$event = $this->dispatchEvent('Model.beforeSave', compact('entity', 'options'));
if ($event->isStopped()) {
return $event->result;
return $event->result();
}
$saved = $this->_associations->saveParents(
@@ -1879,7 +1879,7 @@ protected function _processDelete($entity, $options)
]);
if ($event->isStopped()) {
return $event->result;
return $event->result();
}
$this->_associations->cascadeDelete(
@@ -156,7 +156,7 @@ public function handle(Event $event)
*/
public function matches(Event $event)
{
$request = $event->data['request'];
$request = $event->data('request');
$pass = true;
if (!empty($this->_config['for'])) {
$len = strlen('preg:');
@@ -169,7 +169,7 @@ public function matches(Event $event)
}
}
if ($pass && !empty($this->_config['when'])) {
$response = $event->data['response'];
$response = $event->data('response');
$pass = $this->_config['when']($request, $response);
}
Oops, something went wrong.

0 comments on commit 9ada469

Please sign in to comment.
You can’t perform that action at this time.