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