예제 #1
0
 /**
  * @param AssetInterface $asset
  * @param AssetWriter $writer
  * @param array $cachePath
  * @param array $headers
  */
 public function __construct(AssetInterface $asset, AssetWriter $writer, $cachePath, array $headers = [])
 {
     $file = $asset->getTargetPath();
     $cachePath = $cachePath . '/' . $file;
     $cached = false;
     $cacheTime = time();
     if (is_file($cachePath)) {
         $mTime = $asset->getLastModified();
         $cacheTime = filemtime($cachePath);
         if ($mTime > $cacheTime) {
             @unlink($cachePath);
             $cacheTime = $mTime;
         } else {
             $cached = true;
         }
     }
     if (!$cached) {
         $writer->writeAsset($asset);
     }
     $stream = function () use($cachePath) {
         readfile($cachePath);
     };
     $headers['Content-Length'] = filesize($cachePath);
     if (preg_match('/.+\\.([a-zA-Z0-9]+)/', $file, $matches)) {
         $ext = $matches[1];
         if (isset($this->mimeTypes[$ext])) {
             $headers['Content-Type'] = $this->mimeTypes[$ext];
         }
     }
     parent::__construct($stream, 200, $headers);
     $date = new \DateTime();
     $date->setTimestamp($cacheTime);
     $this->setLastModified($date);
 }
 /**
  * Constructor.
  *
  * @param array|\Traversable $rows
  * @param string             $filename
  *
  * @throws \InvalidArgumentException
  */
 public function __construct($rows, $filename)
 {
     if (!is_array($rows) && !$rows instanceof \Traversable) {
         throw new \InvalidArgumentException('$rows should be an array or an instance of \\Traversable.');
     }
     $this->rows = $rows;
     parent::__construct(array($this, 'output'), 200, array('Content-Type' => 'text/csv', 'Content-disposition' => 'attachment; filename=' . $filename));
 }
예제 #3
0
 /**
  * @param FileInterface $file
  * @param array $headers
  */
 public function __construct(FileInterface $file, array $headers = [])
 {
     $stream = function () use($file) {
         readfile($file->getAbsolutePath());
     };
     $headers['Content-Length'] = $file->getSize();
     $headers['Content-Type'] = $file->getMimeType();
     parent::__construct($stream, 200, $headers);
     $mtime = $file->getUpdated();
     $this->setCache(['etag' => (string) $mtime->getTimestamp(), 'last_modified' => $mtime, 'public' => true]);
 }
예제 #4
0
 /**
  * @param ImageInterface $image
  * @param FilterInterface $filter
  * @param FilteredImageWriter $writer
  * @param array $headers
  */
 public function __construct(ImageInterface $image, FilterInterface $filter, FilteredImageWriter $writer, array $headers = [])
 {
     $path = $writer->getFilteredImagePath($image, $filter);
     $stream = function () use($path) {
         readfile($path);
     };
     $headers['Content-Length'] = filesize($path);
     $headers['Content-Type'] = $image->getMimeType();
     parent::__construct($stream, 200, $headers);
     $this->setLastModified($image->getUpdated());
 }
