8use RecursiveDirectoryIterator;
9use RecursiveIteratorIterator;
10use RecursiveRegexIterator;
27 return PHP_SAPI ===
'cli';
37 return basename(
$_SERVER[
'SCRIPT_FILENAME'],
'.php');
78 $baseDir = realpath(__DIR__ .
'/../../../samples');
79 $directory =
new RecursiveDirectoryIterator(
$baseDir);
80 $iterator =
new RecursiveIteratorIterator($directory);
81 $regex =
new RegexIterator($iterator,
'/^.+\.php$/', RecursiveRegexIterator::GET_MATCH);
84 foreach ($regex as $file) {
85 $file = str_replace(str_replace(
'\\',
'/',
$baseDir) .
'/',
'', str_replace(
'\\',
'/', $file[0]));
86 $info = pathinfo($file);
87 $category = str_replace(
'_',
' ',
$info[
'dirname']);
88 $name = str_replace(
'_',
' ', preg_replace(
'/(|\.php)/',
'',
$info[
'filename']));
89 if (!in_array($category, [
'.',
'boostrap',
'templates'])) {
90 if (!isset(
$files[$category])) {
118 foreach ($writers as $writerType) {
121 $callStartTime = microtime(
true);
122 $writer->save(
$path);
131 return \is_dir($folder) || \mkdir($folder);
141 $tempFolder = sys_get_temp_dir() .
'/phpspreadsheet';
143 throw new RuntimeException(sprintf(
'Directory "%s" was not created', $tempFolder));
159 $originalExtension = pathinfo(
$filename, PATHINFO_EXTENSION);
174 unlink($temporaryFilename);
176 return $temporaryFilename .
'.' . $extension;
182 echo date(
'H:i:s ') .
$message . $eol;
191 $this->
log(
'Peak memory usage: ' . (memory_get_peak_usage(
true) / 1024 / 1024) .
'MB');
202 $callEndTime = microtime(
true);
203 $callTime = $callEndTime - $callStartTime;
204 $reflection =
new ReflectionClass($writer);
205 $format = $reflection->getShortName();
206 $message =
"Write {$format} format to <code>{$path}</code> in " . sprintf(
'%.4f', $callTime) .
' seconds';
220 $callEndTime = microtime(
true);
221 $callTime = $callEndTime - $callStartTime;
222 $message =
"Read {$format} format from <code>{$path}</code> in " . sprintf(
'%.4f', $callTime) .
' seconds';
An exception for terminatinating execution or to throw for unit testing.
Helper class to be used in sample code.
isIndex()
Whether we are executing the index page.
logEndingNotes()
Log ending notes.
write(Spreadsheet $spreadsheet, $filename, array $writers=['Xlsx', 'Xls'])
Write documents.
isCli()
Returns whether we run on CLI or browser.
getSamples()
Returns an array of all known samples.
getPageTitle()
Return the page title.
getScriptFilename()
Return the filename currently being executed.
getTemporaryFolder()
Returns the temporary directory and make sure it exists.
getTemporaryFilename($extension='xlsx')
Return a random temporary file name.
logWrite(IWriter $writer, $path, $callStartTime)
Log a line about the write operation.
logRead($format, $path, $callStartTime)
Log a line about the read operation.
getFilename($filename, $extension='xlsx')
Returns the filename that should be used for sample output.
isDirOrMkdir(string $folder)
getPageHeading()
Return the page heading.
Factory to create readers and writers easily.
static createWriter(Spreadsheet $spreadsheet, $writerType)
Create Writer\IWriter.
setActiveSheetIndex($pIndex)
Set active sheet index.
catch(Exception $e) $message
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']