ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
IOFactory.php
Go to the documentation of this file.
1 <?php
30 if (!defined('PHPEXCEL_ROOT')) {
34  define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
35  require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
36 }
37 
46 {
54  private static $_searchLocations = array(
55  array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ),
56  array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' )
57  );
58 
66  private static $_autoResolveClasses = array(
67  'Excel2007',
68  'Excel5',
69  'Excel2003XML',
70  'OOCalc',
71  'SYLK',
72  'Gnumeric',
73  'CSV',
74  );
75 
79  private function __construct() { }
80 
88  public static function getSearchLocations() {
90  } // function getSearchLocations()
91 
100  public static function setSearchLocations($value) {
101  if (is_array($value)) {
102  self::$_searchLocations = $value;
103  } else {
104  throw new Exception('Invalid parameter passed.');
105  }
106  } // function setSearchLocations()
107 
117  public static function addSearchLocation($type = '', $location = '', $classname = '') {
118  self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
119  } // function addSearchLocation()
120 
131  public static function createWriter(PHPExcel $phpExcel, $writerType = '') {
132  // Search type
133  $searchType = 'IWriter';
134 
135  // Include class
136  foreach (self::$_searchLocations as $searchLocation) {
137  if ($searchLocation['type'] == $searchType) {
138  $className = str_replace('{0}', $writerType, $searchLocation['class']);
139  $classFile = str_replace('{0}', $writerType, $searchLocation['path']);
140 
141  $instance = new $className($phpExcel);
142  if (!is_null($instance)) {
143  return $instance;
144  }
145  }
146  }
147 
148  // Nothing found...
149  throw new Exception("No $searchType found for type $writerType");
150  } // function createWriter()
151 
161  public static function createReader($readerType = '') {
162  // Search type
163  $searchType = 'IReader';
164 
165  // Include class
166  foreach (self::$_searchLocations as $searchLocation) {
167  if ($searchLocation['type'] == $searchType) {
168  $className = str_replace('{0}', $readerType, $searchLocation['class']);
169  $classFile = str_replace('{0}', $readerType, $searchLocation['path']);
170 
171  $instance = new $className();
172  if (!is_null($instance)) {
173  return $instance;
174  }
175  }
176  }
177 
178  // Nothing found...
179  throw new Exception("No $searchType found for type $readerType");
180  } // function createReader()
181 
191  public static function load($pFilename) {
192  $reader = self::createReaderForFile($pFilename);
193  return $reader->load($pFilename);
194  } // function load()
195 
205  public static function identify($pFilename) {
206  $reader = self::createReaderForFile($pFilename);
207  $className = get_class($reader);
208  $classType = explode('_',$className);
209  unset($reader);
210  return array_pop($classType);
211  } // function identify()
212 
222  public static function createReaderForFile($pFilename) {
223 
224  // First, lucky guess by inspecting file extension
225  $pathinfo = pathinfo($pFilename);
226 
227  if (isset($pathinfo['extension'])) {
228  switch (strtolower($pathinfo['extension'])) {
229  case 'xlsx':
230  $reader = self::createReader('Excel2007');
231  break;
232  case 'xls':
233  $reader = self::createReader('Excel5');
234  break;
235  case 'ods':
236  $reader = self::createReader('OOCalc');
237  break;
238  case 'slk':
239  $reader = self::createReader('SYLK');
240  break;
241  case 'xml':
242  $reader = self::createReader('Excel2003XML');
243  break;
244  case 'gnumeric':
245  $reader = self::createReader('Gnumeric');
246  break;
247  case 'csv':
248  // Do nothing
249  // We must not try to use CSV reader since it loads
250  // all files including Excel files etc.
251  break;
252  default:
253  break;
254  }
255 
256  // Let's see if we are lucky
257  if (isset($reader) && $reader->canRead($pFilename)) {
258  return $reader;
259  }
260 
261  }
262 
263  // If we reach here then "lucky guess" didn't give any result
264 
265  // Try loading using self::$_autoResolveClasses
266  foreach (self::$_autoResolveClasses as $autoResolveClass) {
267  $reader = self::createReader($autoResolveClass);
268  if ($reader->canRead($pFilename)) {
269  return $reader;
270  }
271  }
272 
273  } // function createReaderForFile()
274 }