This post originated from an RSS feed registered with PHP Buzz
by Stephan Schmidt.
Original Post: patConfiguration 2.0.0b1 released
Feed Title: a programmer's best friend
Feed URL: http://blog.php-tools.net/rss.php?version=1.0
Feed Description: The blog of PHP Application Tools
I finally managed to release a first public beta of my configuration reader patConfiguration.
Starting with this release it is now driver-based which keeps the codebase small and at the same time allows you to read and write XML, INI and WDDX files using the same API. The XML-reader is the most powerful driver as it allows you to define how you would like the tags to be handled. This means you can define that the tag <foo one="1" bar="totmato"/> should automatically be converted to an array that should contain the attributes of the tag as values. Of course you can also choose which types to use for the attribute values, so they get converted to booleans, floats, integers, strings, arrays or objects.
To achieve this, patConfiguration provides a very easy-to-use built-in tag: <!-- define a new namespace -->
<define ns="shop">
<!-- define a tag in the namespace -->
<define tag="articles" type="array"/>
<!-- Define a tag with no name (indexed array) -->
<define tag="article" name="_none" type="array">
<!-- define three attributes for this tag -->
<define attribute="vendor" type="string"/>
<define attribute="title" type="string"/>
<define attribute="price" type="float" default="99.99"/>
</define>
</define>
Now that you've defined, how patConfiguration should interpret the tags, you may use them in your configuration: <shop:articles>
<shop:article title="Power battery" vendor="Green Lantern"/>
<shop:article title="Batarang" vendor="Batman" price="500"/>
</shop:articles>
Parsing this configuration is extremely easy: <?php
$conf = new patConfiguration(
array(
'configDir' => './config'
)
);
// parse config file
$conf->loadConfig('example_define_basic.xml');
$config = $conf->getConfigValue('articles');
?>
If you worry about the overhead of parsing XML files on every request, you may want to switch-on the caching system which makes loading the configuration faster then reading it from PHP files. If you want to use your tag definitions in more than one file, just use external entities or xInclude tags, patConfiguration supports both, even in PHP4.
After reading this XML-document with patConfiguration, you'll get the following array structure back: Array
(
[0] => Array
(
[vendor] => Green Lantern
[title] => Power battery
[price] => 99.99
)
[1] => Array
(
[vendor] => Batman
[title] => Batarang
[price] => 500
)
)
patConfiguration allows you to return the complete configuration or only parts of it, using a mixture of PHP's array snytax and a path to the desired value. If you'd like to see more features of patConfiguration in action, you may want to take a look at the online examples or download it from our site.
If you are using PEAR 1.4.0 you may also get it from our PEAR-channel: $ pear channel-discover pear.php-tools.net
$ pear install --alldeps pat/patConfiguration
If you stumble upon a bug, please report it using our bugtracker.