8000 changed the EventDispatcher and Event interfaces · alexfilatov/symfony@8b62df7 · GitHub
[go: up one dir, main page]

Skip to content

Commit 8b62df7

Browse files
committed
changed the EventDispatcher and Event interfaces
The three notification methods do not return the Event instance anymore. notify() does not return anything notifyUntil() returns the returned value of the event that has processed the event filter() returns the filtered value Upgrading your listeners: Listeners for notify() and filter() events: nothing to change Listeners for notifyUntil() events: Before: $event->setReturnValue('foo'); return true; After: $event->setProcessed(); return 'foo'; If you notify events, the processing also need to be changed: For filter() notifications: the filtered value is now available as the returned value of the filter() method. For notifyUntil() notifications: Before: $event = $dispatcher->notifyUntil($event); if ($event->isProcessed()) { $ret = $event->getReturnValue(); // do something with $ret } After: $ret = $dispatcher->notifyUntil($event); if ($event->isProcessed()) { // do something with $ret }
1 parent a66d050 commit 8b62df7

19 files changed

+95
-149
lines changed

src/Symfony/Bundle/FrameworkBundle/Debug/EventDispatcher.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public function notify(EventInterface $event)
5555

5656
call_user_func($listener, $event);
5757
}
58-
59-
return $event;
6058
}
6159

6260
/**
@@ -71,7 +69,8 @@ public function notifyUntil(EventInterface $event)
7169

7270
$this->addCall($event, $listener, 'notifyUntil');
7371

74-
if (call_user_func($listener, $event)) {
72+
$ret = call_user_func($listener, $event);
73+
if ($event->isProcessed()) {
7574
if (null !== $this->logger) {
7675
$this->logger->debug(sprintf('Listener "%s" processed the event "%s"', $this->listenerToString($listener), $event->getName()));
7776

@@ -81,12 +80,9 @@ public function notifyUntil(EventInterface $event)
8180
}
8281
}
8382

84-
$event->setProcessed(true);
85-
break;
83+
return $ret;
8684
}
8785
}
88-
89-
return $event;
9086
}
9187

9288
/**
@@ -104,9 +100,7 @@ public function filter(EventInterface $event, $value)
104100
$value = call_user_func($listener, $event, $value);
105101
}
106102

107-
$event->setReturnValue($value);
108-
109-
return $event;
103+
return $value;
110104
}
111105

112106
/**

src/Symfony/Bundle/FrameworkBundle/EventDispatcher.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@ public function notify(EventInterface $event)
5050
}
5151
call_user_func($listener, $event);
5252
}
53-
54-
return $event;
5553
}
5654

5755
/**
@@ -63,13 +61,12 @@ public function notifyUntil(EventInterface $event)
6361
if (is_array($listener) && is_string($listener[0])) {
6462
$listener[0] = $this->container->get($listener[0]);
6563
}
66-
if (call_user_func($listener, $event)) {
67-
$event->setProcessed(true);
68-
break;
64+
65+
$ret = call_user_func($listener, $event);
66+
if ($event->isProcessed()) {
67+
return $ret;
6968
}
7069
}
71-
72-
return $event;
7370
}
7471

7572
/**
@@ -84,8 +81,6 @@ public function filter(EventInterface $event, $value)
8481
$value = call_user_func($listener, $event, $value);
8582
}
8683

87-
$event->setReturnValue($value);
88-
89-
return $event;
84+
return $value;
9085
}
9186
}

src/Symfony/Component/EventDispatcher/Event.php

Lines changed: 10 additions & 24 deletions
E377
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,24 @@ public function getName()
5959
}
6060

6161
/**
62-
* Sets the return value for this event.
62+
* Sets the processed flag to true.
6363
*
64-
* @param mixed $value The return value
64+
* This method must be called by listeners when
65+
* it has processed the event (it is only meaninful
66+
* when the event has been notified with the notifyUntil()
67+
* dispatcher method.
6568
*/
66-
public function setReturnValue($value)
69+
public function setProcessed()
6770
{
68-
$this->value = $value;
69-
}
70-
71-
/**
72-
* Returns the return value.
73-
*
74-
* @return mixed The return value
75-
*/
76-
public function getReturnValue()
77-
{
78-
return $this->value;
79-
}
80-
81-
/**
82-
* Sets the processed flag.
83-
*
84-
* @param Boolean $processed The processed flag value
85-
*/
86-
public function setProcessed($processed)
87-
{
88-
$this->processed = (Boolean) $processed;
71+
$this->processed = true;
8972
}
9073

