1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MyWheels\TwigSpreadsheetBundle\Wrapper; |
4
|
|
|
|
5
|
|
|
use MyWheels\TwigSpreadsheetBundle\Helper\Filesystem; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Exception; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
8
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
9
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\BaseWriter; |
10
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Csv; |
11
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf; |
12
|
|
|
use Symfony\Bridge\Twig\AppVariable; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class DocumentWrapper. |
16
|
|
|
*/ |
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
|
108 |
|
public function __construct(array $context, \Twig_Environment $environment, array $attributes = []) |
36
|
|
|
{ |
37
|
108 |
|
parent::__construct($context, $environment); |
38
|
|
|
|
39
|
108 |
|
$this->object = null; |
40
|
108 |
|
$this->attributes = $attributes; |
41
|
108 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param array $properties |
45
|
|
|
* |
46
|
|
|
* @throws \RuntimeException |
47
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Reader\Exception |
48
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
49
|
|
|
*/ |
50
|
108 |
|
public function start(array $properties = []) |
51
|
|
|
{ |
52
|
|
|
// load template |
53
|
108 |
|
if (isset($properties['template'])) { |
54
|
11 |
|
$templatePath = $this->expandPath($properties['template']); |
55
|
11 |
|
$reader = IOFactory::createReaderForFile($templatePath); |
56
|
11 |
|
$this->object = $reader->load($templatePath); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// create new |
60
|
|
|
else { |
61
|
97 |
|
$this->object = new Spreadsheet(); |
62
|
97 |
|
$this->object->removeSheetByIndex(0); |
63
|
|
|
} |
64
|
|
|
|
65
|
108 |
|
$this->parameters['properties'] = $properties; |
66
|
|
|
|
67
|
108 |
|
$this->setProperties($properties); |
68
|
108 |
|
} |
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
|
100 |
|
public function end() |
79
|
|
|
{ |
80
|
100 |
|
if ($this->object === null) { |
81
|
|
|
throw new \LogicException(); |
82
|
|
|
} |
83
|
|
|
|
84
|
100 |
|
$format = null; |
85
|
|
|
|
86
|
|
|
// try document property |
87
|
100 |
|
if (isset($this->parameters['format'])) { |
88
|
|
|
$format = $this->parameters['format']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
// try Symfony request |
92
|
100 |
|
elseif (isset($this->context['app'])) { |
93
|
|
|
/** |
94
|
|
|
* @var AppVariable |
95
|
|
|
*/ |
96
|
100 |
|
$appVariable = $this->context['app']; |
97
|
100 |
|
if ($appVariable instanceof AppVariable && $appVariable->getRequest() !== null) { |
98
|
100 |
|
$format = $appVariable->getRequest()->getRequestFormat(); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// set default |
103
|
100 |
|
if ($format === null || !\is_string($format)) { |
104
|
|
|
$format = 'xlsx'; |
105
|
|
|
} else { |
106
|
100 |
|
$format = strtolower($format); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// set up mPDF |
110
|
100 |
|
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
|
100 |
|
$writer = IOFactory::createWriter($this->object, ucfirst($format)); |
121
|
100 |
|
$writer->setPreCalculateFormulas($this->attributes['pre_calculate_formulas'] ?? true); |
122
|
|
|
|
123
|
|
|
// set up XML cache |
124
|
100 |
|
if ($this->attributes['cache']['xml'] !== false) { |
125
|
|
|
Filesystem::mkdir($this->attributes['cache']['xml']); |
126
|
|
|
$writer->setUseDiskCaching(true, $this->attributes['cache']['xml']); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
// set special CSV writer attributes |
130
|
100 |
|
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
|
100 |
|
$writer->save('php://output'); |
144
|
|
|
|
145
|
100 |
|
$this->object = null; |
146
|
100 |
|
$this->parameters = []; |
147
|
100 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return Spreadsheet|null |
151
|
|
|
*/ |
152
|
102 |
|
public function getObject() |
153
|
|
|
{ |
154
|
102 |
|
return $this->object; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param Spreadsheet|null $object |
159
|
|
|
*/ |
160
|
|
|
public function setObject(Spreadsheet $object = null) |
161
|
|
|
{ |
162
|
|
|
$this->object = $object; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* {@inheritdoc} |
167
|
|
|
* |
168
|
|
|
* @throws \PhpOffice\PhpSpreadsheet\Exception |
169
|
|
|
*/ |
170
|
|
|
protected function configureMappings(): array |
171
|
|
|
{ |
172
|
|
|
return [ |
173
|
|
|
'category' => function ($value) { $this->object->getProperties()->setCategory($value); }, |
174
|
|
|
'company' => function ($value) { $this->object->getProperties()->setCompany($value); }, |
175
|
|
|
'created' => function ($value) { $this->object->getProperties()->setCreated($value); }, |
176
|
|
|
'creator' => function ($value) { $this->object->getProperties()->setCreator($value); }, |
177
|
|
|
'defaultStyle' => function ($value) { $this->object->getDefaultStyle()->applyFromArray($value); }, |
178
|
|
|
'description' => function ($value) { $this->object->getProperties()->setDescription($value); }, |
179
|
|
|
'format' => function ($value) { $this->parameters['format'] = $value; }, |
180
|
|
|
'keywords' => function ($value) { $this->object->getProperties()->setKeywords($value); }, |
181
|
|
|
'lastModifiedBy' => function ($value) { $this->object->getProperties()->setLastModifiedBy($value); }, |
182
|
|
|
'manager' => function ($value) { $this->object->getProperties()->setManager($value); }, |
183
|
|
|
'modified' => function ($value) { $this->object->getProperties()->setModified($value); }, |
184
|
|
|
'security' => [ |
185
|
|
|
'lockRevision' => function ($value) { $this->object->getSecurity()->setLockRevision($value); }, |
186
|
|
|
'lockStructure' => function ($value) { $this->object->getSecurity()->setLockStructure($value); }, |
187
|
|
|
'lockWindows' => function ($value) { $this->object->getSecurity()->setLockWindows($value); }, |
188
|
|
|
'revisionsPassword' => function ($value) { $this->object->getSecurity()->setRevisionsPassword($value); }, |
189
|
|
|
'workbookPassword' => function ($value) { $this->object->getSecurity()->setWorkbookPassword($value); }, |
190
|
|
|
], |
191
|
|
|
'subject' => function ($value) { $this->object->getProperties()->setSubject($value); }, |
192
|
|
|
'template' => function ($value) { $this->parameters['template'] = $value; }, |
193
|
|
|
'title' => function ($value) { $this->object->getProperties()->setTitle($value); }, |
194
|
|
|
]; |
195
|
|
|
} |
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
|
11 |
|
private function expandPath(string $path): string |
207
|
|
|
{ |
208
|
11 |
|
$loader = $this->environment->getLoader(); |
209
|
|
|
|
210
|
11 |
|
if ($loader instanceof \Twig_Loader_Filesystem && mb_strpos($path, '@') === 0) { |
211
|
|
|
/* |
212
|
|
|
* @var \Twig_Loader_Filesystem |
213
|
|
|
*/ |
214
|
8 |
|
foreach ($loader->getNamespaces() as $namespace) { |
215
|
8 |
|
if (mb_strpos($path, $namespace) === 1) { |
216
|
8 |
|
foreach ($loader->getPaths($namespace) as $namespacePath) { |
217
|
8 |
|
$expandedPathAttribute = str_replace('@'.$namespace, $namespacePath, $path); |
218
|
8 |
|
if (Filesystem::exists($expandedPathAttribute)) { |
219
|
8 |
|
return $expandedPathAttribute; |
220
|
|
|
} |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
3 |
|
return $path; |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|