From 4c10eed3b13aa693ed7178275f597f17ab9d8e41 Mon Sep 17 00:00:00 2001 From: "Florian Aders (EleRas)" Date: Tue, 3 Feb 2015 18:52:37 +0100 Subject: [PATCH] Updated comments and various small improvements in ConfigParser - classes Signed-off-by: Florian Aders (EleRas) --- lib/classes/config/class.ConfigDaemon.php | 97 ++++++++++++++++------ lib/classes/config/class.ConfigParser.php | 23 ++--- lib/classes/config/class.ConfigService.php | 57 ++++++++++--- 3 files changed, 130 insertions(+), 47 deletions(-) diff --git a/lib/classes/config/class.ConfigDaemon.php b/lib/classes/config/class.ConfigDaemon.php index 14aae6ab..ea92cb91 100644 --- a/lib/classes/config/class.ConfigDaemon.php +++ b/lib/classes/config/class.ConfigDaemon.php @@ -31,18 +31,42 @@ * @package Classes */ class ConfigDaemon { + /** + * Holding the commands for this daemon + * @var array + */ private $orders = array(); - - private $fullxml; - - private $daemon; - - private $xpath; - - public $title; + /** + * Store the parsed SimpleXMLElement for usage + * @var SimpleXMLElement + */ + private $fullxml; + + /** + * Memorize if we already parsed the XML + * @var bool + */ private $isparsed = false; + /** + * Sub - area of the full - XML only holding the daemon - data we are interessted in + * @var SimpleXMLElement + */ + private $daemon; + + /** + * xpath leading to this daemon in the full XML + * @var string + */ + private $xpath; + + /** + * Human - readable title of this service + * @var string + */ + public $title; + public function __construct($xml, $xpath) { $this->fullxml = $xml; $this->xpath = $xpath; @@ -53,12 +77,18 @@ class ConfigDaemon { } } + /** + * Parse the XML and populate $this->orders + * @return bool + */ private function _parse() { - if ($this->isparsed) { - return; + // We only want to parse the stuff one time + if ($this->isparsed == true) { + return true; } + $preparsed = array(); - // First: let's handle push everything into an array and expand all includes + // First: let's push everything into an array and expand all includes foreach ($this->daemon[0]->children() as $order) { switch((string)$order->getName()) { case "install": @@ -94,9 +124,27 @@ class ConfigDaemon { } } } + + // Switch flag to indicate we parsed our data $this->isparsed = true; + return true; } - + + /** + * Get config for this daemon + * + * The returned array will be an array of array, each sub-array looking like this: + * array('type' => 'install|file|command', 'content' => '') + * If the type is "file", an additional "name" - element will be added to the array + * To configure a daemon, the steps in the array must be followed in order + * + * @return array + */ + public function getConfig() { + $this->_parse(); + return $this->orders; + } + /** * Parse a single order and return it in a format for easier usage * @@ -119,7 +167,7 @@ class ConfigDaemon { return $parsedorder; } - + /** * Parse a install - order and return it in a format for easier usage * @@ -131,6 +179,7 @@ class ConfigDaemon { if ($order->count() == 0) { return array('type' => 'install', 'content' => (string)$order); } + // Hold the results $visibility = 1; $content = ''; @@ -140,14 +189,14 @@ class ConfigDaemon { case "content": $content = $this->_parseContent((string)$child); break; } } - + if ($visibility > 0) { return array('type' => 'install', 'content' => $content); } else { return ''; } } - + /** * Parse a command - order and return it in a format for easier usage * @@ -159,6 +208,7 @@ class ConfigDaemon { if ($order->count() == 0) { return array('type' => 'command', 'content' => (string)$order); } + // Hold the results $visibility = 1; $content = ''; @@ -168,7 +218,7 @@ class ConfigDaemon { case "content": $content = $this->_parseContent((string)$child); break; } } - + if ($visibility > 0) { return array('type' => 'command', 'content' => $content); } else { @@ -187,6 +237,7 @@ class ConfigDaemon { if ($order->count() == 0) { return array('type' => 'file', 'content' => (string)$order, 'name' => $attributes['name']); } + // Hold the results $visibility = 1; $content = ''; @@ -204,10 +255,10 @@ class ConfigDaemon { if (array_key_exists('backup', $attributes)) { $return[] = array('type' => 'command', 'content' => 'mv "' . $attributes['name'] . '" "' . $attributes['name'] . '.frx.bak"'); } - + // Now the content of the file can be written $return[] = array('type' => 'file', 'content' => $content, 'name' => $attributes['name']); - + // Let's check if the mode of the file should be changed if (array_key_exists('chmod', $attributes)) { $return[] = array('type' => 'command', 'content' => 'chmod ' . $attributes['chmod'] . ' "' . $attributes['name'] . '"'); @@ -253,10 +304,12 @@ class ConfigDaemon { foreach($order->attributes() as $key => $value) { $attributes[(string)$key] = (string)$value; } + $order = $this->_parseContent((string)$order); if (!array_key_exists('mode', $attributes)) { throw new \Exception('"' . $order . '" is missing mode'); } + switch ($attributes['mode']) { case "isfile": if (!is_file($order)) { return -1; }; break; case "isdir": if (!is_dir($order)) { return -1; }; break; @@ -268,10 +321,4 @@ class ConfigDaemon { } return 0; } - - public function getConfig() { - $this->_parse(); - return $this->orders; - - } -} \ No newline at end of file +} diff --git a/lib/classes/config/class.ConfigParser.php b/lib/classes/config/class.ConfigParser.php index 44172eea..50141b84 100644 --- a/lib/classes/config/class.ConfigParser.php +++ b/lib/classes/config/class.ConfigParser.php @@ -34,13 +34,13 @@ class ConfigParser { * @var array */ private $services = array(); - + /** * Store the parsed SimpleXMLElement for usage * @var SimpleXMLElement */ private $xml; - + /** * Memorize if we already parsed the XML * @var bool @@ -64,7 +64,7 @@ class ConfigParser { * @var bool */ public $deprecated = false; - + /** * Constructor * @@ -76,6 +76,7 @@ class ConfigParser { if (!is_readable($filename)) { throw new Exception('File not readable'); } + $this->xml = simplexml_load_file($filename); if ($this->xml === false) { $error = ''; @@ -102,15 +103,17 @@ class ConfigParser { } } } - + /** * Parse the XML and populate $this->services - * @return void + * @return bool */ private function _parse() { + // We only want to parse the stuff one time if ($this->isparsed == true) { return true; } + // Get all services $services = $this->xml->xpath('//services/service'); foreach ($services as $service) { @@ -125,12 +128,12 @@ class ConfigParser { } } } - - // Switch parsed - indicator + + // Switch flag to indicate we parsed our data $this->isparsed = true; return true; } - + /** * Return all services defined by the XML * @@ -140,8 +143,8 @@ class ConfigParser { 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 +} diff --git a/lib/classes/config/class.ConfigService.php b/lib/classes/config/class.ConfigService.php index 3744c93e..756a0a6e 100644 --- a/lib/classes/config/class.ConfigService.php +++ b/lib/classes/config/class.ConfigService.php @@ -31,28 +31,56 @@ * @package Classes */ class ConfigService { + /** + * Holding the available daemons in this service + * @var array + */ private $daemons = array(); - + + /** + * Store the parsed SimpleXMLElement for usage + * @var SimpleXMLElement + */ private $fullxml; - - private $service; - + + /** + * Memorize if we already parsed the XML + * @var bool + */ + private $isparsed = false; + + /** + * xpath leading to this service in the full XML + * @var string + */ private $xpath; - + + /** + * Human - readable title of this service + * @var string + */ public $title; - + public function __construct($xml, $xpath) { $this->fullxml = $xml; $this->xpath = $xpath; - $this->service = $this->fullxml->xpath($this->xpath); - $attributes = $this->service[0]->attributes(); + $service = $this->fullxml->xpath($this->xpath); + $attributes = $service[0]->attributes(); if ($attributes['title'] != '') { $this->title = (string)$attributes['title']; } - $this->parse(); } - private function parse() { + /** + * Parse the XML and populate $this->daemons + * @return bool + */ + private function _parse() { + // We only want to parse the stuff one time + if ($this->isparsed == true) { + return true; + } + $daemons = $this->fullxml->xpath($this->xpath . '/daemon'); foreach ($daemons as $daemon) { if ($daemon->getName() == 'comment') { @@ -81,9 +109,14 @@ class ConfigService { } $this->daemons[$name] = new ConfigDaemon($this->fullxml, $this->xpath . "/daemon" . $nametag . $versiontag); } + + // Switch flag to indicate we parsed our data + $this->isparsed = true; + return true; } - + public function getDaemons() { + $this->_parse(); return $this->daemons; } -} \ No newline at end of file +}