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.5, 2009-01-05
  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.      * Private constructor for PHPExcel_IOFactory
  59.      */
  60.     private function __construct(}
  61.     
  62.     /**
  63.      * Get search locations
  64.      *
  65.      * @return array 
  66.      */
  67.     public static function getSearchLocations({
  68.         return self::$_searchLocations;
  69.     }
  70.     
  71.     /**
  72.      * Set search locations
  73.      * 
  74.      * @param array $value 
  75.      * @throws Exception
  76.      */
  77.     public static function setSearchLocations($value{
  78.         if (is_array($value)) {
  79.             self::$_searchLocations $value;
  80.         else {
  81.             throw new Exception('Invalid parameter passed.');
  82.         }
  83.     }
  84.     
  85.     /**
  86.      * Add search location
  87.      * 
  88.      * @param string $type            Example: IWriter
  89.      * @param string $location        Example: PHPExcel/Writer/{0}.php
  90.      * @param string $classname     Example: PHPExcel_Writer_{0}
  91.      */
  92.     public static function addSearchLocation($type ''$location ''$classname ''{
  93.         self::$_searchLocations[array'type' => $type'path' => $location'class' => $classname );
  94.     }
  95.     
  96.     /**
  97.      * Create PHPExcel_Writer_IWriter
  98.      *
  99.      * @param PHPExcel $phpExcel 
  100.      * @param string  $writerType    Example: Excel2007
  101.      * @return PHPExcel_Writer_IWriter 
  102.      */
  103.     public static function createWriter(PHPExcel $phpExcel$writerType ''{
  104.         // Search type
  105.         $searchType 'IWriter';
  106.         
  107.         // Include class
  108.         foreach (self::$_searchLocations as $searchLocation{
  109.             if ($searchLocation['type'== $searchType{
  110.                 $className str_replace('{0}'$writerType$searchLocation['class']);
  111.                 $classFile str_replace('{0}'$writerType$searchLocation['path']);
  112.                 
  113.                 if (!class_exists($className)) {
  114.                     self::requireFile($classFile);
  115.                 }
  116.                 
  117.                 $instance new $className($phpExcel);
  118.                 if (!is_null($instance)) {
  119.                     return $instance;
  120.                 }
  121.             }
  122.         }
  123.         
  124.         // Nothing found...
  125.         throw new Exception("No $searchType found for type $writerType");
  126.     }
  127.     
  128.     /**
  129.      * Create PHPExcel_Reader_IReader
  130.      *
  131.      * @param string $readerType    Example: Excel2007
  132.      * @return PHPExcel_Reader_IReader 
  133.      */
  134.     public static function createReader($readerType ''{
  135.         // Search type
  136.         $searchType 'IReader';
  137.         
  138.         // Include class
  139.         foreach (self::$_searchLocations as $searchLocation{
  140.             if ($searchLocation['type'== $searchType{
  141.                 $className str_replace('{0}'$readerType$searchLocation['class']);
  142.                 $classFile str_replace('{0}'$readerType$searchLocation['path']);
  143.                 
  144.                 if (!class_exists($className)) {
  145.                     self::requireFile($classFile);
  146.                 }
  147.                 
  148.                 $instance new $className();
  149.                 if (!is_null($instance)) {
  150.                     return $instance;
  151.                 }
  152.             }
  153.         }
  154.         
  155.         // Nothing found...
  156.         throw new Exception("No $searchType found for type $readerType");
  157.     }
  158.     
  159.     /**
  160.      * Require_once file
  161.      *
  162.      * @param string $filename 
  163.      */
  164.     private static function requireFile($filename{
  165.         $includePath get_include_path();
  166.         $includeTokens explode(PATH_SEPARATOR$includePath);
  167.                     
  168.         foreach ($includeTokens as $includeToken{
  169.             if (file_exists($includeToken '/' $filename)) {
  170.                 require_once$filename );
  171.             }
  172.         }
  173.     }
  174. }

Documentation generated on Mon, 05 Jan 2009 20:37:59 +0100 by phpDocumentor 1.4.1