Name | Modified | Size | Downloads / Week |
---|---|---|---|
Parent folder | |||
example_auto_prepend_file.inc | 2008-04-18 | 203 Bytes | |
example.xml | 2008-04-18 | 305 Bytes | |
README | 2008-04-18 | 3.2 kB | |
COPYING | 2008-04-18 | 35.8 kB | |
ConfigurationLoader.class.php | 2008-04-18 | 8.0 kB | |
Totals: 5 Items | 47.5 kB | 0 |
Introduction ------------ ConfigurationLoader generates a php class from a specified xml file. Any property values are hard-coded into this class. This means that you can use xml configuration files without having to re-parse them each time. For instance, if you had an xml file that contained the following: <Configuration> <username>david</username> <password>blah</password> </Configuration> ConfigurationLoader would convert this into the following php class: class Configuration { public $username; public $password; function __construct() { $this->username = "david"; $this->password = "blah; } } Setup ----- Ensure that the folder where ConfigurationLoader.class.php is located is in your search path. Ensure that the path where your xml config file is has read permissions for whichever user runs your apache daemon. Ensure that the path where your php config class will reside has write permissions for whichever user runs your apache daemon. (It's easiest to put them both in the same folder) At the top of each of your pages, write: Require_once(ConfigurationLoader.class.php); ConfigurationLoader::update(</path/to/xml/config/file.xml>, </path/to/generated/php/config/file>); $config = new <nameOfRootElementInXmlFile>(); For example, assuming that your root xml element is called 'config': ConfigurationLoader::update(/home/me/www/app1/config/config.xml, /home/me/www/app1/config/config.php); $config = new config(); This will read config.xml and use it to generate config.php in /home/me/www/app1/config.php You can now create a new instance of the class called config (the name of the class corresponds to the name of the root element in the xml config file), which will contain all your config values. The method will only reparse the xml file if it is newer than the php class file, obviously if the php class doesn't yet exist then the xml file will be parsed anyway. If you want to be more specific about what type you want a particular value to be, specify the type attribute for the value, eg: <connectionString type="string">blahblahblah</connectionString> If your xml file was: <myApp> <database> <connectionString>This is my connection string</connectionString> <timeoutPeriod type="int">10</timeoutPeriod> </database> </myApp> After calling update you could create a new instance of your config class and access it as follows: echo $config->database->connectionString . "<br />"; echo $config->database->timeoutPeriod . "<br />"; which would output: This is my connection string 10 To the page IMPORTANT NOTICES ----------------- I've only tested this class in php 5 on ubuntu linux. Feel free to report bugs but go easy on me! Any hyphens or non-word characters in your xml element names will be converted into underscores when generating the classnames. For example an element named timeout-value will be converted to timeout_value. The simplest approach is to avoid naming your xml elements in this way. Please email me at david.wainwright1@sky.com if you have any problems or suggestions for improvements to this class.