-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.php
91 lines (78 loc) · 2.49 KB
/
demo.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env php
<?php
use Moccalotto\Process\Process;
require 'vendor/autoload.php';
/*
| Execute ls
|------------
| Execute ls and capture the output.
| Then print all the captured output.
*/
$proc = Process::execute('ls -a');
echo $proc->readToEnd();
echo PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;
/*
| Execute cat and talk to it interactively
|------------------------------------------
| Execute cat, and input a few lines into stdin.
| Cat should echo those lines out to stdout.
| Then, we read those lines from stdout.
| Then we ask cat to terminate.
| Then we print cat's exitcode.
*/
$proc = Process::execute('cat');
$proc->write('========================' . PHP_EOL);
$proc->write('==== CAT EXAMPLE ====' . PHP_EOL);
$proc->write('==== cat is working ====' . PHP_EOL);
$proc->write('========================' . PHP_EOL);
// read the lines again.
// Beware that this process is blocking.
// if you call readline on a process that
// has not yet printed a newline character,
// your program will hang!
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
$proc->signalStop();
echo $proc->readErrorToEnd();
echo PHP_EOL;
printf('Exit code: %d', $proc->exitCode());
echo PHP_EOL;
echo PHP_EOL;
echo PHP_EOL;
/*
| Execute a terminal and run commands in it
|-------------------------------------------
| Execute bash.
| Run a few commands inside it, and capture their output.
| Then wait for the commands to finish executing,
| Then run exit to make bash exit neatly.
| Then ask bash to terminate if it has not executed the 'exit' command yet.
| Then print all the captured output.
*/
$proc = Process::execute('/bin/bash');
$proc->write('echo "=========================="' . PHP_EOL);
$proc->write('echo "==== BASH EXAMPLE ===="' . PHP_EOL);
$proc->write('echo "==== several commands ===="' . PHP_EOL);
$proc->write('echo "==== in a shell ===="' . PHP_EOL);
$proc->write('echo "=========================="' . PHP_EOL);
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
echo $proc->readline();
$proc->write('ls' . PHP_EOL);
$proc->write('echo ==========================' . PHP_EOL);
$proc->write('uname' . PHP_EOL);
$proc->write('echo ==========================' . PHP_EOL);
$proc->write('export | head' . PHP_EOL);
$proc->write('echo ==========================' . PHP_EOL);
$proc->write('echo bye...' . PHP_EOL);
$proc->write("exit");
// give all commands time to execute.
usleep(100000);
$proc->signalStop();
echo $proc->readToEnd();