ILIAS  Release_4_0_x_branch Revision 61816
 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 }
36 
38 require_once PHPEXCEL_ROOT . 'PHPExcel.php';
39 
41 require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/IWriter.php';
42 
44 require_once PHPEXCEL_ROOT . 'PHPExcel/Reader/IReader.php';
45 
46 
55 {
61  private static $_searchLocations = array(
62  array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ),
63  array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' )
64  );
65 
71  private static $_autoResolveClasses = array(
72  'Excel2007',
73  'Excel5',
74  'Serialized',
75  'CSV'
76  );
77 
81  private function __construct() { }
82 
88  public static function getSearchLocations() {
90  }
91 
98  public static function setSearchLocations($value) {
99  if (is_array($value)) {
100  self::$_searchLocations = $value;
101  } else {
102  throw new Exception('Invalid parameter passed.');
103  }
104  }
105 
113  public static function addSearchLocation($type = '', $location = '', $classname = '') {
114  self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
115  }
116 
124  public static function createWriter(PHPExcel $phpExcel, $writerType = '') {
125  // Search type
126  $searchType = 'IWriter';
127 
128  // Include class
129  foreach (self::$_searchLocations as $searchLocation) {
130  if ($searchLocation['type'] == $searchType) {
131  $className = str_replace('{0}', $writerType, $searchLocation['class']);
132  $classFile = str_replace('{0}', $writerType, $searchLocation['path']);
133 
134  if (!class_exists($className)) {
135  require_once PHPEXCEL_ROOT . $classFile;
136  }
137 
138  $instance = new $className($phpExcel);
139  if (!is_null($instance)) {
140  return $instance;
141  }
142  }
143  }
144 
145  // Nothing found...
146  throw new Exception("No $searchType found for type $writerType");
147  }
148 
155  public static function createReader($readerType = '') {
156  // Search type
157  $searchType = 'IReader';
158 
159  // Include class
160  foreach (self::$_searchLocations as $searchLocation) {
161  if ($searchLocation['type'] == $searchType) {
162  $className = str_replace('{0}', $readerType, $searchLocation['class']);
163  $classFile = str_replace('{0}', $readerType, $searchLocation['path']);
164 
165  if (!class_exists($className)) {
166  require_once PHPEXCEL_ROOT . $classFile;
167  }
168 
169  $instance = new $className();
170  if (!is_null($instance)) {
171  return $instance;
172  }
173  }
174  }
175 
176  // Nothing found...
177  throw new Exception("No $searchType found for type $readerType");
178  }
179 
186  public static function load($pFilename) {
187  $reader = self::createReaderForFile($pFilename);
188  return $reader->load($pFilename);
189  }
190 
198  public static function createReaderForFile($pFilename) {
199  // Try loading using self::$_autoResolveClasses
200  foreach (self::$_autoResolveClasses as $autoResolveClass) {
201  $reader = self::createReader($autoResolveClass);
202  if ($reader->canRead($pFilename)) {
203  return $reader;
204  }
205  }
206 
207  throw new Exception("Could not automatically determine PHPExcel_Reader_IReader for file.");
208  }
209 }