PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Classes/PHPExcel/IOFactory.php

#
PHP | 282 lines | 120 code | 25 blank | 137 comment | 17 complexity | f3449100a2f550fea601ef37313860c7 MD5 | raw file
Possible License(s): AGPL-1.0, LGPL-2.0, LGPL-2.1, GPL-3.0, LGPL-3.0
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2012 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 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version ##VERSION##, ##DATE##
  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 - 2012 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 Exception
  93. */
  94. public static function setSearchLocations($value) {
  95. if (is_array($value)) {
  96. self::$_searchLocations = $value;
  97. } else {
  98. throw new 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 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 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 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 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 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 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 Exception
  201. */
  202. public static function createReaderForFile($pFilename) {
  203. // First, lucky guess by inspecting file extension
  204. $pathinfo = pathinfo($pFilename);
  205. if (isset($pathinfo['extension'])) {
  206. switch (strtolower($pathinfo['extension'])) {
  207. case 'xlsx':
  208. $extensionType = 'Excel2007';
  209. break;
  210. case 'xls':
  211. case 'xlsm':
  212. $extensionType = 'Excel5';
  213. break;
  214. case 'ods':
  215. $extensionType = 'OOCalc';
  216. break;
  217. case 'slk':
  218. $extensionType = 'SYLK';
  219. break;
  220. case 'xml':
  221. $extensionType = 'Excel2003XML';
  222. break;
  223. case 'gnumeric':
  224. $extensionType = 'Gnumeric';
  225. break;
  226. case 'htm':
  227. case 'html':
  228. $extensionType = 'HTML';
  229. break;
  230. case 'csv':
  231. // Do nothing
  232. // We must not try to use CSV reader since it loads
  233. // all files including Excel files etc.
  234. break;
  235. default:
  236. break;
  237. }
  238. $reader = self::createReader($extensionType);
  239. // Let's see if we are lucky
  240. if (isset($reader) && $reader->canRead($pFilename)) {
  241. return $reader;
  242. }
  243. }
  244. // If we reach here then "lucky guess" didn't give any result
  245. // Try walking through all the options in self::$_autoResolveClasses
  246. foreach (self::$_autoResolveClasses as $autoResolveClass) {
  247. // Ignore our original guess, we know that won't work
  248. if ($reader !== $extensionType) {
  249. $reader = self::createReader($autoResolveClass);
  250. if ($reader->canRead($pFilename)) {
  251. return $reader;
  252. }
  253. }
  254. }
  255. throw new Exception('Unable to identify a reader for this file');
  256. } // function createReaderForFile()
  257. }