comparison vendor/symfony/process/Pipes/AbstractPipes.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children
comparison
equal deleted inserted replaced
16:c2387f117808 17:129ea1e6d783
18 * 18 *
19 * @internal 19 * @internal
20 */ 20 */
21 abstract class AbstractPipes implements PipesInterface 21 abstract class AbstractPipes implements PipesInterface
22 { 22 {
23 public $pipes = array(); 23 public $pipes = [];
24 24
25 private $inputBuffer = ''; 25 private $inputBuffer = '';
26 private $input; 26 private $input;
27 private $blocked = true; 27 private $blocked = true;
28 private $lastError; 28 private $lastError;
30 /** 30 /**
31 * @param resource|string|int|float|bool|\Iterator|null $input 31 * @param resource|string|int|float|bool|\Iterator|null $input
32 */ 32 */
33 public function __construct($input) 33 public function __construct($input)
34 { 34 {
35 if (is_resource($input) || $input instanceof \Iterator) { 35 if (\is_resource($input) || $input instanceof \Iterator) {
36 $this->input = $input; 36 $this->input = $input;
37 } elseif (is_string($input)) { 37 } elseif (\is_string($input)) {
38 $this->inputBuffer = $input; 38 $this->inputBuffer = $input;
39 } else { 39 } else {
40 $this->inputBuffer = (string) $input; 40 $this->inputBuffer = (string) $input;
41 } 41 }
42 } 42 }
47 public function close() 47 public function close()
48 { 48 {
49 foreach ($this->pipes as $pipe) { 49 foreach ($this->pipes as $pipe) {
50 fclose($pipe); 50 fclose($pipe);
51 } 51 }
52 $this->pipes = array(); 52 $this->pipes = [];
53 } 53 }
54 54
55 /** 55 /**
56 * Returns true if a system call has been interrupted. 56 * Returns true if a system call has been interrupted.
57 * 57 *
76 } 76 }
77 77
78 foreach ($this->pipes as $pipe) { 78 foreach ($this->pipes as $pipe) {
79 stream_set_blocking($pipe, 0); 79 stream_set_blocking($pipe, 0);
80 } 80 }
81 if (is_resource($this->input)) { 81 if (\is_resource($this->input)) {
82 stream_set_blocking($this->input, 0); 82 stream_set_blocking($this->input, 0);
83 } 83 }
84 84
85 $this->blocked = false; 85 $this->blocked = false;
86 } 86 }
98 $input = $this->input; 98 $input = $this->input;
99 99
100 if ($input instanceof \Iterator) { 100 if ($input instanceof \Iterator) {
101 if (!$input->valid()) { 101 if (!$input->valid()) {
102 $input = null; 102 $input = null;
103 } elseif (is_resource($input = $input->current())) { 103 } elseif (\is_resource($input = $input->current())) {
104 stream_set_blocking($input, 0); 104 stream_set_blocking($input, 0);
105 } elseif (!isset($this->inputBuffer[0])) { 105 } elseif (!isset($this->inputBuffer[0])) {
106 if (!is_string($input)) { 106 if (!\is_string($input)) {
107 if (!is_scalar($input)) { 107 if (!is_scalar($input)) {
108 throw new InvalidArgumentException(sprintf('%s yielded a value of type "%s", but only scalars and stream resources are supported', get_class($this->input), gettype($input))); 108 throw new InvalidArgumentException(sprintf('%s yielded a value of type "%s", but only scalars and stream resources are supported', \get_class($this->input), \gettype($input)));
109 } 109 }
110 $input = (string) $input; 110 $input = (string) $input;
111 } 111 }
112 $this->inputBuffer = $input; 112 $this->inputBuffer = $input;
113 $this->input->next(); 113 $this->input->next();
115 } else { 115 } else {
116 $input = null; 116 $input = null;
117 } 117 }
118 } 118 }
119 119
120 $r = $e = array(); 120 $r = $e = [];
121 $w = array($this->pipes[0]); 121 $w = [$this->pipes[0]];
122 122
123 // let's have a look if something changed in streams 123 // let's have a look if something changed in streams
124 if (false === @stream_select($r, $w, $e, 0, 0)) { 124 if (false === @stream_select($r, $w, $e, 0, 0)) {
125 return; 125 return;
126 } 126 }
128 foreach ($w as $stdin) { 128 foreach ($w as $stdin) {
129 if (isset($this->inputBuffer[0])) { 129 if (isset($this->inputBuffer[0])) {
130 $written = fwrite($stdin, $this->inputBuffer); 130 $written = fwrite($stdin, $this->inputBuffer);
131 $this->inputBuffer = substr($this->inputBuffer, $written); 131 $this->inputBuffer = substr($this->inputBuffer, $written);
132 if (isset($this->inputBuffer[0])) { 132 if (isset($this->inputBuffer[0])) {
133 return array($this->pipes[0]); 133 return [$this->pipes[0]];
134 } 134 }
135 } 135 }
136 136
137 if ($input) { 137 if ($input) {
138 for (;;) { 138 for (;;) {
143 $written = fwrite($stdin, $data); 143 $written = fwrite($stdin, $data);
144 $data = substr($data, $written); 144 $data = substr($data, $written);
145 if (isset($data[0])) { 145 if (isset($data[0])) {
146 $this->inputBuffer = $data; 146 $this->inputBuffer = $data;
147 147
148 return array($this->pipes[0]); 148 return [$this->pipes[0]];
149 } 149 }
150 } 150 }
151 if (feof($input)) { 151 if (feof($input)) {
152 if ($this->input instanceof \Iterator) { 152 if ($this->input instanceof \Iterator) {
153 $this->input->next(); 153 $this->input->next();
162 if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) { 162 if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) {
163 $this->input = null; 163 $this->input = null;
164 fclose($this->pipes[0]); 164 fclose($this->pipes[0]);
165 unset($this->pipes[0]); 165 unset($this->pipes[0]);
166 } elseif (!$w) { 166 } elseif (!$w) {
167 return array($this->pipes[0]); 167 return [$this->pipes[0]];
168 } 168 }
169 } 169 }
170 170
171 /** 171 /**
172 * @internal 172 * @internal