Skip to content

Commit d699901

Browse files
committed
Support explicitly closing STDIN
1 parent 7dedcb5 commit d699901

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

src/Stdin.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ class Stdin extends Stream
1212

1313
public function __construct(LoopInterface $loop)
1414
{
15+
// STDIN not defined ("php -a") or already closed (`fclose(STDIN)`)
16+
if (!defined('STDIN') || !is_resource(STDIN)) {
17+
parent::__construct(fopen('php://memory', 'r'), $loop);
18+
return $this->close();
19+
}
20+
1521
parent::__construct(STDIN, $loop);
1622

1723
// support starting program with closed STDIN ("example.php 0<&-")

tests/FunctionalExampleTest.php

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

44+
public function testStubShowStdinIsReadableByDefault()
45+
{
46+
$output = $this->execExample('php ../tests/stub/01-check-stdin.php');
47+
48+
$this->assertContains('YES', $output);
49+
}
50+
51+
public function testStubCanCloseStdinAndIsNotReadable()
52+
{
53+
$output = $this->execExample('php ../tests/stub/02-close-stdin.php');
54+
55+
$this->assertContains('NO', $output);
56+
}
57+
4458
private function execExample($command)
4559
{
4660
chdir(__DIR__ . '/../examples/');

tests/stub/01-check-stdin.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
use Clue\React\Stdio\Stdio;
4+
5+
require __DIR__ . '/../../vendor/autoload.php';
6+
7+
$loop = React\EventLoop\Factory::create();
8+
9+
$stdio = new Stdio($loop);
10+
$stdio->end($stdio->isReadable() ? 'YES' : 'NO');
11+
12+
$loop->run();

tests/stub/02-close-stdin.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use Clue\React\Stdio\Stdio;
4+
5+
require __DIR__ . '/../../vendor/autoload.php';
6+
7+
$loop = React\EventLoop\Factory::create();
8+
9+
fclose(STDIN);
10+
$stdio = new Stdio($loop);
11+
$stdio->end($stdio->isReadable() ? 'YES' : 'NO');
12+
13+
$loop->run();

0 commit comments

Comments
 (0)