Skip to content

Commit

Permalink
Merge pull request #1397 from markstory/3.0-helper-events
Browse files Browse the repository at this point in the history
3.0 - Event simplification and Helper event migration
  • Loading branch information
markstory committed Jul 10, 2013
2 parents 0c78675 + 3040dbe commit a7cb84a
Show file tree
Hide file tree
Showing 16 changed files with 259 additions and 333 deletions.
28 changes: 16 additions & 12 deletions lib/Cake/Event/Event.php
@@ -1,20 +1,16 @@
<?php
/**
*
* PHP 5
*
* 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
* @package Cake.Observer
* @since CakePHP(tm) v 2.1
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 2.1
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Event;

Expand Down Expand Up @@ -65,17 +61,16 @@ class Event {
/**
* 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 Event('Order.afterBuy', $this, array('buyer' => $userData));
* $event = new Event('User.afterRegister', $UserModel);
* }}}
*
* @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 array $data any value you wish to be transported with this event to it can be read by listeners
*/
public function __construct($name, $subject = null, $data = null) {
$this->_name = $name;
Expand Down Expand Up @@ -131,4 +126,13 @@ public function isStopped() {
return $this->_stopped;
}

/**
* Access the event data/payload.
*
* @return array
*/
public function data() {
return (array)$this->data;
}

}
61 changes: 40 additions & 21 deletions lib/Cake/Event/EventManager.php
@@ -1,20 +1,16 @@
<?php
/**
*
* PHP 5
*
* 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
* @package Cake.Event
* @since CakePHP(tm) v 2.1
* @license http://www.opensource.org/licenses/mit-license.php MIT License
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since CakePHP(tm) v 2.1
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Event;

Expand All @@ -25,8 +21,6 @@
* 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
* and pass it around to manage all events in your app.
*
* @package Cake.Event
*/
class EventManager {

Expand Down Expand Up @@ -82,7 +76,7 @@ public static function instance($manager = null) {
}

/**
* Adds a new listener to an event. Listeners
* Adds a new listener to an event.
*
* @param callback|Cake\Event\EventListener $callable PHP valid callback type or instance of Cake\Event\EventListener to be called
* when the event named with $eventKey is triggered. If a Cake\Event\EventListener instance is passed, then the `implementedEvents`
Expand All @@ -92,10 +86,9 @@ public static function instance($manager = null) {
* @param string $eventKey The event unique identifier name with which the callback will be associated. If $callable
* is an instance of Cake\Event\EventListener this argument will be ignored
*
* @param array $options used to set the `priority` and `passParams` flags to the listener.
* @param array $options used to set the `priority` flag to the listener. In the future more options may be added.
* Priorities are handled like queues, and multiple attachments added to 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 Cake\Event\EventListener, this parameter will be ignored
* the order of insertion.
*
* @return void
* @throws InvalidArgumentException When event key is missing or callable is not an
Expand All @@ -109,10 +102,9 @@ public function attach($callable, $eventKey = null, $options = array()) {
$this->_attachSubscriber($callable);
return;
}
$options = $options + array('priority' => static::$defaultPriority, 'passParams' => false);
$options = $options + array('priority' => static::$defaultPriority);
$this->_listeners[$eventKey][$options['priority']][] = array(
'callable' => $callable,
'passParams' => $options['passParams'],
);
}

Expand Down Expand Up @@ -243,11 +235,7 @@ public function dispatch($event) {
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);
}
$result = $this->_callListener($listener['callable'], $event);
if ($result === false) {
$event->stopPropagation();
}
Expand All @@ -258,6 +246,37 @@ public function dispatch($event) {
}
}

/**
* Calls a listener.
*
* Direct callback invocation is up to 30% faster than using call_user_func_array.
* Optimize the common cases to provide improved performance.
*
* @param callable $listener The listener to trigger.
* @param Event $event
* @return mixed The result of the $listener function.
*/
protected function _callListener(callable $listener, Event $event) {
$data = $event->data();
$length = count($data);
if ($length) {
$data = array_values($data);
}
switch ($length) {
case 0:
return $listener($event);
case 1:
return $listener($event, $data[0]);
case 2:
return $listener($event, $data[0], $data[1]);
case 3:
return $listener($event, $data[0], $data[1], $data[2]);
default:
array_unshift($data, $event);
return call_user_func_array($listener, $data);
}
}

/**
* Returns a list of all listeners for an eventKey in the order they should be called
*
Expand Down
31 changes: 0 additions & 31 deletions lib/Cake/Test/TestApp/View/Posts/test_nocache_tags.ctp
@@ -1,5 +1,4 @@
<?php
use Cake\Cache\Cache;
use Cake\Model\ConnectionManager;
use Cake\Core\Configure;
?>
Expand All @@ -17,36 +16,6 @@ use Cake\Core\Configure;
</span>
<!--/nocache-->
</p>
<p>
<span class="notice">
<?php
echo __d('cake', 'Your cache is ');
if (Cache::engine('default')):
echo __d('cake', 'set up and initialized properly.');
$settings = Cache::settings();
echo '<p>' . $settings['engine'];
echo __d('cake', ' is being used to cache, to change this edit App/Config/cache.php ');
echo '</p>';

echo 'Settings: <ul>';
foreach ($settings as $name => $value):
if (is_array($value)):
$value = join(',', $value);
endif;
echo '<li>' . $name . ': ' . $value . '</li>';
endforeach;
echo '</ul>';

else:
echo __d('cake', 'NOT working.');
echo '<br />';
if (is_writable(TMP)):
echo __d('cake', 'Edit: App/Config/cache.php to insure you have the newset version of this file and the variable $cakeCache set properly');
endif;
endif;
?>
</span>
</p>
<p>
<span class="notice">
<?php
Expand Down

0 comments on commit a7cb84a

Please sign in to comment.