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
29 private $files = array();
30 private $fileHandles = array();
31 private $readBytes = array(
35 private $haveReadSupport;
37 public function __construct($input, $haveReadSupport)
39 $this->haveReadSupport
= (bool) $haveReadSupport;
41 if ($this->haveReadSupport
) {
42 // Fix for PHP bug #51800: reading from STDOUT pipe hangs forever on Windows if the output is too big.
43 // Workaround for this problem is to use temporary files instead of pipes on Windows platform.
45 // @see https://bugs.php.net/bug.php?id=51800
47 Process
::STDOUT
=> Process
::OUT
,
48 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);
57 if (file_exists($file) && !unlink($file)) {
60 $h = fopen($file, 'xb');
63 if ($tmpCheck ||
$tmpCheck = unlink(tempnam(false, 'sf_check_'))) {
66 restore_error_handler();
67 throw new RuntimeException(sprintf('A temporary file could not be opened to write the process output: %s', $error));
69 if (!$h ||
!$this->fileHandles
[$pipe] = fopen($file, 'rb')) {
72 if (isset($this->files
[$pipe])) {
73 unlink($this->files
[$pipe]);
75 $this->files
[$pipe] = $file;
79 restore_error_handler();
82 parent
::__construct($input);
85 public function __destruct()
94 public function getDescriptors()
96 if (!$this->haveReadSupport
) {
97 $nullstream = fopen('NUL', 'c');
106 // We're not using pipe on Windows platform as it hangs (https://bugs.php.net/bug.php?id=51800)
107 // We're not using file handles as it can produce corrupted output https://bugs.php.net/bug.php?id=65650
108 // So we redirect output within the commandline and pass the nul device to the process
111 array('file', 'NUL', 'w'),
112 array('file', 'NUL', 'w'),
119 public function getFiles()
127 public function readAndWrite($blocking, $close = false)
131 $read = $r = $e = array();
135 @stream_select
($r, $w, $e, 0, Process
::TIMEOUT_PRECISION
* 1E6
);
136 } elseif ($this->fileHandles
) {
137 usleep(Process
::TIMEOUT_PRECISION
* 1E6
);
140 foreach ($this->fileHandles
as $type => $fileHandle) {
141 $data = stream_get_contents($fileHandle, -1, $this->readBytes
[$type]);
143 if (isset($data[0])) {
144 $this->readBytes
[$type] +
= strlen($data);
145 $read[$type] = $data;
149 unset($this->fileHandles
[$type]);
159 public function haveReadSupport()
161 return $this->haveReadSupport
;
167 public function areOpen()
169 return $this->pipes
&& $this->fileHandles
;
175 public function close()
178 foreach ($this->fileHandles
as $handle) {
181 $this->fileHandles
= array();
185 * Removes temporary files.
187 private function removeFiles()
189 foreach ($this->files
as $filename) {
190 if (file_exists($filename)) {
194 $this->files
= array();