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 - 2009 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 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  24.  * @license    http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
  25.  * @version    1.6.7, 2009-04-22
  26.  */
  27.  
  28.  
  29. /** PHPExcel */
  30. require_once 'PHPExcel.php';
  31.  
  32. /** PHPExcel_IWriter */
  33. require_once 'PHPExcel/Writer/IWriter.php';
  34.  
  35. /** PHPExcel_IReader */
  36. require_once 'PHPExcel/Reader/IReader.php';
  37.  
  38.  
  39. /**
  40.  * PHPExcel_IOFactory
  41.  *
  42.  * @category   PHPExcel
  43.  * @package    PHPExcel
  44.  * @copyright  Copyright (c) 2006 - 2009 PHPExcel (http://www.codeplex.com/PHPExcel)
  45.  */
  46. {    
  47.     /**
  48.      * Search locations
  49.      *
  50.      * @var array 
  51.      */
  52.     private static $_searchLocations array(
  53.         array'type' => 'IWriter''path' => 'PHPExcel/Writer/{0}.php''class' => 'PHPExcel_Writer_{0}' ),
  54.         array'type' => 'IReader''path' => 'PHPExcel/Reader/{0}.php''class' => 'PHPExcel_Reader_{0}' )
  55.     );
  56.     
  57.     /**
  58.      * Autoresolve classes
  59.      * 
  60.      * @var array 
  61.      */
  62.     private static $_autoResolveClasses array(
  63.         'Excel2007',
  64.         'Excel5',
  65.         'Serialized',
  66.         'CSV'
  67.     );
  68.     
  69.     /**
  70.      * Private constructor for PHPExcel_IOFactory
  71.      */
  72.     private function __construct(}
  73.     
  74.     /**
  75.      * Get search locations
  76.      *
  77.      * @return array 
  78.      */
  79.     public static function getSearchLocations({
  80.         return self::$_searchLocations;
  81.     }
  82.     
  83.     /**
  84.      * Set search locations
  85.      * 
  86.      * @param array $value 
  87.      * @throws Exception
  88.      */
  89.     public static function setSearchLocations($value{
  90.         if (is_array($value)) {
  91.             self::$_searchLocations $value;
  92.         else {
  93.             throw new Exception('Invalid parameter passed.');
  94.         }
  95.     }
  96.     
  97.     /**
  98.      * Add search location
  99.      * 
  100.      * @param string $type            Example: IWriter
  101.      * @param string $location        Example: PHPExcel/Writer/{0}.php
  102.      * @param string $classname     Example: PHPExcel_Writer_{0}
  103.      */
  104.     public static function addSearchLocation($type ''$location ''$classname ''{
  105.         self::$_searchLocations[array'type' => $type'path' => $location'class' => $classname );
  106.     }
  107.     
  108.     /**
  109.      * Create PHPExcel_Writer_IWriter
  110.      *
  111.      * @param PHPExcel $phpExcel 
  112.      * @param string  $writerType    Example: Excel2007
  113.      * @return PHPExcel_Writer_IWriter 
  114.      */
  115.     public static function createWriter(PHPExcel $phpExcel$writerType ''{
  116.         // Search type
  117.         $searchType 'IWriter';
  118.         
  119.         // Include class
  120.         foreach (self::$_searchLocations as $searchLocation{
  121.             if ($searchLocation['type'== $searchType{
  122.                 $className str_replace('{0}'$writerType$searchLocation['class']);
  123.                 $classFile str_replace('{0}'$writerType$searchLocation['path']);
  124.                 
  125.                 if (!class_exists($className)) {
  126.                     require_once($classFile);
  127.                 }
  128.                 
  129.                 $instance new $className($phpExcel);
  130.                 if (!is_null($instance)) {
  131.                     return $instance;
  132.                 }
  133.             }
  134.         }
  135.         
  136.         // Nothing found...
  137.         throw new Exception("No $searchType found for type $writerType");
  138.     }
  139.     
  140.     /**
  141.      * Create PHPExcel_Reader_IReader
  142.      *
  143.      * @param string $readerType    Example: Excel2007
  144.      * @return PHPExcel_Reader_IReader 
  145.      */
  146.     public static function createReader($readerType ''{
  147.         // Search type
  148.         $searchType 'IReader';
  149.         
  150.         // Include class
  151.         foreach (self::$_searchLocations as $searchLocation{
  152.             if ($searchLocation['type'== $searchType{
  153.                 $className str_replace('{0}'$readerType$searchLocation['class']);
  154.                 $classFile str_replace('{0}'$readerType$searchLocation['path']);
  155.                 
  156.                 if (!class_exists($className)) {
  157.                     require_once($classFile);
  158.                 }
  159.                 
  160.                 $instance new $className();
  161.                 if (!is_null($instance)) {
  162.                     return $instance;
  163.                 }
  164.             }
  165.         }
  166.         
  167.         // Nothing found...
  168.         throw new Exception("No $searchType found for type $readerType");
  169.     }
  170.     
  171.     /**
  172.      * Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
  173.      *
  174.      * @param     string         $pFileName 
  175.      * @return    PHPExcel 
  176.      * @throws     Exception
  177.      */    
  178.     public static function load($pFilename{
  179.         // Try loading using self::$_autoResolveClasses
  180.         foreach (self::$_autoResolveClasses as $autoResolveClass{
  181.             $reader self::createReader($autoResolveClass);
  182.             if ($reader->canRead($pFilename)) {
  183.                 return $reader->load($pFilename);
  184.             }
  185.         }
  186.         
  187.         throw new Exception("Could not automatically determine PHPExcel_Reader_IReader for file.");
  188.     }
  189. }

Documentation generated on Wed, 22 Apr 2009 09:00:12 +0200 by phpDocumentor 1.4.1