added knp-snappy package for Jerry's UB04 work
[openemr.git] / vendor / symfony / process / Pipes / WindowsPipes.php
bloba1e3115519f35fe1ffc4f136e207f15e2304987f
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 /** @var array */
30 private $files = array();
31 /** @var array */
32 private $fileHandles = array();
33 /** @var array */
34 private $readBytes = array(
35 Process::STDOUT => 0,
36 Process::STDERR => 0,
38 /** @var bool */
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
50 $pipes = array(
51 Process::STDOUT => Process::OUT,
52 Process::STDERR => Process::ERR,
54 $tmpCheck = false;
55 $tmpDir = sys_get_temp_dir();
56 $lastError = 'unknown reason';
57 set_error_handler(function ($type, $msg) use (&$lastError) { $lastError = $msg; });
58 for ($i = 0;; ++$i) {
59 foreach ($pipes as $pipe => $name) {
60 $file = sprintf('%s\\sf_proc_%02X.%s', $tmpDir, $i, $name);
61 if (file_exists($file) && !unlink($file)) {
62 continue 2;
64 $h = fopen($file, 'xb');
65 if (!$h) {
66 $error = $lastError;
67 if ($tmpCheck || $tmpCheck = unlink(tempnam(false, 'sf_check_'))) {
68 continue;
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')) {
74 continue 2;
76 if (isset($this->files[$pipe])) {
77 unlink($this->files[$pipe]);
79 $this->files[$pipe] = $file;
81 break;
83 restore_error_handler();
86 parent::__construct($input);
89 public function __destruct()
91 $this->close();
92 $this->removeFiles();
95 /**
96 * {@inheritdoc}
98 public function getDescriptors()
100 if (!$this->haveReadSupport) {
101 $nullstream = fopen('NUL', 'c');
103 return array(
104 array('pipe', 'r'),
105 $nullstream,
106 $nullstream,
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
113 return array(
114 array('pipe', 'r'),
115 array('file', 'NUL', 'w'),
116 array('file', 'NUL', 'w'),
121 * {@inheritdoc}
123 public function getFiles()
125 return $this->files;
129 * {@inheritdoc}
131 public function readAndWrite($blocking, $close = false)
133 $this->unblock();
134 $w = $this->write();
135 $read = $r = $e = array();
137 if ($blocking) {
138 if ($w) {
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;
151 if ($close) {
152 fclose($fileHandle);
153 unset($this->fileHandles[$type]);
157 return $read;
161 * {@inheritdoc}
163 public function haveReadSupport()
165 return $this->haveReadSupport;
169 * {@inheritdoc}
171 public function areOpen()
173 return $this->pipes && $this->fileHandles;
177 * {@inheritdoc}
179 public function close()
181 parent::close();
182 foreach ($this->fileHandles as $handle) {
183 fclose($handle);
185 $this->fileHandles = array();
189 * Removes temporary files.
191 private function removeFiles()
193 foreach ($this->files as $filename) {
194 if (file_exists($filename)) {
195 @unlink($filename);
198 $this->files = array();