Browse Source

Pass dispatcher loop instance to callback.

This should not break existing code but allows users to retrieve more
easily the current dispatcher loop instance without resorting to some
tricks (like relying on the "use()" directive with closures).
Daniele Alessandri 8 years ago
parent
commit
b553c6b9d0

+ 2 - 2
examples/dispatcher_loop.php

@@ -51,7 +51,7 @@ class EventsListener implements Countable
         return $this->events;
     }
 
-    public function __invoke($payload)
+    public function __invoke($payload, $dispatcher)
     {
         $this->events[] = $payload;
     }
@@ -61,7 +61,7 @@ class EventsListener implements Countable
 $dispatcher->attachCallback('events', ($events = new EventsListener()));
 
 // Attach a function to control the dispatcher loop termination with a message.
-$dispatcher->attachCallback('control', function ($payload) use ($dispatcher) {
+$dispatcher->attachCallback('control', function ($payload, $dispatcher) {
     if ($payload === 'terminate_dispatcher') {
         $dispatcher->stop();
     }

+ 3 - 3
src/PubSub/DispatcherLoop.php

@@ -128,7 +128,7 @@ class DispatcherLoop
             if ($kind !== Consumer::MESSAGE && $kind !== Consumer::PMESSAGE) {
                 if (isset($this->subscriptionCallback)) {
                     $callback = $this->subscriptionCallback;
-                    call_user_func($callback, $message);
+                    call_user_func($callback, $message, $this);
                 }
 
                 continue;
@@ -136,10 +136,10 @@ class DispatcherLoop
 
             if (isset($this->callbacks[$message->channel])) {
                 $callback = $this->callbacks[$message->channel];
-                call_user_func($callback, $message->payload);
+                call_user_func($callback, $message->payload, $this);
             } elseif (isset($this->defaultCallback)) {
                 $callback = $this->defaultCallback;
-                call_user_func($callback, $message);
+                call_user_func($callback, $message, $this);
             }
         }
     }

+ 4 - 4
tests/Predis/PubSub/DispatcherLoopTest.php

@@ -52,8 +52,8 @@ class DispatcherLoopTest extends PredisTestCase
                    ->with($this->logicalOr(
                        $this->equalTo('01:argument'),
                        $this->equalTo('01:quit')
-                   ))
-                   ->will($this->returnCallback(function ($arg) use ($dispatcher) {
+                   ), $dispatcher)
+                   ->will($this->returnCallback(function ($arg, $dispatcher) {
                        if ($arg === '01:quit') {
                            $dispatcher->stop();
                        }
@@ -109,8 +109,8 @@ class DispatcherLoopTest extends PredisTestCase
         $callback = $this->getMock('stdClass', array('__invoke'));
         $callback->expects($this->exactly(1))
                  ->method('__invoke')
-                 ->with($this->equalTo('arg:prefixed'))
-                 ->will($this->returnCallback(function ($arg) use ($dispatcher) {
+                 ->with($this->equalTo('arg:prefixed'), $dispatcher)
+                 ->will($this->returnCallback(function ($arg, $dispatcher) {
                      $dispatcher->stop();
                  }));