annotate vendor/symfony/process/Pipes/UnixPipes.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children c2387f117808
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 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@0 51 return array(
Chris@0 52 array('pipe', 'r'),
Chris@0 53 $nullstream,
Chris@0 54 $nullstream,
Chris@0 55 );
Chris@0 56 }
Chris@0 57
Chris@0 58 if ($this->ttyMode) {
Chris@0 59 return array(
Chris@0 60 array('file', '/dev/tty', 'r'),
Chris@0 61 array('file', '/dev/tty', 'w'),
Chris@0 62 array('file', '/dev/tty', 'w'),
Chris@0 63 );
Chris@0 64 }
Chris@0 65
Chris@0 66 if ($this->ptyMode && Process::isPtySupported()) {
Chris@0 67 return array(
Chris@0 68 array('pty'),
Chris@0 69 array('pty'),
Chris@0 70 array('pty'),
Chris@0 71 );
Chris@0 72 }
Chris@0 73
Chris@0 74 return array(
Chris@0 75 array('pipe', 'r'),
Chris@0 76 array('pipe', 'w'), // stdout
Chris@0 77 array('pipe', 'w'), // stderr
Chris@0 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@0 86 return array();
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@0 97 $read = $e = array();
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@14 102 if (($r || $w) && false === @stream_select($r, $w, $e, 0, $blocking ? Process::TIMEOUT_PRECISION * 1E6 : 0)) {
Chris@0 103 // if a system call has been interrupted, forget about it, let's try again
Chris@0 104 // otherwise, an error occurred, let's reset pipes
Chris@0 105 if (!$this->hasSystemCallBeenInterrupted()) {
Chris@0 106 $this->pipes = array();
Chris@0 107 }
Chris@0 108
Chris@0 109 return $read;
Chris@0 110 }
Chris@0 111
Chris@0 112 foreach ($r as $pipe) {
Chris@0 113 // prior PHP 5.4 the array passed to stream_select is modified and
Chris@0 114 // lose key association, we have to find back the key
Chris@0 115 $read[$type = array_search($pipe, $this->pipes, true)] = '';
Chris@0 116
Chris@0 117 do {
Chris@0 118 $data = fread($pipe, self::CHUNK_SIZE);
Chris@0 119 $read[$type] .= $data;
Chris@0 120 } while (isset($data[0]) && ($close || isset($data[self::CHUNK_SIZE - 1])));
Chris@0 121
Chris@0 122 if (!isset($read[$type][0])) {
Chris@0 123 unset($read[$type]);
Chris@0 124 }
Chris@0 125
Chris@0 126 if ($close && feof($pipe)) {
Chris@0 127 fclose($pipe);
Chris@0 128 unset($this->pipes[$type]);
Chris@0 129 }
Chris@0 130 }
Chris@0 131
Chris@0 132 return $read;
Chris@0 133 }
Chris@0 134
Chris@0 135 /**
Chris@0 136 * {@inheritdoc}
Chris@0 137 */
Chris@0 138 public function haveReadSupport()
Chris@0 139 {
Chris@0 140 return $this->haveReadSupport;
Chris@0 141 }
Chris@0 142
Chris@0 143 /**
Chris@0 144 * {@inheritdoc}
Chris@0 145 */
Chris@0 146 public function areOpen()
Chris@0 147 {
Chris@0 148 return (bool) $this->pipes;
Chris@0 149 }
Chris@0 150 }