Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Http.php
Go to the documentation of this file.
1 <?php
8 
9 class Http
10 {
14  private $response;
15 
19  private $mime;
20 
25  public function __construct(
26  \Magento\Framework\HTTP\PhpEnvironment\Response $response,
27  \Magento\Framework\File\Mime $mime
28  ) {
29  $this->response = $response;
30  $this->mime = $mime;
31  }
32 
41  public function send($options = null)
42  {
43  $filepath = $this->getFilePath($options);
44 
45  if (!is_file($filepath) || !is_readable($filepath)) {
46  throw new \InvalidArgumentException("File '{$filepath}' does not exists.");
47  }
48 
49  $mimeType = $this->mime->getMimeType($filepath);
50  if (is_array($options) && isset($options['headers']) && $options['headers'] instanceof \Zend\Http\Headers) {
51  $this->response->setHeaders($options['headers']);
52  }
53  $this->response->setHeader('Content-length', filesize($filepath));
54  $this->response->setHeader('Content-Type', $mimeType);
55 
56  $this->response->sendHeaders();
57 
58  $handle = fopen($filepath, 'r');
59  if ($handle) {
60  while (($buffer = fgets($handle, 4096)) !== false) {
61  echo $buffer;
62  }
63  if (!feof($handle)) {
64  throw new \UnexpectedValueException("Unexpected end of file");
65  }
66  fclose($handle);
67  }
68  }
69 
79  private function getFilePath($options): string
80  {
81  if (is_string($options)) {
82  $filePath = $options;
83  } elseif (is_array($options) && isset($options['filepath'])) {
84  $filePath = $options['filepath'];
85  } else {
86  throw new \InvalidArgumentException("Filename is not set.");
87  }
88 
89  return $filePath;
90  }
91 }
$response
Definition: 404.php:11
__construct(\Magento\Framework\HTTP\PhpEnvironment\Response $response, \Magento\Framework\File\Mime $mime)
Definition: Http.php:25
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$handle