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

Documentation generated on Sun, 27 Feb 2011 16:32:24 -0800 by phpDocumentor 1.4.3