PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / Symfony / Process / Pipes / UnixPipes.php

UnixPipes.php in ManageWP Worker 4.9.25, at src/Symfony/Process/Pipes/UnixPipes.php

211 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
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 /**
13 * UnixPipes implementation uses unix pipes as handles.
14 *
15 * @author Romain Neutron <imprec@gmail.com>
16 *
17 * @internal
18 */
19 class Symfony_Process_Pipes_UnixPipes extends Symfony_Process_Pipes_AbstractPipes
20 {
21 /** @var bool */
22 private $ttyMode;
23 /** @var bool */
24 private $ptyMode;
25 /** @var bool */
26 private $disableOutput;
27
28 public function __construct($ttyMode, $ptyMode, $input, $disableOutput)
29 {
30 $this->ttyMode = (bool) $ttyMode;
31 $this->ptyMode = (bool) $ptyMode;
32 $this->disableOutput = (bool) $disableOutput;
33
34 if (is_resource($input)) {
35 $this->input = $input;
36 } else {
37 $this->inputBuffer = (string) $input;
38 }
39 }
40
41 public function __destruct()
42 {
43 $this->close();
44 }
45
46 /**
47 * {@inheritdoc}
48 */
49 public function getDescriptors()
50 {
51 if ($this->disableOutput) {
52 $nullstream = fopen('/dev/null', 'c');
53
54 return array(
55 array('pipe', 'r'),
56 $nullstream,
57 $nullstream,
58 );
59 }
60
61 if ($this->ttyMode) {
62 return array(
63 array('file', '/dev/tty', 'r'),
64 array('file', '/dev/tty', 'w'),
65 array('file', '/dev/tty', 'w'),
66 );
67 }
68
69 if ($this->ptyMode && Symfony_Process_Process::isPtySupported()) {
70 return array(
71 array('pty'),
72 array('pty'),
73 array('pty'),
74 );
75 }
76
77 return array(
78 array('pipe', 'r'),
79 array('pipe', 'w'), // stdout
80 array('pipe', 'w'), // stderr
81 );
82 }
83
84 /**
85 * {@inheritdoc}
86 */
87 public function getFiles()
88 {
89 return array();
90 }
91
92 /**
93 * {@inheritdoc}
94 */
95 public function readAndWrite($blocking, $close = false)
96 {
97 // only stdin is left open, job has been done !
98 // we can now close it
99 if (1 === count($this->pipes) && array(0) === array_keys($this->pipes)) {
100 fclose($this->pipes[0]);
101 unset($this->pipes[0]);
102 }
103
104 if (empty($this->pipes)) {
105 return array();
106 }
107
108 $this->unblock();
109
110 $read = array();
111
112 if (null !== $this->input) {
113 // if input is a resource, let's add it to stream_select argument to
114 // fill a buffer
115 $r = array_merge($this->pipes, array('input' => $this->input));
116 } else {
117 $r = $this->pipes;
118 }
119 // discard read on stdin
120 unset($r[0]);
121
122 $w = isset($this->pipes[0]) ? array($this->pipes[0]) : null;
123 $e = null;
124
125 // let's have a look if something changed in streams
126 if (false === $n = @stream_select($r, $w, $e, 0, $blocking ? Symfony_Process_Process::TIMEOUT_PRECISION * 1E6 : 0)) {
127 // if a system call has been interrupted, forget about it, let's try again
128 // otherwise, an error occurred, let's reset pipes
129 if (!$this->hasSystemCallBeenInterrupted()) {
130 $this->pipes = array();
131 }
132
133 return $read;
134 }
135
136 // nothing has changed
137 if (0 === $n) {
138 return $read;
139 }
140
141 foreach ($r as $pipe) {
142 // prior PHP 5.4 the array passed to stream_select is modified and
143 // lose key association, we have to find back the key
144 $type = (false !== $found = array_search($pipe, $this->pipes)) ? $found : 'input';
145 $data = '';
146 while ('' !== $dataread = (string) fread($pipe, self::CHUNK_SIZE)) {
147 $data .= $dataread;
148 }
149
150 if ('' !== $data) {
151 if ($type === 'input') {
152 $this->inputBuffer .= $data;
153 } else {
154 $read[$type] = $data;
155 }
156 }
157
158 if (false === $data || (true === $close && feof($pipe) && '' === $data)) {
159 if ($type === 'input') {
160 // no more data to read on input resource
161 // use an empty buffer in the next reads
162 $this->input = null;
163 } else {
164 fclose($this->pipes[$type]);
165 unset($this->pipes[$type]);
166 }
167 }
168 }
169
170 if (null !== $w && 0 < count($w)) {
171 while (strlen($this->inputBuffer)) {
172 $written = fwrite($w[0], $this->inputBuffer, 2 << 18); // write 512k
173 if ($written > 0) {
174 $this->inputBuffer = (string) substr($this->inputBuffer, $written);
175 } else {
176 break;
177 }
178 }
179 }
180
181 // no input to read on resource, buffer is empty and stdin still open
182 if ('' === $this->inputBuffer && null === $this->input && isset($this->pipes[0])) {
183 fclose($this->pipes[0]);
184 unset($this->pipes[0]);
185 }
186
187 return $read;
188 }
189
190 /**
191 * {@inheritdoc}
192 */
193 public function areOpen()
194 {
195 return (bool) $this->pipes;
196 }
197
198 /**
199 * Creates a new UnixPipes instance
200 *
201 * @param Symfony_Process_Process $process
202 * @param string|resource $input
203 *
204 * @return Symfony_Process_Pipes_UnixPipes
205 */
206 public static function create(Symfony_Process_Process $process, $input)
207 {
208 return new self($process->isTty(), $process->isPty(), $input, $process->isOutputDisabled());
209 }
210 }
211