forked from clue/reactphp-shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeferredShell.php
More file actions
192 lines (158 loc) · 5.64 KB
/
DeferredShell.php
File metadata and controls
192 lines (158 loc) · 5.64 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
namespace Clue\React\Shell;
use React\Promise\Deferred;
use React\Stream\DuplexStreamInterface;
use RuntimeException;
class DeferredShell
{
private $stream;
private $pending = array();
private $ending = false;
private $bounding;
private $boundingCommand;
private $eol = PHP_EOL;
private $buffer = '';
private $started = false;
public function __construct(DuplexStreamInterface $stream)
{
$this->stream = $stream;
$this->stream->on('data', array($this, 'handleData'));
$this->stream->on('end', array($this, 'handleEnd'));
$this->setBounding('echo -n {{ bounding }}', md5(uniqid()));
}
/**
* Sets end-of-line (EOL) string terminator to use
*
* @param string $eol
*/
public function setEol($eol)
{
$this->eol = $eol;
}
/**
* Sets bounding to print before and after calling execute()
*
* This bounding will be used to determine the start and end position of
* each executed command.
*
* The $boundingText is a (pseudo-)random string that can be used to mark
* the start and end position of every command. Make sure to use a sufficiently
* unique string that does not occur anyway in your command output.
* The $boundingText will be searched for as-is, make sure to append a
* trailing EOL if your $boundingCommand prints an EOL that should not be
* part of the command output.
*
* The $boundingCommand is responsible for writing out the $boundingText
* to the STDOUT right before and right after calling execute().
* You can use the "{{ bounding }}" placeholder that will be replaced with
* the given $boundingText. The current EOL will automatically be written
* after the $boundingCommand.
*
* @param string $boundingCommand the command used to write the bounding text to the STDOUT
* @param string $boundingText (optional) bounding text to use, defaults to a new random text
* @see self::setEol()
*/
public function setBounding($boundingCommand, $boundingText = null)
{
if ($boundingText === null) {
$boundingText = md5(uniqid());
}
$this->boundingCommand = str_replace('{{ bounding }}', $boundingText, $boundingCommand);
$this->bounding = $boundingText;
}
/**
* Execute the given $command string in this shell instance
*
* @param string $command
* @return Promise Promise<string> resolves with the command output or rejects with a RuntimeException
* @see self::setBounding()
*/
public function execute($command)
{
$deferred = new Deferred();
if ($this->ending) {
$deferred->reject(new RuntimeException('Shell is ending already'));
} else {
$this->pending []= $deferred;
$this->sendCommand($command);
}
return $deferred->promise();
}
/**
* Soft-close the shell once all pending commands have been fulfilled
*
* @see self::close()
*/
public function end()
{
$this->ending = true;
if (!$this->pending) {
$this->close();
}
}
/**
* hard-close the shell now and reject all pending commands
*
* @see self::end()
*/
public function close()
{
$this->ending = true;
foreach ($this->pending as $deferred) {
$deferred->reject(new RuntimeException('Shell is ending'));
}
$this->pending = array();
$this->buffer = '';
$this->stream->removeListener('data', array($this, 'handleData'));
$this->stream->removeListener('end', array($this, 'handleEnd'));
$this->stream->close();
}
private function sendCommand($command)
{
$this->stream->write($this->boundingCommand . $this->eol . $command . $this->eol . $this->boundingCommand . $this->eol);
}
/** @internal */
public function handleData($data)
{
// temporarily buffer everything
$this->buffer .= $data;
do {
// search bounding in buffer
$pos = strpos($this->buffer, $this->bounding);
// bounding has not been found
if ($pos === false) {
if (!$this->started) {
// received junk before actual start
// trim its length to avoid buffering useless data
$this->buffer = (string)substr($this->buffer, 1 - strlen($this->bounding));
}
return;
}
// bounding has been found
if ($this->started) {
// already started, so we found the end bounding
$data = (string)substr($this->buffer, 0, $pos);
// advance buffer behind end of bounding
$this->buffer = (string)substr($this->buffer, $pos + strlen($this->bounding));
$this->started = false;
$deferred = array_shift($this->pending);
/* @var $deferred Deferred */
$deferred->resolve($data);
// last pending message received => close shell
if ($this->ending && !$this->pending) {
$this->close();
}
} else {
// not already started, so we found the start bounding
$this->buffer = (string)substr($this->buffer, $pos + strlen($this->bounding));
$this->started = true;
}
} while ($this->buffer !== '');
}
/** @internal */
public function handleEnd()
{
// STDOUT closed => assume process is closed
$this->close();
}
}