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

Source for file config_loader_file.class.php

Documentation is available at config_loader_file.class.php

  1. <?php
  2. // *** Tangra (Application Framework and Tools for PHP)
  3. //  $Id$
  4. //
  5.  
  6.  
  7. /**
  8.  * Contains class Config_Loader_File
  9.  * @package  tangra_lib
  10.  * @subpackage  core
  11.  */
  12.  
  13. /**
  14.  * Loading parent class
  15.  */
  16. require_once(TANGRA_MAIN_DIR.'core/config_loader.class.php');
  17.  
  18. /**
  19.  * Loading exception
  20.  */
  21. require_once(TANGRA_MAIN_DIR.'exceptions/te_config_loader_file_error_fatal.class.php');
  22.  
  23.  
  24. /**
  25.  * Loads configuration data from file.
  26.  *
  27.  * Loads configuration data from file. According to Tangra Filenames Convention (TODO - provide link) configuration files has *.conf extention.
  28.  *
  29.  * @package  tangra_lib
  30.  * @subpackage  core
  31.  */
  32. class Config_Loader_File extends Config_Loader {
  33.     private $imports = array();
  34.  
  35.     /**
  36.      * Loads the configuration file and parses it. Ater construction object is ready for use (calls to get_conf_value() method)
  37.      *
  38.      * @param string $file - full path to fconfiguration file
  39.      */
  40.     function __construct($file{
  41.         if (file_exists($file)) {
  42.             if (is_file($file)) {
  43.                 if (is_readable($file)) {
  44.                     $file_content $this->load_file($file);
  45.                     $conf_arr $this->parse_file_arr($file_content);
  46.                     $tmp_arr $this->parse_conf_arr($conf_arr);
  47.                     $this->set_pairs(array_merge($this->pairs$tmp_arr));
  48.  
  49.                     if ($this->imports{
  50.                         foreach($this->imports as $import_file{
  51.                             $this->import_file($import_filedirname($file));
  52.                         }
  53.                     }
  54.  
  55.                     foreach($this->pairs as $pair_key => &$pair_value{
  56.                         $pair_value trim($pair_value);
  57.                         $pair_value $this->expand($pair_value$pair_key);
  58.                     }
  59.                 else {
  60.                     throw new TE_Config_Loader_File_Error_Fatal('Config file is not readable. File: '.$file);
  61.                 }
  62.             else {
  63.                 throw new TE_Config_Loader_File_Error_Fatal('Parameter $file is not file. File: '.$file);
  64.             }
  65.         else {
  66.             throw new TE_Config_Loader_File_Error_Fatal('Config file does not exists. File: '.$file);
  67.         }
  68.     }
  69.  
  70.  
  71.     /**
  72.      * Loads
  73.      *
  74.      * @param $string $file 
  75.      * @return boolean - returns file contents as  array one element per row
  76.      * @access private
  77.      */
  78.     private function load_file($file{
  79.         return file($file);
  80.     }
  81.  
  82.  
  83.     /**
  84.      * Parses array loaded by $this->load_file(). Skips rows that are commets or missformed (i.e. starting with blank space)
  85.      *
  86.      * @param array $file_arr 
  87.      * @return array simple associative array that contain just "valid" rows
  88.      * @access private
  89.      *
  90.       */
  91.     private function parse_file_arr($file_arr{
  92.         $ret_arr array();
  93.  
  94.         for($i 0$i count($file_arr)$i++{
  95.             if (strlen($file_arr[$i])) {
  96.                 $first_char substr($file_arr[$i]01);
  97.                 if ($first_char != ' ' && $first_char != "\t" &&
  98.                          $first_char != '/' && $first_char != '#' &&
  99.                          $first_char != "\n"{
  100.                     if ($first_char != '!'{
  101.                         array_push($ret_arr$file_arr[$i]);
  102.                     else {
  103.                         $this->parse_command_line($file_arr[$i]$i);
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.  
  109.         return $ret_arr;
  110.     }
  111.  
  112.  
  113.     /**
  114.      * Parses array returned by $this->parse_file_arr. Populates $this->pairs.
  115.      *
  116.      * @param unknown_type $conf_arr 
  117.      * @return array simple associative array that may be used (and it is used in __construct) for setting $this->pairs
  118.      * @access private
  119.      */
  120.     private function parse_conf_arr($conf_arr{
  121.         $ret_arr array();
  122.         foreach($conf_arr as $row{
  123.             $first_equal_pos strpos($row'=');
  124.             if (($first_equal_pos !== false&& ($first_equal_pos 0)) {
  125.                 $key substr($row0$first_equal_pos);
  126.                 $value substr($row$first_equal_pos 1);
  127.                 $ret_arr[$key$value;
  128.             }
  129.         }
  130.  
  131.         return $ret_arr;
  132.     }
  133.  
  134.  
  135.     /**
  136.      * Parses line that contains command, e.g. starting with "!"
  137.      *
  138.      * @param string $line 
  139.      */
  140.     private function parse_command_line($line$line_no{
  141.         $tmp_arr explode(' '$line);
  142.         if ($tmp_arr == $line{
  143.             $tmp_arr explode("\t"$line);
  144.         }
  145.  
  146.         if (is_array($tmp_arr)) {
  147.             switch(trim($tmp_arr[0])) {
  148.                 case '!import':
  149.                     if (array_key_exists(1$tmp_arr)) {
  150.                         $this->imports[trim($tmp_arr[1]);
  151.                     else {
  152.                         throw new TE_Config_Loader_File_Error_Fatal("No file specified for import at line $line_no.");
  153.                     }
  154.                     break;
  155.                 default:
  156.                     throw new TE_Config_Loader_File_Error_Fatal("Unknown command at line $line_no".$tmp_arr[0]);
  157.                     break;
  158.             }
  159.         else {
  160.             throw new TE_Config_Loader_File_Error_Fatal("Invalid command at line $line_no".$line);
  161.         }
  162.     }
  163.  
  164.  
  165.     private function import_file($file$config_dir{
  166.         $config_dir tangra_normalize_path($config_dir);
  167.  
  168.         $file_fullpath $config_dir.$file;
  169.         if (file_exists($file_fullpath)) {
  170.             $tmp_clf new Config_Loader_File($file_fullpath);
  171.             $pairs $tmp_clf->get_pairs();
  172.             foreach($pairs as $key => $value{
  173.                 $this->add_pair($key$value);
  174.             }
  175.         else {
  176.             throw new TE_Config_Loader_File_Error_Fatal("Can not find import file $file in dir $config_dir ");
  177.         }
  178.     }
  179. }