예제 #5
0
 public function __construct($title, $template, $data = [], $options = [], $headers = [])
 {
     parent::__construct();
     $this->title = $title;
     $this->template = $template;
     $this->data = $data;
     $this->options = $options;
     $this->setCallback(function () use($title, $template, $data, $options) {
         $view = new NWTemplate();
         $view->displayPage($template, $title, $data, $options);
     });
     $this->headers = new ResponseHeaderBag($headers);
 }
 /**
  * @param resource $resource
  * @param int      $status
  * @param array    $headers
  */
 public function __construct($resource, $status = 200, $headers = [])
 {
     if (!is_resource($resource)) {
         throw new \InvalidArgumentException(sprintf('A resource is expected, "%s" given', gettype($resource)));
     }
     $callback = function () use($resource) {
         while (!feof($resource)) {
             $buffer = fread($resource, StreamedFileResponse::CHUNK);
             echo $buffer;
             ob_flush();
         }
         fclose($resource);
     };
     $headers['Content-Type'] = 'application/octet-stream';
     parent::__construct($callback, $status, $headers);
 }
 /**
  * Constructor.
  *
  * @param mixed   $data    The response data or callback
  * @param int     $status  The response status code
  * @param array   $headers An array of response headers
  */
 public function __construct($data = array(), $status = 200, $headers = array())
 {
     if (!isset($headers['Content-Type'])) {
         $headers['Content-Type'] = 'text/csv';
     }
     parent::__construct(null, $status, $headers);
     $this->exporterConfig = new ExporterConfig();
     // If a callback is passed
     if (is_callable($data)) {
         $this->setCallback($data);
     } else {
         $self = $this;
         $this->setCallback(function () use($self, $data) {
             $exporter = new Exporter($self->exporterConfig);
             $exporter->export('php://output', $data);
         });
     }
     $this->streamed = false;
 }
예제 #8
0
 /**
  * CsvStreamedResponse constructor.
  *
  * @param string $filename
  * @param int    $status
  * @param array  $headers
  */
 public function __construct($filename = null, $status = 200, $headers = array())
 {
     parent::__construct(null, $status, $headers);
     $this->headers->set('Content-Type', 'text/csv; charset=utf-8');
     $this->headers->set('Content-Disposition', 'attachment; filename="' . ($filename ?: 'file') . '.csv"');
     $this->callback = function () {
         $handle = fopen('php://output', 'w');
         // пишем шапку
         if (isset($this->encoding) && 'UTF-8' !== strtoupper($this->encoding)) {
             mb_convert_variables($this->encoding, 'UTF-8', $this->colums);
         }
         fputcsv($handle, $this->colums, $this->delimiter, $this->enclosure);
         $fields = array_keys($this->colums);
         if ($this->iterator instanceof IterableResult) {
             // для каждой строки
             foreach ($this->iterator as $row) {
                 $dataRow = array_shift($row);
                 // если задан колбэк для обработки одной строки
                 if (isset($this->dataRowCallback)) {
                     $callback = $this->dataRowCallback;
                     $callback($dataRow);
                 }
                 // раскидываем по колонкам данные
                 $fileRow = array();
                 foreach ($fields as $field) {
                     $fileRow[] = isset($dataRow[$field]) ? $dataRow[$field] : $this->emptyCellValue;
                 }
                 if (isset($this->encoding) && 'UTF-8' !== strtoupper($this->encoding)) {
                     mb_convert_variables($this->encoding, 'UTF-8', $fileRow);
                 }
                 fputcsv($handle, $fileRow, $this->delimiter, $this->enclosure);
             }
         } else {
             throw new \RuntimeException('Unknown iterator. Set a different callback.');
         }
         fclose($handle);
     };
 }
예제 #9
0
 public function __construct($filename, $callback = null, $status = 200, $headers = [])
 {
     parent::__construct($callback, $status, array_merge(['Expires' => 'Mon, 26 Jul 1997 05:00:00 GMT', 'Last-Modified' => gmdate('D, d M Y H:i:s') . ' GMT', 'Cache-Control' => 'no-store, no-cache, must-revalidate', 'Cache-Control' => 'post-check=0, pre-check=0', 'Pragma' => 'no-cache', 'Cache-Control' => 'max-age=3600, must-revalidate', 'Content-Disposition' => 'max-age=3600, must-revalidate'], $headers));
     $this->headers->set('Content-Type', 'text/csv');
     $this->headers->set('Content-Disposition', $this->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $filename, false === preg_match('/^[\\x20-\\x7e]*$/', $filename) ? '' : preg_replace('/[^(x20-x7F)]*$/', '', $filename)));
 }
예제 #10
0
 /**
  * @param string $filename
  * @param int    $status
  */
 public function __construct($filename, $status = 200)
 {
     parent::__construct(null, $status);
     $this->buildHeaders($filename);
 }