ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
IOFactory.php
Go to the documentation of this file.
1<?php
30if (!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 'HTML',
74 'CSV',
75 );
76
80 private function __construct() { }
81
89 public static function getSearchLocations() {
91 } // function getSearchLocations()
92
101 public static function setSearchLocations($value) {
102 if (is_array($value)) {
103 self::$_searchLocations = $value;
104 } else {
105 throw new PHPExcel_Reader_Exception('Invalid parameter passed.');
106 }
107 } // function setSearchLocations()
108
118 public static function addSearchLocation($type = '', $location = '', $classname = '') {
119 self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
120 } // function addSearchLocation()
121
132 public static function createWriter(PHPExcel $phpExcel, $writerType = '') {
133 // Search type
134 $searchType = 'IWriter';
135
136 // Include class
137 foreach (self::$_searchLocations as $searchLocation) {
138 if ($searchLocation['type'] == $searchType) {
139 $className = str_replace('{0}', $writerType, $searchLocation['class']);
140
141 $instance = new $className($phpExcel);
142 if ($instance !== NULL) {
143 return $instance;
144 }
145 }
146 }
147
148 // Nothing found...
149 throw new PHPExcel_Reader_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
170 $instance = new $className();
171 if ($instance !== NULL) {
172 return $instance;
173 }
174 }
175 }
176
177 // Nothing found...
178 throw new PHPExcel_Reader_Exception("No $searchType found for type $readerType");
179 } // function createReader()
180
190 public static function load($pFilename) {
192 return $reader->load($pFilename);
193 } // function load()
194
204 public static function identify($pFilename) {
206 $className = get_class($reader);
207 $classType = explode('_',$className);
208 unset($reader);
209 return array_pop($classType);
210 } // function identify()
211
221 public static function createReaderForFile($pFilename) {
222
223 // First, lucky guess by inspecting file extension
224 $pathinfo = pathinfo($pFilename);
225
226 $extensionType = NULL;
227 if (isset($pathinfo['extension'])) {
228 switch (strtolower($pathinfo['extension'])) {
229 case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet
230 case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded)
231 case 'xltx': // Excel (OfficeOpenXML) Template
232 case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded)
233 $extensionType = 'Excel2007';
234 break;
235 case 'xls': // Excel (BIFF) Spreadsheet
236 case 'xlt': // Excel (BIFF) Template
237 $extensionType = 'Excel5';
238 break;
239 case 'ods': // Open/Libre Offic Calc
240 case 'ots': // Open/Libre Offic Calc Template
241 $extensionType = 'OOCalc';
242 break;
243 case 'slk':
244 $extensionType = 'SYLK';
245 break;
246 case 'xml': // Excel 2003 SpreadSheetML
247 $extensionType = 'Excel2003XML';
248 break;
249 case 'gnumeric':
250 $extensionType = 'Gnumeric';
251 break;
252 case 'htm':
253 case 'html':
254 $extensionType = 'HTML';
255 break;
256 case 'csv':
257 // Do nothing
258 // We must not try to use CSV reader since it loads
259 // all files including Excel files etc.
260 break;
261 default:
262 break;
263 }
264
265 if ($extensionType !== NULL) {
266 $reader = self::createReader($extensionType);
267 // Let's see if we are lucky
268 if (isset($reader) && $reader->canRead($pFilename)) {
269 return $reader;
270 }
271 }
272 }
273
274 // If we reach here then "lucky guess" didn't give any result
275 // Try walking through all the options in self::$_autoResolveClasses
276 foreach (self::$_autoResolveClasses as $autoResolveClass) {
277 // Ignore our original guess, we know that won't work
278 if ($autoResolveClass !== $extensionType) {
279 $reader = self::createReader($autoResolveClass);
280 if ($reader->canRead($pFilename)) {
281 return $reader;
282 }
283 }
284 }
285
286 throw new PHPExcel_Reader_Exception('Unable to identify a reader for this file');
287 } // function createReaderForFile()
288}
$location
Definition: buildRTE.php:44
An exception for terminatinating execution or to throw for unit testing.
static createWriter(PHPExcel $phpExcel, $writerType='')
Create PHPExcel_Writer_IWriter.
Definition: IOFactory.php:132
static setSearchLocations($value)
Set search locations.
Definition: IOFactory.php:101
static $_autoResolveClasses
Definition: IOFactory.php:66
static addSearchLocation($type='', $location='', $classname='')
Add search location.
Definition: IOFactory.php:118
static $_searchLocations
Definition: IOFactory.php:54
static createReader($readerType='')
Create PHPExcel_Reader_IReader.
Definition: IOFactory.php:161
static createReaderForFile($pFilename)
Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution.
Definition: IOFactory.php:221
__construct()
Private constructor for PHPExcel_IOFactory.
Definition: IOFactory.php:80
static load($pFilename)
Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution.
Definition: IOFactory.php:190
static getSearchLocations()
Get search locations.
Definition: IOFactory.php:89
static identify($pFilename)
Identify file type using automatic PHPExcel_Reader_IReader resolution.
Definition: IOFactory.php:204
defined( 'APPLICATION_ENV')||define( 'APPLICATION_ENV'
Definition: bootstrap.php:27
$type