Source for file Config.class.php

Documentation is available at Config.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 Class
  22.  * 
  23.  * The Config Class will perform necessary operations defined by the Config
  24.  * Interface.  Each operation will have a method assigned to it (read).  The
  25.  * client will call the method, passing the necessary arguments.  The class
  26.  * uses a Factory to search for the appropriate Config Operation (Reader).
  27.  * 
  28.  * This will make the Config class universal to any changes made to the Config
  29.  * Package.  The Factory will search inside the Config directory.  It will
  30.  * setup the proper operation ('reader','writer',...) and the type of operation
  31.  * class.  If it's found, the operation will be performed and the results
  32.  * returned.
  33.  *
  34.  * @category Gumbo
  35.  * @package Config
  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 Config Class
  41.  * @version 0.0.1
  42.  */
  43.  
  44. gumbo_load ("Interface_Factory");
  45. gumbo_load ("Interface_Config");
  46. gumbo_load ("Branch");
  47. gumbo_load ("Leaf");
  48. gumbo_load ("Config_Factory");
  49.  
  50. class Gumbo_Config implements Gumbo_Interface_Config, Gumbo_Interface_Factory {
  51.     
  52.     /** ACTION METHODS **/
  53.     /**
  54.      * Returns an instantiated Config Operation
  55.      * 
  56.      * This method is primarily used internally.  It will return a Config
  57.      * Operation class based on the 'type'.  It works specifically on Gumbo
  58.      * defined Config Operations.  The class name is automatically
  59.      * generated based on the arguments.
  60.      * 
  61.      * The first argument 'name' will be the type of Config operation interface to
  62.      * load ('reader','writer',...).
  63.      * 
  64.      * The second argument 'args' will be a string determining what type
  65.      * of operation class to find ('ini','php',...).  This isn't restricted to
  66.      * a file type, but to a defined Config Operation Class.  The type name could
  67.      * be 'foo', and it would load the 'Gumbo_Config_[Reader]_Foo' class (if calling
  68.      * for a 'Reader' operation).
  69.      * 
  70.      * @param string $name configuration interface
  71.      * @param string $args type of configuration interface class
  72.      * @return Gumbo_Interface_Config_* 
  73.      * @throws Gumbo_Exception
  74.      */
  75.     public function factory ($name=null, $args=null) {
  76.         try {
  77.             // verify precondition
  78.             if (!is_string ($name)) {
  79.                 throw new Gumbo_Exception ("Invalid Argument 'name:str' => {$name}:" . gettype ($name));
  80.             }
  81.             if (!is_string ($args)) {
  82.                 throw new Gumbo_Exception ("Inalid Argument 'args:str' => {$args}:" . gettype ($type));
  83.             }
  84.             $name = ucfirst (strtolower ($name));
  85.             $args = ucfirst (strtolower ($args));
  86.             
  87.             // set the Config class name
  88.             $class_name = "Gumbo_Config_{$name}_{$type}";
  89.             if (!class_exists ($class_name, false) && !gumbo_load ($class_name)) {
  90.                 throw new Gumbo_Exception_Class ("Class Does Not Exist: {$class_name}");
  91.             }
  92.             
  93.             return new $class_name ();
  94.         } catch (Gumbo_Exception $e) {
  95.             $e->setFunction (__METHOD__);
  96.             gumbo_trigger ($e);
  97.         }
  98.         return null;
  99.     }
  100.     
  101.     /**
  102.      * Reads the configuration file and returns the values
  103.      * @param string $file file to load (full path)
  104.      * @param string $type type of configuration file
  105.      * @return Gumbo_Interface_Composite 
  106.      * @throws Gumbo_Exception
  107.      * @see Gumbo_Interface_Composite
  108.      */
  109.     public function read ($file, $type="ini") {
  110.         $tree = new Gumbo_Branch ();
  111.         try {
  112.             // verify precondition
  113.             if (!is_string ($file)) {
  114.                 throw new Gumbo_Exception ("Invalid Argument 'file:str' => {$file}:" . gettype ($file));
  115.             }
  116.             if (!file_exists ($file)) {
  117.                 throw new Gumbo_Exception ("File Does Not Exist: {$file}");
  118.             }
  119.             try {
  120.                 // verify precondition
  121.                 if (!is_string ($type)) {
  122.                     throw new Gumbo_Exception ("Invalid Argument 'type:str' => {$type}:" . gettype ($type));
  123.                 }
  124.             } catch (Gumbo_Exception $e) {
  125.                 $e->setFunction (__METHOD__);
  126.                 gumbo_trigger ($e);
  127.                 $type = "ini";
  128.             }
  129.             
  130.             if (($reader = $this->factory ("reader", $type)) instanceof Gumbo_Interface_Config_Reader) {
  131.                 $tree = $reader->read ($file);
  132.             }
  133.         } catch (Gumbo_Exception $e) {
  134.             $e->setFunction (__METHOD__);
  135.             gumbo_trigger ($e);
  136.         }
  137.         return $tree;
  138.     }
  139.     
  140.     /**
  141.      * Writes a configuration file
  142.      * @param Gumbo_Interface_Composite $tree data to write
  143.      * @param string $file file name (full path)
  144.      * @param string $type type of configuration file
  145.      * @param bool $replace replaces the current file
  146.      * @return bool if operation was successful
  147.      * @throws Gumbo_Exception
  148.      * @see Gumbo_Interface_Composite
  149.      */
  150.     public function write (Gumbo_Interface_Composite $tree, $file, $type="ini", $replace=true) {
  151.         try {
  152.             // verify precondition
  153.             if (!is_string ($file)) {
  154.                 throw new Gumbo_Exception ("Invalid Argument 'file:str' => {$file}:" . gettype ($file));
  155.             }
  156.             try {
  157.                 // verify precondition
  158.                 if (!is_string ($type)) {
  159.                     throw new Gumbo_Exception ("Invalid Argument 'type:str' => {$type}:" . gettype ($type));
  160.                 }
  161.             } catch (Gumbo_Exception $e) {
  162.                 $e->setFunction (__METHOD__);
  163.                 gumbo_trigger ($e);
  164.                 $type = "ini";
  165.             }
  166.             try {
  167.                 // verify precondition
  168.                 if (!is_bool ($replace)) {
  169.                     throw new Gumbo_Exception ("Invalid Argument 'replace:bool' => {$replace}:" . gettype ($replace));
  170.                 }
  171.             } catch (Gumbo_Exception $e) {
  172.                 $e->setFunction (__METHOD__);
  173.                 gumbo_trigger ($e);
  174.                 $replace = false;
  175.             }
  176.             
  177.             if (($writer = $this->factory ("writer", $type)) instanceof Gumbo_Interface_Config_Writer) {
  178.                 return $writer->write ($tree, $file, $replace);
  179.             }
  180.         } catch (Gumbo_Exception $e) {
  181.             $e->setFunction (__METHOD__);
  182.             gumbo_trigger ($e);
  183.         }
  184.         return false;
  185.     }
  186.     
  187. }
  188.  
  189. ?>