1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2015-2017 Libre Informatique |
6
|
|
|
* |
7
|
|
|
* This file is licenced under the GNU LGPL v3. |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.md |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Blast\Bundle\CoreBundle\Exporter; |
13
|
|
|
|
14
|
|
|
use Exporter\Source\SourceIteratorInterface; |
15
|
|
|
use Exporter\Writer\TypedWriterInterface; |
16
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
17
|
|
|
|
18
|
|
|
class Exporter |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var TypedWriterInterface[] |
22
|
|
|
*/ |
23
|
|
|
private $writers; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param TypedWriterInterface[] $writers an array of allowed typed writers, indexed by format name |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $writers = array()) |
29
|
|
|
{ |
30
|
|
|
$this->writers = array(); |
31
|
|
|
|
32
|
|
|
foreach ($writers as $writer) { |
33
|
|
|
$this->addWriter($writer); |
34
|
|
|
} |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @throws \RuntimeException |
39
|
|
|
* |
40
|
|
|
* @param string $format |
41
|
|
|
* @param string $filename |
42
|
|
|
* @param SourceIteratorInterface $source |
43
|
|
|
* |
44
|
|
|
* @return StreamedResponse |
45
|
|
|
*/ |
46
|
|
|
public function getResponse($format, $filename, SourceIteratorInterface $source) |
47
|
|
|
{ |
48
|
|
|
if (!array_key_exists($format, $this->writers)) { |
49
|
|
|
throw new \RuntimeException(sprintf( |
50
|
|
|
'Invalid "%s" format, supported formats are : "%s"', |
51
|
|
|
$format, |
52
|
|
|
implode(', ', array_keys($this->writers)) |
53
|
|
|
)); |
54
|
|
|
} |
55
|
|
|
$writer = $this->writers[$format]; |
56
|
|
|
|
57
|
|
|
$callback = function () use ($source, $writer) { |
58
|
|
|
$handler = \Blast\Bundle\CoreBundle\Exporter\Handler::create($source, $writer); |
59
|
|
|
$handler->export(); |
60
|
|
|
}; |
61
|
|
|
|
62
|
|
|
$headers = array( |
63
|
|
|
'Content-Disposition' => sprintf('attachment; filename="%s"', $filename), |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
$headers['Content-Type'] = $writer->getDefaultMimeType(); |
67
|
|
|
|
68
|
|
|
return new StreamedResponse($callback, 200, $headers); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Returns a simple array of export formats. |
73
|
|
|
* |
74
|
|
|
* @return string[] writer formats as returned by the TypedWriterInterface::getFormat() method |
75
|
|
|
*/ |
76
|
|
|
public function getAvailableFormats() |
77
|
|
|
{ |
78
|
|
|
return array_keys($this->writers); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* The main benefit of this method is the type hinting. |
83
|
|
|
* |
84
|
|
|
* @param TypedWriterInterface $writer a possible writer for exporting data |
85
|
|
|
*/ |
86
|
|
|
public function addWriter(TypedWriterInterface $writer) |
87
|
|
|
{ |
88
|
|
|
$this->writers[$writer->getFormat()] = $writer; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|