MConfig
First Look
The MConfig class allows loading and working with configuration files that are a hybrid between classic configuration files (like Properties, key=value) and XML files.
Example:
# un commentaire
cle1=val1
<bloc1>
clebloc1 = valbloc1
<bloc11> clebloc11 = valbloc11 </bloc11>
</bloc1>
<bloc2> # un autre commentaire
clebloc2 = valbloc2
<bloc21> clebloc211 = valbloc211; clebloc212 = valbloc212 </bloc21>
<bloc22>
<bloc221> clebloc221 = valbloc221 </bloc221>
clebloc22
</bloc22>
</bloc2>
cle2 = val2
- If a key has no value, it's considered a boolean and set to true.
- The semicolon is not required after a key-value pair if there are no other key-value pairs on the same line.
- Empty lines and commented lines are ignored.
- Comments can be added at the end of a line.
- Spaces are not significant.
- A key cannot contain the "=" sign, but a value can.
- A value can be enclosed in double quotes (key = "value")
- If a value needs to contain any of these characters
" ; # < >, they must be escaped:\" \; \# \< \>
Usage example:
try{
$myconf = new MConfig("path/to/myconf.conf");
}
catch(MConfigException $e){
echo $e->getMessage();
}
echo $myconf->toString();
$value = $myconf->getParameter("cle1");
$value2 = $myconf->getConfig("bloc2")->getParameter("clebloc2");
Usage
An MConfig object can contain:
- Other MConfig objects (corresponding to configuration file "blocks")
- Key/value parameters
The main methods used with an MConfig object are:
- getConfig(String $key): Returns an MConfig object corresponding to the block with label $key, or null if the block doesn't exist
- getParameter(String $key): Returns the value of the parameter with key $key, or null if the parameter doesn't exist
Other useful methods include:
- getParametersNames(): Returns an array containing all parameter keys within an MConfig object (a block or the global file)
- getConfigsNames(): Returns an array containing all block names (labels) of sub-configurations within an MConfig object
- getSubEntriesNames(): Returns an array containing all block labels and parameter keys within the object
You can also dynamically modify an MConfig object, though this is not the primary purpose of the tool, using the setSubEntry(String $key, mixed $entry) method:
$entry can be either any value or an MConfig object Use with caution!!