4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Process\Pipes;
14 use Symfony\Component\Process\Exception\RuntimeException;
15 use Symfony\Component\Process\Process;
18 * WindowsPipes implementation uses temporary files as handles.
20 * @see https://bugs.php.net/bug.php?id=51800
21 * @see https://bugs.php.net/bug.php?id=65650
23 * @author Romain Neutron <imprec@gmail.com>
27 class WindowsPipes extends AbstractPipes
29 private $files = array();
30 private $fileHandles = array();
31 private $lockHandles = array();
32 private $readBytes = array(
36 private $haveReadSupport;
38 public function __construct($input, $haveReadSupport)
40 $this->haveReadSupport = (bool) $haveReadSupport;
42 if ($this->haveReadSupport) {
43 // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big.
44 // Workaround for this problem is to use temporary files instead of pipes on Windows platform.
46 // @see https://bugs.php.net/bug.php?id=51800
48 Process::STDOUT => Process::OUT,
49 Process::STDERR => Process::ERR,
51 $tmpDir = sys_get_temp_dir();
52 $lastError = 'unknown reason';
53 set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; });
55 foreach ($pipes as $pipe => $name) {
56 $file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name);
58 if (!$h = fopen($file.'.lock', 'w')) {
59 restore_error_handler();
60 throw new RuntimeException(sprintf('A temporary file could not be opened to write the process output: %s', $lastError));
62 if (!flock($h, LOCK_EX | LOCK_NB)) {
65 if (isset($this->lockHandles[$pipe])) {
66 flock($this->lockHandles[$pipe], LOCK_UN);
67 fclose($this->lockHandles[$pipe]);
69 $this->lockHandles[$pipe] = $h;
71 if (!fclose(fopen($file, 'w')) || !$h = fopen($file, 'r')) {
72 flock($this->lockHandles[$pipe], LOCK_UN);
73 fclose($this->lockHandles[$pipe]);
74 unset($this->lockHandles[$pipe]);
77 $this->fileHandles[$pipe] = $h;
78 $this->files[$pipe] = $file;
82 restore_error_handler();
85 parent::__construct($input);
88 public function __destruct()
96 public function getDescriptors()
98 if (!$this->haveReadSupport) {
99 $nullstream = fopen('NUL', 'c');
108 // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/bug.php?id=51800)
109 // We're not using file handles as it can produce corrupted output https://bugs.php.net/bug.php?id=65650
110 // So we redirect output within the commandline and pass the nul device to the process
113 array('file', 'NUL', 'w'),
114 array('file', 'NUL', 'w'),
121 public function getFiles()
129 public function readAndWrite($blocking, $close = false)
133 $read = $r = $e = array();
137 @stream_select($r, $w, $e, 0, Process::TIMEOUT_PRECISION * 1E6);
138 } elseif ($this->fileHandles) {
139 usleep(Process::TIMEOUT_PRECISION * 1E6);
142 foreach ($this->fileHandles as $type => $fileHandle) {
143 $data = stream_get_contents($fileHandle, -1, $this->readBytes[$type]);
145 if (isset($data[0])) {
146 $this->readBytes[$type] += \strlen($data);
147 $read[$type] = $data;
150 ftruncate($fileHandle, 0);
152 flock($this->lockHandles[$type], LOCK_UN);
153 fclose($this->lockHandles[$type]);
154 unset($this->fileHandles[$type], $this->lockHandles[$type]);
164 public function haveReadSupport()
166 return $this->haveReadSupport;
172 public function areOpen()
174 return $this->pipes && $this->fileHandles;
180 public function close()
183 foreach ($this->fileHandles as $type => $handle) {
184 ftruncate($handle, 0);
186 flock($this->lockHandles[$type], LOCK_UN);
187 fclose($this->lockHandles[$type]);
189 $this->fileHandles = $this->lockHandles = array();