9174
/**
9275
* Returns whether the event has been processed by a listener or not.
9376
*
77+
* This method is only meaningful for events notified
78+
* with notifyUntil().
79+
*
9480
* @return Boolean true if the event has been processed, false otherwise
9581
*/
9682
public function isProcessed()

src/Symfony/Component/EventDispatcher/EventDispatcher.php

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -73,35 +73,29 @@ public function disconnect($name, $listener = null)
7373
* Notifies all listeners of a given event.
7474
*
7575
* @param EventInterface $event An EventInterface instance
76-
*
77-
* @return EventInterface The EventInterface instance
7876
*/
7977
public function notify(EventInterface $event)
8078
{
8179
foreach ($this->getListeners($event->getName()) as $listener) {
8280
call_user_func($listener, $event);
8381
}
84-
85-
return $event;
8682
}
8783

8884
/**
89-
* Notifies all listeners of a given event until one returns a non null value.
85+
* Notifies all listeners of a given event until one processes the event.
9086
*
9187
* @param EventInterface $event An EventInterface instance
9288
*
93-
* @return EventInterface The EventInterface instance
89+
* @return mixed The returned value of the listener that processed the event
9490
*/
9591
public function notifyUntil(EventInterface $event)
9692
{
9793
foreach ($this->getListeners($event->getName()) as $listener) {
98-
if (call_user_func($listener, $event)) {
99-
$event->setProcessed(true);
100-
break;
94+
$ret = call_user_func($listener, $event);
95+
if ($event->isProcessed()) {
96+
return $ret;
10197
}
10298
}
103-
104-
return $event;
10599
}
106100

107101
/**
@@ -110,17 +104,15 @@ public function notifyUntil(EventInterface $event)
110104
* @param EventInterface $event An EventInterface instance
111105
* @param mixed $value The value to be filtered
112106
*
113-
* @return EventInterface The EventInterface instance
107+
* @return mixed The filtered value
114108
*/
115109
public function filter(EventInterface $event, $value)
116110
{
117111
foreach ($this->getListeners($event->getName()) as $listener) {
118112
$value = call_user_func($listener, $event, $value);
119113
}
120114

121-
$event->setReturnValue($value);
122-
123-
return $event;
115+
return $value;
124116
}
125117

126118
/**

src/Symfony/Component/EventDispatcher/EventDispatcherInterface.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,20 @@ function disconnect($name, $listener = null);
4545
* Notifies all listeners of a given event.
4646
*
4747
* @param EventInterface $event An EventInterface instance
48-
*
49-
* @return EventInterface The EventInterface instance
5048
*/
5149
function notify(EventInterface $event);
5250

5351
/**
54-
* Notifies all listeners of a given event until one returns a non null value.
52+
* Notifies all listeners of a given event until one processes the event.
53+
*
54+
* A listener tells the dispatcher that it has processed the event
55+
* by calling the setProcessed() method on it.
56+
*
57+
* It can then return a value that will be fowarded to the caller.
5558
*
5659
* @param EventInterface $event An EventInterface instance
5760
*
58-
* @return EventInterface The EventInterface instance
61+
* @return mixed The returned value of the listener that processed the event
5962
*/
6063
function notifyUntil(EventInterface $event);
6164

@@ -65,7 +68,7 @@ function notifyUntil(EventInterface $event);
6568
* @param EventInterface $event An EventInterface instance
6669
* @param mixed $value The value to be filtered
6770
*
68-
* @return EventInterface The EventInterface instance
71+
* @return mixed The filtered value
6972
*/
7073
function filter(EventInterface $event, $value);
7174

src/Symfony/Component/EventDispatcher/EventInterface.php

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,21 @@ function getSubject();
3333
function getName();
3434

3535
/**
36-
* Sets the return value for this event.
36+
* Sets the processed flag to true.
3737
*
38-
* @param mixed $value The return value
38+
* This method must be called by listeners when
39+
* it has processed the event (it is only meaninful
40+
* when the event has been notified with the notifyUntil()
41+
* dispatcher method.
3942
*/
40-
function setReturnValue($value);
41-
42-
/**
43-
* Returns the return value.
44-
*
45-
* @return mixed The return value
46-
*/
47-
function getReturnValue();
48-
49-
/**
50-
* Sets the processed flag.
51-
*
52-
* @param Boolean $processed The processed flag value
53-
*/
54-
function setProcessed($processed);
43+
function setProcessed();
5544

5645
/**
5746
* Returns whether the event has been processed by a listener or not.
5847
*
48+
* This method is only meaningful for events notified
49+
* with notifyUntil().
50+
*
5951
* @return Boolean true if the event has been processed, false otherwise
6052
*/
6153
function isProcessed();

src/Symfony/Component/HttpKernel/Debug/ExceptionListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,10 @@ public function handle(EventInterface $event)
8181
throw $exception;
8282
}
8383

