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.7.0, 2009-08-10
  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 - 2009 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.         'Serialized',
  74.         'CSV'
  75.     );
  76.     
  77.     /**
  78.      * Private constructor for PHPExcel_IOFactory
  79.      */
  80.     private function __construct(}
  81.     
  82.     /**
  83.      * Get search locations
  84.      *
  85.      * @return array 
  86.      */
  87.     public static function getSearchLocations({
  88.         return self::$_searchLocations;
  89.     }
  90.     
  91.     /**
  92.      * Set search locations
  93.      * 
  94.      * @param array $value 
  95.      * @throws Exception
  96.      */
  97.     public static function setSearchLocations($value{
  98.         if (is_array($value)) {
  99.             self::$_searchLocations $value;
  100.         else {
  101.             throw new Exception('Invalid parameter passed.');
  102.         }
  103.     }
  104.     
  105.     /**
  106.      * Add search location
  107.      * 
  108.      * @param string $type            Example: IWriter
  109.      * @param string $location        Example: PHPExcel/Writer/{0}.php
  110.      * @param string $classname     Example: PHPExcel_Writer_{0}
  111.      */
  112.     public static function addSearchLocation($type ''$location ''$classname ''{
  113.         self::$_searchLocations[array'type' => $type'path' => $location'class' => $classname );
  114.     }
  115.     
  116.     /**
  117.      * Create PHPExcel_Writer_IWriter
  118.      *
  119.      * @param PHPExcel $phpExcel 
  120.      * @param string  $writerType    Example: Excel2007
  121.      * @return PHPExcel_Writer_IWriter 
  122.      */
  123.     public static function createWriter(PHPExcel $phpExcel$writerType ''{
  124.         // Search type
  125.         $searchType 'IWriter';
  126.         
  127.         // Include class
  128.         foreach (self::$_searchLocations as $searchLocation{
  129.             if ($searchLocation['type'== $searchType{
  130.                 $className str_replace('{0}'$writerType$searchLocation['class']);
  131.                 $classFile str_replace('{0}'$writerType$searchLocation['path']);
  132.                 
  133.                 if (!class_exists($className)) {
  134.                     require_once PHPEXCEL_ROOT $classFile;
  135.                 }
  136.                 
  137.                 $instance new $className($phpExcel);
  138.                 if (!is_null($instance)) {
  139.                     return $instance;
  140.                 }
  141.             }
  142.         }
  143.         
  144.         // Nothing found...
  145.         throw new Exception("No $searchType found for type $writerType");
  146.     }
  147.     
  148.     /**
  149.      * Create PHPExcel_Reader_IReader
  150.      *
  151.      * @param string $readerType    Example: Excel2007
  152.      * @return PHPExcel_Reader_IReader 
  153.      */
  154.     public static function createReader($readerType ''{
  155.         // Search type
  156.         $searchType 'IReader';
  157.         
  158.         // Include class
  159.         foreach (self::$_searchLocations as $searchLocation{
  160.             if ($searchLocation['type'== $searchType{
  161.                 $className str_replace('{0}'$readerType$searchLocation['class']);
  162.                 $classFile str_replace('{0}'$readerType$searchLocation['path']);
  163.                 
  164.                 if (!class_exists($className)) {
  165.                     require_once PHPEXCEL_ROOT $classFile;
  166.                 }
  167.                 
  168.                 $instance new $className();
  169.                 if (!is_null($instance)) {
  170.                     return $instance;
  171.                 }
  172.             }
  173.         }
  174.         
  175.         // Nothing found...
  176.         throw new Exception("No $searchType found for type $readerType");
  177.     }
  178.     
  179.     /**
  180.      * Loads PHPExcel from file using automatic PHPExcel_Reader_IReader resolution
  181.      *
  182.      * @param     string         $pFileName 
  183.      * @return    PHPExcel 
  184.      */    
  185.     public static function load($pFilename{
  186.         $reader self::createReaderForFile($pFilename);
  187.         return $reader->load($pFilename);
  188.     }
  189.  
  190.     /**
  191.      * Create PHPExcel_Reader_IReader for file using automatic PHPExcel_Reader_IReader resolution
  192.      *
  193.      * @param     string         $pFileName 
  194.      * @return    PHPExcel_Reader_IReader 
  195.      * @throws     Exception
  196.      */    
  197.     public static function createReaderForFile($pFilename{
  198.         // Try loading using self::$_autoResolveClasses
  199.         foreach (self::$_autoResolveClasses as $autoResolveClass{
  200.             $reader self::createReader($autoResolveClass);
  201.             if ($reader->canRead($pFilename)) {
  202.                 return $reader;
  203.             }
  204.         }
  205.  
  206.         throw new Exception("Could not automatically determine PHPExcel_Reader_IReader for file.");
  207.     }
  208. }

Documentation generated on Mon, 10 Aug 2009 08:06:09 +0200 by phpDocumentor 1.4.1