|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sigmapix\Sonata\ImportBundle\Export; |
|
4
|
|
|
|
|
5
|
|
|
use Exporter\Source\SourceIteratorInterface; |
|
6
|
|
|
use Exporter\Writer\CsvWriter; |
|
7
|
|
|
use Exporter\Writer\JsonWriter; |
|
8
|
|
|
use Exporter\Writer\XlsWriter; |
|
9
|
|
|
use Exporter\Writer\XmlWriter; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
11
|
|
|
|
|
12
|
|
|
@trigger_error( |
|
|
|
|
|
|
13
|
|
|
'The '.__NAMESPACE__.'\Exporter class is deprecated since version 3.1 and will be removed in 4.0.'. |
|
14
|
|
|
' Use Exporter\Exporter instead', |
|
15
|
|
|
E_USER_DEPRECATED |
|
16
|
|
|
); |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* NEXT_MAJOR: remove this class, and the dev dependency. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Exporter |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @param string $format |
|
25
|
|
|
* @param string $filename |
|
26
|
|
|
* @param SourceIteratorInterface $source |
|
27
|
|
|
* |
|
28
|
|
|
* @throws \RuntimeException |
|
29
|
|
|
* |
|
30
|
|
|
* @return StreamedResponse |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getResponse($format, $filename, SourceIteratorInterface $source, array $defaultHeaders) |
|
33
|
|
|
{ |
|
34
|
|
|
switch ($format) { |
|
35
|
|
|
case 'xls': |
|
36
|
|
|
$writer = new XlsWriter('php://output'); |
|
37
|
|
|
$contentType = 'application/vnd.ms-excel'; |
|
38
|
|
|
break; |
|
39
|
|
|
case 'xml': |
|
40
|
|
|
$writer = new XmlWriter('php://output'); |
|
41
|
|
|
$contentType = 'text/xml'; |
|
42
|
|
|
break; |
|
43
|
|
|
case 'json': |
|
44
|
|
|
$writer = new JsonWriter('php://output'); |
|
45
|
|
|
$contentType = 'application/json'; |
|
46
|
|
|
break; |
|
47
|
|
|
case 'csv': |
|
48
|
|
|
$writer = new CsvWriter('php://output', ';', '"', '\\', true, true); |
|
49
|
|
|
$contentType = 'text/csv'; |
|
50
|
|
|
break; |
|
51
|
|
|
default: |
|
52
|
|
|
throw new \RuntimeException('Invalid format'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$callback = function () use ($source, $writer, $defaultHeaders) { |
|
56
|
|
|
$handler = Handler::create($source, $writer, $defaultHeaders); |
|
57
|
|
|
$handler->export(); |
|
58
|
|
|
}; |
|
59
|
|
|
|
|
60
|
|
|
return new StreamedResponse($callback, 200, [ |
|
61
|
|
|
'Content-Type' => $contentType, |
|
62
|
|
|
'Content-Disposition' => sprintf('attachment; filename="%s"', $filename), |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: