annotate vendor/symfony/process/Pipes/UnixPipes.php @ 2:92f882872392

Trusted hosts, + remove migration modules
author Chris Cannam
date Tue, 05 Dec 2017 09:26:43 +0000
parents 4c8ae668cc8c
children 1fec387a4317
rev   line source
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 /** @var bool */
Chris@0 26 private $ttyMode;
Chris@0 27 /** @var bool */
Chris@0 28 private $ptyMode;
Chris@0 29 /** @var bool */
Chris@0 30 private $haveReadSupport;
Chris@0 31
Chris@0 32 public function __construct($ttyMode, $ptyMode, $input, $haveReadSupport)
Chris@0 33 {
Chris@0 34 $this->ttyMode = (bool) $ttyMode;
Chris@0 35 $this->ptyMode = (bool) $ptyMode;
Chris@0 36 $this->haveReadSupport = (bool) $haveReadSupport;
Chris@0 37
Chris@0 38 parent::__construct($input);
Chris@0 39 }
Chris@0 40
Chris@0 41 public function __destruct()
Chris@0 42 {
Chris@0 43 $this->close();
Chris@0 44 }
Chris@0 45
Chris@0 46 /**
Chris@0 47 * {@inheritdoc}
Chris@0 48 */
Chris@0 49 public function getDescriptors()
Chris@0 50 {
Chris@0 51 if (!$this->haveReadSupport) {
Chris@0 52 $nullstream = fopen('/dev/null', 'c');
Chris@0 53
Chris@0 54 return array(
Chris@0 55 array('pipe', 'r'),
Chris@0 56 $nullstream,
Chris@0 57 $nullstream,
Chris@0 58 );
Chris@0 59 }
Chris@0 60
Chris@0 61 if ($this->ttyMode) {
Chris@0 62 return array(
Chris@0 63 array('file', '/dev/tty', 'r'),
Chris@0 64 array('file', '/dev/tty', 'w'),
Chris@0 65 array('file', '/dev/tty', 'w'),
Chris@0 66 );
Chris@0 67 }
Chris@0 68
Chris@0 69 if ($this->ptyMode && Process::isPtySupported()) {
Chris@0 70 return array(
Chris@0 71 array('pty'),
Chris@0 72 array('pty'),
Chris@0 73 array('pty'),
Chris@0 74 );
Chris@0 75 }
Chris@0 76
Chris@0 77 return array(
Chris@0 78 array('pipe', 'r'),
Chris@0 79 array('pipe', 'w'), // stdout
Chris@0 80 array('pipe', 'w'), // stderr
Chris@0 81 );
Chris@0 82 }
Chris@0 83
Chris@0 84 /**
Chris@0 85 * {@inheritdoc}
Chris@0 86 */
Chris@0 87 public function getFiles()
Chris@0 88 {
Chris@0 89 return array();
Chris@0 90 }
Chris@0 91
Chris@0 92 /**
Chris@0 93 * {@inheritdoc}
Chris@0 94 */
Chris@0 95 public function readAndWrite($blocking, $close = false)
Chris@0 96 {
Chris@0 97 $this->unblock();
Chris@0 98 $w = $this->write();
Chris@0 99
Chris@0 100 $read = $e = array();
Chris@0 101 $r = $this->pipes;
Chris@0 102 unset($r[0]);
Chris@0 103
Chris@0 104 // let's have a look if something changed in streams
Chris@0 105 if (($r || $w) && false === $n = @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
Chris@0 106 // if a system call has been interrupted, forget about it, let's try again
Chris@0 107 // otherwise, an error occurred, let's reset pipes
Chris@0 108 if (!$this->hasSystemCallBeenInterrupted()) {
Chris@0 109 $this->pipes = array();
Chris@0 110 }
Chris@0 111
Chris@0 112 return $read;
Chris@0 113 }
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 }