tangra logo
   
[ class tree: tangra_lib ] [ index: tangra_lib ] [ all elements ]
 

Source for file config_loader.class.php

Documentation is available at config_loader.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. // $Id$
  4. //
  5.  
  6. /**
  7.  * Contains class Config_Loader
  8.  * @package  tangra_lib
  9.  * @subpackage  core
  10.  */
  11.  
  12. /**
  13.  * Loading exception
  14.  */
  15. require_once(TANGRA_MAIN_DIR.'exceptions/te_config_loader_error_key_missing.class.php');
  16. require_once(TANGRA_MAIN_DIR.'exceptions/te_config_loader_error.class.php');
  17.  
  18.  
  19. /**
  20.  * Base class for configuration loaders.
  21.  *
  22.  * Base class for all classes of Config_Loader family. Used to define the interface.
  23.  * Config_Loader classes are intended to provide unified mechanism for loading configuration from diferent sources as files, db, net connection, etc.
  24.  *
  25.  *
  26.  * @package  tangra_lib
  27.  * @subpackage  core
  28.  *
  29.  */
  30. abstract class Config_Loader extends Tangra_Class {
  31.     /**
  32.      * $pairs holds key - value pairs in associative array. Key is the name of configuration value and value - the value itself.
  33.      * @access  private
  34.      * @var array 
  35.      */
  36.     protected $pairs = array();
  37.  
  38.  
  39.     /**
  40.      * It is supposed loading of the configuration from external resource as file or DB to be performed at construction time of Config_Loader objects.
  41.      * That means that when you createn new instance of Config_Loader class (some descendant of Config_Loader that is not "abstract") your configuration will be loaded and you can use get_conf_value method to extract values.
  42.      *
  43.      * @param mutable $config_resource Intended to be mutable type, i.e. different Config_Loader descendants may require different paramter type.
  44.      */
  45.     abstract function __construct($config_resource);
  46.  
  47.  
  48.     /**
  49.      * Gets coresponding to $key configuration value.
  50.      *
  51.      * @param string key name of configuration setting
  52.      * @param boolean $halt_on_missing if true exception will be trown if configuration setting specified by $key
  53.      * @return unknown 
  54.      * @throws  TE_Config_Loader_Error_Key_Missing
  55.      */
  56.     final public function get_conf_value($key$halt_on_missing true{
  57.         if (array_key_exists($key$this->pairs)) {
  58.             $ret $this->pairs[$key];
  59. //            $ret = trim($this->pairs[$key]);
  60. //            $ret = $this->expand($ret, $key);
  61.         else {
  62.             if ($halt_on_missing{
  63.                 throw new TE_Config_Loader_Error_Key_Missing('Key: '.$key.' is not found.');
  64.             else {
  65.                 $ret NULL;
  66.             }
  67.         }
  68.  
  69.         return $ret;
  70.     }
  71.  
  72. /**
  73.  * Auxiliary method that may be used to set the internal private $pairs variable.
  74.  * This method normally is not called directly - it is intended to be used by writers of new Config_Loader descendants that require weird approach.
  75.  * @param array $pairs Array must be simple associative array with $key => $value structure.
  76.  * @throws Tangra_Exception
  77.  */
  78.     public function set_pairs($pairs{
  79.         if (is_array($pairs)) {
  80.             $this->pairs = $pairs;
  81.         else {
  82.             Tangra_Exception('Parameter $pairs is not an array. Current value: '.$pairs);
  83.         }
  84.     }
  85.  
  86.  
  87.     /**
  88.      * Returns all key-value pairs
  89.      *
  90.      * @return array 
  91.      */
  92.     public function get_pairs({
  93.         return $this->pairs;
  94.     }
  95.  
  96.  
  97.     /**
  98.      * Expands keys that are present in values
  99.      *
  100.      * If key starts with _AUTO a global function will be called to return the value. Name of the global function have to be strtolower($key) where $key is the key to be expanded.
  101.      * Example:
  102.      * If we have in machine_specific.conf the following:
  103.      * ROOT=%_AUTO_DETECT_ROOT%
  104.      * function with name _auto_detect_root will be called and returned value will be used to replace %_AUTO_DETECT_ROOT%
  105.      *
  106.      * @param string $value 
  107.      * @param string $current_key 
  108.      * @return string 
  109.      */
  110.     protected function expand($value$current_key{
  111.         $ret '';
  112.  
  113.         $keys $this->extract_keys($value);
  114.         if ($keys{
  115.             foreach($keys as $key{
  116.                 if ($current_key != $key{
  117.                     if (strtoupper(substr($key05)) == '_AUTO'{
  118.                         $global_func strtolower($key);
  119.                         $tmp call_user_func($global_func);
  120.                         $value str_replace('%'.$key.'%'$tmp$value);
  121.                     else {
  122.                         if (array_key_exists($key$this->pairs)) {
  123.                             $value str_replace('%'.$key.'%'trim($this->pairs[$key])$value);
  124.                         }
  125.                     }
  126.                 else {
  127.                     throw new TE_Config_Loader_Error('Fatal error occured while trying to expand value "'.$value.'" for key "'.$current_key.'". Recursion.');
  128.                 }
  129.             }
  130.             $ret $value;
  131.         else {
  132.             $ret $value;
  133.         }
  134.  
  135.         return $ret;
  136.     }
  137.  
  138.  
  139.     /**
  140.      * Extracts all keys (delimited by % from both sides)
  141.      *
  142.      * @param string $value 
  143.      * @return array 
  144.      * @internal
  145.      */
  146.     private function extract_keys($value{
  147.         $ret array();
  148.  
  149.         $start strpos($value'%');
  150.         if ($start !== FALSE{
  151.             $ret[$this->extract_key($value$start);
  152.         }
  153.  
  154.         return $ret;
  155.     }
  156.  
  157.     /**
  158.      * Extracts single key that start at position $start in string $value
  159.      *
  160.      * @param string $value 
  161.      * @param integer $start 
  162.      * @return string 
  163.      * @internal
  164.      */
  165.     private function extract_key($value$start{
  166.         $end strpos($value'%'$start 1);
  167.         if ($end !== FALSE{
  168.             $ret substr($value$start 1$end $start 1);
  169.         else {
  170.             throw new TE_Config_Loader_Error('Fatal error occured while trying to expand value "'.$value.'". No end delimiter % found.');
  171.         }
  172.  
  173.         return $ret;
  174.     }
  175.  
  176.  
  177.     /**
  178.      * Adds new key-value pair
  179.      *
  180.      * @param string $key 
  181.      * @param string $value 
  182.      */
  183.     public function add_pair($key$value{
  184.         if (!array_key_exists($key$this->pairs)) {
  185.             $this->pairs[$key$value;
  186.         else {
  187.             throw new TE_Config_Loader_Error("Key $key already exists.");
  188.         }
  189.     }
  190. }