123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- /*
- * This file is part of the Sonata package.
- *
- * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Sonata\AdminBundle\Export;
- use Exporter\Source\SourceIteratorInterface;
- use Symfony\Component\HttpFoundation\StreamedResponse;
- use Exporter\Writer\XlsWriter;
- use Exporter\Writer\XmlWriter;
- use Exporter\Writer\JsonWriter;
- use Exporter\Writer\CsvWriter;
- class Exporter
- {
- /**
- * @throws \RuntimeException
- *
- * @param string $format
- * @param string $filename
- * @param SourceIteratorInterface $source
- *
- * @return StreamedResponse
- */
- public function getResponse($format, $filename, SourceIteratorInterface $source)
- {
- switch ($format) {
- case 'xls':
- $writer = new XlsWriter('php://output');
- $contentType = 'application/vnd.ms-excel';
- break;
- case 'xml':
- $writer = new XmlWriter('php://output');
- $contentType = 'text/xml';
- break;
- case 'json':
- $writer = new JsonWriter('php://output');
- $contentType = 'application/json';
- break;
- case 'csv':
- $writer = new CsvWriter('php://output', ',', '"', "", true, true);
- $contentType = 'text/csv';
- break;
- default:
- throw new \RuntimeException('Invalid format');
- }
- $callback = function() use ($source, $writer) {
- $handler = \Exporter\Handler::create($source, $writer);
- $handler->export();
- };
- return new StreamedResponse($callback, 200, array(
- 'Content-Type' => $contentType,
- 'Content-Disposition' => sprintf('attachment; filename=%s', $filename)
- ));
- }
- }
|