Source for file Setting.class.php

Documentation is available at Setting.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 Load
  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.  * Load Setting Class
  22.  *
  23.  * @category Gumbo
  24.  * @package Load
  25.  * @copyright Copyright (c) 2007, iBayou, Michael Luster
  26.  * @license http://www.opensource.org/licenses/bsd-license.php New BSD License
  27.  * @author Michael Luster <mluster79@yahoo.com>
  28.  * @link http://sourceforge.net/projects/phpgumbo
  29.  * @desc Load Setting Class
  30.  * @version 0.0.1
  31.  */
  32.  
  33. gumbo_load ("Interface_Load_Setting");
  34. gumbo_load ("Setting");
  35.  
  36.     
  37.     /**
  38.      * Constructor
  39.      * 
  40.      * The settings parameter will construct a new object from the array, overriding
  41.      * the default settings.  If the value is not set, the default will be used.  The
  42.      * array should contain key=>value pairs, where the key references a property of
  43.      * the object.  The key should exclude the underscore of the property name.  For
  44.      * example: <code>array ("ext"=>"html")</code> will set the $_ext value.
  45.      * 
  46.      * @param array $settings list of settings in key=>val pairs
  47.      * @throws Gumbo_Exception
  48.      */
  49.     public function __construct ($settings=array ()) {
  50.         
  51.         // set the defaults
  52.         parent::add ("class"false);
  53.         parent::add ("dirs");
  54.         parent::add ("ext""php");
  55.         parent::add ("stretch"false);
  56.         parent::add ("separator""_");
  57.         parent::add ("path""include");
  58.         parent::add ("once"false);
  59.         parent::add ("include"false);
  60.         parent::add ("remove"null);
  61.         
  62.         try {
  63.             // verify precondition
  64.             if (!is_array ($settings)) {
  65.                 throw new Gumbo_Exception ("Invalid Argument 'settings:arr' => {$settings}:gettype ($settings));
  66.             }
  67.             
  68.             foreach ($settings as $key=>$val{
  69.                 switch ($key{
  70.                     case "class" $this->setClass ($val)break;
  71.                     case "dirs" $this->setDirs ($val)break;
  72.                     case "extension" $this->setExtension ($val)break;
  73.                     case "ext" $this->setExtension ($val)break;
  74.                     case "once" $this->setOnce ($val)break;
  75.                     case "include" $this->setInclude ($val)break;
  76.                     case "stretch" $this->setStretch ($val)break;
  77.                     case "separator" $this->setSeparator ($val)break;
  78.                     case "path" $this->setPath ($val)break;
  79.                     case "remove" $this->setRemove ($val)break;
  80.                 }
  81.             }
  82.         catch (Gumbo_Exception $e{
  83.             $e->setFunction (__METHOD__);
  84.             gumbo_trigger ($e);
  85.         }
  86.     }
  87.     
  88.     
  89.     
  90.     /** ACTION METHODS **/
  91.     /**
  92.      * Adds a key=>value pair to the setting
  93.      * @param string $key 
  94.      * @param mixed $val 
  95.      * @throws Gumbo_Exception
  96.      */
  97.     public function add ($key$val=null{
  98.         try {
  99.             // verify precondition
  100.             if (!is_string ($key)) {
  101.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  102.             }
  103.             
  104.             switch ($key{
  105.                 case "class" $this->setClass ($val)break;
  106.                 case "dirs" $this->setDirs ($val)break;
  107.                 case "extension" $this->setExtension ($val)break;
  108.                 case "ext" $this->setExtension ($val)break;
  109.                 case "once" $this->setOnce ($val)break;
  110.                 case "include" $this->setInclude ($val)break;
  111.                 case "stretch" $this->setStretch ($val)break;
  112.                 case "separator" $this->setSeparator ($val)break;
  113.                 case "path" $this->setPath ($val)break;
  114.                 case "remove" $this->setRemove ($val)break;
  115.             }
  116.         catch (Gumbo_Exception $e{
  117.             $e->setFunction (__METHOD__);
  118.             gumbo_trigger ($e);
  119.         }
  120.     }
  121.     
  122.     /**
  123.      * Removes the setting
  124.      * @param string $key 
  125.      * @throws Gumbo_Exception
  126.      */
  127.     public function remove ($key{
  128.         try {
  129.             // verify precondition
  130.             if (!is_string ($key)) {
  131.                 throw new Gumbo_Exception ("Invalid Argument 'key:str' => {$key}:gettype ($key));
  132.             }
  133.             if (!$this->exists ($key)) {
  134.                 throw new Gumbo_Exception ("Invalid Setting Key: {$key}");
  135.             }
  136.             
  137.             switch ($key{
  138.                 case "class" parent::add ("class"false)break;
  139.                 case "dirs" parent::add ("dirs"null)break;
  140.                 case "extension" parent::add ("extension""php")break;
  141.                 case "ext" parent::add ("extension""php")break;
  142.                 case "once" parent::add ("once"false)break;
  143.                 case "include" parent::add ("include"false)break;
  144.                 case "stretch" parent::add ("stretch"false)break;
  145.                 case "separator" parent::add ("separator""_")break;
  146.                 case "path" parent::add ("path""include")break;
  147.                 case "remove" parent::add ("remove"null)break;
  148.             }
  149.             
  150.         catch (Gumbo_Exception $e{
  151.             $e->setFunction (__METHOD__);
  152.             gumbo_trigger ($e);
  153.         }
  154.     }
  155.     
  156.     
  157.     
  158.     /**
  159.      * Adds a directory path to the list (additional arguments may be accepted)
  160.      * @param string $dir 
  161.      * @throws Gumbo_Exception
  162.      */
  163.     public function addDir ($dir{
  164.         try {
  165.             // verify precondition
  166.             if (!is_string ($dir)) {
  167.                 throw new Gumbo_Exception ("Invalid Argument 'dir:str' => {$dir}:gettype ($dir));
  168.             }
  169.             
  170.             if (func_num_args (1{
  171.                 foreach (func_get_args (as $val{
  172.                     $this->addDir ($val);
  173.                 }
  174.                 return;
  175.             }
  176.             
  177.             // check if location was previously set
  178.             $tmp array ();
  179.             $tmp [$dir;
  180.             foreach ($this->getDirs (as $val{
  181.                 if ($val == $dir{
  182.                     return;
  183.                 }
  184.                 $tmp [$val;
  185.             }
  186.             
  187.             parent::add ("dirs"$tmp);
  188.         catch (Gumbo_Exception $e{
  189.             $e->setFunction (__METHOD__);
  190.             gumbo_trigger ($e);
  191.         }
  192.     }
  193.     
  194.     
  195.     
  196.     /** MUTATOR METHODS **/
  197.     /**
  198.      * Sets the setting to load a class file
  199.      * @param bool $val 
  200.      * @throws Gumbo_Exception
  201.      */
  202.     public function setClass ($val{
  203.         try {
  204.             // verify precondition
  205.             if (!is_bool ($val)) {
  206.                 throw new Gumbo_Exception ("Invalid Argument 'val:bool' => {$val}:gettype ($val));
  207.             }
  208.             
  209.             parent::add ("class"$val);
  210.         catch (Gumbo_Exception $e{
  211.             $e->setFunction (__METHOD__);
  212.             gumbo_trigger ($e);
  213.         }
  214.     }
  215.     
  216.     /**
  217.      * Adds a list of directories to the list
  218.      * 
  219.      * There are three ways to set the Directory paths.  The first method passes
  220.      * a string, with either a single directory or list of directories separated
  221.      * by the PATH_SEPARATOR constant.  The second passes an array, with each
  222.      * element value a different directory.  The third involves either of the first
  223.      * two methods, but multiple arguments.
  224.      * 
  225.      * 1. $this->setDirs ("path1:path2:path3/path4");
  226.      * 2. $this->setDirs (array ("path1", "path2", "path3/path4"));
  227.      * 3. $this->setDirs ("path1", array ("path2"), "path3/path4:path5");
  228.      * 
  229.      * @param array|string$dirs ...[]
  230.      * @throws Gumbo_Exception
  231.      */
  232.     public function setDirs ($dirs{
  233.         try {
  234.             // verify precondition
  235.             if (!is_array ($dirs&& !is_string ($dirs)) {
  236.                 throw new Gumbo_Exception ("Invalid Argument 'dirs:str|arr' => {$dirs}:gettype ($dirs));
  237.             }
  238.             
  239.             // check for multiple arguments
  240.             if (func_num_args (1{
  241.                 foreach (func_get_args (as $val{
  242.                     $this->setDirs ($val);
  243.                 }
  244.                 return;
  245.             }
  246.             
  247.             // check for an array
  248.             if (is_array ($dirs)) {
  249.                 foreach ($dirs as $dir{
  250.                     $this->setDirs ($dir);
  251.                 }
  252.                 return;
  253.             }
  254.             
  255.             // check the path separator
  256.             if (strstr ($dirsPATH_SEPARATOR)) {
  257.                 $dirs explode (PATH_SEPARATOR$dirs);
  258.                 foreach ($dirs as $dir{
  259.                     $this->setDirs ($dir);
  260.                 }
  261.                 return;
  262.             }
  263.             
  264.             // add the new Directory
  265.             $this->addDir ($dir);
  266.         catch (Gumbo_Exception $e{
  267.             $e->setFunction (__METHOD__);
  268.             gumbo_trigger ($e);
  269.         }
  270.     }
  271.     
  272.     /**
  273.      * Sets the file extension (leading "." not necessary)
  274.      * @param string $ext 
  275.      * @throws Gumbo_Exception
  276.      */
  277.     public function setExtension ($ext{
  278.         try {
  279.             // verify precondition
  280.             if (!is_null ($ext&& !is_string ($ext)) {
  281.                 throw new Gumbo_Exception ("Invalid Argument 'ext:str' => {$ext}:gettype ($ext));
  282.             }
  283.             
  284.             parent::add ("ext"$ext);
  285.         catch (Gumbo_Exception $e{
  286.             $e->setFunction (__METHOD__);
  287.             gumbo_trigger ($e);
  288.         }
  289.     }
  290.     
  291.     /**
  292.      * Sets the load only once setting
  293.      * @param bool $once 
  294.      * @throws Gumbo_Exception
  295.      */
  296.     public function setOnce ($once{
  297.         try {
  298.             // verify precondition
  299.             if (!is_bool ($once)) {
  300.                 throw new Gumbo_Exception ("Invalid Argument 'once:bool' => {$once}:gettype ($once));
  301.             }
  302.             
  303.             parent::add ("once"$once);
  304.         catch (Gumbo_Exception $e{
  305.             $e->setFunction (__METHOD__);
  306.             gumbo_trigger ($e);
  307.         }
  308.     }
  309.     
  310.     /**
  311.      * Sets to use include instead of require
  312.      * @param bool $include 
  313.      * @throws Gumbo_Exception
  314.      */
  315.     public function setInclude ($include{
  316.         try {
  317.             // verify precondition
  318.             if (!is_bool ($include)) {
  319.                 throw new Gumbo_Exception ("Invalid Argument 'include:bool' => {$include}:gettype ($include));
  320.             }
  321.             
  322.             parent::add ("include"$include);
  323.         catch (Gumbo_Exception $e{
  324.             $e->setFunction (__METHOD__);
  325.             gumbo_trigger ($e);
  326.         }
  327.     }
  328.     
  329.     /**
  330.      * Sets to stretch the file name as additional directory paths
  331.      * @param bool $stretch 
  332.      * @throws Gumbo_Exception
  333.      */
  334.     public function setStretch ($stretch{
  335.         try {
  336.             // verify precondition
  337.             if (!is_bool ($stretch)) {
  338.                 throw new Gumbo_Exception ("Invalid Argument 'stretch:bool' => {$stretch}:gettype ($stretch));
  339.             }
  340.             
  341.             parent::add ("stretch"$stretch);
  342.         catch (Gumbo_Exception $e{
  343.             $e->setFunction (__METHOD__);
  344.             gumbo_trigger ($e);
  345.         }
  346.     }
  347.     
  348.     /**
  349.      * Sets the stretch separator character
  350.      * @param string $sep 
  351.      * @throws Gumbo_Exception
  352.      */
  353.     public function setSeparator ($sep{
  354.         try {
  355.             // verify precondition
  356.             if (!is_null ($sep&& !is_string ($sep)) {
  357.                 throw new Gumbo_Exception ("Invalid Argument 'sep:str' => {$sep}:gettype ($sep));
  358.             }
  359.             
  360.             parent::add ("separator"$sep);
  361.         catch (Gumbo_Exception $e{
  362.             $e->setFunction (__METHOD__);
  363.             gumbo_trigger ($e);
  364.         }
  365.     }
  366.     
  367.     /**
  368.      * Sets the path to start searching from.
  369.      * Possible values:
  370.      * - include: search the include path (default)
  371.      * - home: start from Gumbo home directory
  372.      * - absolute: ignores starting point and uses dirs as absolute paths
  373.      * 
  374.      * @param string $path 
  375.      * @throws Gumbo_Exception
  376.      */
  377.     public function setPath ($path{
  378.         try {
  379.             // verify precondition
  380.             if (!is_string ($path)) {
  381.                 throw new Gumbo_Exception ("Invalid Argument 'path:str' => {$path}:gettype ($path));
  382.             }
  383.             
  384.             switch ($path{
  385.                 case "home" break;
  386.                 case "absolute" break;
  387.                 case "include" break;
  388.                 default : throw new Gumbo_Exception ("Invalid Path: {$path}");
  389.             }
  390.             
  391.             parent::add ("path"$path);
  392.         catch (Gumbo_Exception $e{
  393.             $e->setFunction (__METHOD__);
  394.             gumbo_trigger ($e);
  395.         }
  396.     }
  397.     
  398.     /**
  399.      * Sets a string pattern to remove from the file name before search
  400.      * @param string $remove 
  401.      * @throws Gumbo_Exception
  402.      */
  403.     public function setRemove ($remove{
  404.         try {
  405.             // verify precondition
  406.             if (!is_string ($remove)) {
  407.                 throw new Gumbo_Exception ("Invalid Argument 'remove:str' => {$remove}:gettype ($remove));
  408.             }
  409.             
  410.             parent::add ("remove"$remove);
  411.         catch (Gumbo_Exception $e{
  412.             $e->setFunction (__METHOD__);
  413.             gumbo_trigger ($e);
  414.         }
  415.     }
  416.     
  417.     
  418.     
  419.     /** ACCESSOR METHODS **/
  420.     /**
  421.      * Returns if the setting is for a class load
  422.      * @return bool 
  423.      */
  424.     public function getClass ({
  425.         return $this->get ("class");
  426.     }
  427.     
  428.     /**
  429.      * Returns the list of directory paths
  430.      * @return array 
  431.      */
  432.     public function getDirs ({
  433.         return $this->get ("dirs");
  434.     }
  435.     
  436.     /**
  437.      * Returns the file extension
  438.      * @return string 
  439.      */
  440.     public function getExtension ({
  441.         return $this->get ("ext");
  442.     }
  443.     
  444.     /**
  445.      * Returns if the file should be loaded once
  446.      * @return bool 
  447.      */
  448.     public function getOnce ({
  449.         return $this->get ("once");
  450.     }
  451.     
  452.     /**
  453.      * Returns if the include function should be used instead of require
  454.      * @return bool 
  455.      */
  456.     public function getInclude ({
  457.         return $this->get ("include");
  458.     }
  459.     
  460.     /**
  461.      * Returns if the file name should stretch
  462.      * @return bool 
  463.      */
  464.     public function getStretch ({
  465.         return $this->get ("stretch");
  466.     }
  467.     
  468.     /**
  469.      * Returns the stretch separator character
  470.      * @return string 
  471.      */
  472.     public function getSeparator ({
  473.         return $this->get ("separator");
  474.     }
  475.     
  476.     /**
  477.      * Returns the path to start searching from
  478.      * @return string 
  479.      */
  480.     public function getPath ({
  481.         return $this->get ("path");
  482.     }
  483.     
  484.     /**
  485.      * Returns the remove string pattern
  486.      * @return string 
  487.      */
  488.     public function getRemove ({
  489.         return $this->get ("remove");
  490.     }
  491.     
  492. }
  493.  
  494. ?>