Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Stream.php
Go to the documentation of this file.
1 <?php
2 
35 {
41  protected $stream;
42 
50  protected $stream_name;
51 
57  protected $_cleanup;
58 
64  public function getStream()
65  {
66  return $this->stream;
67  }
68 
75  public function setStream($stream)
76  {
77  $this->stream = $stream;
78  return $this;
79  }
80 
86  public function getCleanup() {
87  return $this->_cleanup;
88  }
89 
95  public function setCleanup($cleanup = true) {
96  $this->_cleanup = $cleanup;
97  }
98 
104  public function getStreamName() {
105  return $this->stream_name;
106  }
107 
114  public function setStreamName($stream_name) {
115  $this->stream_name = $stream_name;
116  return $this;
117  }
118 
119 
139  public function __construct($code, $headers, $body = null, $version = '1.1', $message = null)
140  {
141 
142  if(is_resource($body)) {
143  $this->setStream($body);
144  $body = '';
145  }
146  parent::__construct($code, $headers, $body, $version, $message);
147  }
148 
156  public static function fromStream($response_str, $stream)
157  {
158  $code = self::extractCode($response_str);
159  $headers = self::extractHeaders($response_str);
160  $version = self::extractVersion($response_str);
161  $message = self::extractMessage($response_str);
162 
163  return new self($code, $headers, $stream, $version, $message);
164  }
165 
178  public function getBody()
179  {
180  if($this->stream != null) {
181  $this->readStream();
182  }
183  return parent::getBody();
184  }
185 
194  public function getRawBody()
195  {
196  if($this->stream) {
197  $this->readStream();
198  }
199  return $this->body;
200  }
201 
209  protected function readStream()
210  {
211  if(!is_resource($this->stream)) {
212  return '';
213  }
214 
215  if(isset($headers['content-length'])) {
216  $this->body = stream_get_contents($this->stream, $headers['content-length']);
217  } else {
218  $this->body = stream_get_contents($this->stream);
219  }
220  fclose($this->stream);
221  $this->stream = null;
222  }
223 
224  public function __destruct()
225  {
226  if(is_resource($this->stream)) {
227  fclose($this->stream);
228  $this->stream = null;
229  }
230  if($this->_cleanup) {
231  @unlink($this->stream_name);
232  }
233  }
234 
235 }
static extractHeaders($response_str)
Definition: Response.php:500
static fromStream($response_str, $stream)
Definition: Stream.php:156
static extractVersion($response_str)
Definition: Response.php:483
setCleanup($cleanup=true)
Definition: Stream.php:95
__construct($code, $headers, $body=null, $version='1.1', $message=null)
Definition: Stream.php:139
setStreamName($stream_name)
Definition: Stream.php:114
static extractMessage($response_str)
Definition: Response.php:466
static extractCode($response_str)
Definition: Response.php:449