Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Process\Pipes;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\Process\Process;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * UnixPipes implementation uses unix pipes as handles.
|
Chris@0
|
18 *
|
Chris@0
|
19 * @author Romain Neutron <imprec@gmail.com>
|
Chris@0
|
20 *
|
Chris@0
|
21 * @internal
|
Chris@0
|
22 */
|
Chris@0
|
23 class UnixPipes extends AbstractPipes
|
Chris@0
|
24 {
|
Chris@0
|
25 private $ttyMode;
|
Chris@0
|
26 private $ptyMode;
|
Chris@0
|
27 private $haveReadSupport;
|
Chris@0
|
28
|
Chris@0
|
29 public function __construct($ttyMode, $ptyMode, $input, $haveReadSupport)
|
Chris@0
|
30 {
|
Chris@0
|
31 $this->ttyMode = (bool) $ttyMode;
|
Chris@0
|
32 $this->ptyMode = (bool) $ptyMode;
|
Chris@0
|
33 $this->haveReadSupport = (bool) $haveReadSupport;
|
Chris@0
|
34
|
Chris@0
|
35 parent::__construct($input);
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 public function __destruct()
|
Chris@0
|
39 {
|
Chris@0
|
40 $this->close();
|
Chris@0
|
41 }
|
Chris@0
|
42
|
Chris@0
|
43 /**
|
Chris@0
|
44 * {@inheritdoc}
|
Chris@0
|
45 */
|
Chris@0
|
46 public function getDescriptors()
|
Chris@0
|
47 {
|
Chris@0
|
48 if (!$this->haveReadSupport) {
|
Chris@0
|
49 $nullstream = fopen('/dev/null', 'c');
|
Chris@0
|
50
|
Chris@17
|
51 return [
|
Chris@17
|
52 ['pipe', 'r'],
|
Chris@0
|
53 $nullstream,
|
Chris@0
|
54 $nullstream,
|
Chris@17
|
55 ];
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 if ($this->ttyMode) {
|
Chris@17
|
59 return [
|
Chris@17
|
60 ['file', '/dev/tty', 'r'],
|
Chris@17
|
61 ['file', '/dev/tty', 'w'],
|
Chris@17
|
62 ['file', '/dev/tty', 'w'],
|
Chris@17
|
63 ];
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 if ($this->ptyMode && Process::isPtySupported()) {
|
Chris@17
|
67 return [
|
Chris@17
|
68 ['pty'],
|
Chris@17
|
69 ['pty'],
|
Chris@17
|
70 ['pty'],
|
Chris@17
|
71 ];
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@17
|
74 return [
|
Chris@17
|
75 ['pipe', 'r'],
|
Chris@17
|
76 ['pipe', 'w'], // stdout
|
Chris@17
|
77 ['pipe', 'w'], // stderr
|
Chris@17
|
78 ];
|
Chris@0
|
79 }
|
Chris@0
|
80
|
Chris@0
|
81 /**
|
Chris@0
|
82 * {@inheritdoc}
|
Chris@0
|
83 */
|
Chris@0
|
84 public function getFiles()
|
Chris@0
|
85 {
|
Chris@17
|
86 return [];
|
Chris@0
|
87 }
|
Chris@0
|
88
|
Chris@0
|
89 /**
|
Chris@0
|
90 * {@inheritdoc}
|
Chris@0
|
91 */
|
Chris@0
|
92 public function readAndWrite($blocking, $close = false)
|
Chris@0
|
93 {
|
Chris@0
|
94 $this->unblock();
|
Chris@0
|
95 $w = $this->write();
|
Chris@0
|
96
|
Chris@17
|
97 $read = $e = [];
|
Chris@0
|
98 $r = $this->pipes;
|
Chris@0
|
99 unset($r[0]);
|
Chris@0
|
100
|
Chris@0
|
101 // let's have a look if something changed in streams
|
Chris@17
|
102 set_error_handler([$this, 'handleError']);
|
Chris@16
|
103 if (($r || $w) && false === stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
|
Chris@16
|
104 restore_error_handler();
|
Chris@0
|
105 // if a system call has been interrupted, forget about it, let's try again
|
Chris@0
|
106 // otherwise, an error occurred, let's reset pipes
|
Chris@0
|
107 if (!$this->hasSystemCallBeenInterrupted()) {
|
Chris@17
|
108 $this->pipes = [];
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 return $read;
|
Chris@0
|
112 }
|
Chris@16
|
113 restore_error_handler();
|
Chris@0
|
114
|
Chris@0
|
115 foreach ($r as $pipe) {
|
Chris@0
|
116 // prior PHP 5.4 the array passed to stream_select is modified and
|
Chris@0
|
117 // lose key association, we have to find back the key
|
Chris@0
|
118 $read[$type = array_search($pipe, $this->pipes, true)] = '';
|
Chris@0
|
119
|
Chris@0
|
120 do {
|
Chris@0
|
121 $data = fread($pipe, self::CHUNK_SIZE);
|
Chris@0
|
122 $read[$type] .= $data;
|
Chris@0
|
123 } while (isset($data[0]) && ($close || isset($data[self::CHUNK_SIZE - 1])));
|
Chris@0
|
124
|
Chris@0
|
125 if (!isset($read[$type][0])) {
|
Chris@0
|
126 unset($read[$type]);
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 if ($close && feof($pipe)) {
|
Chris@0
|
130 fclose($pipe);
|
Chris@0
|
131 unset($this->pipes[$type]);
|
Chris@0
|
132 }
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 return $read;
|
Chris@0
|
136 }
|
Chris@0
|
137
|
Chris@0
|
138 /**
|
Chris@0
|
139 * {@inheritdoc}
|
Chris@0
|
140 */
|
Chris@0
|
141 public function haveReadSupport()
|
Chris@0
|
142 {
|
Chris@0
|
143 return $this->haveReadSupport;
|
Chris@0
|
144 }
|
Chris@0
|
145
|
Chris@0
|
146 /**
|
Chris@0
|
147 * {@inheritdoc}
|
Chris@0
|
148 */
|
Chris@0
|
149 public function areOpen()
|
Chris@0
|
150 {
|
Chris@0
|
151 return (bool) $this->pipes;
|
Chris@0
|
152 }
|
Chris@0
|
153 }
|