Class Graphite_IniParser

Ini file parser.

Utility class which performs Mustache-like template expansion on a file before parsing it into a PHP array structure.

Tokens in the ini file are strings that start with `{{` and end wtih `}}`. The exact text inside the delimiters becomes the token. These tokens can be replaced with alternate content by passing an array of replacement values to Graphite_IniParser::parse(). The array should use the exact token text as the key and the replacement value as the value for each substition to be made. Any tokens which do not appear as keys in the replacement variables array will be used verbatium in the output. This allows using a default value as the token itself.

PHP's parse_ini_string()/parse_ini_file() functions are used to parse the ini file. There are several things to be aware of as a result of this.

  • Semicolons (;) should be used for comments.
  • Unquoted numerics are parsed as php integers thus numbers starting by 0 are evaluated as octals and numbers starting by 0x are evaluated as hexadecimals.
  • If a value in the ini file contains any non-alphanumeric characters it needs to be enclosed in double-quotes (").
  • Values enclosed in double quotes can contain new lines.
  • Values not enclosed in double quotes are subject to constant expansion. This means that values matching the names of constants defined at the time the ini file is parsed will be replaced with the value of the constant.
  • There are reserved words which must not be used as keys for ini files. These include: null, yes, no, true, false, on, off, none. Values null, no and false results in "", yes and true results in "1". Characters ?{}|&~![()^" must not be used anywhere in the key and have a special meaning in the value.
Example:
 ; example ini file
 [section]
 key1 = "{{TOKEN1}}"
 key2 = "{{TOKEN2}}"

  1.  <?php
  2.  $ini Graphite_IniParser::parse('/path/to/file.ini'array(
  3.     'TOKEN1' => 'replacement 1',
  4.     'TOKEN2' => 'replacement 2',
  5.   ));

Author(s):
Copyright:   2012 Bryan Davis and contributors. All Rights Reserved.
License:   Simplified BSD License

Constants

RE_PARSE Regular expression used to find substituion tokens in the ini file.

Method Summary

public static array parse( $file , [ $vars = null] )
Parse an ini file and return an array of it's contents.
public string substitute( $match )
Substitute a match with data found in our variables.

Methods

parse

static array parse( string $file , [array $vars = null] )

Parse an ini file and return an array of it's contents.

Parameters:

Name Type Description
$file string Path to ini file
$vars array Variable values to substiute in the file

substitute

string substitute( array $match )

Substitute a match with data found in our variables.

Parameters:

Name Type Description
$match array Regex matches

Located in /src/Graphite/IniParser.php [line 65]