ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PhpOffice\PhpSpreadsheet\Helper\Sample Class Reference

Helper class to be used in sample code. More...

+ Collaboration diagram for PhpOffice\PhpSpreadsheet\Helper\Sample:

Public Member Functions

 isCli ()
 Returns whether we run on CLI or browser. More...
 
 getScriptFilename ()
 Return the filename currently being executed. More...
 
 isIndex ()
 Whether we are executing the index page. More...
 
 getPageTitle ()
 Return the page title. More...
 
 getPageHeading ()
 Return the page heading. More...
 
 getSamples ()
 Returns an array of all known samples. More...
 
 write (Spreadsheet $spreadsheet, $filename, array $writers=['Xlsx', 'Xls'])
 Write documents. More...
 
 getFilename ($filename, $extension='xlsx')
 Returns the filename that should be used for sample output. More...
 
 getTemporaryFilename ($extension='xlsx')
 Return a random temporary file name. More...
 
 log ($message)
 
 logEndingNotes ()
 Log ending notes. More...
 
 logWrite (IWriter $writer, $path, $callStartTime)
 Log a line about the write operation. More...
 
 logRead ($format, $path, $callStartTime)
 Log a line about the read operation. More...
 

Protected Member Functions

 isDirOrMkdir (string $folder)
 

Private Member Functions

 getTemporaryFolder ()
 Returns the temporary directory and make sure it exists. More...
 

Detailed Description

Helper class to be used in sample code.

Definition at line 18 of file Sample.php.

Member Function Documentation

◆ getFilename()

PhpOffice\PhpSpreadsheet\Helper\Sample::getFilename (   $filename,
  $extension = 'xlsx' 
)

Returns the filename that should be used for sample output.

Parameters
string$filename
string$extension
Returns
string

Definition at line 157 of file Sample.php.

References $filename, and PhpOffice\PhpSpreadsheet\Helper\Sample\getTemporaryFolder().

Referenced by PhpOffice\PhpSpreadsheet\Helper\Sample\write().

158  {
159  $originalExtension = pathinfo($filename, PATHINFO_EXTENSION);
160 
161  return $this->getTemporaryFolder() . '/' . str_replace('.' . $originalExtension, '.' . $extension, basename($filename));
162  }
getTemporaryFolder()
Returns the temporary directory and make sure it exists.
Definition: Sample.php:139
$filename
Definition: buildRTE.php:89
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getPageHeading()

PhpOffice\PhpSpreadsheet\Helper\Sample::getPageHeading ( )

Return the page heading.

Returns
string

Definition at line 65 of file Sample.php.

References PhpOffice\PhpSpreadsheet\Helper\Sample\getScriptFilename(), and PhpOffice\PhpSpreadsheet\Helper\Sample\isIndex().

66  {
67  return $this->isIndex() ? '' : '<h1>' . str_replace('_', ' ', $this->getScriptFilename()) . '</h1>';
68  }
getScriptFilename()
Return the filename currently being executed.
Definition: Sample.php:35
isIndex()
Whether we are executing the index page.
Definition: Sample.php:45
+ Here is the call graph for this function:

◆ getPageTitle()

PhpOffice\PhpSpreadsheet\Helper\Sample::getPageTitle ( )

Return the page title.

Returns
string

Definition at line 55 of file Sample.php.

References PhpOffice\PhpSpreadsheet\Helper\Sample\getScriptFilename(), and PhpOffice\PhpSpreadsheet\Helper\Sample\isIndex().

56  {
57  return $this->isIndex() ? 'PHPSpreadsheet' : $this->getScriptFilename();
58  }
getScriptFilename()
Return the filename currently being executed.
Definition: Sample.php:35
isIndex()
Whether we are executing the index page.
Definition: Sample.php:45
+ Here is the call graph for this function:

◆ getSamples()

PhpOffice\PhpSpreadsheet\Helper\Sample::getSamples ( )

Returns an array of all known samples.

Returns
string[][] [$name => $path]

Definition at line 75 of file Sample.php.

References $baseDir, $f, $files, $info, and $name.

