Source for file Factory.class.php

Documentation is available at Factory.class.php

  1. <?php
  2. /**
  3.  * Gumbo Library Framework
  4.  *
  5.  * LICENSE
  6.  * This library is being released under the terms of the New BSD License.  A
  7.  * copy of the license is packaged with the software (LICENSE.txt).  If no
  8.  * copy is found, a copy of the license template can be found at:
  9.  * http://www.opensource.org/licenses/bsd-license.php
  10.  * 
  11.  * @category Gumbo
  12.  * @package Factory
  13.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  14.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  15.  * @author Michael Luster <mluster79@yahoo.com>
  16.  * @link http://sourceforge.net/projects/phpgumbo
  17.  * @version 0.0.1
  18.  */
  19.  
  20. /**
  21.  * Generic Factory Class
  22.  * 
  23.  * The class will return an object of the defined Class Name.  It will automatically
  24.  * determine if the class uses the Singleton interface.  It will pass arguments
  25.  * in the order they appear, but only through the 'new ClassName (..)' call.
  26.  * 
  27.  * The Factory class returns any instance of an object, or null if any failures
  28.  * occur.  This class can be easily extended by other Factories.  The child factory
  29.  * would override the 'factory' method to perform specific operations on the input
  30.  * sent by the client.  It would call the parent (Gumbo_Factory) factory method to
  31.  * get a new object.  If the returned results are 'null', the child class would be
  32.  * able perform any extra processing before returning to the client.
  33.  *
  34.  * @category Gumbo
  35.  * @package Factory
  36.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  37.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  38.  * @author Michael Luster <mluster79@yahoo.com>
  39.  * @link http://sourceforge.net/projects/phpgumbo
  40.  * @desc Generic Factory Class
  41.  * @version 0.0.1
  42.  * @see Gumbo_Converter, Gumbo_Filter
  43.  */
  44.  
  45. gumbo_load ("Interface_Factory");
  46.  
  47. class Gumbo_Factory implements Gumbo_Interface_Factory {
  48.     
  49.     /**
  50.      * Returns an instantiated object.
  51.      * 
  52.      * Each additional argument will be sent to the Constructor of the Object
  53.      * in the order as it appears.
  54.      * 
  55.      * @precondition class_exists()
  56.      * @param string $name name of the class
  57.      * @param mixed $args additional arguments
  58.      * @return StdClass 
  59.      * @throws Gumbo_Exception
  60.      */
  61.     public function factory ($name=null$args=null{
  62.         try {
  63.             // verify precondition
  64.             if (!is_string ($name)) {
  65.                 throw new Gumbo_Exception ("Invalid Argument 'name:str' => {$name}:gettype ($name));
  66.             }
  67.             
  68.             // verify that the class exists
  69.             if (!class_exists ($namefalse)) {
  70.                 throw new Gumbo_Exception ("Class Does Not Exist: {$name}");
  71.             }
  72.             
  73.             // check if implements Singleton
  74.             $singleton false;
  75.             foreach (class_implements ($namefalseas $interface{
  76.                 if ($interface == "Gumbo_Interface_Singleton"{
  77.                     $singleton true;
  78.                 }
  79.             }
  80.             
  81.             // return Singleton Object
  82.             if ($singleton{
  83.                 return call_user_func (array ($name"instance"));
  84.             }
  85.             
  86.             // set any arguments
  87.             $args array ();
  88.             if (func_num_args (1{
  89.                 foreach (func_get_args (as $key=>$val{
  90.                     if ($key == 0continue}
  91.                     $args [$val;
  92.                 }
  93.             }
  94.             
  95.             // return new object instance
  96.             $reflection new ReflectionClass ($name);
  97.             return call_user_func_array (array ($reflection"newInstance")$args);
  98.             
  99.         catch (Gumbo_Exception $e{
  100.             $e->setFunction (__METHOD__);
  101.             gumbo_trigger ($e);
  102.         }
  103.         return null;
  104.     }
  105.     
  106. }
  107.  
  108.  
  109. ?>