Skip to content

Commit 63b9b68

Browse files
committed
Clean up Event system.
* Remove passParams. This option is now enabled always implicitly. You cannot turn it off. * Update Event class to make data protected and expose data() method. * Update tests. This change simplifies the event system a bit and removes additional mostly useless configuration.
1 parent acf6949 commit 63b9b68

File tree

3 files changed

+65
-67
lines changed

3 files changed

+65
-67
lines changed

lib/Cake/Event/Event.php

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22
/**
3-
*
43
* PHP 5
54
*
65
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
@@ -10,11 +9,11 @@
109
* For full copyright and license information, please see the LICENSE.txt
1110
* Redistributions of files must retain the above copyright notice.
1211
*
13-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
14-
* @link http://cakephp.org CakePHP(tm) Project
15-
* @package Cake.Observer
16-
* @since CakePHP(tm) v 2.1
17-
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13+
* @link http://cakephp.org CakePHP(tm) Project
14+
* @package Cake.Observer
15+
* @since CakePHP(tm) v 2.1
16+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
1817
*/
1918
namespace Cake\Event;
2019

@@ -46,7 +45,7 @@ class Event {
4645
*
4746
* @var mixed $data
4847
*/
49-
public $data = null;
48+
protected $_data = null;
5049

5150
/**
5251
* Property used to retain the result value of the event listeners
@@ -65,21 +64,20 @@ class Event {
6564
/**
6665
* Constructor
6766
*
68-
* @param string $name Name of the event
69-
* @param object $subject the object that this event applies to (usually the object that is generating the event)
70-
* @param mixed $data any value you wish to be transported with this event to it can be read by listeners
71-
*
7267
* ## Examples of usage:
7368
*
7469
* {{{
7570
* $event = new Event('Order.afterBuy', $this, array('buyer' => $userData));
7671
* $event = new Event('User.afterRegister', $UserModel);
7772
* }}}
7873
*
74+
* @param string $name Name of the event
75+
* @param object $subject the object that this event applies to (usually the object that is generating the event)
76+
* @param array $data any value you wish to be transported with this event to it can be read by listeners
7977
*/
8078
public function __construct($name, $subject = null, $data = null) {
8179
$this->_name = $name;
82-
$this->data = $data;
80+
$this->_data = $data;
8381
$this->_subject = $subject;
8482
}
8583

@@ -90,7 +88,7 @@ public function __construct($name, $subject = null, $data = null) {
9088
* @return mixed
9189
*/
9290
public function __get($attribute) {
93-
if ($attribute === 'name' || $attribute === 'subject') {
91+
if ($attribute === 'data' || $attribute === 'name' || $attribute === 'subject') {
9492
return $this->{$attribute}();
9593
}
9694
}
@@ -131,4 +129,13 @@ public function isStopped() {
131129
return $this->_stopped;
132130
}
133131

132+
/**
133+
* Access the event data/payload.
134+
*
135+
* @return array
136+
*/
137+
public function data() {
138+
return (array)$this->_data;
139+
}
140+
134141
}

lib/Cake/Event/EventManager.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ public static function instance($manager = null) {
8282
}
8383

8484
/**
85-
* Adds a new listener to an event. Listeners
85+
* Adds a new listener to an event.
8686
*
8787
* @param callback|Cake\Event\EventListener $callable PHP valid callback type or instance of Cake\Event\EventListener to be called
8888
* when the event named with $eventKey is triggered. If a Cake\Event\EventListener instance is passed, then the `implementedEvents`
@@ -92,10 +92,9 @@ public static function instance($manager = null) {
9292
* @param string $eventKey The event unique identifier name with which the callback will be associated. If $callable
9393
* is an instance of Cake\Event\EventListener this argument will be ignored
9494
*
95-
* @param array $options used to set the `priority` and `passParams` flags to the listener.
95+
* @param array $options used to set the `priority` flag to the listener. In the future more options may be added.
9696
* Priorities are handled like queues, and multiple attachments added to the same priority queue will be treated in
97-
* the order of insertion. `passParams` means that the event data property will be converted to function arguments
98-
* when the listener is called. If $called is an instance of Cake\Event\EventListener, this parameter will be ignored
97+
* the order of insertion.
9998
*
10099
* @return void
101100
* @throws InvalidArgumentException When event key is missing or callable is not an
@@ -109,10 +108,9 @@ public function attach($callable, $eventKey = null, $options = array()) {
109108
$this->_attachSubscriber($callable);
110109
return;
111110
}
112-
$options = $options + array('priority' => static::$defaultPriority, 'passParams' => false);
111+
$options = $options + array('priority' => static::$defaultPriority);
113112
$this->_listeners[$eventKey][$options['priority']][] = array(
114113
'callable' => $callable,
115-
'passParams' => $options['passParams'],
116114
);
117115
}
118116

@@ -243,8 +241,10 @@ public function dispatch($event) {
243241
if ($event->isStopped()) {
244242
break;
245243
}
246-
if ($listener['passParams'] === true) {
247-
$result = call_user_func_array($listener['callable'], $event->data);
244+
$data = $event->data();
245+
if ($data !== null) {
246+
array_unshift($data, $event);
247+
$result = call_user_func_array($listener['callable'], $data);
248248
} else {
249249
$result = call_user_func($listener['callable'], $event);
250250
}

lib/Cake/Test/TestCase/Event/EventManagerTest.php

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
<?php
22
/**
3-
* ControllerTestCaseTest file
4-
*
5-
* Test Case for ControllerTestCase class
6-
*
73
* PHP 5
84
*
95
* CakePHP : Rapid Development Framework (http://cakephp.org)
@@ -13,11 +9,11 @@
139
* For full copyright and license information, please see the LICENSE.txt
1410
* Redistributions of files must retain the above copyright notice.
1511
*
16-
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
17-
* @link http://cakephp.org CakePHP Project
18-
* @package Cake.Test.Case.Event
19-
* @since CakePHP v 2.1
20-
* @license http://www.opensource.org/licenses/mit-license.php MIT License
12+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13+
* @link http://cakephp.org CakePHP Project
14+
* @package Cake.Test.Case.Event
15+
* @since CakePHP v 2.1
16+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
2117
*/
2218
namespace Cake\Test\TestCase\Event;
2319

@@ -75,7 +71,7 @@ class CustomTestEventListerner extends EventTestListener implements EventListene
7571
public function implementedEvents() {
7672
return array(
7773
'fake.event' => 'listenerFunction',
78-
'another.event' => array('callable' => 'secondListenerFunction', 'passParams' => true),
74+
'another.event' => array('callable' => 'secondListenerFunction'),
7975
'multiple.handlers' => array(
8076
array('callable' => 'listenerFunction'),
8177
array('callable' => 'thirdListenerFunction')
@@ -109,12 +105,12 @@ public function testAttachListeners() {
109105
$manager = new EventManager;
110106
$manager->attach('fakeFunction', 'fake.event');
111107
$expected = array(
112-
array('callable' => 'fakeFunction', 'passParams' => false)
108+
array('callable' => 'fakeFunction')
113109
);
114110
$this->assertEquals($expected, $manager->listeners('fake.event'));
115111

116112
$manager->attach('fakeFunction2', 'fake.event');
117-
$expected[] = array('callable' => 'fakeFunction2', 'passParams' => false);
113+
$expected[] = array('callable' => 'fakeFunction2');
118114
$this->assertEquals($expected, $manager->listeners('fake.event'));
119115

120116
$manager->attach('inQ5', 'fake.event', array('priority' => 5));
@@ -123,9 +119,9 @@ public function testAttachListeners() {
123119

124120
$expected = array_merge(
125121
array(
126-
array('callable' => 'inQ1', 'passParams' => false),
127-
array('callable' => 'inQ5', 'passParams' => false),
128-
array('callable' => 'otherInQ5', 'passParams' => false)
122+
array('callable' => 'inQ1'),
123+
array('callable' => 'inQ5'),
124+
array('callable' => 'otherInQ5')
129125
),
130126
$expected
131127
);
@@ -141,15 +137,15 @@ public function testAttachMultipleEventKeys() {
141137
$manager = new EventManager;
142138
$manager->attach('fakeFunction', 'fake.event');
143139
$manager->attach('fakeFunction2', 'another.event');
144-
$manager->attach('fakeFunction3', 'another.event', array('priority' => 1, 'passParams' => true));
140+
$manager->attach('fakeFunction3', 'another.event', array('priority' => 1));
145141
$expected = array(
146-
array('callable' => 'fakeFunction', 'passParams' => false)
142+
array('callable' => 'fakeFunction')
147143
);
148144
$this->assertEquals($expected, $manager->listeners('fake.event'));
149145

150146
$expected = array(
151-
array('callable' => 'fakeFunction3', 'passParams' => true),
152-
array('callable' => 'fakeFunction2', 'passParams' => false)
147+
array('callable' => 'fakeFunction3'),
148+
array('callable' => 'fakeFunction2')
153149
);
154150
$this->assertEquals($expected, $manager->listeners('another.event'));
155151
}
@@ -170,7 +166,7 @@ public function testDetach() {
170166

171167
$manager->detach(array('AClass', 'anotherMethod'), 'another.event');
172168
$expected = array(
173-
array('callable' => 'fakeFunction', 'passParams' => false)
169+
array('callable' => 'fakeFunction')
174170
);
175171
$this->assertEquals($expected, $manager->listeners('another.event'));
176172

@@ -191,7 +187,7 @@ public function testDetachFromAll() {
191187

192188
$manager->detach(array('AClass', 'aMethod'));
193189
$expected = array(
194-
array('callable' => 'fakeFunction', 'passParams' => false)
190+
array('callable' => 'fakeFunction')
195191
);
196192
$this->assertEquals($expected, $manager->listeners('another.event'));
197193
$this->assertEquals(array(), $manager->listeners('fake.event'));
@@ -302,50 +298,45 @@ public function testDispatchPrioritized() {
302298
$this->assertEquals($expected, $listener->callStack);
303299
}
304300

305-
/**
306-
* Tests event dispatching with passed params
307-
*
308-
* @return void
309-
*/
310-
public function testDispatchPassingParams() {
311-
$manager = new EventManager;
312-
$listener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
313-
$anotherListener = $this->getMock(__NAMESPACE__ . '\EventTestListener');
314-
$manager->attach(array($listener, 'listenerFunction'), 'fake.event');
315-
$manager->attach(array($anotherListener, 'secondListenerFunction'), 'fake.event', array('passParams' => true));
316-
$event = new Event('fake.event', $this, array('some' => 'data'));
317-
318-
$listener->expects($this->once())->method('listenerFunction')->with($event);
319-
$anotherListener->expects($this->once())->method('secondListenerFunction')->with('data');
320-
$manager->dispatch($event);
321-
}
322-
323301
/**
324302
* Tests subscribing a listener object and firing the events it subscribed to
325303
*
326304
* @return void
327305
*/
328306
public function testAttachSubscriber() {
329-
$manager = new EventManager;
307+
$manager = new EventManager();
330308
$listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListerner', array('secondListenerFunction'));
331309
$manager->attach($listener);
332-
$event = new Event('fake.event');
333310

311+
$event = new Event('fake.event');
334312
$manager->dispatch($event);
335313

336314
$expected = array('listenerFunction');
337315
$this->assertEquals($expected, $listener->callStack);
338316

339-
$listener->expects($this->at(0))->method('secondListenerFunction')->with('data');
340317
$event = new Event('another.event', $this, array('some' => 'data'));
318+
$listener->expects($this->at(0))
319+
->method('secondListenerFunction')
320+
->with($event, 'data');
341321
$manager->dispatch($event);
322+
}
342323

324+
/**
325+
* Test implementedEvents binding multiple callbacks to the same event name.
326+
*
327+
* @return void
328+
*/
329+
public function testAttachSubscriberMultiple() {
343330
$manager = new EventManager;
344331
$listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListerner', array('listenerFunction', 'thirdListenerFunction'));
345332
$manager->attach($listener);
346333
$event = new Event('multiple.handlers');
347-
$listener->expects($this->once())->method('listenerFunction')->with($event);
348-
$listener->expects($this->once())->method('thirdListenerFunction')->with($event);
334+
$listener->expects($this->once())
335+
->method('listenerFunction')
336+
->with($event);
337+
$listener->expects($this->once())
338+
->method('thirdListenerFunction')
339+
->with($event);
349340
$manager->dispatch($event);
350341
}
351342

@@ -359,11 +350,11 @@ public function testDetachSubscriber() {
359350
$listener = $this->getMock(__NAMESPACE__ . '\CustomTestEventListerner', array('secondListenerFunction'));
360351
$manager->attach($listener);
361352
$expected = array(
362-
array('callable' => array($listener, 'secondListenerFunction'), 'passParams' => true)
353+
array('callable' => array($listener, 'secondListenerFunction'))
363354
);
364355
$this->assertEquals($expected, $manager->listeners('another.event'));
365356
$expected = array(
366-
array('callable' => array($listener, 'listenerFunction'), 'passParams' => false)
357+
array('callable' => array($listener, 'listenerFunction'))
367358
);
368359
$this->assertEquals($expected, $manager->listeners('fake.event'));
369360
$manager->detach($listener);

0 commit comments

Comments
 (0)