Source for file Php.class.php

Documentation is available at Php.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 Config
  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.  * Config PHP Reader
  22.  * 
  23.  * A PHP Reader will simply include the defined PHP file, and parse an array into
  24.  * a Composite object
  25.  *
  26.  * @category Gumbo
  27.  * @package Config
  28.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  29.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  30.  * @author Michael Luster <mluster79@yahoo.com>
  31.  * @link http://sourceforge.net/projects/phpgumbo
  32.  * @desc Config PHP Reader
  33.  * @version 0.0.1
  34.  */
  35.  
  36. gumbo_load ("Interface_Config_Reader");
  37.  
  38. class Gumbo_Config_Reader_Php implements Gumbo_Interface_Config_Reader {
  39.     
  40.     /** @var string $_name config variable name */
  41.     private $_name = "config";
  42.     
  43.     
  44.     
  45.     /**
  46.      * Constructor
  47.      * @param string $name config variable name
  48.      */
  49.     public function __construct ($name=null{
  50.         if (!is_null ($name)) $this->setName ($name)}
  51.     }
  52.     
  53.     
  54.     
  55.     /** ACTION METHODS **/
  56.     /**
  57.      * Reads the given configuration into memory, and returns the results
  58.      * @param string $file 
  59.      * @return Gumbo_Interface_Composite 
  60.      * @throws Gumbo_Exception
  61.      * @uses Gumbo_Converter, Gumbo_Branch
  62.      */
  63.     public function read ($file{
  64.         gumbo_load ("Interface_Composite");
  65.         gumbo_load ("Branch");
  66.         gumbo_load ("Converter");
  67.         
  68.         try {
  69.             // verify precondition
  70.             if (!is_string ($file)) {
  71.                 throw new Gumbo_Exception ("Invalid Argument 'file:str' => {$file}:gettype ($file));
  72.             }
  73.             if (!file_exists ($file)) {
  74.                 throw new Gumbo_Exception ("File Not Found: {$file}");
  75.             }
  76.             $name $this->getName ();
  77.             
  78.             // include the file into memory
  79.             include ($file);
  80.             if (!isset ($$name)) {
  81.                 throw new Gumbo_Exception ("Invalid Configuration Variable: {$name}");
  82.             }
  83.             if (!is_array ($$name)) {
  84.                 throw new Gumbo_Exception ("Invalid Variable Type: " gettype ($$name));
  85.             }
  86.             
  87.             $obj new Gumbo_Converter ();
  88.             if (($res $obj->factory ("array")->convert ($$name"composite")) instanceof Gumbo_Interface_Composite{
  89.                 return $res;
  90.             }
  91.         catch (Gumbo_Exception $e{
  92.             $e->setFunction (__METHOD__);
  93.             gumbo_trigger ($e);
  94.         }
  95.         return new Gumbo_Branch ();
  96.     }
  97.     
  98.     
  99.     
  100.     /** MUTATOR METHODS **/
  101.     /**
  102.      * Sets the config variable name
  103.      * @precondition valid PHP variable name
  104.      * @postcondition removes non alpha-numeric (excluding underscores) from the name
  105.      * @param string $name 
  106.      */
  107.     public function setName ($name{
  108.         try {
  109.             // verify precondition
  110.             if (!is_string ($name)) {
  111.                 throw new Gumbo_Exception ("Invalid Argument 'name:str' => {$name}:getType ($name));
  112.             }
  113.             if (!ereg ("[_a-zA-Z]"substr ($name01))) {
  114.                 throw new Gumbo_Exception ("Invalid Variable Name: {$name}");
  115.             }
  116.             $this->_name = preg_replace ("[^a-zA-Z0-9_]"""$name);
  117.         catch (Gumbo_Exception $e{
  118.             $e->setFunction (__METHOD__);
  119.             gumbo_trigger ($e);
  120.         }
  121.     }
  122.     
  123.     
  124.     
  125.     /** ACCESSOR METHODS **/
  126.     /**
  127.      * Returns the Config variable name
  128.      * @return string 
  129.      */
  130.     public function getName ({
  131.         return $this->_name;
  132.     }
  133.     
  134. }
  135.  
  136. ?>