Skip to content

Commit a35b37f

Browse files
authored
Merge pull request #151 from clue-labs/trolol
[RFC] Improve compatibility with legacy versions
2 parents 14c870e + c4f0e45 commit a35b37f

11 files changed

Lines changed: 135 additions & 100 deletions

.travis.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
language: php
22

33
php:
4+
# - 5.3 # requires old distro, see below
45
- 5.4
56
- 5.5
67
- 5.6
@@ -13,6 +14,9 @@ php:
1314
dist: trusty
1415

1516
matrix:
17+
include:
18+
- php: 5.3
19+
dist: precise
1620
allow_failures:
1721
- php: hhvm
1822

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ A `stream_select()` based event loop.
158158
This uses the [`stream_select()`](http://php.net/manual/en/function.stream-select.php)
159159
function and is the only implementation which works out of the box with PHP.
160160

161-
This event loop works out of the box on PHP 5.4 through PHP 7+ and HHVM.
161+
This event loop works out of the box on PHP 5.3 through PHP 7+ and HHVM.
162162
This means that no installation is required and this library works on all
163163
platforms and supported PHP versions.
164164
Accordingly, the [`Factory`](#factory) will use this event loop by default if
@@ -574,7 +574,7 @@ $ composer require react/event-loop
574574
```
575575

576576
This project aims to run on any platform and thus does not require any PHP
577-
extensions and supports running on legacy PHP 5.4 through current PHP 7+ and
577+
extensions and supports running on legacy PHP 5.3 through current PHP 7+ and
578578
HHVM.
579579
It's *highly recommended to use PHP 7+* for this project.
580580

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"keywords": ["event-loop", "asynchronous"],
55
"license": "MIT",
66
"require": {
7-
"php": ">=5.4.0"
7+
"php": ">=5.3.0"
88
},
99
"require-dev": {
1010
"phpunit/phpunit": "~4.8.35 || ^5.7 || ^6.4"

src/ExtEventLoop.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ final class ExtEventLoop implements LoopInterface
2626
private $timerCallback;
2727
private $timerEvents;
2828
private $streamCallback;
29-
private $readEvents = [];
30-
private $writeEvents = [];
31-
private $readListeners = [];
32-
private $writeListeners = [];
33-
private $readRefs = [];
34-
private $writeRefs = [];
29+
private $readEvents = array();
30+
private $writeEvents = array();
31+
private $readListeners = array();
32+
private $writeListeners = array();
33+
private $readRefs = array();
34+
private $writeRefs = array();
3535
private $running;
3636
private $signals;
37-
private $signalEvents = [];
37+
private $signalEvents = array();
3838

3939
public function __construct(EventBaseConfig $config = null)
4040
{
@@ -215,10 +215,11 @@ private function scheduleTimer(TimerInterface $timer)
215215
*/
216216
private function createTimerCallback()
217217
{
218-
$this->timerCallback = function ($_, $__, $timer) {
218+
$timers = $this->timerEvents;
219+
$this->timerCallback = function ($_, $__, $timer) use ($timers) {
219220
call_user_func($timer->getCallback(), $timer);
220221

221-
if (!$timer->isPeriodic() && $this->timerEvents->contains($timer)) {
222+
if (!$timer->isPeriodic() && $timers->contains($timer)) {
222223
$this->cancelTimer($timer);
223224
}
224225
};
@@ -233,15 +234,17 @@ private function createTimerCallback()
233234
*/
234235
private function createStreamCallback()
235236
{
236-
$this->streamCallback = function ($stream, $flags) {
237+
$read =& $this->readListeners;
238+
$write =& $this->writeListeners;
239+
$this->streamCallback = function ($stream, $flags) use (&$read, &$write) {
237240
$key = (int) $stream;
238241

239-
if (Event::READ === (Event::READ & $flags) && isset($this->readListeners[$key])) {
240-
call_user_func($this->readListeners[$key], $stream);
242+
if (Event::READ === (Event::READ & $flags) && isset($read[$key])) {
243+
call_user_func($read[$key], $stream);
241244
}
242245

243-
if (Event::WRITE === (Event::WRITE & $flags) && isset($this->writeListeners[$key])) {
244-
call_user_func($this->writeListeners[$key], $stream);
246+
if (Event::WRITE === (Event::WRITE & $flags) && isset($write[$key])) {
247+
call_user_func($write[$key], $stream);
245248
}
246249
};
247250
}

src/ExtLibevLoop.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ final class ExtLibevLoop implements LoopInterface
2828
private $loop;
2929
private $futureTickQueue;
3030
private $timerEvents;
31-
private $readEvents = [];
32-
private $writeEvents = [];
31+
private $readEvents = array();
32+
private $writeEvents = array();
3333
private $running;
3434
private $signals;
35-
private $signalEvents = [];
35+
private $signalEvents = array();
3636

3737
public function __construct()
3838
{
@@ -100,11 +100,13 @@ public function addTimer($interval, $callback)
100100
{
101101
$timer = new Timer( $interval, $callback, false);
102102

103-
$callback = function () use ($timer) {
103+
$that = $this;
104+
$timers = $this->timerEvents;
105+
$callback = function () use ($timer, $timers, $that) {
104106
call_user_func($timer->getCallback(), $timer);
105107

106-
if ($this->timerEvents->contains($timer)) {
107-
$this->cancelTimer($timer);
108+
if ($timers->contains($timer)) {
109+
$that->cancelTimer($timer);
108110
}
109111
};
110112

@@ -148,8 +150,9 @@ public function addSignal($signal, $listener)
148150
$this->signals->add($signal, $listener);
149151

150152
if (!isset($this->signalEvents[$signal])) {
151-
$this->signalEvents[$signal] = new SignalEvent(function () use ($signal) {
152-
$this->signals->call($signal);
153+
$signals = $this->signals;
154+
$this->signalEvents[$signal] = new SignalEvent(function () use ($signals, $signal) {
155+
$signals->call($signal);
153156
}, $signal);
154157
$this->loop->add($this->signalEvents[$signal]);
155158
}

src/ExtLibeventLoop.php

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ final class ExtLibeventLoop implements LoopInterface
4242
private $timerCallback;
4343
private $timerEvents;
4444
private $streamCallback;
45-
private $readEvents = [];
46-
private $writeEvents = [];
47-
private $readListeners = [];
48-
private $writeListeners = [];
45+
private $readEvents = array();
46+
private $writeEvents = array();
47+
private $readListeners = array();
48+
private $writeListeners = array();
4949
private $running;
5050
private $signals;
51-
private $signalEvents = [];
51+
private $signalEvents = array();
5252

5353
public function __construct()
5454
{
@@ -228,24 +228,26 @@ private function scheduleTimer(TimerInterface $timer)
228228
*/
229229
private function createTimerCallback()
230230
{
231-
$this->timerCallback = function ($_, $__, $timer) {
231+
$that = $this;
232+
$timers = $this->timerEvents;
233+
$this->timerCallback = function ($_, $__, $timer) use ($timers, $that) {
232234
call_user_func($timer->getCallback(), $timer);
233235

234236
// Timer already cancelled ...
235-
if (!$this->timerEvents->contains($timer)) {
237+
if (!$timers->contains($timer)) {
236238
return;
237239
}
238240

239241
// Reschedule periodic timers ...
240242
if ($timer->isPeriodic()) {
241243
event_add(
242-
$this->timerEvents[$timer],
243-
$timer->getInterval() * self::MICROSECONDS_PER_SECOND
244+
$timers[$timer],
245+
$timer->getInterval() * ExtLibeventLoop::MICROSECONDS_PER_SECOND
244246
);
245247

246248
// Clean-up one shot timers ...
247249
} else {
248-
$this->cancelTimer($timer);
250+
$that->cancelTimer($timer);
249251
}
250252
};
251253
}
@@ -259,15 +261,17 @@ private function createTimerCallback()
259261
*/
260262
private function createStreamCallback()
261263
{
262-
$this->streamCallback = function ($stream, $flags) {
264+
$read =& $this->readListeners;
265+
$write =& $this->writeListeners;
266+
$this->streamCallback = function ($stream, $flags) use (&$read, &$write) {
263267
$key = (int) $stream;
264268

265-
if (EV_READ === (EV_READ & $flags) && isset($this->readListeners[$key])) {
266-
call_user_func($this->readListeners[$key], $stream);
269+
if (EV_READ === (EV_READ & $flags) && isset($read[$key])) {
270+
call_user_func($read[$key], $stream);
267271
}
268272

269-
if (EV_WRITE === (EV_WRITE & $flags) && isset($this->writeListeners[$key])) {
270-
call_user_func($this->writeListeners[$key], $stream);
273+
if (EV_WRITE === (EV_WRITE & $flags) && isset($write[$key])) {
274+
call_user_func($write[$key], $stream);
271275
}
272276
};
273277
}

src/SignalsHandler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
*/
88
final class SignalsHandler
99
{
10-
private $signals = [];
10+
private $signals = array();
1111

1212
public function add($signal, $listener)
1313
{
1414
if (!isset($this->signals[$signal])) {
15-
$this->signals[$signal] = [];
15+
$this->signals[$signal] = array();
1616
}
1717

1818
if (in_array($listener, $this->signals[$signal])) {

src/StreamSelectLoop.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,10 @@ final class StreamSelectLoop implements LoopInterface
5656

5757
private $futureTickQueue;
5858
private $timers;
59-
private $readStreams = [];
60-
private $readListeners = [];
61-
private $writeStreams = [];
62-
private $writeListeners = [];
59+
private $readStreams = array();
60+
private $readListeners = array();
61+
private $writeStreams = array();
62+
private $writeListeners = array();
6363
private $running;
6464
private $pcntl = false;
6565
private $signals;

0 commit comments

Comments
 (0)