Skip to content

Commit c40a5e1

Browse files
committed
Forward compatibility with latest Stream releases
1 parent 09cc0e0 commit c40a5e1

File tree

5 files changed

+31
-24
lines changed

5 files changed

+31
-24
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"require": {
1414
"php": ">=5.3",
1515
"react/event-loop": "0.3.*|0.4.*",
16-
"react/stream": "^0.5 || ^0.4.2",
16+
"react/stream": "^0.7 || ^0.6",
1717
"clue/utf8-react": "^1.0 || ^0.1",
1818
"clue/term-react": "^1.0 || ^0.1.1"
1919
},

src/Stdio.php

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
use Evenement\EventEmitter;
88
use React\EventLoop\LoopInterface;
99
use React\Stream\DuplexStreamInterface;
10+
use React\Stream\ReadableResourceStream;
1011
use React\Stream\ReadableStreamInterface;
1112
use React\Stream\Util;
13+
use React\Stream\WritableResourceStream;
1214
use React\Stream\WritableStreamInterface;
13-
use React\Stream\Stream;
1415

1516
class Stdio extends EventEmitter implements DuplexStreamInterface
1617
{
@@ -268,21 +269,16 @@ private function restoreTtyMode()
268269
private function createStdin(LoopInterface $loop)
269270
{
270271
// STDIN not defined ("php -a") or already closed (`fclose(STDIN)`)
271-
if (!defined('STDIN') || !is_resource(STDIN)) {
272-
$stream = new Stream(fopen('php://memory', 'r'), $loop);
273-
$stream->close();
274-
return $stream;
275-
}
276-
277-
$stream = new Stream(STDIN, $loop);
278-
279-
// support starting program with closed STDIN ("example.php 0<&-")
272+
// also support starting program with closed STDIN ("example.php 0<&-")
280273
// the stream is a valid resource and is not EOF, but fstat fails
281-
if (fstat(STDIN) === false) {
274+
if (!defined('STDIN') || !is_resource(STDIN) || fstat(STDIN) === false) {
275+
$stream = new ReadableResourceStream(fopen('php://memory', 'r'), $loop);
282276
$stream->close();
283277
return $stream;
284278
}
285279

280+
$stream = new ReadableResourceStream(STDIN, $loop);
281+
286282
if (function_exists('readline_callback_handler_install')) {
287283
// Prefer `ext-readline` to install dummy handler to turn on raw input mode.
288284
// We will nevery actually feed the readline handler and instead
@@ -313,12 +309,13 @@ private function createStdin(LoopInterface $loop)
313309
private function createStdout(LoopInterface $loop)
314310
{
315311
// STDOUT not defined ("php -a") or already closed (`fclose(STDOUT)`)
316-
if (!defined('STDOUT') || !is_resource(STDOUT)) {
317-
$output = new Stream(fopen('php://memory', 'r+'), $loop);
312+
// also support starting program with closed STDOUT ("example.php >&-")
313+
// the stream is a valid resource and is not EOF, but fstat fails
314+
if (!defined('STDOUT') || !is_resource(STDOUT) || fstat(STDOUT) === false) {
315+
$output = new WritableResourceStream(fopen('php://memory', 'r+'), $loop);
318316
$output->close();
319317
} else {
320-
$output = new Stream(STDOUT, $loop);
321-
$output->pause();
318+
$output = new WritableResourceStream(STDOUT, $loop);
322319
}
323320

324321
return $output;

tests/FunctionalExampleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,17 @@ public function testPeriodicExampleWithClosedInputQuitsImmediately()
4141
$this->assertNotContains('you just said:', $output);
4242
}
4343

44+
public function testPeriodicExampleWithClosedInputAndOutputQuitsImmediatelyWithoutOutput()
45+
{
46+
$output = $this->execExample('php 01-periodic.php <&- >&- 2>&1');
47+
48+
if (strpos($output, 'said') !== false) {
49+
$this->markTestIncomplete('Your platform exhibits a closed STDIN bug, this may need some further debugging');
50+
}
51+
52+
$this->assertEquals('', $output);
53+
}
54+
4455
public function testStubShowStdinIsReadableByDefault()
4556
{
4657
$output = $this->execExample('php ../tests/stub/01-check-stdin.php');

tests/ReadlineTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use Clue\React\Stdio\Readline;
4-
use React\Stream\ReadableStream;
4+
use React\Stream\ThroughStream;
55

66
class ReadlineTest extends TestCase
77
{
@@ -11,7 +11,7 @@ class ReadlineTest extends TestCase
1111

1212
public function setUp()
1313
{
14-
$this->input = new ReadableStream();
14+
$this->input = new ThroughStream();
1515
$this->output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
1616

1717
$this->readline = new Readline($this->input, $this->output);

tests/StdioTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
<?php
22

3-
use React\EventLoop\Factory;
43
use Clue\React\Stdio\Stdio;
54
use Clue\React\Stdio\Readline;
6-
use React\Stream\ReadableStream;
7-
use React\Stream\WritableStream;
5+
use React\EventLoop\Factory;
6+
use React\Stream\ThroughStream;
87

98
class StdioTest extends TestCase
109
{
@@ -408,7 +407,7 @@ public function testDataEventWithoutNewlineWillBeForwardedAsIs()
408407

409408
public function testEndEventWillBeForwarded()
410409
{
411-
$input = new ReadableStream();
410+
$input = new ThroughStream();
412411
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
413412

414413
//$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
@@ -423,7 +422,7 @@ public function testEndEventWillBeForwarded()
423422

424423
public function testErrorEventFromInputWillBeForwarded()
425424
{
426-
$input = new ReadableStream();
425+
$input = new ThroughStream();
427426
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
428427

429428
//$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
@@ -439,7 +438,7 @@ public function testErrorEventFromInputWillBeForwarded()
439438
public function testErrorEventFromOutputWillBeForwarded()
440439
{
441440
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
442-
$output = new WritableStream();
441+
$output = new ThroughStream();
443442

444443
//$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
445444
$readline = new Readline($input, $output);

0 commit comments

Comments
 (0)