Skip to content

Commit 169cd6c

Browse files
committed
Update to use global default loop with react/event-loop v1.2+
1 parent bbf3af7 commit 169cd6c

4 files changed

Lines changed: 15 additions & 7 deletions

File tree

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"php": ">=5.4.2"
3030
, "ratchet/rfc6455": "^0.3.1"
3131
, "react/socket": "^1.0 || ^0.8 || ^0.7 || ^0.6 || ^0.5"
32-
, "react/event-loop": ">=0.4"
32+
, "react/event-loop": "^1.0 || ^0.5 || ^0.4"
3333
, "guzzlehttp/psr7": "^1.7|^2.0"
3434
, "symfony/http-foundation": "^2.6|^3.0|^4.0|^5.0|^6.0|^7.0"
3535
, "symfony/routing": "^2.6|^3.0|^4.0|^5.0|^6.0|^7.0"

src/Ratchet/App.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
<?php
22
namespace Ratchet;
3+
use React\EventLoop\Factory as LegacyLoopFactory;
4+
use React\EventLoop\Loop;
35
use React\EventLoop\LoopInterface;
4-
use React\EventLoop\Factory as LoopFactory;
56
use React\Socket\Server as Reactor;
67
use React\Socket\SecureServer as SecureReactor;
78
use Ratchet\Http\HttpServerInterface;
@@ -72,7 +73,8 @@ public function __construct($httpHost = 'localhost', $port = 8080, $address = '1
7273
}
7374

7475
if (null === $loop) {
75-
$loop = LoopFactory::create();
76+
// prefer default Loop (reactphp/event-loop v1.2+) over legacy \React\EventLoop\Factory
77+
$loop = class_exists('React\EventLoop\Loop') ? Loop::get() : LegacyLoopFactory::create();
7678
}
7779

7880
$this->httpHost = $httpHost;

src/Ratchet/Server/IoServer.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
<?php
22
namespace Ratchet\Server;
33
use Ratchet\MessageComponentInterface;
4+
use React\EventLoop\Factory as LegacyLoopFactory;
5+
use React\EventLoop\Loop;
46
use React\EventLoop\LoopInterface;
57
use React\Socket\ConnectionInterface as SocketConnection;
68
use React\Socket\ServerInterface;
7-
use React\EventLoop\Factory as LoopFactory;
89
use React\Socket\Server as Reactor;
910
use React\Socket\SecureServer as SecureReactor;
1011

@@ -14,7 +15,7 @@
1415
*/
1516
class IoServer {
1617
/**
17-
* @var \React\EventLoop\LoopInterface
18+
* @var ?\React\EventLoop\LoopInterface
1819
*/
1920
public $loop;
2021

@@ -60,7 +61,9 @@ public function __construct(MessageComponentInterface $app, ServerInterface $soc
6061
* @return IoServer
6162
*/
6263
public static function factory(MessageComponentInterface $component, $port = 80, $address = '0.0.0.0') {
63-
$loop = LoopFactory::create();
64+
// prefer default Loop (reactphp/event-loop v1.2+) over legacy \React\EventLoop\Factory
65+
$loop = class_exists('React\EventLoop\Loop') ? Loop::get() : LegacyLoopFactory::create();
66+
6467
$socket = new Reactor($address . ':' . $port, $loop);
6568

6669
return new static($component, $socket, $loop);

tests/unit/Server/IoServerTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,10 @@ public function testOnClose() {
111111
}
112112

113113
public function testFactory() {
114-
$this->assertInstanceOf('Ratchet\\Server\\IoServer', IoServer::factory($this->app, 0));
114+
$server = IoServer::factory($this->app, 0);
115+
$server->socket->close();
116+
117+
$this->assertInstanceOf('Ratchet\\Server\\IoServer', $server);
115118
}
116119

117120
public function testNoLoopProvidedError() {

0 commit comments

Comments
 (0)