1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpOffice\PhpSpreadsheet\Helper; |
4
|
|
|
|
5
|
|
|
use PhpOffice\PhpSpreadsheet\IOFactory; |
6
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet; |
7
|
|
|
use PhpOffice\PhpSpreadsheet\Writer\IWriter; |
8
|
|
|
use RecursiveDirectoryIterator; |
9
|
|
|
use RecursiveIteratorIterator; |
10
|
|
|
use RecursiveRegexIterator; |
11
|
|
|
use ReflectionClass; |
12
|
|
|
use RegexIterator; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Helper class to be used in sample code. |
16
|
|
|
*/ |
17
|
|
|
class Sample |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Returns whether we run on CLI or browser. |
21
|
|
|
* |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
1 |
|
public function isCli() |
25
|
|
|
{ |
26
|
1 |
|
return PHP_SAPI === 'cli'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Return the filename currently being executed. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getScriptFilename() |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
return basename($_SERVER['SCRIPT_FILENAME'], '.php'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Whether we are executing the index page. |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
|
|
public function isIndex() |
45
|
|
|
{ |
46
|
|
|
return $this->getScriptFilename() === 'index'; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return the page title. |
51
|
|
|
* |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getPageTitle() |
55
|
|
|
{ |
56
|
|
|
return $this->isIndex() ? 'PHPSpreadsheet' : $this->getScriptFilename(); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return the page heading. |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getPageHeading() |
65
|
|
|
{ |
66
|
|
|
return $this->isIndex() ? '' : '<h1>' . str_replace('_', ' ', $this->getScriptFilename()) . '</h1>'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns an array of all known samples. |
71
|
|
|
* |
72
|
|
|
* @return string[] [$name => $path] |
73
|
|
|
*/ |
74
|
|
|
public function getSamples() |
75
|
|
|
{ |
76
|
|
|
// Populate samples |
77
|
|
|
$baseDir = realpath(__DIR__ . '/../../../samples'); |
78
|
|
|
$directory = new RecursiveDirectoryIterator($baseDir); |
79
|
|
|
$iterator = new RecursiveIteratorIterator($directory); |
80
|
|
|
$regex = new RegexIterator($iterator, '/^.+\.php$/', RecursiveRegexIterator::GET_MATCH); |
81
|
|
|
|
82
|
|
|
$files = []; |
83
|
|
|
foreach ($regex as $file) { |
84
|
|
|
$file = str_replace($baseDir . '/', '', $file[0]); |
85
|
|
|
$info = pathinfo($file); |
86
|
|
|
$category = str_replace('_', ' ', $info['dirname']); |
87
|
|
|
$name = str_replace('_', ' ', preg_replace('/(|\.php)/', '', $info['filename'])); |
88
|
|
|
if (!in_array($category, ['.', 'boostrap', 'templates'])) { |
89
|
|
|
if (!isset($files[$category])) { |
90
|
|
|
$files[$category] = []; |
91
|
|
|
} |
92
|
|
|
$files[$category][$name] = $file; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// Sort everything |
97
|
|
|
ksort($files); |
98
|
|
|
foreach ($files as &$f) { |
99
|
|
|
asort($f); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $files; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Write documents. |
107
|
|
|
* |
108
|
|
|
* @param Spreadsheet $spreadsheet |
109
|
|
|
* @param string $filename |
110
|
|
|
* @param string[] $writers |
111
|
|
|
*/ |
112
|
1 |
|
public function write(Spreadsheet $spreadsheet, $filename, array $writers = ['Xlsx', 'Xls']) |
113
|
|
|
{ |
114
|
|
|
// Set active sheet index to the first sheet, so Excel opens this as the first sheet |
115
|
1 |
|
$spreadsheet->setActiveSheetIndex(0); |
116
|
|
|
|
117
|
|
|
// Write documents |
118
|
1 |
|
foreach ($writers as $writerType) { |
119
|
1 |
|
$path = $this->getFilename($filename, mb_strtolower($writerType)); |
120
|
1 |
|
$writer = IOFactory::createWriter($spreadsheet, $writerType); |
121
|
1 |
|
$callStartTime = microtime(true); |
122
|
1 |
|
$writer->save($path); |
123
|
1 |
|
$this->logWrite($writer, $path, $callStartTime); |
124
|
|
|
} |
125
|
|
|
|
126
|
1 |
|
$this->logEndingNotes(); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Returns the temporary directory and make sure it exists. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
|
|
*/ |
134
|
1 |
|
private function getTemporaryFolder() |
135
|
|
|
{ |
136
|
1 |
|
$tempFolder = sys_get_temp_dir() . '/phpspreadsheet'; |
137
|
1 |
|
if (!is_dir($tempFolder)) { |
138
|
1 |
|
mkdir($tempFolder); |
139
|
|
|
} |
140
|
|
|
|
141
|
1 |
|
return $tempFolder; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Returns the filename that should be used for sample output. |
146
|
|
|
* |
147
|
|
|
* @param string $filename |
148
|
|
|
* @param string $extension |
149
|
|
|
* |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
1 |
|
public function getFilename($filename, $extension = 'xlsx') |
153
|
|
|
{ |
154
|
1 |
|
return $this->getTemporaryFolder() . '/' . str_replace('.php', '.' . $extension, basename($filename)); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Return a random temporary file name. |
159
|
|
|
* |
160
|
|
|
* @param string $extension |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
|
|
*/ |
164
|
|
|
public function getTemporaryFilename($extension = 'xlsx') |
165
|
|
|
{ |
166
|
|
|
$temporaryFilename = tempnam($this->getTemporaryFolder(), 'phpspreadsheet-'); |
167
|
|
|
unlink($temporaryFilename); |
168
|
|
|
|
169
|
|
|
return $temporaryFilename . '.' . $extension; |
170
|
|
|
} |
171
|
|
|
|
172
|
1 |
|
public function log($message) |
173
|
|
|
{ |
174
|
1 |
|
$eol = $this->isCli() ? PHP_EOL : '<br />'; |
175
|
1 |
|
echo date('H:i:s ') . $message . $eol; |
176
|
1 |
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Log ending notes. |
180
|
|
|
*/ |
181
|
1 |
|
public function logEndingNotes() |
182
|
|
|
{ |
183
|
|
|
// Do not show execution time for index |
184
|
1 |
|
$this->log('Peak memory usage: ' . (memory_get_peak_usage(true) / 1024 / 1024) . 'MB'); |
185
|
1 |
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Log a line about the write operation. |
189
|
|
|
* |
190
|
|
|
* @param IWriter $writer |
191
|
|
|
* @param string $path |
192
|
|
|
* @param float $callStartTime |
193
|
|
|
* |
194
|
|
|
* @throws \ReflectionException |
195
|
|
|
*/ |
196
|
1 |
|
public function logWrite(IWriter $writer, $path, $callStartTime) |
197
|
|
|
{ |
198
|
1 |
|
$callEndTime = microtime(true); |
199
|
1 |
|
$callTime = $callEndTime - $callStartTime; |
200
|
1 |
|
$reflection = new ReflectionClass($writer); |
201
|
1 |
|
$format = $reflection->getShortName(); |
202
|
1 |
|
$message = "Write {$format} format to <code>{$path}</code> in " . sprintf('%.4f', $callTime) . ' seconds'; |
203
|
|
|
|
204
|
1 |
|
$this->log($message); |
205
|
1 |
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Log a line about the read operation. |
209
|
|
|
* |
210
|
|
|
* @param string $format |
211
|
|
|
* @param string $path |
212
|
|
|
* @param float $callStartTime |
213
|
|
|
*/ |
214
|
|
|
public function logRead($format, $path, $callStartTime) |
215
|
|
|
{ |
216
|
|
|
$callEndTime = microtime(true); |
217
|
|
|
$callTime = $callEndTime - $callStartTime; |
218
|
|
|
$message = "Read {$format} format from <code>{$path}</code> in " . sprintf('%.4f', $callTime) . ' seconds'; |
219
|
|
|
|
220
|
|
|
$this->log($message); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: