From 534375160a8ad99d3ae383688a15b56f52559127 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Tue, 17 Jul 2012 17:22:51 -0400 Subject: [PATCH] [Server] IoServer cleanup Removed React event loop as a requirement; loop can be run outside Removed Server as a requirement for Connection; wasn't used anymore --- src/Ratchet/Server/IoConnection.php | 8 +------- src/Ratchet/Server/IoServer.php | 11 ++++++++--- tests/Ratchet/Tests/Server/IoServerTest.php | 10 +++++++++- 3 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/Ratchet/Server/IoConnection.php b/src/Ratchet/Server/IoConnection.php index c712313..9eccab2 100644 --- a/src/Ratchet/Server/IoConnection.php +++ b/src/Ratchet/Server/IoConnection.php @@ -7,19 +7,13 @@ use React\Socket\ConnectionInterface as ReactConn; * {@inheritdoc} */ class IoConnection implements ConnectionInterface { - /** - * @var Ratchet\Server\IOServer - */ - protected $server; - /** * @var React\Socket\ConnectionInterface */ protected $conn; - public function __construct(ReactConn $conn, IoServer $server) { + public function __construct(ReactConn $conn) { $this->conn = $conn; - $this->server = $server; } /** diff --git a/src/Ratchet/Server/IoServer.php b/src/Ratchet/Server/IoServer.php index 6130a8a..7ed523e 100644 --- a/src/Ratchet/Server/IoServer.php +++ b/src/Ratchet/Server/IoServer.php @@ -31,9 +31,9 @@ class IoServer { /** * @param Ratchet\MessageComponentInterface The Ratchet application stack to host * @param React\Socket\ServerInterface The React socket server to run the Ratchet application off of - * @param React\EventLoop\LoopInterface The React looper to run the Ratchet application off of + * @param React\EventLoop\LoopInterface|null The React looper to run the Ratchet application off of */ - public function __construct(MessageComponentInterface $app, ServerInterface $socket, LoopInterface $loop) { + public function __construct(MessageComponentInterface $app, ServerInterface $socket, LoopInterface $loop = null) { gc_enable(); set_time_limit(0); ob_implicit_flush(); @@ -64,8 +64,13 @@ class IoServer { /** * Run the application by entering the event loop + * @throws RuntimeException If a loop was not previously specified */ public function run() { + if (null === $this->loop) { + throw new \RuntimeException("A React Loop was not provided during instantiation"); + } + $this->loop->run(); } @@ -73,7 +78,7 @@ class IoServer { * Triggered when a new connection is received from React */ public function handleConnect($conn) { - $conn->decor = new IoConnection($conn, $this); + $conn->decor = new IoConnection($conn); $conn->decor->resourceId = (int)$conn->stream; $conn->decor->remoteAddress = $conn->getRemoteAddress(); diff --git a/tests/Ratchet/Tests/Server/IoServerTest.php b/tests/Ratchet/Tests/Server/IoServerTest.php index 141cbef..63e0f51 100644 --- a/tests/Ratchet/Tests/Server/IoServerTest.php +++ b/tests/Ratchet/Tests/Server/IoServerTest.php @@ -20,7 +20,7 @@ class IoServerTest extends \PHPUnit_Framework_TestCase { public function setUp() { $this->app = new Component; - $loop = new StreamSelectLoop(0); + $loop = new StreamSelectLoop; $this->reactor = new Server($loop); $this->reactor->listen(0); @@ -86,4 +86,12 @@ class IoServerTest extends \PHPUnit_Framework_TestCase { public function testFactory() { $this->assertInstanceOf('\\Ratchet\\Server\\IoServer', IoServer::factory($this->app, 0)); } + + public function testNoLoopProvidedError() { + $loop = new StreamSelectLoop; + $io = new IoServer(new Component, new Server($loop)); + + $this->setExpectedException('RuntimeException'); + $io->run(); + } } \ No newline at end of file