PHPExcel
[ class tree: PHPExcel ] [ index: PHPExcel ] [ all elements ]

Source for file IOFactory.php

Documentation is available at IOFactory.php

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

Documentation generated on Tue, 01 Jun 2010 17:05:02 +0200 by phpDocumentor 1.4.3