Ejemplo n.º 1
0
 /**
  * Reads a config template and config file and parses them for documented
  * and undocumented config entries.
  * 
  * @param String config_path file path to the config file
  * @param String config_template_path file path to the config template
  */
 function Config($config_path = NULL, $config_template_path = NULL)
 {
     $this->config_path = $config_path;
     $this->config_template_path = $config_template_path;
     $this->entries = array();
     $this->undocumented_entries = array();
     $config = NULL;
     if (is_string($config_template_path)) {
         if (!file_exists($config_template_path) || !is_array($config = file($config_template_path))) {
             throw new Exception("Could not read config template file: " . $this->config_template_path);
         }
     } else {
         if (is_array($config_template_path)) {
             $config = $config_template_path;
         }
     }
     if (is_array($config)) {
         // parse config template
         $current_question = NULL;
         $current_type = NULL;
         $current_info = NULL;
         foreach ($config as $line) {
             $match = array();
             // new question:
             if (preg_match("#^([ \t]*///[ \t]*)(" . implode("|", ConfigEntry::get_config_types()) . ")([ \t]*:[ \t]*)(.*)[ \t]*\$#i", $line, $match) > 0) {
                 $current_type = $match[2];
                 $current_question = $match[4];
                 $current_info = NULL;
                 continue;
             }
             // additional info:
             if (preg_match("#^([ \t]*///[ \t]*)([^/].*)[ \t]*\$#", $line, $match) > 0) {
                 if (!is_string($current_info)) {
                     $current_info = "";
                 }
                 $current_info .= $match[2];
                 continue;
             }
             // config entry:
             if (preg_match("#^([ \t]*)([ \t]*define[ \t]*\\([ \t]*\")([a-zA-Z0-9_]+)(\"[ \t]*,[ \t]*)\"?(?U)(.*)(?-U)\"?([ \t]*\\)[ \t]*;.*)\$#", $line, $match) > 0) {
                 if (!is_string($current_type) || !is_string($current_question)) {
                     $this->add_undocumented_entry($match[3], $match[5]);
                     continue;
                 }
                 $this->add_entry($match[3], $current_type, $current_question, $current_info, ConfigEntry::config_value_to_answer($current_type, $match[5]), NULL);
                 continue;
             }
             // other comment:
             if (preg_match("#^[ \t]*//.*\$#", $line) > 0) {
                 continue;
             }
             // otherwise, the current config entry ends:
             $current_type = NULL;
             $current_question = NULL;
             $current_info = NULL;
         }
     }
     $config = NULL;
     if (is_string($config_path)) {
         if (!file_exists($config_path) || !is_array($config = file($config_path))) {
             $config = NULL;
         }
     } else {
         if (is_array($config_path)) {
             $config = $config_path;
         }
     }
     if (is_array($config)) {
         // parse config file
         $config = file($config_path);
         foreach ($config as $line) {
             // scan undocumented entries:
             $match = array();
             if (preg_match("#^([ \t]*)([ \t]*define[ \t]*\\([ \t]*\")([a-zA-Z0-9_]+)(\"[ \t]*,[ \t]*)\"?(?U)(.*)(?-U)\"?([ \t]*\\)[ \t]*;.*)\$#", $line, $match) < 1) {
                 continue;
             }
             if (is_object($this->get_undocumented_entry($match[3]))) {
                 $this->get_undocumented_entry($match[3])->set_config_answer($match[5]);
             }
         }
         if (is_array($config)) {
             foreach ($this->entries as $entry) {
                 $entry->parse_config($config);
             }
         }
     }
 }