76  {
77  // Populate samples
78  $baseDir = realpath(__DIR__ . '/../../../samples');
79  $directory = new RecursiveDirectoryIterator($baseDir);
80  $iterator = new RecursiveIteratorIterator($directory);
81  $regex = new RegexIterator($iterator, '/^.+\.php$/', RecursiveRegexIterator::GET_MATCH);
82 
83  $files = [];
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])) {
91  $files[$category] = [];
92  }
93  $files[$category][$name] = $file;
94  }
95  }
96 
97  // Sort everything
98  ksort($files);
99  foreach ($files as &$f) {
100  asort($f);
101  }
102 
103  return $files;
104  }
$files
Definition: metarefresh.php:49
$info
Definition: index.php:5

◆ getScriptFilename()

PhpOffice\PhpSpreadsheet\Helper\Sample::getScriptFilename ( )

Return the filename currently being executed.

Returns
string

Definition at line 35 of file Sample.php.

References $_SERVER.

Referenced by PhpOffice\PhpSpreadsheet\Helper\Sample\getPageHeading(), PhpOffice\PhpSpreadsheet\Helper\Sample\getPageTitle(), and PhpOffice\PhpSpreadsheet\Helper\Sample\isIndex().

36  {
37  return basename($_SERVER['SCRIPT_FILENAME'], '.php');
38  }
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
+ Here is the caller graph for this function:

◆ getTemporaryFilename()

PhpOffice\PhpSpreadsheet\Helper\Sample::getTemporaryFilename (   $extension = 'xlsx')

Return a random temporary file name.

Parameters
string$extension
Returns
string

Definition at line 171 of file Sample.php.

References PhpOffice\PhpSpreadsheet\Helper\Sample\getTemporaryFolder().

172  {
173  $temporaryFilename = tempnam($this->getTemporaryFolder(), 'phpspreadsheet-');
174  unlink($temporaryFilename);
175 
176  return $temporaryFilename . '.' . $extension;
177  }
getTemporaryFolder()
Returns the temporary directory and make sure it exists.
Definition: Sample.php:139
+ Here is the call graph for this function:

◆ getTemporaryFolder()

PhpOffice\PhpSpreadsheet\Helper\Sample::getTemporaryFolder ( )
private

Returns the temporary directory and make sure it exists.

Returns
string

Definition at line 139 of file Sample.php.

References PhpOffice\PhpSpreadsheet\Helper\Sample\isDirOrMkdir().

Referenced by PhpOffice\PhpSpreadsheet\Helper\Sample\getFilename(), and PhpOffice\PhpSpreadsheet\Helper\Sample\getTemporaryFilename().

