IOFactory.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2011 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 - 2011 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.6, 2011-02-27
  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 - 2011 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. 'CSV',
  70. );
  71. /**
  72. * Private constructor for PHPExcel_IOFactory
  73. */
  74. private function __construct() { }
  75. /**
  76. * Get search locations
  77. *
  78. * @static
  79. * @access public
  80. * @return array
  81. */
  82. public static function getSearchLocations() {
  83. return self::$_searchLocations;
  84. } // function getSearchLocations()
  85. /**
  86. * Set search locations
  87. *
  88. * @static
  89. * @access public
  90. * @param array $value
  91. * @throws Exception
  92. */
  93. public static function setSearchLocations($value) {
  94. if (is_array($value)) {
  95. self::$_searchLocations = $value;
  96. } else {
  97. throw new Exception('Invalid parameter passed.');
  98. }
  99. } // function setSearchLocations()
  100. /**
  101. * Add search location
  102. *
  103. * @static
  104. * @access public
  105. * @param string $type Example: IWriter
  106. * @param string $location Example: PHPExcel/Writer/{0}.php
  107. * @param string $classname Example: PHPExcel_Writer_{0}
  108. */
  109. public static function addSearchLocation($type = '', $location = '', $classname = '') {
  110. self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
  111. } // function addSearchLocation()
  112. /**
  113. * Create PHPExcel_Writer_IWriter
  114. *
  115. * @static
  116. * @access public
  117. * @param PHPExcel $phpExcel
  118. * @param string $writerType Example: Excel2007
  119. * @return PHPExcel_Writer_IWriter
  120. * @throws Exception
  121. */
  122. public static function createWriter(PHPExcel $phpExcel, $writerType = '') {
  123. // Search type
  124. $searchType = 'IWriter';
  125. // Include class
  126. foreach (self::$_searchLocations as $searchLocation) {
  127. if ($searchLocation['type'] == $searchType) {
  128. $className = str_replace('{0}', $writerType, $searchLocation['class']);
  129. $classFile = str_replace('{0}', $writerType, $searchLocation['path']);
  130. $instance = new $className($phpExcel);
  131. if (!is_null($instance)) {
  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. $classFile = str_replace('{0}', $readerType, $searchLocation['path']);
  156. $instance = new $className();
  157. if (!is_null($instance)) {
  158. return $instance;
  159. }
  160. }
  161. }
  162. // Nothing found...
  163. throw new Exception("No $searchType found for type $readerType");
  164. } // function createReader()
  165. /**
  166. * Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
  167. *
  168. * @static
  169. * @access public
  170. * @param string $pFileName
  171. * @return PHPExcel
  172. * @throws Exception
  173. */
  174. public static function load($pFilename) {
  175. $reader = self::createReaderForFile($pFilename);
  176. return $reader->load($pFilename);
  177. } // function load()
  178. /**
  179. * Identify file type using automatic PHPExcel_Reader_IReader resolution
  180. *
  181. * @static
  182. * @access public
  183. * @param string $pFileName
  184. * @return string
  185. * @throws Exception
  186. */
  187. public static function identify($pFilename) {
  188. $reader = self::createReaderForFile($pFilename);
  189. $className = get_class($reader);
  190. $classType = explode('_',$className);
  191. unset($reader);
  192. return array_pop($classType);
  193. } // function identify()
  194. /**
  195. * Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution
  196. *
  197. * @static
  198. * @access public
  199. * @param string $pFileName
  200. * @return PHPExcel_Reader_IReader
  201. * @throws Exception
  202. */
  203. public static function createReaderForFile($pFilename) {
  204. // First, lucky guess by inspecting file extension
  205. $pathinfo = pathinfo($pFilename);
  206. if (isset($pathinfo['extension'])) {
  207. switch (strtolower($pathinfo['extension'])) {
  208. case 'xlsx':
  209. $reader = self::createReader('Excel2007');
  210. break;
  211. case 'xls':
  212. $reader = self::createReader('Excel5');
  213. break;
  214. case 'ods':
  215. $reader = self::createReader('OOCalc');
  216. break;
  217. case 'slk':
  218. $reader = self::createReader('SYLK');
  219. break;
  220. case 'xml':
  221. $reader = self::createReader('Excel2003XML');
  222. break;
  223. case 'gnumeric':
  224. $reader = self::createReader('Gnumeric');
  225. break;
  226. case 'csv':
  227. // Do nothing
  228. // We must not try to use CSV reader since it loads
  229. // all files including Excel files etc.
  230. break;
  231. default:
  232. break;
  233. }
  234. // Let's see if we are lucky
  235. if (isset($reader) && $reader->canRead($pFilename)) {
  236. return $reader;
  237. }
  238. }
  239. // If we reach here then "lucky guess" didn't give any result
  240. // Try loading using self::$_autoResolveClasses
  241. foreach (self::$_autoResolveClasses as $autoResolveClass) {
  242. $reader = self::createReader($autoResolveClass);
  243. if ($reader->canRead($pFilename)) {
  244. return $reader;
  245. }
  246. }
  247. } // function createReaderForFile()
  248. }