2011-12-13 04:52:40 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2017-06-10 21:33:55 +00:00
|
|
|
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
|
2017-06-10 22:10:52 +00:00
|
|
|
* Copyright (c) Cake Software Foundation, Inc. (https://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.
|
|
|
|
*
|
2017-06-10 22:10:52 +00:00
|
|
|
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
|
2017-06-10 21:33:55 +00:00
|
|
|
* @link https://cakephp.org CakePHP(tm) Project
|
2011-12-13 04:52:40 +00:00
|
|
|
* @package Cake.Event
|
|
|
|
* @since CakePHP(tm) v 2.1
|
2017-06-10 22:23:14 +00:00
|
|
|
* @license https://opensource.org/licenses/mit-license.php MIT License
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
|
2011-12-13 05:25:28 +00:00
|
|
|
App::uses('CakeEventListener', 'Event');
|
2019-06-20 01:52:57 +00:00
|
|
|
App::uses('CakeEvent', 'Event');
|
2011-12-13 04:52:40 +00:00
|
|
|
|
|
|
|
/**
|
2012-07-27 02:30:17 +00:00
|
|
|
* The event manager is responsible for keeping track of event listeners, passing the correct
|
|
|
|
* data to them, and firing them in the correct order, when associated events are triggered. You
|
|
|
|
* can create multiple instances of this object to manage local events or keep a single instance
|
2011-12-13 04:52:40 +00:00
|
|
|
* and pass it around to manage all events in your app.
|
|
|
|
*
|
|
|
|
* @package Cake.Event
|
|
|
|
*/
|
|
|
|
class CakeEventManager {
|
|
|
|
|
|
|
|
/**
|
2012-07-27 02:30:17 +00:00
|
|
|
* The default priority queue value for new, attached listeners
|
2011-12-13 04:52:40 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var int
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
public static $defaultPriority = 10;
|
|
|
|
|
2011-12-14 05:05:11 +00:00
|
|
|
/**
|
|
|
|
* The globally available instance, used for dispatching events attached from any scope
|
|
|
|
*
|
|
|
|
* @var CakeEventManager
|
|
|
|
*/
|
|
|
|
protected static $_generalManager = null;
|
|
|
|
|
2011-12-13 04:52:40 +00:00
|
|
|
/**
|
|
|
|
* List of listener callbacks associated to
|
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var object
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
protected $_listeners = array();
|
|
|
|
|
2011-12-14 05:05:11 +00:00
|
|
|
/**
|
2012-07-27 02:30:17 +00:00
|
|
|
* Internal flag to distinguish a common manager from the singleton
|
2011-12-14 05:05:11 +00:00
|
|
|
*
|
2014-07-03 13:36:42 +00:00
|
|
|
* @var bool
|
2011-12-14 05:05:11 +00:00
|
|
|
*/
|
|
|
|
protected $_isGlobal = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the globally available instance of a CakeEventManager
|
|
|
|
* this is used for dispatching events attached from outside the scope
|
|
|
|
* other managers were created. Usually for creating hook systems or inter-class
|
|
|
|
* communication
|
|
|
|
*
|
2012-07-27 02:30:17 +00:00
|
|
|
* If called with the first parameter, it will be set as the globally available instance
|
2011-12-14 05:05:11 +00:00
|
|
|
*
|
2014-06-02 12:57:11 +00:00
|
|
|
* @param CakeEventManager $manager Optional event manager instance.
|
2011-12-14 05:05:11 +00:00
|
|
|
* @return CakeEventManager the global event manager
|
|
|
|
*/
|
|
|
|
public static function instance($manager = null) {
|
|
|
|
if ($manager instanceof CakeEventManager) {
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$_generalManager = $manager;
|
2011-12-14 05:05:11 +00:00
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
if (empty(static::$_generalManager)) {
|
|
|
|
static::$_generalManager = new CakeEventManager();
|
2011-12-14 05:05:11 +00:00
|
|
|
}
|
|
|
|
|
2015-07-21 08:22:53 +00:00
|
|
|
static::$_generalManager->_isGlobal = true;
|
|
|
|
return static::$_generalManager;
|
2011-12-14 05:05:11 +00:00
|
|
|
}
|
|
|
|
|
2011-12-13 04:52:40 +00:00
|
|
|
/**
|
2012-07-18 01:55:29 +00:00
|
|
|
* Adds a new listener to an event. Listeners
|
2011-12-13 04:52:40 +00:00
|
|
|
*
|
2020-04-21 09:32:16 +00:00
|
|
|
* @param callable|CakeEventListener $callable PHP valid callback type or instance of CakeEventListener to be called
|
2012-07-27 02:30:17 +00:00
|
|
|
* when the event named with $eventKey is triggered. If a CakeEventListener instance is passed, then the `implementedEvents`
|
2011-12-25 01:07:22 +00:00
|
|
|
* method will be called on the object to register the declared events individually as methods to be managed by this class.
|
|
|
|
* It is possible to define multiple event handlers per event name.
|
|
|
|
*
|
2012-07-27 02:30:17 +00:00
|
|
|
* @param string $eventKey The event unique identifier name with which the callback will be associated. If $callable
|
2011-12-13 05:25:28 +00:00
|
|
|
* is an instance of CakeEventListener this argument will be ignored
|
2011-12-25 01:07:22 +00:00
|
|
|
*
|
2011-12-13 05:25:28 +00:00
|
|
|
* @param array $options used to set the `priority` and `passParams` flags to the listener.
|
2012-07-27 02:30:17 +00:00
|
|
|
* Priorities are handled like queues, and multiple attachments added to the same priority queue will be treated in
|
2011-12-13 05:25:28 +00:00
|
|
|
* the order of insertion. `passParams` means that the event data property will be converted to function arguments
|
|
|
|
* when the listener is called. If $called is an instance of CakeEventListener, this parameter will be ignored
|
2011-12-25 01:07:22 +00:00
|
|
|
*
|
2011-12-13 04:52:40 +00:00
|
|
|
* @return void
|
2012-03-04 00:55:18 +00:00
|
|
|
* @throws InvalidArgumentException When event key is missing or callable is not an
|
|
|
|
* instance of CakeEventListener.
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
2011-12-13 05:25:28 +00:00
|
|
|
public function attach($callable, $eventKey = null, $options = array()) {
|
|
|
|
if (!$eventKey && !($callable instanceof CakeEventListener)) {
|
|
|
|
throw new InvalidArgumentException(__d('cake_dev', 'The eventKey variable is required'));
|
|
|
|
}
|
|
|
|
if ($callable instanceof CakeEventListener) {
|
2011-12-25 01:07:22 +00:00
|
|
|
$this->_attachSubscriber($callable);
|
2011-12-13 05:25:28 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
$options = $options + array('priority' => static::$defaultPriority, 'passParams' => false);
|
2011-12-13 04:52:40 +00:00
|
|
|
$this->_listeners[$eventKey][$options['priority']][] = array(
|
|
|
|
'callable' => $callable,
|
|
|
|
'passParams' => $options['passParams'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2011-12-25 01:07:22 +00:00
|
|
|
/**
|
|
|
|
* Auxiliary function to attach all implemented callbacks of a CakeEventListener class instance
|
|
|
|
* as individual methods on this manager
|
|
|
|
*
|
2014-06-02 12:57:11 +00:00
|
|
|
* @param CakeEventListener $subscriber Event listener.
|
2011-12-25 01:07:22 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _attachSubscriber(CakeEventListener $subscriber) {
|
2013-12-04 03:39:14 +00:00
|
|
|
foreach ((array)$subscriber->implementedEvents() as $eventKey => $function) {
|
2011-12-25 01:07:22 +00:00
|
|
|
$options = array();
|
|
|
|
$method = $function;
|
|
|
|
if (is_array($function) && isset($function['callable'])) {
|
|
|
|
list($method, $options) = $this->_extractCallable($function, $subscriber);
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif (is_array($function) && is_numeric(key($function))) {
|
2011-12-25 01:07:22 +00:00
|
|
|
foreach ($function as $f) {
|
|
|
|
list($method, $options) = $this->_extractCallable($f, $subscriber);
|
|
|
|
$this->attach($method, $eventKey, $options);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (is_string($method)) {
|
|
|
|
$method = array($subscriber, $function);
|
|
|
|
}
|
|
|
|
$this->attach($method, $eventKey, $options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Auxiliary function to extract and return a PHP callback type out of the callable definition
|
|
|
|
* from the return value of the `implementedEvents` method on a CakeEventListener
|
|
|
|
*
|
2012-07-27 02:30:17 +00:00
|
|
|
* @param array $function the array taken from a handler definition for an event
|
2011-12-25 01:07:22 +00:00
|
|
|
* @param CakeEventListener $object The handler object
|
2020-04-21 09:32:16 +00:00
|
|
|
* @return callable
|
2011-12-25 01:07:22 +00:00
|
|
|
*/
|
|
|
|
protected function _extractCallable($function, $object) {
|
|
|
|
$method = $function['callable'];
|
|
|
|
$options = $function;
|
|
|
|
unset($options['callable']);
|
|
|
|
if (is_string($method)) {
|
|
|
|
$method = array($object, $method);
|
|
|
|
}
|
|
|
|
return array($method, $options);
|
|
|
|
}
|
|
|
|
|
2011-12-13 04:52:40 +00:00
|
|
|
/**
|
|
|
|
* Removes a listener from the active listeners.
|
|
|
|
*
|
2020-04-21 09:32:16 +00:00
|
|
|
* @param callable|CakeEventListener $callable any valid PHP callback type or an instance of CakeEventListener
|
2012-11-28 22:30:47 +00:00
|
|
|
* @param string $eventKey The event unique identifier name with which the callback has been associated
|
2011-12-13 04:52:40 +00:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function detach($callable, $eventKey = null) {
|
2011-12-14 03:59:36 +00:00
|
|
|
if ($callable instanceof CakeEventListener) {
|
|
|
|
return $this->_detachSubscriber($callable, $eventKey);
|
|
|
|
}
|
2011-12-13 04:52:40 +00:00
|
|
|
if (empty($eventKey)) {
|
|
|
|
foreach (array_keys($this->_listeners) as $eventKey) {
|
|
|
|
$this->detach($callable, $eventKey);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (empty($this->_listeners[$eventKey])) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
foreach ($this->_listeners[$eventKey] as $priority => $callables) {
|
|
|
|
foreach ($callables as $k => $callback) {
|
|
|
|
if ($callback['callable'] === $callable) {
|
|
|
|
unset($this->_listeners[$eventKey][$priority][$k]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-14 03:59:36 +00:00
|
|
|
/**
|
|
|
|
* Auxiliary function to help detach all listeners provided by an object implementing CakeEventListener
|
|
|
|
*
|
|
|
|
* @param CakeEventListener $subscriber the subscriber to be detached
|
|
|
|
* @param string $eventKey optional event key name to unsubscribe the listener from
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function _detachSubscriber(CakeEventListener $subscriber, $eventKey = null) {
|
2013-12-04 18:56:02 +00:00
|
|
|
$events = (array)$subscriber->implementedEvents();
|
2011-12-14 03:59:36 +00:00
|
|
|
if (!empty($eventKey) && empty($events[$eventKey])) {
|
|
|
|
return;
|
2012-02-23 13:38:02 +00:00
|
|
|
} elseif (!empty($eventKey)) {
|
2011-12-14 03:59:36 +00:00
|
|
|
$events = array($eventKey => $events[$eventKey]);
|
|
|
|
}
|
|
|
|
foreach ($events as $key => $function) {
|
|
|
|
if (is_array($function)) {
|
2011-12-25 01:07:22 +00:00
|
|
|
if (is_numeric(key($function))) {
|
|
|
|
foreach ($function as $handler) {
|
|
|
|
$handler = isset($handler['callable']) ? $handler['callable'] : $handler;
|
|
|
|
$this->detach(array($subscriber, $handler), $key);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2011-12-14 03:59:36 +00:00
|
|
|
$function = $function['callable'];
|
|
|
|
}
|
|
|
|
$this->detach(array($subscriber, $function), $key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-13 04:52:40 +00:00
|
|
|
/**
|
|
|
|
* Dispatches a new event to all configured listeners
|
|
|
|
*
|
2012-05-13 00:43:31 +00:00
|
|
|
* @param string|CakeEvent $event the event key name or instance of CakeEvent
|
2014-04-06 19:17:51 +00:00
|
|
|
* @return CakeEvent
|
2014-11-30 21:45:40 +00:00
|
|
|
* @triggers $event
|
2011-12-13 04:52:40 +00:00
|
|
|
*/
|
|
|
|
public function dispatch($event) {
|
|
|
|
if (is_string($event)) {
|
2012-01-05 17:31:00 +00:00
|
|
|
$event = new CakeEvent($event);
|
2011-12-13 04:52:40 +00:00
|
|
|
}
|
2011-12-14 05:05:11 +00:00
|
|
|
|
2013-09-16 07:38:54 +00:00
|
|
|
$listeners = $this->listeners($event->name());
|
|
|
|
if (empty($listeners)) {
|
2014-04-06 19:17:51 +00:00
|
|
|
return $event;
|
2011-12-13 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-16 07:38:54 +00:00
|
|
|
foreach ($listeners as $listener) {
|
2011-12-13 04:52:40 +00:00
|
|
|
if ($event->isStopped()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ($listener['passParams'] === true) {
|
2011-12-26 02:22:12 +00:00
|
|
|
$result = call_user_func_array($listener['callable'], $event->data);
|
2011-12-13 04:52:40 +00:00
|
|
|
} else {
|
2011-12-26 02:22:12 +00:00
|
|
|
$result = call_user_func($listener['callable'], $event);
|
|
|
|
}
|
|
|
|
if ($result === false) {
|
|
|
|
$event->stopPropagation();
|
|
|
|
}
|
|
|
|
if ($result !== null) {
|
|
|
|
$event->result = $result;
|
2011-12-13 04:52:40 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-06 19:17:51 +00:00
|
|
|
return $event;
|
2011-12-13 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-07-27 02:30:17 +00:00
|
|
|
* Returns a list of all listeners for an eventKey in the order they should be called
|
2011-12-13 04:52:40 +00:00
|
|
|
*
|
2014-06-02 12:57:11 +00:00
|
|
|
* @param string $eventKey Event key.
|
2011-12-13 04:52:40 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function listeners($eventKey) {
|
2014-02-26 04:32:21 +00:00
|
|
|
$localListeners = array();
|
|
|
|
$priorities = array();
|
2013-09-13 10:41:41 +00:00
|
|
|
if (!$this->_isGlobal) {
|
2014-02-26 04:32:21 +00:00
|
|
|
$localListeners = $this->prioritisedListeners($eventKey);
|
|
|
|
$localListeners = empty($localListeners) ? array() : $localListeners;
|
2013-09-13 10:41:41 +00:00
|
|
|
}
|
2015-07-21 08:22:53 +00:00
|
|
|
$globalListeners = static::instance()->prioritisedListeners($eventKey);
|
2014-02-26 04:32:21 +00:00
|
|
|
$globalListeners = empty($globalListeners) ? array() : $globalListeners;
|
2013-09-13 10:41:41 +00:00
|
|
|
|
2014-02-26 04:32:21 +00:00
|
|
|
$priorities = array_merge(array_keys($globalListeners), array_keys($localListeners));
|
|
|
|
$priorities = array_unique($priorities);
|
|
|
|
asort($priorities);
|
2013-09-13 08:56:38 +00:00
|
|
|
|
2012-01-12 23:43:56 +00:00
|
|
|
$result = array();
|
2014-02-26 04:32:21 +00:00
|
|
|
foreach ($priorities as $priority) {
|
|
|
|
if (isset($globalListeners[$priority])) {
|
|
|
|
$result = array_merge($result, $globalListeners[$priority]);
|
|
|
|
}
|
|
|
|
if (isset($localListeners[$priority])) {
|
|
|
|
$result = array_merge($result, $localListeners[$priority]);
|
|
|
|
}
|
2012-01-12 23:43:56 +00:00
|
|
|
}
|
|
|
|
return $result;
|
2011-12-13 04:52:40 +00:00
|
|
|
}
|
|
|
|
|
2013-09-13 10:41:41 +00:00
|
|
|
/**
|
|
|
|
* Returns the listeners for the specified event key indexed by priority
|
|
|
|
*
|
2014-06-02 12:57:11 +00:00
|
|
|
* @param string $eventKey Event key.
|
2013-09-13 10:41:41 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function prioritisedListeners($eventKey) {
|
|
|
|
if (empty($this->_listeners[$eventKey])) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
return $this->_listeners[$eventKey];
|
|
|
|
}
|
2012-01-12 23:43:56 +00:00
|
|
|
}
|