fix: handle stdio properly
This commit is contained in:
24
examples/hello-world.php
Normal file
24
examples/hello-world.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
use Nih\CommandBuilder\Command;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$output = (new Command('echo'))
|
||||
->arg('Hello, World!')
|
||||
->output();
|
||||
|
||||
var_dump($output);
|
||||
|
||||
// object(Nih\CommandBuilder\Output)#9 (3) {
|
||||
// ["stdout"]=>
|
||||
// string(14) "Hello, World!
|
||||
// "
|
||||
// ["stderr"]=>
|
||||
// string(0) ""
|
||||
// ["code"]=>
|
||||
// object(Nih\CommandBuilder\ExitStatus)#8 (1) {
|
||||
// ["code"]=>
|
||||
// int(0)
|
||||
// }
|
||||
// }
|
||||
17
examples/plumbing.php
Normal file
17
examples/plumbing.php
Normal file
@@ -0,0 +1,17 @@
|
||||
<?php
|
||||
|
||||
use Nih\CommandBuilder\Command;
|
||||
use Nih\CommandBuilder\Stdio;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$echo = (new Command('echo'))
|
||||
->arg('Hello, World!')
|
||||
->stdout(Stdio::piped())
|
||||
->spawn();
|
||||
|
||||
$cat = (new Command('cat'))
|
||||
->stdin(Stdio::stream($echo->stdout))
|
||||
->status();
|
||||
|
||||
// Prints "Hello, World!\n"
|
||||
35
examples/stdio.php
Normal file
35
examples/stdio.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
use Nih\CommandBuilder\Command;
|
||||
use Nih\CommandBuilder\Stdio;
|
||||
|
||||
require_once __DIR__ . '/../vendor/autoload.php';
|
||||
|
||||
$child = (new Command('cat'))
|
||||
->stdin(Stdio::piped())
|
||||
->stdout(Stdio::piped())
|
||||
->spawn();
|
||||
|
||||
$child->stdin?->write('Hello, World!');
|
||||
$output = $child->waitWithOutput();
|
||||
|
||||
var_dump($output);
|
||||
|
||||
// object(Nih\CommandBuilder\Output)#6 (3) {
|
||||
// ["stdout"]=>
|
||||
// string(13) "Hello, World!"
|
||||
// ["stderr"]=>
|
||||
// NULL
|
||||
// ["code"]=>
|
||||
// object(Nih\CommandBuilder\ExitStatus)#4 (1) {
|
||||
// ["code"]=>
|
||||
// int(0)
|
||||
// }
|
||||
// }
|
||||
|
||||
(new Command('echo'))
|
||||
->arg('Hello, World!')
|
||||
->stdout(Stdio::inherit())
|
||||
->status();
|
||||
|
||||
// Hello, World!
|
||||
Reference in New Issue
Block a user