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@0
|
14 use Symfony\Component\Process\Process;
|
Chris@0
|
15 use Symfony\Component\Process\Exception\RuntimeException;
|
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@0
|
29 private $files = array();
|
Chris@0
|
30 private $fileHandles = array();
|
Chris@0
|
31 private $readBytes = array(
|
Chris@0
|
32 Process::STDOUT => 0,
|
Chris@0
|
33 Process::STDERR => 0,
|
Chris@0
|
34 );
|
Chris@0
|
35 private $haveReadSupport;
|
Chris@0
|
36
|
Chris@0
|
37 public function __construct($input, $haveReadSupport)
|
Chris@0
|
38 {
|
Chris@0
|
39 $this->haveReadSupport = (bool) $haveReadSupport;
|
Chris@0
|
40
|
Chris@0
|
41 if ($this->haveReadSupport) {
|
Chris@0
|
42 // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big.
|
Chris@0
|
43 // Workaround for this problem is to use temporary files instead of pipes on Windows platform.
|
Chris@0
|
44 //
|
Chris@0
|
45 // @see https://bugs.php.net/bug.php?id=51800
|
Chris@0
|
46 $pipes = array(
|
Chris@0
|
47 Process::STDOUT => Process::OUT,
|
Chris@0
|
48 Process::STDERR => Process::ERR,
|
Chris@0
|
49 );
|
Chris@0
|
50 $tmpCheck = false;
|
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@0
|
57 if (file_exists($file) && !unlink($file)) {
|
Chris@0
|
58 continue 2;
|
Chris@0
|
59 }
|
Chris@0
|
60 $h = fopen($file, 'xb');
|
Chris@0
|
61 if (!$h) {
|
Chris@0
|
62 $error = $lastError;
|
Chris@0
|
63 if ($tmpCheck || $tmpCheck = unlink(tempnam(false, 'sf_check_'))) {
|
Chris@0
|
64 continue;
|
Chris@0
|
65 }
|
Chris@0
|
66 restore_error_handler();
|
Chris@0
|
67 throw new RuntimeException(sprintf('A temporary file could not be opened to write the process output: %s', $error));
|
Chris@0
|
68 }
|
Chris@0
|
69 if (!$h || !$this->fileHandles[$pipe] = fopen($file, 'rb')) {
|
Chris@0
|
70 continue 2;
|
Chris@0
|
71 }
|
Chris@0
|
72 if (isset($this->files[$pipe])) {
|
Chris@0
|
73 unlink($this->files[$pipe]);
|
Chris@0
|
74 }
|
Chris@0
|
75 $this->files[$pipe] = $file;
|
Chris@0
|
76 }
|
Chris@0
|
77 break;
|
Chris@0
|
78 }
|
Chris@0
|
79 restore_error_handler();
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 parent::__construct($input);
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 public function __destruct()
|
Chris@0
|
86 {
|
Chris@0
|
87 $this->close();
|
Chris@0
|
88 $this->removeFiles();
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * {@inheritdoc}
|
Chris@0
|
93 */
|
Chris@0
|
94 public function getDescriptors()
|
Chris@0
|
95 {
|
Chris@0
|
96 if (!$this->haveReadSupport) {
|
Chris@0
|
97 $nullstream = fopen('NUL', 'c');
|
Chris@0
|
98
|
Chris@0
|
99 return array(
|
Chris@0
|
100 array('pipe', 'r'),
|
Chris@0
|
101 $nullstream,
|
Chris@0
|
102 $nullstream,
|
Chris@0
|
103 );
|
Chris@0
|
104 }
|
Chris@0
|
105
|
Chris@0
|
106 // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/bug.php?id=51800)
|
Chris@0
|
107 // We're not using file handles as it can produce corrupted output https://bugs.php.net/bug.php?id=65650
|
Chris@0
|
108 // So we redirect output within the commandline and pass the nul device to the process
|
Chris@0
|
109 return array(
|
Chris@0
|
110 array('pipe', 'r'),
|
Chris@0
|
111 array('file', 'NUL', 'w'),
|
Chris@0
|
112 array('file', 'NUL', 'w'),
|
Chris@0
|
113 );
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * {@inheritdoc}
|
Chris@0
|
118 */
|
Chris@0
|
119 public function getFiles()
|
Chris@0
|
120 {
|
Chris@0
|
121 return $this->files;
|
Chris@0
|
122 }
|
Chris@0
|
123
|
Chris@0
|
124 /**
|
Chris@0
|
125 * {@inheritdoc}
|
Chris@0
|
126 */
|
Chris@0
|
127 public function readAndWrite($blocking, $close = false)
|
Chris@0
|
128 {
|
Chris@0
|
129 $this->unblock();
|
Chris@0
|
130 $w = $this->write();
|
Chris@0
|
131 $read = $r = $e = array();
|
Chris@0
|
132
|
Chris@0
|
133 if ($blocking) {
|
Chris@0
|
134 if ($w) {
|
Chris@0
|
135 @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6);
|
Chris@0
|
136 } elseif ($this->fileHandles) {
|
Chris@0
|
137 usleep(Process::TIMEOUT_PRECISION * 1E6);
|
Chris@0
|
138 }
|
Chris@0
|
139 }
|
Chris@0
|
140 foreach ($this->fileHandles as $type => $fileHandle) {
|
Chris@0
|
141 $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]);
|
Chris@0
|
142
|
Chris@0
|
143 if (isset($data[0])) {
|
Chris@0
|
144 $this->readBytes[$type] += strlen($data);
|
Chris@0
|
145 $read[$type] = $data;
|
Chris@0
|
146 }
|
Chris@0
|
147 if ($close) {
|
Chris@0
|
148 fclose($fileHandle);
|
Chris@0
|
149 unset($this->fileHandles[$type]);
|
Chris@0
|
150 }
|
Chris@0
|
151 }
|
Chris@0
|
152
|
Chris@0
|
153 return $read;
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /**
|
Chris@0
|
157 * {@inheritdoc}
|
Chris@0
|
158 */
|
Chris@0
|
159 public function haveReadSupport()
|
Chris@0
|
160 {
|
Chris@0
|
161 return $this->haveReadSupport;
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 /**
|
Chris@0
|
165 * {@inheritdoc}
|
Chris@0
|
166 */
|
Chris@0
|
167 public function areOpen()
|
Chris@0
|
168 {
|
Chris@0
|
169 return $this->pipes && $this->fileHandles;
|
Chris@0
|
170 }
|
Chris@0
|
171
|
Chris@0
|
172 /**
|
Chris@0
|
173 * {@inheritdoc}
|
Chris@0
|
174 */
|
Chris@0
|
175 public function close()
|
Chris@0
|
176 {
|
Chris@0
|
177 parent::close();
|
Chris@0
|
178 foreach ($this->fileHandles as $handle) {
|
Chris@0
|
179 fclose($handle);
|
Chris@0
|
180 }
|
Chris@0
|
181 $this->fileHandles = array();
|
Chris@0
|
182 }
|
Chris@0
|
183
|
Chris@0
|
184 /**
|
Chris@0
|
185 * Removes temporary files.
|
Chris@0
|
186 */
|
Chris@0
|
187 private function removeFiles()
|
Chris@0
|
188 {
|
Chris@0
|
189 foreach ($this->files as $filename) {
|
Chris@0
|
190 if (file_exists($filename)) {
|
Chris@0
|
191 @unlink($filename);
|
Chris@0
|
192 }
|
Chris@0
|
193 }
|
Chris@0
|
194 $this->files = array();
|
Chris@0
|
195 }
|
Chris@0
|
196 }
|