Skip to content

Commit 9fdd946

Browse files
committed
Forward compatebility with ReactPHP packages
1 parent 47347be commit 9fdd946

File tree

6 files changed

+73
-23
lines changed

6 files changed

+73
-23
lines changed

composer.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212
],
1313
"require": {
1414
"php": ">=5.3",
15-
"react/event-loop": "0.3.*|0.4.*",
16-
"react/stream": "^0.4.2",
17-
"clue/utf8-react": "^0.1",
18-
"clue/term-react": "^0.1.1"
15+
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
16+
"react/stream": "^1.0 || ^0.7.2",
17+
"clue/utf8-react": "^1.1",
18+
"clue/term-react": "^1.1"
1919
},
2020
"suggest": {
2121
"ext-mbstring": "Using ext-mbstring should provide slightly better performance for handling I/O"

src/Stdin.php

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

33
namespace Clue\React\Stdio;
44

5-
use React\Stream\ReadableStream;
6-
use React\Stream\Stream;
5+
use Evenement\EventEmitter;
6+
use React\Stream\ReadableResourceStream;
7+
use React\Stream\ReadableStreamInterface;
78
use React\EventLoop\LoopInterface;
9+
use React\Stream\Util;
10+
use React\Stream\WritableStreamInterface;
811

9-
// TODO: only implement ReadableStream
10-
class Stdin extends Stream
12+
class Stdin extends EventEmitter implements ReadableStreamInterface
1113
{
1214
private $oldMode = null;
1315

16+
private $stream;
17+
1418
public function __construct(LoopInterface $loop)
1519
{
16-
parent::__construct(STDIN, $loop);
20+
$this->stream = new ReadableResourceStream(STDIN, $loop);
21+
Util::forwardEvents($this->stream, $this, array('data', 'error', 'end', 'close'));
1722
}
1823

1924
public function resume()
@@ -24,7 +29,7 @@ public function resume()
2429
// Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
2530
shell_exec('stty -icanon -echo');
2631

27-
parent::resume();
32+
$this->stream->resume();
2833
}
2934
}
3035

@@ -35,14 +40,23 @@ public function pause()
3540
shell_exec(sprintf('stty %s', $this->oldMode));
3641

3742
$this->oldMode = null;
38-
parent::pause();
43+
$this->stream->pause();
3944
}
4045
}
4146

4247
public function close()
4348
{
44-
$this->pause();
45-
parent::close();
49+
$this->stream->close();
50+
}
51+
52+
public function isReadable()
53+
{
54+
return $this->stream->isReadable();
55+
}
56+
57+
public function pipe(WritableStreamInterface $dest, array $options = array())
58+
{
59+
return $this->stream->pipe($dest, $options);
4660
}
4761

4862
public function __destruct()

src/Stdio.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function __construct(LoopInterface $loop, ReadableStreamInterface $input
2626
}
2727

2828
if ($output === null) {
29-
$output = new Stdout(STDOUT);
29+
$output = new Stdout($loop);
3030
}
3131

3232
if ($readline === null) {

src/Stdout.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,39 @@
22

33
namespace Clue\React\Stdio;
44

5-
use React\Stream\WritableStream;
5+
use Evenement\EventEmitter;
6+
use React\EventLoop\LoopInterface;
7+
use React\Stream\Util;
8+
use React\Stream\WritableResourceStream;
9+
use React\Stream\WritableStreamInterface;
610

7-
class Stdout extends WritableStream
11+
class Stdout extends EventEmitter implements WritableStreamInterface
812
{
13+
private $stream;
14+
15+
public function __construct(LoopInterface $loop)
16+
{
17+
$this->stream = new WritableResourceStream(STDOUT, $loop);
18+
Util::forwardEvents($this->stream, $this, array('data', 'error', 'end', 'close'));
19+
}
20+
21+
public function isWritable()
22+
{
23+
return $this->stream->isWritable();
24+
}
25+
926
public function write($data)
1027
{
11-
// TODO: use non-blocking output instead
28+
return $this->stream->write($data);
29+
}
1230

13-
fwrite(STDOUT, $data);
31+
public function end($data = null)
32+
{
33+
return $this->stream->end($data);
34+
}
1435

15-
return true;
36+
public function close()
37+
{
38+
return $this->stream->close();
1639
}
1740
}

tests/ReadlineTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use Clue\React\Stdio\Readline;
44
use React\Stream\ReadableStream;
5+
use React\Stream\ThroughStream;
56

67
class ReadlineTest extends TestCase
78
{
@@ -11,7 +12,7 @@ class ReadlineTest extends TestCase
1112

1213
public function setUp()
1314
{
14-
$this->input = new ReadableStream();
15+
$this->input = new ThroughStream();
1516
$this->output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
1617

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

tests/StdioTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Clue\React\Stdio\Stdio;
55
use Clue\React\Stdio\Readline;
66
use React\Stream\ReadableStream;
7+
use React\Stream\ThroughStream;
78
use React\Stream\WritableStream;
89

910
class StdioTest extends TestCase
@@ -18,6 +19,17 @@ public function setUp()
1819
public function testCtorDefaultArgs()
1920
{
2021
$stdio = new Stdio($this->loop);
22+
23+
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
24+
$inputProperty = new ReflectionProperty($stdio, 'input');
25+
$inputProperty->setAccessible(true);
26+
$inputProperty->setValue($stdio, $input);
27+
28+
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
29+
$outputProperty = new ReflectionProperty($stdio, 'output');
30+
$outputProperty->setAccessible(true);
31+
$outputProperty->setValue($stdio, $output);
32+
2133
$stdio->close();
2234
}
2335

@@ -461,7 +473,7 @@ public function testDataEventWillBeForwarded()
461473

462474
public function testEndEventWillBeForwarded()
463475
{
464-
$input = new ReadableStream();
476+
$input = new ThroughStream();
465477
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
466478

467479
//$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
@@ -476,7 +488,7 @@ public function testEndEventWillBeForwarded()
476488

477489
public function testErrorEventFromInputWillBeForwarded()
478490
{
479-
$input = new ReadableStream();
491+
$input = new ThroughStream();
480492
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
481493

482494
//$readline = $this->getMockBuilder('Clue\React\Stdio\Readline')->disableOriginalConstructor()->getMock();
@@ -492,7 +504,7 @@ public function testErrorEventFromInputWillBeForwarded()
492504
public function testErrorEventFromOutputWillBeForwarded()
493505
{
494506
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
495-
$output = new WritableStream();
507+
$output = new ThroughStream();
496508

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

0 commit comments

Comments
 (0)