Skip to content

Commit

Permalink
Forward compatebility with ReactPHP packages
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus committed Oct 26, 2017
1 parent 47347be commit eca3e3b
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 23 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
],
"require": {
"php": ">=5.3",
"react/event-loop": "0.3.*|0.4.*",
"react/stream": "^0.4.2",
"clue/utf8-react": "^0.1",
"clue/term-react": "^0.1.1"
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3",
"react/stream": "^1.0 || ^0.7.2",
"clue/utf8-react": "^1.1",
"clue/term-react": "^1.1"
},
"suggest": {
"ext-mbstring": "Using ext-mbstring should provide slightly better performance for handling I/O"
Expand Down
32 changes: 23 additions & 9 deletions src/Stdin.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

namespace Clue\React\Stdio;

use React\Stream\ReadableStream;
use React\Stream\Stream;
use Evenement\EventEmitter;
use React\Stream\ReadableResourceStream;
use React\Stream\ReadableStreamInterface;
use React\EventLoop\LoopInterface;
use React\Stream\Util;
use React\Stream\WritableStreamInterface;

// TODO: only implement ReadableStream
class Stdin extends Stream
class Stdin extends EventEmitter implements ReadableStreamInterface
{
private $oldMode = null;

private $stream;

public function __construct(LoopInterface $loop)
{
parent::__construct(STDIN, $loop);
$this->stream = new ReadableResourceStream(STDIN, $loop);
Util::forwardEvents($this->stream, $this, array('data', 'error', 'end', 'close'));
}

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

parent::resume();
$this->stream->resume();
}
}

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

$this->oldMode = null;
parent::pause();
$this->stream->pause();
}
}

public function close()
{
$this->pause();
parent::close();
$this->stream->close();
}

public function isReadable()
{
return $this->stream->isReadable();
}

public function pipe(WritableStreamInterface $dest, array $options = array())
{
return $this->stream->pipe($dest, $options);
}

public function __destruct()
Expand Down
2 changes: 1 addition & 1 deletion src/Stdio.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function __construct(LoopInterface $loop, ReadableStreamInterface $input
}

if ($output === null) {
$output = new Stdout(STDOUT);
$output = new Stdout($loop);
}

if ($readline === null) {
Expand Down
33 changes: 28 additions & 5 deletions src/Stdout.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,39 @@

namespace Clue\React\Stdio;

use React\Stream\WritableStream;
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
use React\Stream\Util;
use React\Stream\WritableResourceStream;
use React\Stream\WritableStreamInterface;

class Stdout extends WritableStream
class Stdout extends EventEmitter implements WritableStreamInterface
{
private $stream;

public function __construct(LoopInterface $loop)
{
$this->stream = new WritableResourceStream(STDOUT, $loop);
Util::forwardEvents($this->stream, $this, array('data', 'error', 'end', 'close'));
}

public function isWritable()
{
return $this->stream->isWritable();
}

public function write($data)
{
// TODO: use non-blocking output instead
return $this->stream->write($data);
}

fwrite(STDOUT, $data);
public function end($data = null)
{
return $this->stream->end($data);
}

return true;
public function close()
{
return $this->stream->close();
}
}
3 changes: 2 additions & 1 deletion tests/ReadlineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Clue\React\Stdio\Readline;
use React\Stream\ReadableStream;
use React\Stream\ThroughStream;

class ReadlineTest extends TestCase
{
Expand All @@ -11,7 +12,7 @@ class ReadlineTest extends TestCase

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

$this->readline = new Readline($this->input, $this->output);
Expand Down
18 changes: 15 additions & 3 deletions tests/StdioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Clue\React\Stdio\Stdio;
use Clue\React\Stdio\Readline;
use React\Stream\ReadableStream;
use React\Stream\ThroughStream;
use React\Stream\WritableStream;

class StdioTest extends TestCase
Expand All @@ -18,6 +19,17 @@ public function setUp()
public function testCtorDefaultArgs()
{
$stdio = new Stdio($this->loop);

$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock();
$inputProperty = new ReflectionProperty($stdio, 'input');
$inputProperty->setAccessible(true);
$inputProperty->setValue($stdio, $input);

$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
$outputProperty = new ReflectionProperty($stdio, 'output');
$outputProperty->setAccessible(true);
$outputProperty->setValue($stdio, $output);

$stdio->close();
}

Expand Down Expand Up @@ -461,7 +473,7 @@ public function testDataEventWillBeForwarded()

public function testEndEventWillBeForwarded()
{
$input = new ReadableStream();
$input = new ThroughStream();
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

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

public function testErrorEventFromInputWillBeForwarded()
{
$input = new ReadableStream();
$input = new ThroughStream();
$output = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();

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

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

0 comments on commit eca3e3b

Please sign in to comment.