User login

Drupal's .info file parser versus PHP's .ini file parser

Searched words: 
what is faster, reading .ini or database [to be answered when we have something to test

why does Drupal have its own .ini style parsing function - http://d6-api.drupal.org/api/function/drupal_parse_info_file - instead of using PHP's parse_ini_file - http://us3.php.net/manual/en/function.parse-ini-file.php ? (no, this is not academic for me!)
[4:18pm] Berdir: benjamin-agaric: because it's a different syntax.
[4:19pm] Berdir: benjamin-agaric: there is no standard for .ini files, and php does use a different one for php.ini than drupal does

I think the only Drupal module to write .info file must be Module Builder. Unfortunately, it does so in a very hard-coded way:

<?php
function generate_info($form_values) {
  $info = MODULE_BUILDER_INFO_ID_COMMENT ."\n";
  $info .= 'name = ';
  if (!empty($form_values['module_readable_name'])) {
    $info .= $form_values['module_readable_name'];
  }
  else {
    $info .= $form_values['module_root_name'];
  }
  $info .= "\n";

  $info .= 'description = '. $form_values['module_short_description'] ."\n";
  if (!empty($form_values['module_dependencies'])) {
    foreach (explode(' ', $form_values['module_dependencies']) as $dep) {
      $info .= 'dependencies[] = '. $dep ."\n";
    }
  }
  if (!empty($form_values['module_package'])) {
    $info .= 'package = '. $form_values['module_package'] ."\n";
  }
  $info .= "core = 6.x\n";

  return $info;
}
?>

Only found a very simple writer for .ini files that, interestingly, is a writer and reader of INI files in PHP. Why does everyone hate parse_ini_file? Here is the writing function:

<?php
/**
* Write data to an INI file

* The data array has to be like this:

*  Array
*  (
*      [Section1] => Array
*          (
*              [key1] => val1
*              [key2] => val2
*          )
*      [Section2] => Array
*          (
*              [key3] => val3
*              [key4] => val4
*          )    
*  )
*
* @param string $filePath
* @param array $data
*/ 
function ini_write($filePath, array $data) 

    $output = ''; 
     
    foreach ($data as $section => $values) 
    { 
        //values must be an array 
        if (!is_array($values)) { 
            continue; 
        } 
         
        //add section 
        $output .= "[$section]\r\n"; 
         
        //add key/value pairs 
        foreach ($values as $key => $val) { 
            $output .= "$key=$val\r\n"; 
        } 
        $output .= "\r\n"; 
    } 
     
    //write data to file 
    file_put_contents($filePath, trim($output)); 

?>

Resolution

Comments

Is the only difference that

Is the only difference that Drupal's .info files allow arbitrarily-deep nested arrays while PHP's native parse_ini_file() allows only one dimension?

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Syntax highlight code surrounded by the {syntaxhighlighter SPEC}...{/syntaxhighlighter} tags, where SPEC is a Syntaxhighlighter options string or "class="OPTIONS" title="the title".
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.