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\Process
;
15 use Symfony\Component\Process\Exception\RuntimeException
;
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
30 private $files = array();
32 private $fileHandles = array();
34 private $readBytes = array(
39 private $haveReadSupport;
41 public function __construct($input, $haveReadSupport)
43 $this->haveReadSupport
= (bool) $haveReadSupport;
45 if ($this->haveReadSupport
) {
46 // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big.
47 // Workaround for this problem is to use temporary files instead of pipes on Windows platform.
49 // @see https://bugs.php.net/bug.php?id=51800
51 Process
::STDOUT
=> Process
::OUT
,
52 Process
::STDERR
=> Process
::ERR
,
55 $tmpDir = sys_get_temp_dir();
56 $lastError = 'unknown reason';
57 set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; });
59 foreach ($pipes as $pipe => $name) {
60 $file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name);
61 if (file_exists($file) && !unlink($file)) {
64 $h = fopen($file, 'xb');
67 if ($tmpCheck ||
$tmpCheck = unlink(tempnam(false, 'sf_check_'))) {
70 restore_error_handler();
71 throw new RuntimeException(sprintf('A temporary file could not be opened to write the process output: %s', $error));
73 if (!$h ||
!$this->fileHandles
[$pipe] = fopen($file, 'rb')) {
76 if (isset($this->files
[$pipe])) {
77 unlink($this->files
[$pipe]);
79 $this->files
[$pipe] = $file;
83 restore_error_handler();
86 parent
::__construct($input);
89 public function __destruct()
98 public function getDescriptors()
100 if (!$this->haveReadSupport
) {
101 $nullstream = fopen('NUL', 'c');
110 // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/bug.php?id=51800)
111 // We're not using file handles as it can produce corrupted output https://bugs.php.net/bug.php?id=65650
112 // So we redirect output within the commandline and pass the nul device to the process
115 array('file', 'NUL', 'w'),
116 array('file', 'NUL', 'w'),
123 public function getFiles()
131 public function readAndWrite($blocking, $close = false)
135 $read = $r = $e = array();
139 @stream_select
($r, $w, $e, 0, Process
::TIMEOUT_PRECISION
* 1E6
);
140 } elseif ($this->fileHandles
) {
141 usleep(Process
::TIMEOUT_PRECISION
* 1E6
);
144 foreach ($this->fileHandles
as $type => $fileHandle) {
145 $data = stream_get_contents($fileHandle, -1, $this->readBytes
[$type]);
147 if (isset($data[0])) {
148 $this->readBytes
[$type] +
= strlen($data);
149 $read[$type] = $data;
153 unset($this->fileHandles
[$type]);
163 public function haveReadSupport()
165 return $this->haveReadSupport
;
171 public function areOpen()
173 return $this->pipes
&& $this->fileHandles
;
179 public function close()
182 foreach ($this->fileHandles
as $handle) {
185 $this->fileHandles
= array();
189 * Removes temporary files.
191 private function removeFiles()
193 foreach ($this->files
as $filename) {
194 if (file_exists($filename)) {
198 $this->files
= array();