From 827b3cc5f6d1a663c6d3cc7f6b03ba8b38fef6ca Mon Sep 17 00:00:00 2001 From: "Florian Aders (EleRas)" Date: Tue, 3 Feb 2015 18:14:00 +0100 Subject: [PATCH] Added configuration - descriptions to ConfigParser, only parse complete file at first access Signed-off-by: Florian Aders (EleRas) --- lib/classes/config/class.ConfigParser.php | 57 +++++++++++++++++++++-- 1 file changed, 54 insertions(+), 3 deletions(-) diff --git a/lib/classes/config/class.ConfigParser.php b/lib/classes/config/class.ConfigParser.php index caec269f..44172eea 100644 --- a/lib/classes/config/class.ConfigParser.php +++ b/lib/classes/config/class.ConfigParser.php @@ -41,6 +41,30 @@ class ConfigParser { */ private $xml; + /** + * Memorize if we already parsed the XML + * @var bool + */ + private $isparsed = false; + + /** + * Name of the distribution this configuration is for + * @var string + */ + public $distributionName = ''; + + /** + * Version of the distribution this configuration is for + * @var string + */ + public $distributionVersion = ''; + + /** + * Show if this configuration is deprecated + * @var bool + */ + public $deprecated = false; + /** * Constructor * @@ -58,16 +82,35 @@ class ConfigParser { foreach(libxml_get_errors() as $error) { $error .= "\t" . $error->message; } - throw new Exception($error); + throw new \Exception($error); + } + + // Let's see if we can find a block in the XML + $distribution = $this->xml->xpath('//distribution'); + + // No distribution found - can't use this file + if (!is_array($distribution)) { + throw new \Exception('Invalid XML, no distribution found'); + } + + // Search for attributes we understand + foreach($distribution[0]->attributes() as $key => $value) { + switch ((string)$key) { + case "name": $this->distributionName = (string)$value; break; + case "version": $this->distributionVersion = (string)$value; break; + case "deprecated": (string)$value == 'true' ? $this->deprecated = true : $this->deprecated = false; break; + } } - $this->parse(); } /** * Parse the XML and populate $this->services * @return void */ - private function parse() { + private function _parse() { + if ($this->isparsed == true) { + return true; + } // Get all services $services = $this->xml->xpath('//services/service'); foreach ($services as $service) { @@ -82,6 +125,10 @@ class ConfigParser { } } } + + // Switch parsed - indicator + $this->isparsed = true; + return true; } /** @@ -91,6 +138,10 @@ class ConfigParser { * @return array */ public function getServices() { + // Let's parse this shit(!) + $this->_parse(); + + // Return our carefully searched for services return $this->services; } } \ No newline at end of file