[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/libraries/PHPExcel/PHPExcel/ -> IOFactory.php (source)

   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    1.7.7, 2012-05-19
  26   */
  27  
  28  
  29  /**    PHPExcel root directory */
  30  if (!defined('PHPEXCEL_ROOT')) {
  31      /**
  32       * @ignore
  33       */
  34      define('PHPEXCEL_ROOT', dirname(__FILE__) . '/../');
  35      require (PHPEXCEL_ROOT . 'PHPExcel/Autoloader.php');
  36  }
  37  
  38  /**
  39   * PHPExcel_IOFactory
  40   *
  41   * @category   PHPExcel
  42   * @package    PHPExcel
  43   * @copyright  Copyright (c) 2006 - 2012 PHPExcel (http://www.codeplex.com/PHPExcel)
  44   */
  45  class PHPExcel_IOFactory
  46  {
  47      /**
  48       * Search locations
  49       *
  50       * @var    array
  51       * @access    private
  52       * @static
  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      /**
  60       * Autoresolve classes
  61       *
  62       * @var    array
  63       * @access    private
  64       * @static
  65       */
  66      private static $_autoResolveClasses = array(
  67          'Excel2007',
  68          'Excel5',
  69          'Excel2003XML',
  70          'OOCalc',
  71          'SYLK',
  72          'Gnumeric',
  73          'CSV',
  74      );
  75  
  76      /**
  77       *    Private constructor for PHPExcel_IOFactory
  78       */
  79      private function __construct() { }
  80  
  81      /**
  82       * Get search locations
  83       *
  84       * @static
  85       * @access    public
  86       * @return    array
  87       */
  88  	public static function getSearchLocations() {
  89          return self::$_searchLocations;
  90      }    //    function getSearchLocations()
  91  
  92      /**
  93       * Set search locations
  94       *
  95       * @static
  96       * @access    public
  97       * @param    array $value
  98       * @throws    Exception
  99       */
 100  	public static function setSearchLocations($value) {
 101          if (is_array($value)) {
 102              self::$_searchLocations = $value;
 103          } else {
 104              throw new Exception('Invalid parameter passed.');
 105          }
 106      }    //    function setSearchLocations()
 107  
 108      /**
 109       * Add search location
 110       *
 111       * @static
 112       * @access    public
 113       * @param    string $type        Example: IWriter
 114       * @param    string $location    Example: PHPExcel/Writer/{0}.php
 115       * @param    string $classname     Example: PHPExcel_Writer_{0}
 116       */
 117  	public static function addSearchLocation($type = '', $location = '', $classname = '') {
 118          self::$_searchLocations[] = array( 'type' => $type, 'path' => $location, 'class' => $classname );
 119      }    //    function addSearchLocation()
 120  
 121      /**
 122       * Create PHPExcel_Writer_IWriter
 123       *
 124       * @static
 125       * @access    public
 126       * @param    PHPExcel $phpExcel
 127       * @param    string  $writerType    Example: Excel2007
 128       * @return    PHPExcel_Writer_IWriter
 129       * @throws    Exception
 130       */
 131  	public static function createWriter(PHPExcel $phpExcel, $writerType = '') {
 132          // Search type
 133          $searchType = 'IWriter';
 134  
 135          // Include class
 136          foreach (self::$_searchLocations as $searchLocation) {
 137              if ($searchLocation['type'] == $searchType) {
 138                  $className = str_replace('{0}', $writerType, $searchLocation['class']);
 139                  $classFile = str_replace('{0}', $writerType, $searchLocation['path']);
 140  
 141                  $instance = new $className($phpExcel);
 142                  if ($instance !== NULL) {
 143                      return $instance;
 144                  }
 145              }
 146          }
 147  
 148          // Nothing found...
 149          throw new Exception("No $searchType found for type $writerType");
 150      }    //    function createWriter()
 151  
 152      /**
 153       * Create PHPExcel_Reader_IReader
 154       *
 155       * @static
 156       * @access    public
 157       * @param    string $readerType    Example: Excel2007
 158       * @return    PHPExcel_Reader_IReader
 159       * @throws    Exception
 160       */
 161  	public static function createReader($readerType = '') {
 162          // Search type
 163          $searchType = 'IReader';
 164  
 165          // Include class
 166          foreach (self::$_searchLocations as $searchLocation) {
 167              if ($searchLocation['type'] == $searchType) {
 168                  $className = str_replace('{0}', $readerType, $searchLocation['class']);
 169                  $classFile = str_replace('{0}', $readerType, $searchLocation['path']);
 170  
 171                  $instance = new $className();
 172                  if ($instance !== NULL) {
 173                      return $instance;
 174                  }
 175              }
 176          }
 177  
 178          // Nothing found...
 179          throw new Exception("No $searchType found for type $readerType");
 180      }    //    function createReader()
 181  
 182      /**
 183       * Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
 184       *
 185       * @static
 186       * @access public
 187       * @param     string         $pFileName
 188       * @return    PHPExcel
 189       * @throws    Exception
 190       */
 191  	public static function load($pFilename) {
 192          $reader = self::createReaderForFile($pFilename);
 193          return $reader->load($pFilename);
 194      }    //    function load()
 195  
 196      /**
 197       * Identify file type using automatic PHPExcel_Reader_IReader resolution
 198       *
 199       * @static
 200       * @access public
 201       * @param     string         $pFileName
 202       * @return    string
 203       * @throws    Exception
 204       */
 205  	public static function identify($pFilename) {
 206          $reader = self::createReaderForFile($pFilename);
 207          $className = get_class($reader);
 208          $classType = explode('_',$className);
 209          unset($reader);
 210          return array_pop($classType);
 211      }    //    function identify()
 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
 219       * @return    PHPExcel_Reader_IReader
 220       * @throws    Exception
 221       */
 222  	public static function createReaderForFile($pFilename) {
 223  
 224          // First, lucky guess by inspecting file extension
 225          $pathinfo = pathinfo($pFilename);
 226  
 227          if (isset($pathinfo['extension'])) {
 228              switch (strtolower($pathinfo['extension'])) {
 229                  case 'xlsx':
 230                      $reader = self::createReader('Excel2007');
 231                      break;
 232                  case 'xls':
 233                  case 'xlsm':
 234                      $reader = self::createReader('Excel5');
 235                      break;
 236                  case 'ods':
 237                      $reader = self::createReader('OOCalc');
 238                      break;
 239                  case 'slk':
 240                      $reader = self::createReader('SYLK');
 241                      break;
 242                  case 'xml':
 243                      $reader = self::createReader('Excel2003XML');
 244                      break;
 245                  case 'gnumeric':
 246                      $reader = self::createReader('Gnumeric');
 247                      break;
 248                  case 'csv':
 249                      // Do nothing
 250                      // We must not try to use CSV reader since it loads
 251                      // all files including Excel files etc.
 252                      break;
 253                  default:
 254                      break;
 255              }
 256  
 257              // Let's see if we are lucky
 258              if (isset($reader) && $reader->canRead($pFilename)) {
 259                  return $reader;
 260              }
 261  
 262          }
 263  
 264          // If we reach here then "lucky guess" didn't give any result
 265  
 266          // Try loading using self::$_autoResolveClasses
 267          foreach (self::$_autoResolveClasses as $autoResolveClass) {
 268              $reader = self::createReader($autoResolveClass);
 269              if ($reader->canRead($pFilename)) {
 270                  return $reader;
 271              }
 272          }
 273  
 274      }    //    function createReaderForFile()
 275  }


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1