summaryrefslogtreecommitdiff
path: root/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event
diff options
context:
space:
mode:
authorLudovic Pouzenc <ludovic@pouzenc.fr>2012-08-02 11:09:40 +0000
committerLudovic Pouzenc <ludovic@pouzenc.fr>2012-08-02 11:09:40 +0000
commit6dfd5d507d9863f987b30b0a5ab4268aea2ed875 (patch)
tree9c3de66b4aaa1e6bfa3c6442d807ec70bc479d11 /poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event
parentf4792ba1a7785220fef5adb1cb3e7ce8ac40152f (diff)
download2012-php-weave-6dfd5d507d9863f987b30b0a5ab4268aea2ed875.tar.gz
2012-php-weave-6dfd5d507d9863f987b30b0a5ab4268aea2ed875.tar.bz2
2012-php-weave-6dfd5d507d9863f987b30b0a5ab4268aea2ed875.zip
J'étais parti sur un download pourri de Cake. Les gars on abusé sur GitHub.
git-svn-id: file:///var/svn/2012-php-weave/trunk@7 d972a294-176a-4cf9-8ea1-fcd5b0c30f5c
Diffstat (limited to 'poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event')
-rw-r--r--poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEvent.php132
-rw-r--r--poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEventListener.php48
-rw-r--r--poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEventManager.php276
3 files changed, 456 insertions, 0 deletions
diff --git a/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEvent.php b/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEvent.php
new file mode 100644
index 0000000..52de3cf
--- /dev/null
+++ b/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEvent.php
@@ -0,0 +1,132 @@
+<?php
+/**
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @package Cake.Observer
+ * @since CakePHP(tm) v 2.1
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+/**
+ * Represent the transport class of events across the system, it receives a name, and 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 is applying to.
+ *
+ * @package Cake.Event
+ */
+class CakeEvent {
+
+/**
+ * Name of the event
+ *
+ * @var string $name
+ */
+ 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 $data
+ */
+ public $data = null;
+
+/**
+ * Property used to retain the result value of the event listeners
+ *
+ * @var mixed $result
+ */
+ public $result = null;
+
+/**
+ * Flags an event as stopped or not, default is false
+ *
+ * @var boolean
+ */
+ 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:
+ *
+ * {{{
+ * $event = new CakeEvent('Order.afterBuy', $this, array('buyer' => $userData));
+ * $event = new CakeEvent('User.afterRegister', $UserModel);
+ * }}}
+ *
+ */
+ 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
+ * @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() {
+ return $this->_stopped = true;
+ }
+
+/**
+ * Check if the event is stopped
+ *
+ * @return boolean True if the event is stopped
+ */
+ public function isStopped() {
+ return $this->_stopped;
+ }
+
+}
diff --git a/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEventListener.php b/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEventListener.php
new file mode 100644
index 0000000..c491594
--- /dev/null
+++ b/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEventListener.php
@@ -0,0 +1,48 @@
+<?php
+/**
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @package Cake.Observer
+ * @since CakePHP(tm) v 2.1
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+/**
+ * Objects implementing this interface should declare the `implementedEvents` function
+ * to hint the event manager what methods should be called when an event is triggered.
+ *
+ * @package Cake.Event
+ */
+interface CakeEventListener {
+
+/**
+ * Returns a list of events this object is implementing, when the class is registered
+ * in an event manager, each individual method will be associated to the respective event.
+ *
+ * ## Example:
+ *
+ * {{{
+ * public function implementedEvents() {
+ * return array(
+ * 'Order.complete' => 'sendEmail',
+ * 'Article.afterBuy' => 'decrementInventory',
+ * 'User.onRegister' => array('callable' => 'logRegistration', 'priority' => 20, 'passParams' => true)
+ * );
+ * }
+ * }}}
+ *
+ * @return array associative array or event key names pointing to the function
+ * that should be called in the object when the respective event is fired
+ */
+ public function implementedEvents();
+
+}
diff --git a/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEventManager.php b/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEventManager.php
new file mode 100644
index 0000000..252364d
--- /dev/null
+++ b/poc/poc02-compiling-cake/src/vendor/cakephp-2.2.1-0-gcc44130/lib/Cake/Event/CakeEventManager.php
@@ -0,0 +1,276 @@
+<?php
+/**
+ *
+ * PHP 5
+ *
+ * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
+ * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * Redistributions of files must retain the above copyright notice.
+ *
+ * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
+ * @link http://cakephp.org CakePHP(tm) Project
+ * @package Cake.Event
+ * @since CakePHP(tm) v 2.1
+ * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
+ */
+
+App::uses('CakeEventListener', 'Event');
+
+/**
+ * The event manager is responsible for keeping track of event listeners and pass the correct
+ * data to them, and fire them in the correct order, when associated events are triggered. You
+ * can create multiple instances of this objects to manage local events or keep a single instance
+ * and pass it around to manage all events in your app.
+ *
+ * @package Cake.Event
+ */
+class CakeEventManager {
+
+/**
+ * The default priority queue value for new attached listeners
+ *
+ * @var int
+ */
+ public static $defaultPriority = 10;
+
+/**
+ * The globally available instance, used for dispatching events attached from any scope
+ *
+ * @var CakeEventManager
+ */
+ protected static $_generalManager = null;
+
+/**
+ * List of listener callbacks associated to
+ *
+ * @var object $Listeners
+ */
+ protected $_listeners = array();
+
+/**
+ * Internal flag to distinguish a common manager from the sigleton
+ *
+ * @var boolean
+ */
+ 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
+ *
+ * If called with a first params, it will be set as the globally available instance
+ *
+ * @param CakeEventManager $manager
+ * @return CakeEventManager the global event manager
+ */
+ public static function instance($manager = null) {
+ if ($manager instanceof CakeEventManager) {
+ self::$_generalManager = $manager;
+ }
+ if (empty(self::$_generalManager)) {
+ self::$_generalManager = new CakeEventManager;
+ }
+
+ self::$_generalManager->_isGlobal = true;
+ return self::$_generalManager;
+ }
+
+/**
+ * Adds a new listener to an event. Listeners
+ *
+ * @param callback|CakeEventListener $callable PHP valid callback type or instance of CakeEventListener to be called
+ * when the event named with $eventKey is triggered. If a CakeEventListener instances is passed, then the `implementedEvents`
+ * 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.
+ *
+ * @param string $eventKey The event unique identifier name to with the callback will be associated. If $callable
+ * is an instance of CakeEventListener this argument will be ignored
+ *
+ * @param array $options used to set the `priority` and `passParams` flags to the listener.
+ * Priorities are handled like queues, and multiple attachments into the same priority queue will be treated in
+ * 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
+ *
+ * @return void
+ * @throws InvalidArgumentException When event key is missing or callable is not an
+ * instance of CakeEventListener.
+ */
+ 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) {
+ $this->_attachSubscriber($callable);
+ return;
+ }
+ $options = $options + array('priority' => self::$defaultPriority, 'passParams' => false);
+ $this->_listeners[$eventKey][$options['priority']][] = array(
+ 'callable' => $callable,
+ 'passParams' => $options['passParams'],
+ );
+ }
+
+/**
+ * Auxiliary function to attach all implemented callbacks of a CakeEventListener class instance
+ * as individual methods on this manager
+ *
+ * @param CakeEventListener $subscriber
+ * @return void
+ */
+ protected function _attachSubscriber(CakeEventListener $subscriber) {
+ foreach ($subscriber->implementedEvents() as $eventKey => $function) {
+ $options = array();
+ $method = $function;
+ if (is_array($function) && isset($function['callable'])) {
+ list($method, $options) = $this->_extractCallable($function, $subscriber);
+ } elseif (is_array($function) && is_numeric(key($function))) {
+ 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
+ *
+ * @param array $function the array taken from a handler definition for a event
+ * @param CakeEventListener $object The handler object
+ * @return callback
+ */
+ 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);
+ }
+
+/**
+ * Removes a listener from the active listeners.
+ *
+ * @param callback|CakeEventListener $callable any valid PHP callback type or an instance of CakeEventListener
+ * @return void
+ */
+ public function detach($callable, $eventKey = null) {
+ if ($callable instanceof CakeEventListener) {
+ return $this->_detachSubscriber($callable, $eventKey);
+ }
+ 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;
+ }
+ }
+ }
+ }
+
+/**
+ * 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) {
+ $events = $subscriber->implementedEvents();
+ if (!empty($eventKey) && empty($events[$eventKey])) {
+ return;
+ } elseif (!empty($eventKey)) {
+ $events = array($eventKey => $events[$eventKey]);
+ }
+ foreach ($events as $key => $function) {
+ if (is_array($function)) {
+ if (is_numeric(key($function))) {
+ foreach ($function as $handler) {
+ $handler = isset($handler['callable']) ? $handler['callable'] : $handler;
+ $this->detach(array($subscriber, $handler), $key);
+ }
+ continue;
+ }
+ $function = $function['callable'];
+ }
+ $this->detach(array($subscriber, $function), $key);
+ }
+ }
+
+/**
+ * Dispatches a new event to all configured listeners
+ *
+ * @param string|CakeEvent $event the event key name or instance of CakeEvent
+ * @return void
+ */
+ public function dispatch($event) {
+ if (is_string($event)) {
+ $event = new CakeEvent($event);
+ }
+
+ if (!$this->_isGlobal) {
+ self::instance()->dispatch($event);
+ }
+
+ if (empty($this->_listeners[$event->name()])) {
+ return;
+ }
+
+ foreach ($this->listeners($event->name()) as $listener) {
+ if ($event->isStopped()) {
+ break;
+ }
+ if ($listener['passParams'] === true) {
+ $result = call_user_func_array($listener['callable'], $event->data);
+ } else {
+ $result = call_user_func($listener['callable'], $event);
+ }
+ if ($result === false) {
+ $event->stopPropagation();
+ }
+ if ($result !== null) {
+ $event->result = $result;
+ }
+ continue;
+ }
+ }
+
+/**
+ * Returns a list of all listeners for a eventKey in the order they should be called
+ *
+ * @param string $eventKey
+ * @return array
+ */
+ public function listeners($eventKey) {
+ if (empty($this->_listeners[$eventKey])) {
+ return array();
+ }
+ ksort($this->_listeners[$eventKey]);
+ $result = array();
+ foreach ($this->_listeners[$eventKey] as $priorityQ) {
+ $result = array_merge($result, $priorityQ);
+ }
+ return $result;
+ }
+
+}