From 26df37142d085088085e299555302c411fa98fd4 Mon Sep 17 00:00:00 2001 From: Simon Frings Date: Mon, 2 Aug 2021 13:52:52 +0200 Subject: [PATCH] Simplify usage by supporting new default loop --- README.md | 2 +- composer.json | 4 ++-- examples/random-colors.php | 9 ++------- examples/remove-codes.php | 9 ++------- examples/stdin-codes.php | 7 +------ tests/FunctionalControlCodeParserTest.php | 7 +++---- 6 files changed, 11 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index e04fa08..ecb0a82 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,7 @@ It wraps a given `ReadableStreamInterface` and exposes its plain data through the same interface. ```php -$stdin = new ReadableResourceStream(STDIN, $loop); +$stdin = new React\Stream\ReadableResourceStream(STDIN); $stream = new ControlCodeParser($stdin); diff --git a/composer.json b/composer.json index d1b3ce5..cbb2671 100644 --- a/composer.json +++ b/composer.json @@ -18,10 +18,10 @@ }, "require": { "php": ">=5.3", - "react/stream": "^1.0 || ^0.7" + "react/stream": "^1.2" }, "require-dev": { "phpunit/phpunit": "^9.3 || ^5.7 || ^4.8", - "react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3" + "react/event-loop": "^1.2" } } diff --git a/examples/random-colors.php b/examples/random-colors.php index 8673391..68beb26 100644 --- a/examples/random-colors.php +++ b/examples/random-colors.php @@ -11,25 +11,22 @@ // with random colors: // $ phpunit --color=always | php random-colors.php -use React\EventLoop\Factory; use Clue\React\Term\ControlCodeParser; use React\Stream\ReadableResourceStream; use React\Stream\WritableResourceStream; require __DIR__ . '/../vendor/autoload.php'; -$loop = Factory::create(); - if (function_exists('posix_isatty') && posix_isatty(STDIN)) { // Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead) shell_exec('stty -icanon -echo'); } // process control codes from STDIN -$stdin = new ReadableResourceStream(STDIN, $loop); +$stdin = new ReadableResourceStream(STDIN); $parser = new ControlCodeParser($stdin); -$stdout = new WritableResourceStream(STDOUT, $loop); +$stdout = new WritableResourceStream(STDOUT); // pass all c0 codes through to output $parser->on('c0', array($stdout, 'write')); @@ -55,5 +52,3 @@ // start with random color $stdin->emit('data', array("\033[m")); - -$loop->run(); diff --git a/examples/remove-codes.php b/examples/remove-codes.php index c87b2ca..5e7db8d 100644 --- a/examples/remove-codes.php +++ b/examples/remove-codes.php @@ -8,26 +8,23 @@ // codes like this: // $ phpunit --color=always | php remove-codes.php -use React\EventLoop\Factory; use Clue\React\Term\ControlCodeParser; use React\Stream\ReadableResourceStream; use React\Stream\WritableResourceStream; require __DIR__ . '/../vendor/autoload.php'; -$loop = Factory::create(); - if (function_exists('posix_isatty') && posix_isatty(STDIN)) { // Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead) shell_exec('stty -icanon -echo'); } // process control codes from STDIN -$stdin = new ReadableResourceStream(STDIN, $loop); +$stdin = new ReadableResourceStream(STDIN); $parser = new ControlCodeParser($stdin); // pipe data from STDIN to STDOUT without any codes -$stdout = new WritableResourceStream(STDOUT, $loop); +$stdout = new WritableResourceStream(STDOUT); $parser->pipe($stdout); // only forward \r, \n and \t @@ -36,5 +33,3 @@ $stdout->write($code); } }); - -$loop->run(); diff --git a/examples/stdin-codes.php b/examples/stdin-codes.php index f0d8d76..8845dbb 100644 --- a/examples/stdin-codes.php +++ b/examples/stdin-codes.php @@ -9,21 +9,18 @@ // codes like this: // $ phpunit --color=always | php stdin-codes.php -use React\EventLoop\Factory; use Clue\React\Term\ControlCodeParser; use React\Stream\ReadableResourceStream; require __DIR__ . '/../vendor/autoload.php'; -$loop = Factory::create(); - if (function_exists('posix_isatty') && posix_isatty(STDIN)) { // Disable icanon (so we can fread each keypress) and echo (we'll do echoing here instead) shell_exec('stty -icanon -echo'); } // process control codes from STDIN -$stdin = new ReadableResourceStream(STDIN, $loop); +$stdin = new ReadableResourceStream(STDIN); $parser = new ControlCodeParser($stdin); $decoder = function ($code) { @@ -42,5 +39,3 @@ $parser->on('data', function ($bytes) { echo 'Data: ' . $bytes . PHP_EOL; }); - -$loop->run(); diff --git a/tests/FunctionalControlCodeParserTest.php b/tests/FunctionalControlCodeParserTest.php index 666f509..d5008a5 100644 --- a/tests/FunctionalControlCodeParserTest.php +++ b/tests/FunctionalControlCodeParserTest.php @@ -3,6 +3,7 @@ namespace Clue\Tests\React\Term; use Clue\React\Term\ControlCodeParser; +use React\EventLoop\Loop; use React\EventLoop\Factory; use React\Stream\ReadableResourceStream; @@ -10,9 +11,7 @@ class FunctionalControlCodeParserTest extends TestCase { public function testPipingReadme() { - $loop = Factory::create(); - - $input = new ReadableResourceStream(fopen(__DIR__ . '/../README.md', 'r+'), $loop); + $input = new ReadableResourceStream(fopen(__DIR__ . '/../README.md', 'r+')); $parser = new ControlCodeParser($input); $buffer = ''; @@ -20,7 +19,7 @@ public function testPipingReadme() $buffer .= $chunk; }); - $loop->run(); + Loop::run(); $readme = str_replace( "\n",