<?php
namespace PhpOffice\PhpSpreadsheet\Helper;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
> use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
< use PhpOffice\PhpSpreadsheet\Writer\Pdf;
use RecursiveDirectoryIterator;
use RecursiveIteratorIterator;
use RecursiveRegexIterator;
use ReflectionClass;
use RegexIterator;
> use RuntimeException;
/**
* Helper class to be used in sample code.
*/
class Sample
{
/**
* Returns whether we run on CLI or browser.
*
* @return bool
*/
public function isCli()
{
return PHP_SAPI === 'cli';
}
/**
* Return the filename currently being executed.
*
* @return string
*/
public function getScriptFilename()
{
return basename($_SERVER['SCRIPT_FILENAME'], '.php');
}
/**
* Whether we are executing the index page.
*
* @return bool
*/
public function isIndex()
{
return $this->getScriptFilename() === 'index';
}
/**
* Return the page title.
*
* @return string
*/
public function getPageTitle()
{
return $this->isIndex() ? 'PHPSpreadsheet' : $this->getScriptFilename();
}
/**
* Return the page heading.
*
* @return string
*/
public function getPageHeading()
{
return $this->isIndex() ? '' : '<h1>' . str_replace('_', ' ', $this->getScriptFilename()) . '</h1>';
}
/**
* Returns an array of all known samples.
*
< * @return string[] [$name => $path]
> * @return string[][] [$name => $path]
*/
public function getSamples()
{
// Populate samples
$baseDir = realpath(__DIR__ . '/../../../samples');
> if ($baseDir === false) {
$directory = new RecursiveDirectoryIterator($baseDir);
> // @codeCoverageIgnoreStart
$iterator = new RecursiveIteratorIterator($directory);
> throw new RuntimeException('realpath returned false');
$regex = new RegexIterator($iterator, '/^.+\.php$/', RecursiveRegexIterator::GET_MATCH);
> // @codeCoverageIgnoreEnd
> }
$files = [];
foreach ($regex as $file) {
$file = str_replace(str_replace('\\', '/', $baseDir) . '/', '', str_replace('\\', '/', $file[0]));
> if (is_array($file)) {
$info = pathinfo($file);
> // @codeCoverageIgnoreStart
$category = str_replace('_', ' ', $info['dirname']);
> throw new RuntimeException('str_replace returned array');
$name = str_replace('_', ' ', preg_replace('/(|\.php)/', '', $info['filename']));
> // @codeCoverageIgnoreEnd
if (!in_array($category, ['.', 'boostrap', 'templates'])) {
> }
< $category = str_replace('_', ' ', $info['dirname']);
< $name = str_replace('_', ' ', preg_replace('/(|\.php)/', '', $info['filename']));
> $category = str_replace('_', ' ', $info['dirname'] ?? '');
> $name = str_replace('_', ' ', (string) preg_replace('/(|\.php)/', '', $info['filename']));
}
$files[$category][$name] = $file;
}
}
// Sort everything
ksort($files);
foreach ($files as &$f) {
asort($f);
}
return $files;
}
/**
* Write documents.
*
< * @param Spreadsheet $spreadsheet
* @param string $filename
* @param string[] $writers
*/
< public function write(Spreadsheet $spreadsheet, $filename, array $writers = ['Xlsx', 'Xls'])
> public function write(Spreadsheet $spreadsheet, $filename, array $writers = ['Xlsx', 'Xls']): void
{
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$spreadsheet->setActiveSheetIndex(0);
// Write documents
foreach ($writers as $writerType) {
$path = $this->getFilename($filename, mb_strtolower($writerType));
$writer = IOFactory::createWriter($spreadsheet, $writerType);
< if ($writer instanceof Pdf) {
< // PDF writer needs temporary directory
< $tempDir = $this->getTemporaryFolder();
< $writer->setTempDir($tempDir);
< }
$callStartTime = microtime(true);
$writer->save($path);
< $this->logWrite($writer, $path, $callStartTime);
> $this->logWrite($writer, $path, /** @scrutinizer ignore-type */ $callStartTime);
}
$this->logEndingNotes();
}
> protected function isDirOrMkdir(string $folder): bool
/**
> {
* Returns the temporary directory and make sure it exists.
> return \is_dir($folder) || \mkdir($folder);
*
> }
* @return string
>
*/
private function getTemporaryFolder()
{
$tempFolder = sys_get_temp_dir() . '/phpspreadsheet';
< if (!is_dir($tempFolder)) {
< if (!mkdir($tempFolder) && !is_dir($tempFolder)) {
< throw new \RuntimeException(sprintf('Directory "%s" was not created', $tempFolder));
< }
> if (!$this->isDirOrMkdir($tempFolder)) {
> throw new RuntimeException(sprintf('Directory "%s" was not created', $tempFolder));
}
return $tempFolder;
}
/**
* Returns the filename that should be used for sample output.
*
* @param string $filename
* @param string $extension
*
* @return string
*/
public function getFilename($filename, $extension = 'xlsx')
{
$originalExtension = pathinfo($filename, PATHINFO_EXTENSION);
< return $this->getTemporaryFolder() . '/' . str_replace('.' . $originalExtension, '.' . $extension, basename($filename));
> return $this->getTemporaryFolder() . '/' . str_replace('.' . /** @scrutinizer ignore-type */ $originalExtension, '.' . $extension, basename($filename));
}
/**
* Return a random temporary file name.
*
* @param string $extension
*
* @return string
*/
public function getTemporaryFilename($extension = 'xlsx')
{
$temporaryFilename = tempnam($this->getTemporaryFolder(), 'phpspreadsheet-');
> if ($temporaryFilename === false) {
unlink($temporaryFilename);
> // @codeCoverageIgnoreStart
> throw new RuntimeException('tempnam returned false');
return $temporaryFilename . '.' . $extension;
> // @codeCoverageIgnoreEnd
}
> }
< public function log($message)
> public function log(string $message): void
{
$eol = $this->isCli() ? PHP_EOL : '<br />';
echo date('H:i:s ') . $message . $eol;
}
> public function titles(string $category, string $functionName, ?string $description = null): void
/**
> {
* Log ending notes.
> $this->log(sprintf('%s Functions:', $category));
*/
> $description === null
public function logEndingNotes()
> ? $this->log(sprintf('Function: %s()', rtrim($functionName, '()')))
{
> : $this->log(sprintf('Function: %s() - %s.', rtrim($functionName, '()'), rtrim($description, '.')));
// Do not show execution time for index
> }
$this->log('Peak memory usage: ' . (memory_get_peak_usage(true) / 1024 / 1024) . 'MB');
>
}
> public function displayGrid(array $matrix): void
> {
/**
> $renderer = new TextGrid($matrix, $this->isCli());
* Log a line about the write operation.
> echo $renderer->render();
*
> }
* @param IWriter $writer
>
* @param string $path
> public function logCalculationResult(
* @param float $callStartTime
> Worksheet $worksheet,
*/
> string $functionName,
public function logWrite(IWriter $writer, $path, $callStartTime)
> string $formulaCell,
{
> ?string $descriptionCell = null
$callEndTime = microtime(true);
> ): void {
$callTime = $callEndTime - $callStartTime;
> if ($descriptionCell !== null) {
$reflection = new ReflectionClass($writer);
> $this->log($worksheet->getCell($descriptionCell)->getValue());
$format = $reflection->getShortName();
> }
$message = "Write {$format} format to <code>{$path}</code> in " . sprintf('%.4f', $callTime) . ' seconds';
> $this->log($worksheet->getCell($formulaCell)->getValue());
> $this->log(sprintf('%s() Result is ', $functionName) . $worksheet->getCell($formulaCell)->getCalculatedValue());
$this->log($message);
> }
}
>
< public function logEndingNotes()
> public function logEndingNotes(): void
< * @param IWriter $writer
< public function logWrite(IWriter $writer, $path, $callStartTime)
> public function logWrite(IWriter $writer, $path, $callStartTime): void
*
* @param string $format
* @param string $path
* @param float $callStartTime
*/
< public function logRead($format, $path, $callStartTime)
> public function logRead($format, $path, $callStartTime): void
{
$callEndTime = microtime(true);
$callTime = $callEndTime - $callStartTime;
$message = "Read {$format} format from <code>{$path}</code> in " . sprintf('%.4f', $callTime) . ' seconds';
$this->log($message);
}
}