ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
IOFactory.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
13 abstract class IOFactory
14 {
15  private static $readers = [
16  'Xlsx' => Reader\Xlsx::class,
17  'Xls' => Reader\Xls::class,
18  'Xml' => Reader\Xml::class,
19  'Ods' => Reader\Ods::class,
20  'Slk' => Reader\Slk::class,
21  'Gnumeric' => Reader\Gnumeric::class,
22  'Html' => Reader\Html::class,
23  'Csv' => Reader\Csv::class,
24  ];
25 
26  private static $writers = [
27  'Xls' => Writer\Xls::class,
28  'Xlsx' => Writer\Xlsx::class,
29  'Ods' => Writer\Ods::class,
30  'Csv' => Writer\Csv::class,
31  'Html' => Writer\Html::class,
32  'Tcpdf' => Writer\Pdf\Tcpdf::class,
33  'Dompdf' => Writer\Pdf\Dompdf::class,
34  'Mpdf' => Writer\Pdf\Mpdf::class,
35  ];
36 
44  public static function createWriter(Spreadsheet $spreadsheet, $writerType)
45  {
46  if (!isset(self::$writers[$writerType])) {
47  throw new Writer\Exception("No writer found for type $writerType");
48  }
49 
50  // Instantiate writer
51  $className = self::$writers[$writerType];
52 
53  return new $className($spreadsheet);
54  }
55 
63  public static function createReader($readerType)
64  {
65  if (!isset(self::$readers[$readerType])) {
66  throw new Reader\Exception("No reader found for type $readerType");
67  }
68 
69  // Instantiate reader
70  $className = self::$readers[$readerType];
71 
72  return new $className();
73  }
74 
82  public static function load($pFilename)
83  {
84  $reader = self::createReaderForFile($pFilename);
85 
86  return $reader->load($pFilename);
87  }
88 
96  public static function identify($pFilename)
97  {
98  $reader = self::createReaderForFile($pFilename);
99  $className = get_class($reader);
100  $classType = explode('\\', $className);
101  unset($reader);
102 
103  return array_pop($classType);
104  }
105 
113  public static function createReaderForFile($filename)
114  {
116 
117  // First, lucky guess by inspecting file extension
118  $guessedReader = self::getReaderTypeFromExtension($filename);
119  if ($guessedReader !== null) {
120  $reader = self::createReader($guessedReader);
121 
122  // Let's see if we are lucky
123  if ($reader->canRead($filename)) {
124  return $reader;
125  }
126  }
127 
128  // If we reach here then "lucky guess" didn't give any result
129  // Try walking through all the options in self::$autoResolveClasses
130  foreach (self::$readers as $type => $class) {
131  // Ignore our original guess, we know that won't work
132  if ($type !== $guessedReader) {
133  $reader = self::createReader($type);
134  if ($reader->canRead($filename)) {
135  return $reader;
136  }
137  }
138  }
139 
140  throw new Reader\Exception('Unable to identify a reader for this file');
141  }
142 
150  private static function getReaderTypeFromExtension($filename)
151  {
152  $pathinfo = pathinfo($filename);
153  if (!isset($pathinfo['extension'])) {
154  return null;
155  }
156 
157  switch (strtolower($pathinfo['extension'])) {
158  case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet
159  case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded)
160  case 'xltx': // Excel (OfficeOpenXML) Template
161  case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded)
162  return 'Xlsx';
163  case 'xls': // Excel (BIFF) Spreadsheet
164  case 'xlt': // Excel (BIFF) Template
165  return 'Xls';
166  case 'ods': // Open/Libre Offic Calc
167  case 'ots': // Open/Libre Offic Calc Template
168  return 'Ods';
169  case 'slk':
170  return 'Slk';
171  case 'xml': // Excel 2003 SpreadSheetML
172  return 'Xml';
173  case 'gnumeric':
174  return 'Gnumeric';
175  case 'htm':
176  case 'html':
177  return 'Html';
178  case 'csv':
179  // Do nothing
180  // We must not try to use CSV reader since it loads
181  // all files including Excel files etc.
182  return null;
183  default:
184  return null;
185  }
186  }
187 
194  public static function registerWriter($writerType, $writerClass): void
195  {
196  if (!is_a($writerClass, Writer\IWriter::class, true)) {
197  throw new Writer\Exception('Registered writers must implement ' . Writer\IWriter::class);
198  }
199 
200  self::$writers[$writerType] = $writerClass;
201  }
202 
209  public static function registerReader($readerType, $readerClass): void
210  {
211  if (!is_a($readerClass, Reader\IReader::class, true)) {
212  throw new Reader\Exception('Registered readers must implement ' . Reader\IReader::class);
213  }
214 
215  self::$readers[$readerType] = $readerClass;
216  }
217 }
static static static createWriter(Spreadsheet $spreadsheet, $writerType)
Create Writer.
Definition: IOFactory.php:44
static registerReader($readerType, $readerClass)
Register a reader with its type and class name.
Definition: IOFactory.php:209
static getReaderTypeFromExtension($filename)
Guess a reader type from the file extension, if any.
Definition: IOFactory.php:150
$type
static load($pFilename)
Loads Spreadsheet from file using automatic Reader resolution.
Definition: IOFactory.php:82
static createReader($readerType)
Create Reader.
Definition: IOFactory.php:63
Factory to create readers and writers easily.
Definition: IOFactory.php:13
static createReaderForFile($filename)
Create Reader for file using automatic Reader resolution.
Definition: IOFactory.php:113
$filename
Definition: buildRTE.php:89
static registerWriter($writerType, $writerClass)
Register a writer with its type and class name.
Definition: IOFactory.php:194
static identify($pFilename)
Identify file type using automatic Reader resolution.
Definition: IOFactory.php:96
static assertFile($filename)
Assert that given path is an existing file and is readable, otherwise throw exception.
Definition: File.php:143