1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Symfony package. |
5
|
|
|
* |
6
|
|
|
* (c) Fabien Potencier <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Symfony\Component\Process\Pipes; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Process\Exception\InvalidArgumentException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Romain Neutron <[email protected]> |
18
|
|
|
* |
19
|
|
|
* @internal |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractPipes implements PipesInterface |
22
|
|
|
{ |
23
|
|
|
public array $pipes = []; |
24
|
|
|
|
25
|
|
|
private string $inputBuffer = ''; |
26
|
|
|
/** @var resource|string|\Iterator */ |
27
|
|
|
private $input; |
28
|
|
|
private bool $blocked = true; |
29
|
|
|
private ?string $lastError = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param resource|string|\Iterator $input |
33
|
|
|
*/ |
34
|
|
|
public function __construct($input) |
35
|
|
|
{ |
36
|
|
|
if (\is_resource($input) || $input instanceof \Iterator) { |
37
|
|
|
$this->input = $input; |
38
|
|
|
} else { |
39
|
|
|
$this->inputBuffer = (string) $input; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function close(): void |
44
|
|
|
{ |
45
|
|
|
foreach ($this->pipes as $pipe) { |
46
|
|
|
if (\is_resource($pipe)) { |
47
|
|
|
fclose($pipe); |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
$this->pipes = []; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns true if a system call has been interrupted. |
55
|
|
|
*/ |
56
|
|
|
protected function hasSystemCallBeenInterrupted(): bool |
57
|
|
|
{ |
58
|
|
|
$lastError = $this->lastError; |
59
|
|
|
$this->lastError = null; |
60
|
|
|
|
61
|
|
|
// stream_select returns false when the `select` system call is interrupted by an incoming signal |
62
|
|
|
return null !== $lastError && false !== stripos($lastError, 'interrupted system call'); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Unblocks streams. |
67
|
|
|
*/ |
68
|
|
|
protected function unblock(): void |
69
|
|
|
{ |
70
|
|
|
if (!$this->blocked) { |
71
|
|
|
return; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ($this->pipes as $pipe) { |
75
|
|
|
stream_set_blocking($pipe, 0); |
76
|
|
|
} |
77
|
|
|
if (\is_resource($this->input)) { |
78
|
|
|
stream_set_blocking($this->input, 0); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->blocked = false; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Writes input to stdin. |
86
|
|
|
* |
87
|
|
|
* @throws InvalidArgumentException When an input iterator yields a non supported value |
88
|
|
|
*/ |
89
|
|
|
protected function write(): ?array |
90
|
|
|
{ |
91
|
|
|
if (!isset($this->pipes[0])) { |
92
|
|
|
return null; |
93
|
|
|
} |
94
|
|
|
$input = $this->input; |
95
|
|
|
|
96
|
|
|
if ($input instanceof \Iterator) { |
97
|
|
|
if (!$input->valid()) { |
98
|
|
|
$input = null; |
99
|
|
|
} elseif (\is_resource($input = $input->current())) { |
100
|
|
|
stream_set_blocking($input, 0); |
101
|
|
|
} elseif (!isset($this->inputBuffer[0])) { |
102
|
|
|
if (!\is_string($input)) { |
103
|
|
|
if (!\is_scalar($input)) { |
104
|
|
|
throw new InvalidArgumentException(sprintf('"%s" yielded a value of type "%s", but only scalars and stream resources are supported.', get_debug_type($this->input), get_debug_type($input))); |
105
|
|
|
} |
106
|
|
|
$input = (string) $input; |
107
|
|
|
} |
108
|
|
|
$this->inputBuffer = $input; |
109
|
|
|
$this->input->next(); |
110
|
|
|
$input = null; |
111
|
|
|
} else { |
112
|
|
|
$input = null; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$r = $e = []; |
117
|
|
|
$w = [$this->pipes[0]]; |
118
|
|
|
|
119
|
|
|
// let's have a look if something changed in streams |
120
|
|
|
if (false === @stream_select($r, $w, $e, 0, 0)) { |
121
|
|
|
return null; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
foreach ($w as $stdin) { |
125
|
|
|
if (isset($this->inputBuffer[0])) { |
126
|
|
|
$written = fwrite($stdin, $this->inputBuffer); |
127
|
|
|
$this->inputBuffer = substr($this->inputBuffer, $written); |
128
|
|
|
if (isset($this->inputBuffer[0])) { |
129
|
|
|
return [$this->pipes[0]]; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
if ($input) { |
134
|
|
|
while (true) { |
135
|
|
|
$data = fread($input, self::CHUNK_SIZE); |
|
|
|
|
136
|
|
|
if (!isset($data[0])) { |
137
|
|
|
break; |
138
|
|
|
} |
139
|
|
|
$written = fwrite($stdin, $data); |
140
|
|
|
$data = substr($data, $written); |
141
|
|
|
if (isset($data[0])) { |
142
|
|
|
$this->inputBuffer = $data; |
143
|
|
|
|
144
|
|
|
return [$this->pipes[0]]; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
if (feof($input)) { |
|
|
|
|
148
|
|
|
if ($this->input instanceof \Iterator) { |
149
|
|
|
$this->input->next(); |
150
|
|
|
} else { |
151
|
|
|
$this->input = null; |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// no input to read on resource, buffer is empty |
158
|
|
|
if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) { |
159
|
|
|
$this->input = null; |
160
|
|
|
fclose($this->pipes[0]); |
161
|
|
|
unset($this->pipes[0]); |
162
|
|
|
} elseif (!$w) { |
163
|
|
|
return [$this->pipes[0]]; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return null; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @internal |
171
|
|
|
*/ |
172
|
|
|
public function handleError(int $type, string $msg): void |
173
|
|
|
{ |
174
|
|
|
$this->lastError = $msg; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|