| Total Complexity | 38 |
| Total Lines | 179 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
| 1 | <?php |
||
| 16 | class Unix extends Pipes |
||
|
1 ignored issue
–
show
|
|||
| 17 | { |
||
| 18 | |||
| 19 | /** @var bool */ |
||
| 20 | private $ttyMode; |
||
| 21 | /** @var bool */ |
||
| 22 | private $ptyMode; |
||
| 23 | /** @var bool */ |
||
| 24 | private $disableOutput; |
||
| 25 | |||
| 26 | public function __construct($ttyMode, $ptyMode, $input, $disableOutput) |
||
| 27 | { |
||
| 28 | $this->ttyMode = (bool) $ttyMode; |
||
| 29 | $this->ptyMode = (bool) $ptyMode; |
||
| 30 | $this->disableOutput = (bool) $disableOutput; |
||
| 31 | |||
| 32 | if (is_resource($input)) { |
||
| 33 | $this->input = $input; |
||
| 34 | } else { |
||
| 35 | $this->inputBuffer = (string) $input; |
||
| 36 | } |
||
| 37 | } |
||
| 38 | |||
| 39 | public function __destruct() |
||
| 40 | { |
||
| 41 | $this->close(); |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | public function getDescriptors() |
||
| 48 | { |
||
| 49 | if ($this->disableOutput) { |
||
| 50 | $nullstream = fopen('/dev/null', 'c'); |
||
| 51 | |||
| 52 | return [ |
||
| 53 | ['pipe', 'r'], |
||
| 54 | $nullstream, |
||
| 55 | $nullstream, |
||
| 56 | ]; |
||
| 57 | } |
||
| 58 | |||
| 59 | if ($this->ttyMode) { |
||
| 60 | return [ |
||
| 61 | ['file', '/dev/tty', 'r'], |
||
| 62 | ['file', '/dev/tty', 'w'], |
||
| 63 | ['file', '/dev/tty', 'w'], |
||
| 64 | ]; |
||
| 65 | } |
||
| 66 | |||
| 67 | if ($this->ptyMode && Process::isPtySupported()) { |
||
| 68 | return [ |
||
| 69 | ['pty'], |
||
| 70 | ['pty'], |
||
| 71 | ['pty'], |
||
| 72 | ]; |
||
| 73 | } |
||
| 74 | |||
| 75 | return [ |
||
| 76 | ['pipe', 'r'], |
||
| 77 | ['pipe', 'w'], // stdout |
||
| 78 | ['pipe', 'w'], // stderr |
||
| 79 | ]; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * {@inheritdoc} |
||
| 84 | */ |
||
| 85 | public function getFiles() |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * {@inheritdoc} |
||
| 92 | */ |
||
| 93 | public function readAndWrite($blocking, $close = false) |
||
| 94 | { |
||
| 95 | |||
| 96 | if (1 === count($this->pipes) && [0] === array_keys($this->pipes)) { |
||
| 97 | fclose($this->pipes[0]); |
||
| 98 | unset($this->pipes[0]); |
||
| 99 | } |
||
| 100 | |||
| 101 | if (empty($this->pipes)) { |
||
| 102 | return []; |
||
| 103 | } |
||
| 104 | |||
| 105 | $this->unblock(); |
||
| 106 | |||
| 107 | $read = []; |
||
| 108 | |||
| 109 | if (null !== $this->input) { |
||
| 110 | $r = array_merge($this->pipes, ['input' => $this->input]); |
||
| 111 | } else { |
||
| 112 | $r = $this->pipes; |
||
| 113 | } |
||
| 114 | |||
| 115 | unset($r[0]); |
||
| 116 | |||
| 117 | $w = isset($this->pipes[0]) ? [$this->pipes[0]] : null; |
||
| 118 | $e = null; |
||
| 119 | |||
| 120 | if (false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) { |
||
| 121 | |||
| 122 | if (!$this->hasSystemCallBeenInterrupted()) { |
||
| 123 | $this->pipes = []; |
||
| 124 | } |
||
| 125 | |||
| 126 | return $read; |
||
| 127 | } |
||
| 128 | |||
| 129 | if (0 === $n) { |
||
| 130 | return $read; |
||
| 131 | } |
||
| 132 | |||
| 133 | foreach ($r as $pipe) { |
||
| 134 | |||
| 135 | $type = (false !== $found = array_search($pipe, $this->pipes)) ? $found : 'input'; |
||
| 136 | $data = ''; |
||
| 137 | while ('' !== $dataread = (string) fread($pipe, self::CHUNK_SIZE)) { |
||
| 138 | $data .= $dataread; |
||
| 139 | } |
||
| 140 | |||
| 141 | if ('' !== $data) { |
||
| 142 | if ('input' === $type) { |
||
| 143 | $this->inputBuffer .= $data; |
||
| 144 | } else { |
||
| 145 | $read[$type] = $data; |
||
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | if (false === $data || (true === $close && feof($pipe) && '' === $data)) { |
||
| 150 | if ('input' === $type) { |
||
| 151 | $this->input = null; |
||
| 152 | } else { |
||
| 153 | fclose($this->pipes[$type]); |
||
| 154 | unset($this->pipes[$type]); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | |||
| 159 | if (null !== $w && 0 < count($w)) { |
||
| 160 | while (strlen($this->inputBuffer)) { |
||
| 161 | $written = fwrite($w[0], $this->inputBuffer, 2 << 18); // write 512k |
||
| 162 | if ($written > 0) { |
||
| 163 | $this->inputBuffer = (string) substr($this->inputBuffer, $written); |
||
| 164 | } else { |
||
| 165 | break; |
||
| 166 | } |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | if ('' === $this->inputBuffer && null === $this->input && isset($this->pipes[0])) { |
||
| 171 | fclose($this->pipes[0]); |
||
| 172 | unset($this->pipes[0]); |
||
| 173 | } |
||
| 174 | |||
| 175 | return $read; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | public function areOpen() |
||
| 182 | { |
||
| 183 | return (bool) $this->pipes; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * 创建一个新的 UnixPipes 实例 |
||
| 188 | * @param Process $process |
||
|
1 ignored issue
–
show
|
|||
| 189 | * @param string|resource $input |
||
|
1 ignored issue
–
show
|
|||
| 190 | * @return self |
||
| 191 | */ |
||
| 192 | public static function create(Process $process, $input) |
||
| 195 | } |
||
| 196 | } |
||
| 197 |