Skip to content

[RFC] Improve compatibility with legacy versions #151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: php

php:
# - 5.3 # requires old distro, see below
- 5.4
- 5.5
- 5.6
Expand All @@ -13,6 +14,9 @@ php:
dist: trusty

matrix:
include:
- php: 5.3
dist: precise
allow_failures:
- php: hhvm

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ A `stream_select()` based event loop.
This uses the [`stream_select()`](http://php.net/manual/en/function.stream-select.php)
function and is the only implementation which works out of the box with PHP.

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

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

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"keywords": ["event-loop", "asynchronous"],
"license": "MIT",
"require": {
"php": ">=5.4.0"
"php": ">=5.3.0"
},
"require-dev": {
"phpunit/phpunit": "~4.8.35 || ^5.7 || ^6.4"
Expand Down
31 changes: 17 additions & 14 deletions src/ExtEventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ final class ExtEventLoop implements LoopInterface
private $timerCallback;
private $timerEvents;
private $streamCallback;
private $readEvents = [];
private $writeEvents = [];
private $readListeners = [];
private $writeListeners = [];
private $readRefs = [];
private $writeRefs = [];
private $readEvents = array();
private $writeEvents = array();
private $readListeners = array();
private $writeListeners = array();
private $readRefs = array();
private $writeRefs = array();
private $running;
private $signals;
private $signalEvents = [];
private $signalEvents = array();

public function __construct(EventBaseConfig $config = null)
{
Expand Down Expand Up @@ -215,10 +215,11 @@ private function scheduleTimer(TimerInterface $timer)
*/
private function createTimerCallback()
{
$this->timerCallback = function ($_, $__, $timer) {
$timers = $this->timerEvents;
$this->timerCallback = function ($_, $__, $timer) use ($timers) {
call_user_func($timer->getCallback(), $timer);

if (!$timer->isPeriodic() && $this->timerEvents->contains($timer)) {
if (!$timer->isPeriodic() && $timers->contains($timer)) {
$this->cancelTimer($timer);
}
};
Expand All @@ -233,15 +234,17 @@ private function createTimerCallback()
*/
private function createStreamCallback()
{
$this->streamCallback = function ($stream, $flags) {
$read =& $this->readListeners;
$write =& $this->writeListeners;
$this->streamCallback = function ($stream, $flags) use (&$read, &$write) {
$key = (int) $stream;

if (Event::READ === (Event::READ & $flags) && isset($this->readListeners[$key])) {
call_user_func($this->readListeners[$key], $stream);
if (Event::READ === (Event::READ & $flags) && isset($read[$key])) {
call_user_func($read[$key], $stream);
}

if (Event::WRITE === (Event::WRITE & $flags) && isset($this->writeListeners[$key])) {
call_user_func($this->writeListeners[$key], $stream);
if (Event::WRITE === (Event::WRITE & $flags) && isset($write[$key])) {
call_user_func($write[$key], $stream);
}
};
}
Expand Down
19 changes: 11 additions & 8 deletions src/ExtLibevLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ final class ExtLibevLoop implements LoopInterface
private $loop;
private $futureTickQueue;
private $timerEvents;
private $readEvents = [];
private $writeEvents = [];
private $readEvents = array();
private $writeEvents = array();
private $running;
private $signals;
private $signalEvents = [];
private $signalEvents = array();

public function __construct()
{
Expand Down Expand Up @@ -100,11 +100,13 @@ public function addTimer($interval, $callback)
{
$timer = new Timer( $interval, $callback, false);

$callback = function () use ($timer) {
$that = $this;
$timers = $this->timerEvents;
$callback = function () use ($timer, $timers, $that) {
call_user_func($timer->getCallback(), $timer);

if ($this->timerEvents->contains($timer)) {
$this->cancelTimer($timer);
if ($timers->contains($timer)) {
$that->cancelTimer($timer);
}
};

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

if (!isset($this->signalEvents[$signal])) {
$this->signalEvents[$signal] = new SignalEvent(function () use ($signal) {
$this->signals->call($signal);
$signals = $this->signals;
$this->signalEvents[$signal] = new SignalEvent(function () use ($signals, $signal) {
$signals->call($signal);
}, $signal);
$this->loop->add($this->signalEvents[$signal]);
}
Expand Down
34 changes: 19 additions & 15 deletions src/ExtLibeventLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,13 @@ final class ExtLibeventLoop implements LoopInterface
private $timerCallback;
private $timerEvents;
private $streamCallback;
private $readEvents = [];
private $writeEvents = [];
private $readListeners = [];
private $writeListeners = [];
private $readEvents = array();
private $writeEvents = array();
private $readListeners = array();
private $writeListeners = array();
private $running;
private $signals;
private $signalEvents = [];
private $signalEvents = array();

public function __construct()
{
Expand Down Expand Up @@ -228,24 +228,26 @@ private function scheduleTimer(TimerInterface $timer)
*/
private function createTimerCallback()
{
$this->timerCallback = function ($_, $__, $timer) {
$that = $this;
$timers = $this->timerEvents;
$this->timerCallback = function ($_, $__, $timer) use ($timers, $that) {
call_user_func($timer->getCallback(), $timer);

// Timer already cancelled ...
if (!$this->timerEvents->contains($timer)) {
if (!$timers->contains($timer)) {
return;
}

// Reschedule periodic timers ...
if ($timer->isPeriodic()) {
event_add(
$this->timerEvents[$timer],
$timer->getInterval() * self::MICROSECONDS_PER_SECOND
$timers[$timer],
$timer->getInterval() * ExtLibeventLoop::MICROSECONDS_PER_SECOND
);

// Clean-up one shot timers ...
} else {
$this->cancelTimer($timer);
$that->cancelTimer($timer);
}
};
}
Expand All @@ -259,15 +261,17 @@ private function createTimerCallback()
*/
private function createStreamCallback()
{
$this->streamCallback = function ($stream, $flags) {
$read =& $this->readListeners;
$write =& $this->writeListeners;
$this->streamCallback = function ($stream, $flags) use (&$read, &$write) {
$key = (int) $stream;

if (EV_READ === (EV_READ & $flags) && isset($this->readListeners[$key])) {
call_user_func($this->readListeners[$key], $stream);
if (EV_READ === (EV_READ & $flags) && isset($read[$key])) {
call_user_func($read[$key], $stream);
}

if (EV_WRITE === (EV_WRITE & $flags) && isset($this->writeListeners[$key])) {
call_user_func($this->writeListeners[$key], $stream);
if (EV_WRITE === (EV_WRITE & $flags) && isset($write[$key])) {
call_user_func($write[$key], $stream);
}
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/SignalsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
*/
final class SignalsHandler
{
private $signals = [];
private $signals = array();

public function add($signal, $listener)
{
if (!isset($this->signals[$signal])) {
$this->signals[$signal] = [];
$this->signals[$signal] = array();
}

if (in_array($listener, $this->signals[$signal])) {
Expand Down
8 changes: 4 additions & 4 deletions src/StreamSelectLoop.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ final class StreamSelectLoop implements LoopInterface

private $futureTickQueue;
private $timers;
private $readStreams = [];
private $readListeners = [];
private $writeStreams = [];
private $writeListeners = [];
private $readStreams = array();
private $readListeners = array();
private $writeStreams = array();
private $writeListeners = array();
private $running;
private $pcntl = false;
private $signals;
Expand Down
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy