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.2, 2010-01-11
  26.  */
  27.  
  28.  
  29. /** PHPExcel root directory */
  30. if (!defined('PHPEXCEL_ROOT')) {
  31.     /**
  32.      * @ignore
  33.      */
  34.     define('PHPEXCEL_ROOT'dirname(__FILE__'/../');
  35. }
  36.  
  37. /** PHPExcel */
  38. require_once PHPEXCEL_ROOT 'PHPExcel.php';
  39.  
  40. /** PHPExcel_IWriter */
  41. require_once PHPEXCEL_ROOT 'PHPExcel/Writer/IWriter.php';
  42.  
  43. /** PHPExcel_IReader */
  44. require_once PHPEXCEL_ROOT 'PHPExcel/Reader/IReader.php';
  45.  
  46.  
  47. /**
  48.  * PHPExcel_IOFactory
  49.  *
  50.  * @category   PHPExcel
  51.  * @package    PHPExcel
  52.  * @copyright  Copyright (c) 2006 - 2010 PHPExcel (http://www.codeplex.com/PHPExcel)
  53.  */
  54. {
  55.     /**
  56.      * Search locations
  57.      *
  58.      * @var array 
  59.      */
  60.     private static $_searchLocations array(
  61.         array'type' => 'IWriter''path' => 'PHPExcel/Writer/{0}.php''class' => 'PHPExcel_Writer_{0}' ),
  62.         array'type' => 'IReader''path' => 'PHPExcel/Reader/{0}.php''class' => 'PHPExcel_Reader_{0}' )
  63.     );
  64.  
  65.     /**
  66.      * Autoresolve classes
  67.      *
  68.      * @var array 
  69.      */
  70.     private static $_autoResolveClasses array(
  71.         'Excel2007',
  72.         'Excel5',
  73.         'Excel2003XML',
  74.         'OOCalc',
  75.         'SYLK',
  76.         'Serialized',
  77.         'CSV',
  78.     );
  79.  
  80.     /**
  81.      * Private constructor for PHPExcel_IOFactory
  82.      */
  83.     private function __construct(}
  84.  
  85.     /**
  86.      * Get search locations
  87.      *
  88.      * @return array 
  89.      */
  90.     public static function getSearchLocations({
  91.         return self::$_searchLocations;
  92.     }
  93.  
  94.     /**
  95.      * Set search locations
  96.      *
  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.     }
  107.  
  108.     /**
  109.      * Add search location
  110.      *
  111.      * @param string $type            Example: IWriter
  112.      * @param string $location        Example: PHPExcel/Writer/{0}.php
  113.      * @param string $classname     Example: PHPExcel_Writer_{0}
  114.      */
  115.     public static function addSearchLocation($type ''$location ''$classname ''{
  116.         self::$_searchLocations[array'type' => $type'path' => $location'class' => $classname );
  117.     }
  118.  
  119.     /**
  120.      * Create PHPExcel_Writer_IWriter
  121.      *
  122.      * @param PHPExcel $phpExcel 
  123.      * @param string  $writerType    Example: Excel2007
  124.      * @return PHPExcel_Writer_IWriter 
  125.      */
  126.     public static function createWriter(PHPExcel $phpExcel$writerType ''{
  127.         // Search type
  128.         $searchType 'IWriter';
  129.  
  130.         // Include class
  131.         foreach (self::$_searchLocations as $searchLocation{
  132.             if ($searchLocation['type'== $searchType{
  133.                 $className str_replace('{0}'$writerType$searchLocation['class']);
  134.                 $classFile str_replace('{0}'$writerType$searchLocation['path']);
  135.  
  136.                 if (!class_exists($className)) {
  137.                     require_once PHPEXCEL_ROOT $classFile;
  138.                 }
  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.     }
  150.  
  151.     /**
  152.      * Create PHPExcel_Reader_IReader
  153.      *
  154.      * @param string $readerType    Example: Excel2007
  155.      * @return PHPExcel_Reader_IReader 
  156.      */
  157.     public static function createReader($readerType ''{
  158.         // Search type
  159.         $searchType 'IReader';
  160.  
  161.         // Include class
  162.         foreach (self::$_searchLocations as $searchLocation{
  163.             if ($searchLocation['type'== $searchType{
  164.                 $className str_replace('{0}'$readerType$searchLocation['class']);
  165.                 $classFile str_replace('{0}'$readerType$searchLocation['path']);
  166.  
  167.                 if (!class_exists($className)) {
  168.                     require_once PHPEXCEL_ROOT $classFile;
  169.                 }
  170.  
  171.                 $instance new $className();
  172.                 if (!is_null($instance)) {
  173.                     return $instance;
  174.                 }
  175.             }
  176.         }
  177.  
  178.         // Nothing found...
  179.         throw new Exception("No $searchType found for type $readerType");
  180.     }
  181.  
  182.     /**
  183.      * Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
  184.      *
  185.      * @param     string         $pFileName 
  186.      * @return    PHPExcel 
  187.      */
  188.     public static function load($pFilename{
  189.         $reader self::createReaderForFile($pFilename);
  190.         return $reader->load($pFilename);
  191.     }
  192.  
  193.     /**
  194.      * Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution
  195.      *
  196.      * @param     string         $pFileName 
  197.      * @return    PHPExcel_Reader_IReader 
  198.      */
  199.     public static function createReaderForFile($pFilename{
  200.  
  201.         // First, lucky guess by inspecting file extension
  202.         $pathinfo pathinfo($pFilename);
  203.  
  204.         if (isset($pathinfo['extension'])) {
  205.  
  206.             switch (strtolower($pathinfo['extension'])) {
  207.                 case 'xlsx':
  208.                     $reader self::createReader('Excel2007');
  209.                     break;
  210.  
  211.                 case 'xls':
  212.                     $reader self::createReader('Excel5');
  213.                     break;
  214.  
  215.                 case 'ods':
  216.                     $reader self::createReader('OOCalc');
  217.                     break;
  218.  
  219.                 case 'slk':
  220.                     $reader self::createReader('SYLK');
  221.                     break;
  222.  
  223.                 case 'xml':
  224.                     $reader self::createReader('Excel2003XML');
  225.                     break;
  226.  
  227.                 case 'csv':
  228.                     // Do nothing
  229.                     // We must not try to use CSV reader since it loads
  230.                     // all files including Excel files etc.
  231.                     break;
  232.  
  233.                 default:
  234.                     break;
  235.  
  236.             }
  237.  
  238.             // Let's see if we are lucky
  239.             if ($reader->canRead($pFilename)) {
  240.                 return $reader;
  241.             }
  242.  
  243.         }
  244.  
  245.         // If we reach here then "lucky guess" didn't give any result
  246.  
  247.         // Try loading using self::$_autoResolveClasses
  248.         foreach (self::$_autoResolveClasses as $autoResolveClass{
  249.             $reader self::createReader($autoResolveClass);
  250.             if ($reader->canRead($pFilename)) {
  251.                 return $reader;
  252.             }
  253.         }
  254.  
  255.     }
  256. }

Documentation generated on Mon, 11 Jan 2010 08:11:33 +0100 by phpDocumentor 1.4.1