diff --git a/src/Loop/Events/Signal.php b/src/Loop/Events/Signal.php index 4eb6c78..abf6a0c 100644 --- a/src/Loop/Events/Signal.php +++ b/src/Loop/Events/Signal.php @@ -57,6 +57,14 @@ public function __invoke() $this->call(); } + /** + * {@inheritdoc} + */ + public function setCallback(callable $callback) + { + $this->callback = $callback; + } + /** * {@inheritdoc} */ diff --git a/src/Loop/Events/SignalInterface.php b/src/Loop/Events/SignalInterface.php index 2afaa4e..03e46e7 100644 --- a/src/Loop/Events/SignalInterface.php +++ b/src/Loop/Events/SignalInterface.php @@ -21,6 +21,11 @@ public function call(); */ public function __invoke(); + /** + * @param callable $callback + */ + public function setCallback(callable $callback); + /** * Enables listening for the signal. */ diff --git a/src/Loop/Events/SocketEvent.php b/src/Loop/Events/SocketEvent.php index 7383f9a..fc3f0e6 100644 --- a/src/Loop/Events/SocketEvent.php +++ b/src/Loop/Events/SocketEvent.php @@ -67,6 +67,14 @@ public function __invoke($expired) $this->call($expired); } + /** + * {@inheritdoc} + */ + public function setCallback(callable $callback) + { + $this->callback = $callback; + } + /** * {@inheritdoc} */ diff --git a/src/Loop/Events/SocketEventInterface.php b/src/Loop/Events/SocketEventInterface.php index 31735e9..0c6b833 100644 --- a/src/Loop/Events/SocketEventInterface.php +++ b/src/Loop/Events/SocketEventInterface.php @@ -28,6 +28,11 @@ public function call($expired); */ public function __invoke($expired); + /** + * @param callable $callback + */ + public function setCallback(callable $callback); + /** * Listens for data or the ability to write. * diff --git a/tests/Loop/Events/SignalTest.php b/tests/Loop/Events/SignalTest.php index 11fb8bb..4d2a529 100644 --- a/tests/Loop/Events/SignalTest.php +++ b/tests/Loop/Events/SignalTest.php @@ -69,6 +69,25 @@ public function testInvoke() $signal(); } + /** + * @depends testCall + */ + public function testSetCallback() + { + $signo = 1; + + $callback = $this->createCallback(2); + $callback->method('__invoke') + ->with($this->identicalTo($signo)); + + $signal = $this->createSignal($signo, $this->createCallback(0)); + + $signal->setCallback($callback); + + $signal->call(); + $signal->call(); + } + public function testEnable() { $signal = $this->createSignal(1, $this->createCallback(0)); diff --git a/tests/Loop/Events/SocketEventTest.php b/tests/Loop/Events/SocketEventTest.php index bd57c1f..9f2bf67 100644 --- a/tests/Loop/Events/SocketEventTest.php +++ b/tests/Loop/Events/SocketEventTest.php @@ -95,6 +95,25 @@ public function testListen() $event->listen(); } + + /** + * @depends testCall + */ + public function testSetCallback() + { + list($socket) = $this->createSockets(); + + $callback = $this->createCallback(2); + $callback->method('__invoke') + ->with($this->identicalTo($socket), $this->identicalTo(false)); + + $event = $this->createSocketEvent($socket, $this->createCallback(0)); + + $event->setCallback($callback); + + $event->call(false); + $event->call(false); + } /** * @depends testListen