ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Sample.php
Go to the documentation of this file.
1 <?php
2 
4 
11 use ReflectionClass;
12 use RegexIterator;
14 
18 class Sample
19 {
25  public function isCli()
26  {
27  return PHP_SAPI === 'cli';
28  }
29 
35  public function getScriptFilename()
36  {
37  return basename($_SERVER['SCRIPT_FILENAME'], '.php');
38  }
39 
45  public function isIndex()
46  {
47  return $this->getScriptFilename() === 'index';
48  }
49 
55  public function getPageTitle()
56  {
57  return $this->isIndex() ? 'PHPSpreadsheet' : $this->getScriptFilename();
58  }
59 
65  public function getPageHeading()
66  {
67  return $this->isIndex() ? '' : '<h1>' . str_replace('_', ' ', $this->getScriptFilename()) . '</h1>';
68  }
69 
75  public function getSamples()
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  }
105 
112  public function write(Spreadsheet $spreadsheet, $filename, array $writers = ['Xlsx', 'Xls']): 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  }
128 
129  protected function isDirOrMkdir(string $folder): bool
130  {
131  return \is_dir($folder) || \mkdir($folder);
132  }
133 
139  private function getTemporaryFolder()
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  }
148 
157  public function getFilename($filename, $extension = 'xlsx')
158  {
159  $originalExtension = pathinfo($filename, PATHINFO_EXTENSION);
160 
161  return $this->getTemporaryFolder() . '/' . str_replace('.' . $originalExtension, '.' . $extension, basename($filename));
162  }
163 
171  public function getTemporaryFilename($extension = 'xlsx')
172  {
173  $temporaryFilename = tempnam($this->getTemporaryFolder(), 'phpspreadsheet-');
174  unlink($temporaryFilename);
175 
176  return $temporaryFilename . '.' . $extension;
177  }
178 
179  public function log($message): void
180  {
181  $eol = $this->isCli() ? PHP_EOL : '<br />';
182  echo date('H:i:s ') . $message . $eol;
183  }
184 
188  public function logEndingNotes(): 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  }
193 
200  public function logWrite(IWriter $writer, $path, $callStartTime): 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  }
210 
218  public function logRead($format, $path, $callStartTime): 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  }
226 }
static static static createWriter(Spreadsheet $spreadsheet, $writerType)
Create Writer.
Definition: IOFactory.php:44
Helper class to be used in sample code.
Definition: Sample.php:18
$path
Definition: aliased.php:25
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
$format
Definition: metadata.php:141
$files
Definition: metarefresh.php:49
logRead($format, $path, $callStartTime)
Log a line about the read operation.
Definition: Sample.php:218
getPageHeading()
Return the page heading.
Definition: Sample.php:65
getFilename($filename, $extension='xlsx')
Returns the filename that should be used for sample output.
Definition: Sample.php:157
getTemporaryFolder()
Returns the temporary directory and make sure it exists.
Definition: Sample.php:139
PHP_EOL
Definition: complexTest.php:5
isCli()
Returns whether we run on CLI or browser.
Definition: Sample.php:25
getScriptFilename()
Return the filename currently being executed.
Definition: Sample.php:35
catch(Exception $e) $message
logWrite(IWriter $writer, $path, $callStartTime)
Log a line about the write operation.
Definition: Sample.php:200
setActiveSheetIndex($pIndex)
Set active sheet index.
write(Spreadsheet $spreadsheet, $filename, array $writers=['Xlsx', 'Xls'])
Write documents.
Definition: Sample.php:112
getPageTitle()
Return the page title.
Definition: Sample.php:55
getSamples()
Returns an array of all known samples.
Definition: Sample.php:75
$filename
Definition: buildRTE.php:89
isIndex()
Whether we are executing the index page.
Definition: Sample.php:45
getTemporaryFilename($extension='xlsx')
Return a random temporary file name.
Definition: Sample.php:171
$info
Definition: index.php:5
logEndingNotes()
Log ending notes.
Definition: Sample.php:188