84-
$event->setReturnValue($response);
84+
$event->setProcessed();
8585

8686
$handling = false;
8787

88-
return true;
88+
return $response;
8989
}
9090
}

src/Symfony/Component/HttpKernel/HttpKernel.php

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
5454

5555
// exception
5656
$event = new Event($this, 'core.exception', array('request_type' => $type, 'request' => $request, 'exception' => $e));
57-
$this->dispatcher->notifyUntil($event);
57+
$response = $this->dispatcher->notifyUntil($event);
5858
if (!$event->isProcessed()) {
5959
throw $e;
6060
}
6161

62-
$response = $this->filterResponse($event->getReturnValue(), $request, 'A "core.exception" listener returned a non response object.', $type);
62+
$response = $this->filterResponse($response, $request, 'A "core.exception" listener returned a non response object.', $type);
6363
}
6464

6565
return $response;
@@ -82,9 +82,9 @@ protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
8282
{
8383
// request
8484
$event = new Event($this, 'core.request', array('request_type' => $type, 'request' => $request));
85-
$this->dispatcher->notifyUntil($event);
85+
$response = $this->dispatcher->notifyUntil($event);
8686
if ($event->isProcessed()) {
87-
return $this->filterResponse($event->getReturnValue(), $request, 'A "core.request" listener returned a non response object.', $type);
87+
return $this->filterResponse($response, $request, 'A "core.request" listener returned a non response object.', $type);
8888
}
8989

9090
// load controller
@@ -93,8 +93,7 @@ protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
9393
}
9494

9595
$event = new Event($this, 'core.controller', array('request_type' => $type, 'request' => $request));
96-
$this->dispatcher->filter($event, $controller);
97-
$controller = $event->getReturnValue();
96+
$controller = $this->dispatcher->filter($event, $controller);
9897

9998
// controller must be a callable
10099
if (!is_callable($controller)) {
@@ -109,9 +108,9 @@ protected function handleRaw(Request $request, $type = self::MASTER_REQUEST)
109108

110109
// view
111110
$event = new Event($this, 'core.view', array('request_type' => $type, 'request' => $request));
112-
$this->dispatcher->filter($event, $retval);
111+
$response = $this->dispatcher->filter($event, $retval);
113112

114-
return $this->filterResponse($event->getReturnValue(), $request, sprintf('The controller must return a response (instead of %s).', is_object($event->getReturnValue()) ? 'an object of class '.get_class($event->getReturnValue()) : is_array($event->getReturnValue()) ? 'an array' : str_replace("\n", '', var_export($event->getReturnValue(), true))), $type);
113+
return $this->filterResponse($response, $request, sprintf('The controller must return a response (instead of %s).', is_object($response) ? 'an object of class '.get_class($response) : is_array($response) ? 'an array' : str_replace("\n", '', var_export($response, true))), $type);
115114
}
116115

117116
/**
@@ -131,8 +130,7 @@ protected function filterResponse($response, $request, $message, $type)
131130
throw new \RuntimeException($message);
132131
}
133132

134-
$event = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response);
135-
$response = $event->getReturnValue();
133+
$response = $this->dispatcher->filter(new Event($this, 'core.response', array('request_type' => $type, 'request' => $request)), $response);
136134

137135
if (!$response instanceof Response) {
138136
throw new \RuntimeException('A "core.response" listener returned a non response object.');

src/Symfony/Component/HttpKernel/Security/Firewall.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,11 @@ public function handle(EventInterface $event)
8585
}
8686

8787
// initiate the listener chain
88-
$e = $this->dispatcher->notifyUntil(new Event($request, 'core.security', array('request' => $request)));
89-
if ($e->isProcessed()) {
90-
$event->setReturnValue($e->getReturnValue());
88+
$ret = $this->dispatcher->notifyUntil($event = new Event($request, 'core.security', array('request' => $request)));
89+
if ($event->isProcessed()) {
90+
$event->setProcessed();
9191

92-
return true;
92+
return $ret;
9393
}
94-
95-
return;
9694
}
9795
}

src/Symfony/Component/HttpKernel/Security/Firewall/BasicAuthenticationListener.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,9 @@ public function handle(EventInterface $event)
101101
return;
102102
}
103103

104-
$event->setReturnValue($this->authenticationEntryPoint->start($request, $failed));
104+
$event->setProcessed();
105105

106-
return true;
106+
return $this->authenticationEntryPoint->start($request, $failed);
107107
}
108108
}
109109
}

0 commit comments

Comments
 (0)
0