1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MewesK\TwigSpreadsheetBundle\Wrapper; |
4
|
|
|
|
5
|
|
|
use MewesK\TwigSpreadsheetBundle\Helper\Filesystem; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Settings; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\BaseWriter; |
11
|
|
|
use Symfony\Bridge\Twig\AppVariable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class DocumentWrapper. |
15
|
|
|
*/ |
16
|
|
|
class DocumentWrapper extends BaseWrapper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Spreadsheet|null |
20
|
|
|
*/ |
21
|
|
|
protected $object; |
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $attributes; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* DocumentWrapper constructor. |
29
|
|
|
* |
30
|
|
|
* @param array $context |
31
|
|
|
* @param \Twig_Environment $environment |
32
|
|
|
* @param array $attributes |
33
|
|
|
*/ |
34
|
115 |
|
public function __construct(array $context, \Twig_Environment $environment, array $attributes = []) |
35
|
|
|
{ |
36
|
115 |
|
parent::__construct($context, $environment); |
37
|
|
|
|
38
|
115 |
|
$this->object = null; |
39
|
115 |
|
$this->attributes = $attributes; |
40
|
115 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param array $properties |
44
|
|
|
* |
45
|
|
|
* @throws \RuntimeException |
46
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception |
47
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
48
|
|
|
*/ |
49
|
115 |
|
public function start(array $properties = []) |
50
|
|
|
{ |
51
|
|
|
// load template |
52
|
115 |
|
if (isset($properties['template'])) { |
53
|
17 |
|
$templatePath = $this->expandPath($properties['template']); |
54
|
17 |
|
$reader = IOFactory::createReaderForFile($templatePath); |
55
|
17 |
|
$this->object = $reader->load($templatePath); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
// create new |
59
|
|
|
else { |
60
|
98 |
|
$this->object = new Spreadsheet(); |
61
|
98 |
|
$this->object->removeSheetByIndex(0); |
62
|
|
|
} |
63
|
|
|
|
64
|
115 |
|
$this->parameters['properties'] = $properties; |
65
|
|
|
|
66
|
115 |
|
$this->setProperties($properties); |
67
|
115 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @throws \InvalidArgumentException |
71
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
72
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Writer\Exception |
73
|
|
|
* @throws \Symfony\Component\Filesystem\Exception\IOException |
74
|
|
|
*/ |
75
|
107 |
|
public function end() |
76
|
|
|
{ |
77
|
107 |
|
$format = null; |
78
|
|
|
|
79
|
|
|
// try document property |
80
|
107 |
|
if (isset($this->parameters['format'])) { |
81
|
|
|
$format = $this->parameters['format']; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
// try Symfony request |
85
|
107 |
|
elseif (isset($this->context['app'])) { |
86
|
|
|
/** |
87
|
|
|
* @var AppVariable |
88
|
|
|
*/ |
89
|
107 |
|
$appVariable = $this->context['app']; |
90
|
107 |
|
if ($appVariable instanceof AppVariable && $appVariable->getRequest() !== null) { |
91
|
107 |
|
$format = $appVariable->getRequest()->getRequestFormat(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
// set default |
96
|
107 |
|
if ($format === null || !is_string($format)) { |
97
|
|
|
$format = 'xlsx'; |
98
|
|
|
} |
99
|
|
|
|
100
|
107 |
|
switch (strtolower($format)) { |
101
|
107 |
|
case 'csv': |
102
|
2 |
|
$writerType = 'Csv'; |
103
|
2 |
|
break; |
104
|
105 |
|
case 'ods': |
105
|
27 |
|
$writerType = 'Ods'; |
106
|
27 |
|
break; |
107
|
78 |
|
case 'pdf': |
108
|
1 |
|
$writerType = 'Pdf'; |
109
|
1 |
|
if (!class_exists('\Mpdf\Mpdf')) { |
110
|
|
|
throw new Exception('Error loading mPDF. Is mPDF correctly installed?'); |
111
|
|
|
} |
112
|
1 |
|
Settings::setPdfRendererName(Settings::PDF_RENDERER_MPDF); |
113
|
1 |
|
break; |
114
|
77 |
|
case 'xls': |
115
|
34 |
|
$writerType = 'Xls'; |
116
|
34 |
|
break; |
117
|
43 |
|
case 'xlsx': |
118
|
43 |
|
$writerType = 'Xlsx'; |
119
|
43 |
|
break; |
120
|
|
|
default: |
121
|
|
|
throw new \InvalidArgumentException(sprintf('Unknown format "%s"', $format)); |
122
|
|
|
} |
123
|
|
|
|
124
|
107 |
|
if ($this->object !== null) { |
125
|
|
|
if ( |
126
|
107 |
|
$this->attributes['diskCachingDirectory'] !== null && |
127
|
107 |
|
!Filesystem::exists($this->attributes['diskCachingDirectory']) |
128
|
|
|
) { |
129
|
1 |
|
Filesystem::mkdir($this->attributes['diskCachingDirectory']); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @var BaseWriter $writer |
134
|
|
|
*/ |
135
|
107 |
|
$writer = IOFactory::createWriter($this->object, $writerType); |
136
|
107 |
|
$writer->setPreCalculateFormulas($this->attributes['preCalculateFormulas'] ?? true); |
137
|
107 |
|
$writer->setUseDiskCaching( |
138
|
107 |
|
$this->attributes['diskCachingDirectory'] !== null, |
139
|
107 |
|
$this->attributes['diskCachingDirectory'] ?? null |
140
|
|
|
); |
141
|
107 |
|
$writer->save('php://output'); |
142
|
|
|
} |
143
|
|
|
|
144
|
107 |
|
$this->object = null; |
145
|
107 |
|
$this->parameters = []; |
146
|
107 |
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return Spreadsheet|null |
150
|
|
|
*/ |
151
|
103 |
|
public function getObject() |
152
|
|
|
{ |
153
|
103 |
|
return $this->object; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param Spreadsheet|null $object |
158
|
|
|
*/ |
159
|
|
|
public function setObject(Spreadsheet $object = null) |
160
|
|
|
{ |
161
|
|
|
$this->object = $object; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* {@inheritdoc} |
166
|
|
|
*/ |
167
|
|
|
protected function configureMappings(): array |
168
|
|
|
{ |
169
|
|
|
return [ |
170
|
|
|
'category' => function ($value) { $this->object->getProperties()->setCategory($value); }, |
171
|
|
|
'company' => function ($value) { $this->object->getProperties()->setCompany($value); }, |
172
|
|
|
'created' => function ($value) { $this->object->getProperties()->setCreated($value); }, |
173
|
|
|
'creator' => function ($value) { $this->object->getProperties()->setCreator($value); }, |
174
|
|
|
'defaultStyle' => function ($value) { $this->object->getDefaultStyle()->applyFromArray($value); }, |
175
|
|
|
'description' => function ($value) { $this->object->getProperties()->setDescription($value); }, |
176
|
|
|
'format' => function ($value) { $this->parameters['format'] = $value; }, |
177
|
|
|
'keywords' => function ($value) { $this->object->getProperties()->setKeywords($value); }, |
178
|
|
|
'lastModifiedBy' => function ($value) { $this->object->getProperties()->setLastModifiedBy($value); }, |
179
|
|
|
'manager' => function ($value) { $this->object->getProperties()->setManager($value); }, |
180
|
|
|
'modified' => function ($value) { $this->object->getProperties()->setModified($value); }, |
181
|
|
|
'security' => [ |
182
|
|
|
'lockRevision' => function ($value) { $this->object->getSecurity()->setLockRevision($value); }, |
183
|
|
|
'lockStructure' => function ($value) { $this->object->getSecurity()->setLockStructure($value); }, |
184
|
|
|
'lockWindows' => function ($value) { $this->object->getSecurity()->setLockWindows($value); }, |
185
|
|
|
'revisionsPassword' => function ($value) { $this->object->getSecurity()->setRevisionsPassword($value); }, |
186
|
|
|
'workbookPassword' => function ($value) { $this->object->getSecurity()->setWorkbookPassword($value); }, |
187
|
|
|
], |
188
|
|
|
'subject' => function ($value) { $this->object->getProperties()->setSubject($value); }, |
189
|
|
|
'template' => function ($value) { $this->parameters['template'] = $value; }, |
190
|
|
|
'title' => function ($value) { $this->object->getProperties()->setTitle($value); }, |
191
|
|
|
]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Resolves paths using Twig namespaces. |
196
|
|
|
* The path must start with the namespace. |
197
|
|
|
* Namespaces are case sensitive. |
198
|
|
|
* |
199
|
|
|
* @param string $path |
200
|
|
|
* |
201
|
|
|
* @return string |
202
|
|
|
*/ |
203
|
17 |
|
private function expandPath(string $path): string |
204
|
|
|
{ |
205
|
17 |
|
$loader = $this->environment->getLoader(); |
206
|
|
|
|
207
|
17 |
|
if ($loader instanceof \Twig_Loader_Filesystem && mb_strpos($path, '@') === 0) { |
208
|
|
|
/* |
209
|
|
|
* @var \Twig_Loader_Filesystem |
210
|
|
|
*/ |
211
|
11 |
|
foreach ($loader->getNamespaces() as $namespace) { |
212
|
11 |
|
if (mb_strpos($path, $namespace) === 1) { |
213
|
11 |
|
foreach ($loader->getPaths($namespace) as $namespacePath) { |
214
|
11 |
|
$expandedPathAttribute = str_replace('@'.$namespace, $namespacePath, $path); |
215
|
11 |
|
if (file_exists($expandedPathAttribute)) { |
216
|
11 |
|
return $expandedPathAttribute; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
|
223
|
6 |
|
return $path; |
224
|
|
|
} |
225
|
|
|
} |
226
|
|
|
|