/** * @param ItemReaderInterface $reader * @param ExportProcessor $processor * @param ItemWriterInterface $writer * @param array $contextParameters * @param int $batchSize * @param string $format * * @return StreamedResponse */ public function handle(ItemReaderInterface $reader, ExportProcessor $processor, ItemWriterInterface $writer, array $contextParameters, $batchSize, $format) { if (!isset($contextParameters['gridName'])) { throw new InvalidArgumentException('Parameter "gridName" must be provided.'); } $context = new Context($contextParameters); $executor = new StepExecutor(); $executor->setBatchSize($batchSize); $executor->setReader($reader)->setProcessor($processor)->setWriter($writer); foreach ([$executor->getReader(), $executor->getProcessor(), $executor->getWriter()] as $element) { if ($element instanceof ContextAwareInterface) { $element->setImportExportContext($context); } } $contentType = $this->guesser->guessByFileExtension($format); if (!$contentType) { $contentType = 'application/octet-stream'; } // prepare response $response = new StreamedResponse($this->exportCallback($context, $executor)); $response->headers->set('Content-Type', $contentType); $response->headers->set('Content-Transfer-Encoding', 'binary'); $outputFileName = sprintf('datagrid_%s_%s.%s', str_replace('-', '_', $contextParameters['gridName']), date('Y_m_d_H_i_s'), $format); $response->headers->set('Content-Disposition', $response->headers->makeDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $outputFileName)); return $response; }
/** * Tries to guess MIME type of the given file * * @param string $fileName * @return string|null */ protected function getFileContentType($fileName) { $extension = pathinfo($fileName, PATHINFO_EXTENSION); return $this->mimeTypeGuesser->guessByFileExtension($extension); }