Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Process\Pipes;
|
Chris@0
|
13
|
Chris@17
|
14 use Symfony\Component\Process\Exception\RuntimeException;
|
Chris@0
|
15 use Symfony\Component\Process\Process;
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * WindowsPipes implementation uses temporary files as handles.
|
Chris@0
|
19 *
|
Chris@0
|
20 * @see https://bugs.php.net/bug.php?id=51800
|
Chris@0
|
21 * @see https://bugs.php.net/bug.php?id=65650
|
Chris@0
|
22 *
|
Chris@0
|
23 * @author Romain Neutron <imprec@gmail.com>
|
Chris@0
|
24 *
|
Chris@0
|
25 * @internal
|
Chris@0
|
26 */
|
Chris@0
|
27 class WindowsPipes extends AbstractPipes
|
Chris@0
|
28 {
|
Chris@17
|
29 private $files = [];
|
Chris@17
|
30 private $fileHandles = [];
|
Chris@17
|
31 private $lockHandles = [];
|
Chris@17
|
32 private $readBytes = [
|
Chris@0
|
33 Process::STDOUT => 0,
|
Chris@0
|
34 Process::STDERR => 0,
|
Chris@17
|
35 ];
|
Chris@0
|
36 private $haveReadSupport;
|
Chris@0
|
37
|
Chris@0
|
38 public function __construct($input, $haveReadSupport)
|
Chris@0
|
39 {
|
Chris@0
|
40 $this->haveReadSupport = (bool) $haveReadSupport;
|
Chris@0
|
41
|
Chris@0
|
42 if ($this->haveReadSupport) {
|
Chris@0
|
43 // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big.
|
Chris@0
|
44 // Workaround for this problem is to use temporary files instead of pipes on Windows platform.
|
Chris@0
|
45 //
|
Chris@0
|
46 // @see https://bugs.php.net/bug.php?id=51800
|
Chris@17
|
47 $pipes = [
|
Chris@0
|
48 Process::STDOUT => Process::OUT,
|
Chris@0
|
49 Process::STDERR => Process::ERR,
|
Chris@17
|
50 ];
|
Chris@0
|
51 $tmpDir = sys_get_temp_dir();
|
Chris@0
|
52 $lastError = 'unknown reason';
|
Chris@0
|
53 set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; });
|
Chris@0
|
54 for ($i = 0;; ++$i) {
|
Chris@0
|
55 foreach ($pipes as $pipe => $name) {
|
Chris@0
|
56 $file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name);
|
Chris@17
|
57
|
Chris@17
|
58 if (!$h = fopen($file.'.lock', 'w')) {
|
Chris@17
|
59 restore_error_handler();
|
Chris@17
|
60 throw new RuntimeException(sprintf('A temporary file could not be opened to write the process output: %s', $lastError));
|
Chris@17
|
61 }
|
Chris@17
|
62 if (!flock($h, LOCK_EX | LOCK_NB)) {
|
Chris@0
|
63 continue 2;
|
Chris@0
|
64 }
|
Chris@17
|
65 if (isset($this->lockHandles[$pipe])) {
|
Chris@17
|
66 flock($this->lockHandles[$pipe], LOCK_UN);
|
Chris@17
|
67 fclose($this->lockHandles[$pipe]);
|
Chris@0
|
68 }
|
Chris@17
|
69 $this->lockHandles[$pipe] = $h;
|
Chris@17
|
70
|
Chris@17
|
71 if (!fclose(fopen($file, 'w')) || !$h = fopen($file, 'r')) {
|
Chris@17
|
72 flock($this->lockHandles[$pipe], LOCK_UN);
|
Chris@17
|
73 fclose($this->lockHandles[$pipe]);
|
Chris@17
|
74 unset($this->lockHandles[$pipe]);
|
Chris@0
|
75 continue 2;
|
Chris@0
|
76 }
|
Chris@17
|
77 $this->fileHandles[$pipe] = $h;
|
Chris@0
|
78 $this->files[$pipe] = $file;
|
Chris@0
|
79 }
|
Chris@0
|
80 break;
|
Chris@0
|
81 }
|
Chris@0
|
82 restore_error_handler();
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 parent::__construct($input);
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 public function __destruct()
|
Chris@0
|
89 {
|
Chris@0
|
90 $this->close();
|
Chris@0
|
91 }
|
Chris@0
|
92
|
Chris@0
|
93 /**
|
Chris@0
|
94 * {@inheritdoc}
|
Chris@0
|
95 */
|
Chris@0
|
96 public function getDescriptors()
|
Chris@0
|
97 {
|
Chris@0
|
98 if (!$this->haveReadSupport) {
|
Chris@0
|
99 $nullstream = fopen('NUL', 'c');
|
Chris@0
|
100
|
Chris@17
|
101 return [
|
Chris@17
|
102 ['pipe', 'r'],
|
Chris@0
|
103 $nullstream,
|
Chris@0
|
104 $nullstream,
|
Chris@17
|
105 ];
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/bug.php?id=51800)
|
Chris@0
|
109 // We're not using file handles as it can produce corrupted output https://bugs.php.net/bug.php?id=65650
|
Chris@0
|
110 // So we redirect output within the commandline and pass the nul device to the process
|
Chris@17
|
111 return [
|
Chris@17
|
112 ['pipe', 'r'],
|
Chris@17
|
113 ['file', 'NUL', 'w'],
|
Chris@17
|
114 ['file', 'NUL', 'w'],
|
Chris@17
|
115 ];
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 /**
|
Chris@0
|
119 * {@inheritdoc}
|
Chris@0
|
120 */
|
Chris@0
|
121 public function getFiles()
|
Chris@0
|
122 {
|
Chris@0
|
123 return $this->files;
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * {@inheritdoc}
|
Chris@0
|
128 */
|
Chris@0
|
129 public function readAndWrite($blocking, $close = false)
|
Chris@0
|
130 {
|
Chris@0
|
131 $this->unblock();
|
Chris@0
|
132 $w = $this->write();
|
Chris@17
|
133 $read = $r = $e = [];
|
Chris@0
|
134
|
Chris@0
|
135 if ($blocking) {
|
Chris@0
|
136 if ($w) {
|
Chris@0
|
137 @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6);
|
Chris@0
|
138 } elseif ($this->fileHandles) {
|
Chris@0
|
139 usleep(Process::TIMEOUT_PRECISION * 1E6);
|
Chris@0
|
140 }
|
Chris@0
|
141 }
|
Chris@0
|
142 foreach ($this->fileHandles as $type => $fileHandle) {
|
Chris@0
|
143 $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]);
|
Chris@0
|
144
|
Chris@0
|
145 if (isset($data[0])) {
|
Chris@17
|
146 $this->readBytes[$type] += \strlen($data);
|
Chris@0
|
147 $read[$type] = $data;
|
Chris@0
|
148 }
|
Chris@0
|
149 if ($close) {
|
Chris@17
|
150 ftruncate($fileHandle, 0);
|
Chris@0
|
151 fclose($fileHandle);
|
Chris@17
|
152 flock($this->lockHandles[$type], LOCK_UN);
|
Chris@17
|
153 fclose($this->lockHandles[$type]);
|
Chris@17
|
154 unset($this->fileHandles[$type], $this->lockHandles[$type]);
|
Chris@0
|
155 }
|
Chris@0
|
156 }
|
Chris@0
|
157
|
Chris@0
|
158 return $read;
|
Chris@0
|
159 }
|
Chris@0
|
160
|
Chris@0
|
161 /**
|
Chris@0
|
162 * {@inheritdoc}
|
Chris@0
|
163 */
|
Chris@0
|
164 public function haveReadSupport()
|
Chris@0
|
165 {
|
Chris@0
|
166 return $this->haveReadSupport;
|
Chris@0
|
167 }
|
Chris@0
|
168
|
Chris@0
|
169 /**
|
Chris@0
|
170 * {@inheritdoc}
|
Chris@0
|
171 */
|
Chris@0
|
172 public function areOpen()
|
Chris@0
|
173 {
|
Chris@0
|
174 return $this->pipes && $this->fileHandles;
|
Chris@0
|
175 }
|
Chris@0
|
176
|
Chris@0
|
177 /**
|
Chris@0
|
178 * {@inheritdoc}
|
Chris@0
|
179 */
|
Chris@0
|
180 public function close()
|
Chris@0
|
181 {
|
Chris@0
|
182 parent::close();
|
Chris@17
|
183 foreach ($this->fileHandles as $type => $handle) {
|
Chris@17
|
184 ftruncate($handle, 0);
|
Chris@0
|
185 fclose($handle);
|
Chris@17
|
186 flock($this->lockHandles[$type], LOCK_UN);
|
Chris@17
|
187 fclose($this->lockHandles[$type]);
|
Chris@0
|
188 }
|
Chris@17
|
189 $this->fileHandles = $this->lockHandles = [];
|
Chris@0
|
190 }
|
Chris@0
|
191 }
|