PageRenderTime 75ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/add-ons/PHPExcel/PHPExcel/IOFactory.php

https://github.com/jcplat/console-seolan
PHP | 212 lines | 84 code | 25 blank | 103 comment | 15 complexity | 9651cb9113aba87b843610138f4f6bd2 MD5 | raw file
Possible License(s): LGPL-2.0, LGPL-2.1, GPL-3.0, Apache-2.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * PHPExcel
  4. *
  5. * Copyright (c) 2006 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24. * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL
  25. * @version 1.7.1, 2009-11-02
  26. */
  27. /** PHPExcel root directory */
  28. if (!defined('PHPEXCEL_ROOT')) {
  29. /**
  30. * @ignore
  31. */
  32. define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
  33. }
  34. /** PHPExcel */
  35. require_once PHPEXCEL_ROOT . 'PHPExcel.php';
  36. /** PHPExcel_IWriter */
  37. require_once PHPEXCEL_ROOT . 'PHPExcel/Writer/IWriter.php';
  38. /** PHPExcel_IReader */
  39. require_once PHPEXCEL_ROOT . 'PHPExcel/Reader/IReader.php';
  40. /**
  41. * PHPExcel_IOFactory
  42. *
  43. * @category PHPExcel
  44. * @package PHPExcel
  45. * @copyright Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  46. */
  47. class PHPExcel_IOFactory
  48. {
  49. /**
  50. * Search locations
  51. *
  52. * @var array
  53. */
  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. /**
  59. * Autoresolve classes
  60. *
  61. * @var array
  62. */
  63. private static $_autoResolveClasses = array(
  64. 'Excel2007',
  65. 'Excel5',
  66. 'Excel2003XML',
  67. 'OOCalc',
  68. 'SYLK',
  69. 'CSV',
  70. 'Serialized'
  71. );
  72. /**
  73. * Private constructor for PHPExcel_IOFactory
  74. */
  75. private function __construct() { }
  76. /**
  77. * Get search locations
  78. *
  79. * @return array
  80. */
  81. public static function getSearchLocations() {
  82. return self::$_searchLocations;
  83. }
  84. /**
  85. * Set search locations
  86. *
  87. * @param array $value
  88. * @throws Exception
  89. */
  90. public static function setSearchLocations($value) {
  91. if (is_array($value)) {
  92. self::$_searchLocations = $value;
  93. } else {
  94. throw new Exception('Invalid parameter passed.');
  95. }
  96. }
  97. /**
  98. * Add search location
  99. *
  100. * @param string $type Example: IWriter
  101. * @param string $location Example: PHPExcel/Writer/{0}.php
  102. * @param string $classname Example: PHPExcel_Writer_{0}
  103. */
  104. public static function addSearchLocation($type = '', $location = '', $classname = '') {
  105. self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
  106. }
  107. /**
  108. * Create PHPExcel_Writer_IWriter
  109. *
  110. * @param PHPExcel $phpExcel
  111. * @param string $writerType Example: Excel2007
  112. * @return PHPExcel_Writer_IWriter
  113. */
  114. public static function createWriter(PHPExcel $phpExcel, $writerType = '') {
  115. // Search type
  116. $searchType = 'IWriter';
  117. // Include class
  118. foreach (self::$_searchLocations as $searchLocation) {
  119. if ($searchLocation['type'] == $searchType) {
  120. $className = str_replace('{0}', $writerType, $searchLocation['class']);
  121. $classFile = str_replace('{0}', $writerType, $searchLocation['path']);
  122. if (!class_exists($className)) {
  123. require_once PHPEXCEL_ROOT . $classFile;
  124. }
  125. $instance = new $className($phpExcel);
  126. if (!is_null($instance)) {
  127. return $instance;
  128. }
  129. }
  130. }
  131. // Nothing found...
  132. throw new Exception("No $searchType found for type $writerType");
  133. }
  134. /**
  135. * Create PHPExcel_Reader_IReader
  136. *
  137. * @param string $readerType Example: Excel2007
  138. * @return PHPExcel_Reader_IReader
  139. */
  140. public static function createReader($readerType = '') {
  141. // Search type
  142. $searchType = 'IReader';
  143. // Include class
  144. foreach (self::$_searchLocations as $searchLocation) {
  145. if ($searchLocation['type'] == $searchType) {
  146. $className = str_replace('{0}', $readerType, $searchLocation['class']);
  147. $classFile = str_replace('{0}', $readerType, $searchLocation['path']);
  148. if (!class_exists($className)) {
  149. require_once PHPEXCEL_ROOT . $classFile;
  150. }
  151. $instance = new $className();
  152. if (!is_null($instance)) {
  153. return $instance;
  154. }
  155. }
  156. }
  157. // Nothing found...
  158. throw new Exception("No $searchType found for type $readerType");
  159. }
  160. /**
  161. * Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
  162. *
  163. * @param string $pFileName
  164. * @return PHPExcel
  165. */
  166. public static function load($pFilename) {
  167. $reader = self::createReaderForFile($pFilename);
  168. return $reader->load($pFilename);
  169. }
  170. /**
  171. * Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution
  172. *
  173. * @param string $pFileName
  174. * @return PHPExcel_Reader_IReader
  175. * @throws Exception
  176. */
  177. public static function createReaderForFile($pFilename) {
  178. // Try loading using self::$_autoResolveClasses
  179. foreach (self::$_autoResolveClasses as $autoResolveClass) {
  180. $reader = self::createReader($autoResolveClass);
  181. if ($reader->canRead($pFilename)) {
  182. return $reader;
  183. }
  184. }
  185. throw new Exception("Could not automatically determine PHPExcel_Reader_IReader for file.");
  186. }
  187. }