So I have a PHP file that I use to define all my constants but now want the flexibility of using a XML instead.
Example PHP Config file
define("LOGIN_URL", "secure_login.php");
define("REDIRECT", "redirect.php");
define("MAPPED_VALUE_GREEN", "object_green");
define("MAPPED_VALUE_KEY", "object_key_12345");
What I'm going to do is:
<scriptNameConfig>
<urls>
<url alias="LOGIN_URL" scriptname="secure_login.php" location="optional/path/to/file"/>
<url alias="REDIRECTL" scriptname="redirect.php" location="optional/path/to/file"/>
</urls>
<mapping>
<mapped name="MAPPED_VALUE_GREEN" data="object_green"/>
<mapped name="MAPPED_VALUE_KEY" data="object_key_12345"/>
</mapping>
</scriptNameConfig>
Now this is okay but I want to use XPATH to extract the values from this type of config file but would like it to be dynamic enough so I don't have to hard code the functions
This is my base class
class ParseXMLConfig {
protected $xml;
public function __construct($xml) {
if(is_file($xml)) {
$this->xml = simplexml_load_file($xml);
} else {
$this->xml = simplexml_load_string($xml);
}
}
}
And I extend it like this
class ParseURLS extends ParseXMLConfig {
public function getUrlArr($url_alias) {
$attr = false;
$el = $this->xml->xpath("//url[@alias='$url_alias']");
if($el && count($el) === 1) {
$attr = (array) $el[0]->attributes();
$attr = $attr['@attributes'];
}
return $attr; // this will return the element array with all attributes
}
}
But the problem is if I wanted to introduce a new value in the confie file I have to add some sort of function for XPATH to parse it. wanted to know if anyone has an idea on how to go about doing this generic/dynamic so adding/changing or removing elements/attributes in the XML config file would be easier and less hard coding.
Thanks for any tips/examples/code/ideas, -Phill