[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/lib/phpexcel/PHPExcel/ -> IOFactory.php (source)

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


Generated: Thu Aug 11 10:00:09 2016 Cross-referenced by PHPXref 0.7.1