140  {
141  $tempFolder = sys_get_temp_dir() . '/phpspreadsheet';
142  if (!$this->isDirOrMkdir($tempFolder)) {
143  throw new RuntimeException(sprintf('Directory "%s" was not created', $tempFolder));
144  }
145 
146  return $tempFolder;
147  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isCli()

PhpOffice\PhpSpreadsheet\Helper\Sample::isCli ( )

Returns whether we run on CLI or browser.

Returns
bool

Definition at line 25 of file Sample.php.

Referenced by PhpOffice\PhpSpreadsheet\Helper\Sample\log().

26  {
27  return PHP_SAPI === 'cli';
28  }
+ Here is the caller graph for this function:

◆ isDirOrMkdir()

PhpOffice\PhpSpreadsheet\Helper\Sample::isDirOrMkdir ( string  $folder)
protected

Definition at line 129 of file Sample.php.

Referenced by PhpOffice\PhpSpreadsheet\Helper\Sample\getTemporaryFolder().

129  : bool
130  {
131  return \is_dir($folder) || \mkdir($folder);
132  }
+ Here is the caller graph for this function:

◆ isIndex()

PhpOffice\PhpSpreadsheet\Helper\Sample::isIndex ( )

Whether we are executing the index page.

Returns
bool

Definition at line 45 of file Sample.php.

References PhpOffice\PhpSpreadsheet\Helper\Sample\getScriptFilename().

Referenced by PhpOffice\PhpSpreadsheet\Helper\Sample\getPageHeading(), and PhpOffice\PhpSpreadsheet\Helper\Sample\getPageTitle().

46  {
47  return $this->getScriptFilename() === 'index';
48  }
getScriptFilename()
Return the filename currently being executed.
Definition: Sample.php:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ log()

PhpOffice\PhpSpreadsheet\Helper\Sample::log (   $message)

Definition at line 179 of file Sample.php.

References $message, PhpOffice\PhpSpreadsheet\Helper\Sample\isCli(), and PHP_EOL.

Referenced by PhpOffice\PhpSpreadsheet\Helper\Sample\logEndingNotes(), PhpOffice\PhpSpreadsheet\Helper\Sample\logRead(), and PhpOffice\PhpSpreadsheet\Helper\Sample\logWrite().

179  : void
180  {
181  $eol = $this->isCli() ? PHP_EOL : '<br />';
182  echo date('H:i:s ') . $message . $eol;
183  }
PHP_EOL
Definition: complexTest.php:5
isCli()
Returns whether we run on CLI or browser.
Definition: Sample.php:25
catch(Exception $e) $message
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ logEndingNotes()

PhpOffice\PhpSpreadsheet\Helper\Sample::logEndingNotes ( )

Log ending notes.

Definition at line 188 of file Sample.php.

References PhpOffice\PhpSpreadsheet\Helper\Sample\log().

Referenced by PhpOffice\PhpSpreadsheet\Helper\Sample\write().

188  : void
189  {
190  // Do not show execution time for index
191  $this->log('Peak memory usage: ' . (memory_get_peak_usage(true) / 1024 / 1024) . 'MB');
192  }
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ logRead()

PhpOffice\PhpSpreadsheet\Helper\Sample::logRead (   $format,
  $path,
  $callStartTime 
)

Log a line about the read operation.

Parameters
string$format
string$path
float$callStartTime

Definition at line 218 of file Sample.php.

References $message, and PhpOffice\PhpSpreadsheet\Helper\Sample\log().

218  : void
219  {
220  $callEndTime = microtime(true);
221  $callTime = $callEndTime - $callStartTime;
222  $message = "Read {$format} format from <code>{$path}</code> in " . sprintf('%.4f', $callTime) . ' seconds';
223 
224  $this->log($message);
225  }
catch(Exception $e) $message
+ Here is the call graph for this function:

◆ logWrite()

PhpOffice\PhpSpreadsheet\Helper\Sample::logWrite ( IWriter  $writer,
  $path,
  $callStartTime 
)

Log a line about the write operation.

Parameters
string$path
float$callStartTime

Definition at line 200 of file Sample.php.

References $format, $message, and PhpOffice\PhpSpreadsheet\Helper\Sample\log().

Referenced by PhpOffice\PhpSpreadsheet\Helper\Sample\write().

200  : void
201  {
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';
207 
208  $this->log($message);
209  }
$format
Definition: metadata.php:141
catch(Exception $e) $message
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ write()

PhpOffice\PhpSpreadsheet\Helper\Sample::write ( Spreadsheet  $spreadsheet,
  $filename,
array  $writers = ['Xlsx',
'Xls']   
)

Write documents.

Parameters
string$filename
string[]$writers

Definition at line 112 of file Sample.php.

References $filename, $path, PhpOffice\PhpSpreadsheet\IOFactory\createWriter(), PhpOffice\PhpSpreadsheet\Helper\Sample\getFilename(), PhpOffice\PhpSpreadsheet\Helper\Sample\logEndingNotes(), PhpOffice\PhpSpreadsheet\Helper\Sample\logWrite(), and PhpOffice\PhpSpreadsheet\Spreadsheet\setActiveSheetIndex().

112  : void
113  {
114  // Set active sheet index to the first sheet, so Excel opens this as the first sheet
115  $spreadsheet->setActiveSheetIndex(0);
116 
117  // Write documents
118  foreach ($writers as $writerType) {
119  $path = $this->getFilename($filename, mb_strtolower($writerType));
120  $writer = IOFactory::createWriter($spreadsheet, $writerType);
121  $callStartTime = microtime(true);
122  $writer->save($path);
123  $this->logWrite($writer, $path, $callStartTime);
124  }
125 
126  $this->logEndingNotes();
127  }
static static static createWriter(Spreadsheet $spreadsheet, $writerType)
Create Writer.
Definition: IOFactory.php:44
$path
Definition: aliased.php:25
getFilename($filename, $extension='xlsx')
Returns the filename that should be used for sample output.
Definition: Sample.php:157
logWrite(IWriter $writer, $path, $callStartTime)
Log a line about the write operation.
Definition: Sample.php:200
$filename
Definition: buildRTE.php:89
logEndingNotes()
Log ending notes.
Definition: Sample.php:188
+ Here is the call graph for this function:

The documentation for this class was generated from the following file: