IOFactory.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2013 PHPExcel
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * @category PHPExcel
  22. * @package PHPExcel
  23. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.9, 2013-06-02
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
  33. require(PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  34. }
  35. /**
  36. * PHPExcel_IOFactory
  37. *
  38. * @category PHPExcel
  39. * @package PHPExcel
  40. * @copyright Copyright (c) 2006 - 2013 PHPExcel (http://www.codeplex.com/PHPExcel)
  41. */
  42. class PHPExcel_IOFactory
  43. {
  44. /**
  45. * Search locations
  46. *
  47. * @var array
  48. * @access private
  49. * @static
  50. */
  51. private static $_searchLocations = array(
  52. array( 'type' => 'IWriter', 'path' => 'PHPExcel/Writer/{0}.php', 'class' => 'PHPExcel_Writer_{0}' ),
  53. array( 'type' => 'IReader', 'path' => 'PHPExcel/Reader/{0}.php', 'class' => 'PHPExcel_Reader_{0}' )
  54. );
  55. /**
  56. * Autoresolve classes
  57. *
  58. * @var array
  59. * @access private
  60. * @static
  61. */
  62. private static $_autoResolveClasses = array(
  63. 'Excel2007',
  64. 'Excel5',
  65. 'Excel2003XML',
  66. 'OOCalc',
  67. 'SYLK',
  68. 'Gnumeric',
  69. 'HTML',
  70. 'CSV',
  71. );
  72. /**
  73. * Private constructor for PHPExcel_IOFactory
  74. */
  75. private function __construct() { }
  76. /**
  77. * Get search locations
  78. *
  79. * @static
  80. * @access public
  81. * @return array
  82. */
  83. public static function getSearchLocations() {
  84. return self::$_searchLocations;
  85. } // function getSearchLocations()
  86. /**
  87. * Set search locations
  88. *
  89. * @static
  90. * @access public
  91. * @param array $value
  92. * @throws PHPExcel_Reader_Exception
  93. */
  94. public static function setSearchLocations($value) {
  95. if (is_array($value)) {
  96. self::$_searchLocations = $value;
  97. } else {
  98. throw new PHPExcel_Reader_Exception('Invalid parameter passed.');
  99. }
  100. } // function setSearchLocations()
  101. /**
  102. * Add search location
  103. *
  104. * @static
  105. * @access public
  106. * @param string $type Example: IWriter
  107. * @param string $location Example: PHPExcel/Writer/{0}.php
  108. * @param string $classname Example: PHPExcel_Writer_{0}
  109. */
  110. public static function addSearchLocation($type = '', $location = '', $classname = '') {
  111. self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
  112. } // function addSearchLocation()
  113. /**
  114. * Create PHPExcel_Writer_IWriter
  115. *
  116. * @static
  117. * @access public
  118. * @param PHPExcel $phpExcel
  119. * @param string $writerType Example: Excel2007
  120. * @return PHPExcel_Writer_IWriter
  121. * @throws PHPExcel_Reader_Exception
  122. */
  123. public static function createWriter(PHPExcel $phpExcel, $writerType = '') {
  124. // Search type
  125. $searchType = 'IWriter';
  126. // Include class
  127. foreach (self::$_searchLocations as $searchLocation) {
  128. if ($searchLocation['type'] == $searchType) {
  129. $className = str_replace('{0}', $writerType, $searchLocation['class']);
  130. $instance = new $className($phpExcel);
  131. if ($instance !== NULL) {
  132. return $instance;
  133. }
  134. }
  135. }
  136. // Nothing found...
  137. throw new PHPExcel_Reader_Exception("No $searchType found for type $writerType");
  138. } // function createWriter()
  139. /**
  140. * Create PHPExcel_Reader_IReader
  141. *
  142. * @static
  143. * @access public
  144. * @param string $readerType Example: Excel2007
  145. * @return PHPExcel_Reader_IReader
  146. * @throws PHPExcel_Reader_Exception
  147. */
  148. public static function createReader($readerType = '') {
  149. // Search type
  150. $searchType = 'IReader';
  151. // Include class
  152. foreach (self::$_searchLocations as $searchLocation) {
  153. if ($searchLocation['type'] == $searchType) {
  154. $className = str_replace('{0}', $readerType, $searchLocation['class']);
  155. $instance = new $className();
  156. if ($instance !== NULL) {
  157. return $instance;
  158. }
  159. }
  160. }
  161. // Nothing found...
  162. throw new PHPExcel_Reader_Exception("No $searchType found for type $readerType");
  163. } // function createReader()
  164. /**
  165. * Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
  166. *
  167. * @static
  168. * @access public
  169. * @param string $pFilename The name of the spreadsheet file
  170. * @return PHPExcel
  171. * @throws PHPExcel_Reader_Exception
  172. */
  173. public static function load($pFilename) {
  174. $reader = self::createReaderForFile($pFilename);
  175. return $reader->load($pFilename);
  176. } // function load()
  177. /**
  178. * Identify file type using automatic PHPExcel_Reader_IReader resolution
  179. *
  180. * @static
  181. * @access public
  182. * @param string $pFilename The name of the spreadsheet file to identify
  183. * @return string
  184. * @throws PHPExcel_Reader_Exception
  185. */
  186. public static function identify($pFilename) {
  187. $reader = self::createReaderForFile($pFilename);
  188. $className = get_class($reader);
  189. $classType = explode('_',$className);
  190. unset($reader);
  191. return array_pop($classType);
  192. } // function identify()
  193. /**
  194. * Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution
  195. *
  196. * @static
  197. * @access public
  198. * @param string $pFilename The name of the spreadsheet file
  199. * @return PHPExcel_Reader_IReader
  200. * @throws PHPExcel_Reader_Exception
  201. */
  202. public static function createReaderForFile($pFilename) {
  203. // First, lucky guess by inspecting file extension
  204. $pathinfo = pathinfo($pFilename);
  205. $extensionType = NULL;
  206. if (isset($pathinfo['extension'])) {
  207. switch (strtolower($pathinfo['extension'])) {
  208. case 'xlsx': // Excel (OfficeOpenXML) Spreadsheet
  209. case 'xlsm': // Excel (OfficeOpenXML) Macro Spreadsheet (macros will be discarded)
  210. case 'xltx': // Excel (OfficeOpenXML) Template
  211. case 'xltm': // Excel (OfficeOpenXML) Macro Template (macros will be discarded)
  212. $extensionType = 'Excel2007';
  213. break;
  214. case 'xls': // Excel (BIFF) Spreadsheet
  215. case 'xlt': // Excel (BIFF) Template
  216. $extensionType = 'Excel5';
  217. break;
  218. case 'ods': // Open/Libre Offic Calc
  219. case 'ots': // Open/Libre Offic Calc Template
  220. $extensionType = 'OOCalc';
  221. break;
  222. case 'slk':
  223. $extensionType = 'SYLK';
  224. break;
  225. case 'xml': // Excel 2003 SpreadSheetML
  226. $extensionType = 'Excel2003XML';
  227. break;
  228. case 'gnumeric':
  229. $extensionType = 'Gnumeric';
  230. break;
  231. case 'htm':
  232. case 'html':
  233. $extensionType = 'HTML';
  234. break;
  235. case 'csv':
  236. // Do nothing
  237. // We must not try to use CSV reader since it loads
  238. // all files including Excel files etc.
  239. break;
  240. default:
  241. break;
  242. }
  243. if ($extensionType !== NULL) {
  244. $reader = self::createReader($extensionType);
  245. // Let's see if we are lucky
  246. if (isset($reader) && $reader->canRead($pFilename)) {
  247. return $reader;
  248. }
  249. }
  250. }
  251. // If we reach here then "lucky guess" didn't give any result
  252. // Try walking through all the options in self::$_autoResolveClasses
  253. foreach (self::$_autoResolveClasses as $autoResolveClass) {
  254. // Ignore our original guess, we know that won't work
  255. if ($autoResolveClass !== $extensionType) {
  256. $reader = self::createReader($autoResolveClass);
  257. if ($reader->canRead($pFilename)) {
  258. return $reader;
  259. }
  260. }
  261. }
  262. throw new PHPExcel_Reader_Exception('Unable to identify a reader for this file');
  263. } // function createReaderForFile()
  264. }