PHPPowerPoint
[ class tree: PHPPowerPoint ] [ index: PHPPowerPoint ] [ all elements ]

Source for file IOFactory.php

Documentation is available at IOFactory.php

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

Documentation generated on Sat, 25 Apr 2009 11:37:24 +0200 by phpDocumentor 1.4.1