1 | <?php |
||
17 | class DocumentWrapper extends BaseWrapper |
||
18 | { |
||
19 | /** |
||
20 | * @var Spreadsheet|null |
||
21 | */ |
||
22 | protected $object; |
||
23 | /** |
||
24 | * @var array |
||
25 | */ |
||
26 | protected $attributes; |
||
27 | |||
28 | /** |
||
29 | * DocumentWrapper constructor. |
||
30 | * |
||
31 | * @param array $context |
||
32 | * @param \Twig_Environment $environment |
||
33 | * @param array $attributes |
||
34 | */ |
||
35 | 121 | public function __construct(array $context, \Twig_Environment $environment, array $attributes = []) |
|
42 | |||
43 | /** |
||
44 | * @param array $properties |
||
45 | * |
||
46 | * @throws \RuntimeException |
||
47 | * @throws \PhpOffice\PhpSpreadsheet\Reader\Exception |
||
48 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
49 | */ |
||
50 | 121 | public function start(array $properties = []) |
|
69 | |||
70 | /** |
||
71 | * @throws \LogicException |
||
72 | * @throws \RuntimeException |
||
73 | * @throws \InvalidArgumentException |
||
74 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
75 | * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
||
76 | * @throws \Symfony\Component\Filesystem\Exception\IOException |
||
77 | */ |
||
78 | 113 | public function end() |
|
79 | { |
||
80 | 113 | if ($this->object === null) { |
|
81 | throw new \LogicException(); |
||
82 | } |
||
83 | |||
84 | 113 | $format = null; |
|
85 | |||
86 | // try document property |
||
87 | 113 | if (isset($this->parameters['format'])) { |
|
88 | $format = $this->parameters['format']; |
||
89 | } |
||
90 | |||
91 | // try Symfony request |
||
92 | 113 | elseif (isset($this->context['app'])) { |
|
93 | /** |
||
94 | * @var AppVariable |
||
95 | */ |
||
96 | 113 | $appVariable = $this->context['app']; |
|
97 | 113 | if ($appVariable instanceof AppVariable && $appVariable->getRequest() !== null) { |
|
98 | 113 | $format = $appVariable->getRequest()->getRequestFormat(); |
|
99 | } |
||
100 | } |
||
101 | |||
102 | // set default |
||
103 | 113 | if ($format === null || !\is_string($format)) { |
|
104 | $format = 'xlsx'; |
||
105 | } else { |
||
106 | 113 | $format = strtolower($format); |
|
107 | } |
||
108 | |||
109 | // set up mPDF |
||
110 | 113 | if ($format === 'pdf') { |
|
111 | 1 | if (!class_exists('\Mpdf\Mpdf')) { |
|
112 | throw new \RuntimeException('Error loading mPDF. Is mPDF correctly installed?'); |
||
113 | } |
||
114 | 1 | IOFactory::registerWriter('Pdf', Mpdf::class); |
|
115 | } |
||
116 | |||
117 | /** |
||
118 | * @var BaseWriter $writer |
||
119 | */ |
||
120 | 113 | $writer = IOFactory::createWriter($this->object, ucfirst($format)); |
|
121 | 113 | $writer->setPreCalculateFormulas($this->attributes['pre_calculate_formulas'] ?? true); |
|
122 | |||
123 | // set up XML cache |
||
124 | 113 | if ($this->attributes['cache']['xml'] !== false) { |
|
125 | 25 | Filesystem::mkdir($this->attributes['cache']['xml']); |
|
126 | 25 | $writer->setUseDiskCaching(true, $this->attributes['cache']['xml']); |
|
127 | } |
||
128 | |||
129 | // set special CSV writer attributes |
||
130 | 113 | if ($writer instanceof Csv) { |
|
131 | /** |
||
132 | * @var Csv $writer |
||
133 | */ |
||
134 | 3 | $writer->setDelimiter($this->attributes['csv_writer']['delimiter']); |
|
135 | 3 | $writer->setEnclosure($this->attributes['csv_writer']['enclosure']); |
|
136 | 3 | $writer->setExcelCompatibility($this->attributes['csv_writer']['excel_compatibility']); |
|
137 | 3 | $writer->setIncludeSeparatorLine($this->attributes['csv_writer']['include_separator_line']); |
|
138 | 3 | $writer->setLineEnding($this->attributes['csv_writer']['line_ending']); |
|
139 | 3 | $writer->setSheetIndex($this->attributes['csv_writer']['sheet_index']); |
|
140 | 3 | $writer->setUseBOM($this->attributes['csv_writer']['use_bom']); |
|
141 | } |
||
142 | |||
143 | 113 | $writer->save('php://output'); |
|
144 | |||
145 | 113 | $this->object = null; |
|
146 | 113 | $this->parameters = []; |
|
147 | 113 | } |
|
148 | |||
149 | /** |
||
150 | * @return Spreadsheet|null |
||
151 | */ |
||
152 | 109 | public function getObject() |
|
156 | |||
157 | /** |
||
158 | * @param Spreadsheet|null $object |
||
159 | */ |
||
160 | public function setObject(Spreadsheet $object = null) |
||
164 | |||
165 | /** |
||
166 | * {@inheritdoc} |
||
167 | * |
||
168 | * @throws \PhpOffice\PhpSpreadsheet\Exception |
||
169 | */ |
||
170 | protected function configureMappings(): array |
||
196 | |||
197 | /** |
||
198 | * Resolves paths using Twig namespaces. |
||
199 | * The path must start with the namespace. |
||
200 | * Namespaces are case sensitive. |
||
201 | * |
||
202 | * @param string $path |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | 17 | private function expandPath(string $path): string |
|
228 | } |
||
229 |