composer package updates
[openemr.git] / vendor / symfony / process / Pipes / WindowsPipes.php
blobd5fa2fdeef67c9ac99536547c1156c411c8a388a
1 <?php
3 /*
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;
17 /**
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>
25 * @internal
27 class WindowsPipes extends AbstractPipes
29 private $files = array();
30 private $fileHandles = array();
31 private $readBytes = array(
32 Process::STDOUT => 0,
33 Process::STDERR => 0,
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
46 $pipes = array(
47 Process::STDOUT => Process::OUT,
48 Process::STDERR => Process::ERR,
50 $tmpCheck = false;
51 $tmpDir = sys_get_temp_dir();
52 $lastError = 'unknown reason';
53 set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; });
54 for ($i = 0;; ++$i) {
55 foreach ($pipes as $pipe => $name) {
56 $file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name);
57 if (file_exists($file) && !unlink($file)) {
58 continue 2;
60 $h = fopen($file, 'xb');
61 if (!$h) {
62 $error = $lastError;
63 if ($tmpCheck || $tmpCheck = unlink(tempnam(false, 'sf_check_'))) {
64 continue;
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')) {
70 continue 2;
72 if (isset($this->files[$pipe])) {
73 unlink($this->files[$pipe]);
75 $this->files[$pipe] = $file;
77 break;
79 restore_error_handler();
82 parent::__construct($input);
85 public function __destruct()
87 $this->close();
88 $this->removeFiles();
91 /**
92 * {@inheritdoc}
94 public function getDescriptors()
96 if (!$this->haveReadSupport) {
97 $nullstream = fopen('NUL', 'c');
99 return array(
100 array('pipe', 'r'),
101 $nullstream,
102 $nullstream,
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
109 return array(
110 array('pipe', 'r'),
111 array('file', 'NUL', 'w'),
112 array('file', 'NUL', 'w'),
117 * {@inheritdoc}
119 public function getFiles()
121 return $this->files;
125 * {@inheritdoc}
127 public function readAndWrite($blocking, $close = false)
129 $this->unblock();
130 $w = $this->write();
131 $read = $r = $e = array();
133 if ($blocking) {
134 if ($w) {
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;
147 if ($close) {
148 fclose($fileHandle);
149 unset($this->fileHandles[$type]);
153 return $read;
157 * {@inheritdoc}
159 public function haveReadSupport()
161 return $this->haveReadSupport;
165 * {@inheritdoc}
167 public function areOpen()
169 return $this->pipes && $this->fileHandles;
173 * {@inheritdoc}
175 public function close()
177 parent::close();
178 foreach ($this->fileHandles as $handle) {
179 fclose($handle);
181 $this->fileHandles = array();
185 * Removes temporary files.
187 private function removeFiles()
189 foreach ($this->files as $filename) {
190 if (file_exists($filename)) {
191 @unlink($filename);
194 $this->files = array();