Skip to content

Commit c205e2b

Browse files
committed
Replace internal Stdout class with proper Stream handling
1 parent 21bf3c0 commit c205e2b

File tree

6 files changed

+27
-43
lines changed

6 files changed

+27
-43
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.4.2",
16+
"react/stream": "^0.5 || ^0.4.2",
1717
"clue/utf8-react": "^1.0 || ^0.1",
1818
"clue/term-react": "^1.0 || ^0.1.1"
1919
},

examples/11-login.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,16 @@
2323
$first = false;
2424
} else {
2525
$password = $line;
26-
$stdio->end();
27-
}
28-
});
29-
30-
$loop->run();
31-
32-
echo <<<EOT
26+
$stdio->end(<<<EOT
3327
---------------------
3428
Confirmation:
3529
---------------------
3630
Username: $username
3731
Password: $password
3832
39-
EOT;
33+
EOT
34+
);
35+
}
36+
});
37+
38+
$loop->run();

src/Io/Stdout.php

Lines changed: 0 additions & 31 deletions
This file was deleted.

src/Stdio.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use React\Stream\ReadableStreamInterface;
1111
use React\Stream\Util;
1212
use React\Stream\WritableStreamInterface;
13+
use React\Stream\Stream;
1314

1415
class Stdio extends EventEmitter implements DuplexStreamInterface
1516
{
@@ -28,7 +29,14 @@ public function __construct(LoopInterface $loop, ReadableStreamInterface $input
2829
}
2930

3031
if ($output === null) {
31-
$output = new Stdout();
32+
// STDOUT not defined ("php -a") or already closed (`fclose(STDOUT)`)
33+
if (!defined('STDOUT') || !is_resource(STDOUT)) {
34+
$output = new Stream(fopen('php://memory', 'r+'), $loop);
35+
$output->close();
36+
} else {
37+
$output = new Stream(STDOUT, $loop);
38+
$output->pause();
39+
}
3240
}
3341

3442
if ($readline === null) {

tests/ReadlineTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,9 @@ public function testAutocompleteThrowsIfNotCallable()
526526
$this->assertSame($this->readline, $this->readline->setAutocomplete(123));
527527
}
528528

529+
/**
530+
* @doesNotPerformAssertions
531+
*/
529532
public function testAutocompleteKeyDoesNothingIfUnused()
530533
{
531534
$this->readline->onKeyTab();

tests/StdioTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ public function setUp()
1515
$this->loop = Factory::create();
1616
}
1717

18+
/**
19+
* @doesNotPerformAssertions
20+
*/
1821
public function testCtorDefaultArgs()
1922
{
20-
$stdio = new Stdio($this->loop);
21-
$stdio->close();
23+
new Stdio($this->loop);
24+
25+
// Closing STDIN/STDOUT is not a good idea for reproducible tests
26+
// $stdio->close();
2227
}
2328

2429
public function testCtorReadlineArgWillBeReturnedBygetReadline()

0 commit comments

Comments
 (0)