File tree Expand file tree Collapse file tree 2 files changed +55
-18
lines changed Expand file tree Collapse file tree 2 files changed +55
-18
lines changed Original file line number Diff line number Diff line change @@ -14,39 +14,37 @@ class Stdin extends Stream
1414 public function __construct (LoopInterface $ loop )
1515 {
1616 parent ::__construct (STDIN , $ loop );
17- }
1817
19- public function resume ()
20- {
21- if ($ this ->oldMode === null ) {
18+ if ($ this ->isTty ()) {
2219 $ this ->oldMode = shell_exec ('stty -g ' );
2320
2421 // Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead)
2522 shell_exec ('stty -icanon -echo ' );
26-
27- parent ::resume ();
2823 }
2924 }
3025
31- public function pause ()
26+ public function close ()
3227 {
33- if ( $ this ->oldMode !== null ) {
34- // Reset stty so it behaves normally again
35- shell_exec ( sprintf ( ' stty %s ' , $ this -> oldMode ));
28+ $ this ->restore ();
29+ parent :: close ();
30+ }
3631
37- $ this -> oldMode = null ;
38- parent :: pause ();
39- }
32+ public function __destruct ()
33+ {
34+ $ this -> restore ();
4035 }
4136
42- public function close ()
37+ private function restore ()
4338 {
44- $ this ->pause ();
45- parent ::close ();
39+ if ($ this ->oldMode !== null && $ this ->isTty ()) {
40+ // Reset stty so it behaves normally again
41+ shell_exec (sprintf ('stty %s ' , $ this ->oldMode ));
42+ $ this ->oldMode = null ;
43+ }
4644 }
4745
48- public function __destruct ()
46+ private function isTty ()
4947 {
50- $ this -> pause ( );
48+ return ( is_resource ( STDIN ) && function_exists ( ' posix_isatty ' ) && posix_isatty ( STDIN ) );
5149 }
5250}
Original file line number Diff line number Diff line change 1+ <?php
2+
3+ class FunctionalExampleTest extends TestCase
4+ {
5+ public function testPeriodicExampleWithPipedInputEndsBecauseInputEnds ()
6+ {
7+ $ output = $ this ->execExample ('echo hello | php 01-periodic.php ' );
8+
9+ $ this ->assertContains ('you just said: hello\n ' , $ output );
10+ }
11+
12+ public function testPeriodicExampleWithNullInputQuitsImmediately ()
13+ {
14+ $ output = $ this ->execExample ('php 01-periodic.php < /dev/null ' );
15+
16+ $ this ->assertNotContains ('you just said: ' , $ output );
17+ }
18+
19+ public function testPeriodicExampleWithNoInputQuitsImmediately ()
20+ {
21+ $ output = $ this ->execExample ('true | php 01-periodic.php ' );
22+
23+ $ this ->assertNotContains ('you just said: ' , $ output );
24+ }
25+
26+ public function testPeriodicExampleWithSleepNoInputQuitsOnEnd ()
27+ {
28+ $ output = $ this ->execExample ('sleep 0.1 | php 01-periodic.php ' );
29+
30+ $ this ->assertNotContains ('you just said: ' , $ output );
31+ }
32+
33+ private function execExample ($ command )
34+ {
35+ chdir (__DIR__ . '/../examples/ ' );
36+
37+ return shell_exec ($ command );
38+ }
39+ }
You can’t perform that action at this time.
0 commit comments