Updated comments and various small improvements in ConfigParser - classes

Signed-off-by: Florian Aders (EleRas) <eleras@froxlor.org>
This commit is contained in:
Florian Aders (EleRas)
2015-02-03 18:52:37 +01:00
parent 827b3cc5f6
commit 4c10eed3b1
3 changed files with 130 additions and 47 deletions

View File

@@ -31,18 +31,42 @@
* @package Classes * @package Classes
*/ */
class ConfigDaemon { class ConfigDaemon {
/**
* Holding the commands for this daemon
* @var array
*/
private $orders = array(); private $orders = array();
/**
* Store the parsed SimpleXMLElement for usage
* @var SimpleXMLElement
*/
private $fullxml; 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; private $daemon;
/**
* xpath leading to this daemon in the full XML
* @var string
*/
private $xpath; private $xpath;
/**
* Human - readable title of this service
* @var string
*/
public $title; public $title;
private $isparsed = false;
public function __construct($xml, $xpath) { public function __construct($xml, $xpath) {
$this->fullxml = $xml; $this->fullxml = $xml;
$this->xpath = $xpath; $this->xpath = $xpath;
@@ -53,12 +77,18 @@ class ConfigDaemon {
} }
} }
/**
* Parse the XML and populate $this->orders
* @return bool
*/
private function _parse() { private function _parse() {
if ($this->isparsed) { // We only want to parse the stuff one time
return; if ($this->isparsed == true) {
return true;
} }
$preparsed = array(); $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) { foreach ($this->daemon[0]->children() as $order) {
switch((string)$order->getName()) { switch((string)$order->getName()) {
case "install": case "install":
@@ -94,7 +124,25 @@ class ConfigDaemon {
} }
} }
} }
// Switch flag to indicate we parsed our data
$this->isparsed = true; $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' => '<TEXT>')
* 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;
} }
/** /**
@@ -131,6 +179,7 @@ class ConfigDaemon {
if ($order->count() == 0) { if ($order->count() == 0) {
return array('type' => 'install', 'content' => (string)$order); return array('type' => 'install', 'content' => (string)$order);
} }
// Hold the results // Hold the results
$visibility = 1; $visibility = 1;
$content = ''; $content = '';
@@ -159,6 +208,7 @@ class ConfigDaemon {
if ($order->count() == 0) { if ($order->count() == 0) {
return array('type' => 'command', 'content' => (string)$order); return array('type' => 'command', 'content' => (string)$order);
} }
// Hold the results // Hold the results
$visibility = 1; $visibility = 1;
$content = ''; $content = '';
@@ -187,6 +237,7 @@ class ConfigDaemon {
if ($order->count() == 0) { if ($order->count() == 0) {
return array('type' => 'file', 'content' => (string)$order, 'name' => $attributes['name']); return array('type' => 'file', 'content' => (string)$order, 'name' => $attributes['name']);
} }
// Hold the results // Hold the results
$visibility = 1; $visibility = 1;
$content = ''; $content = '';
@@ -253,10 +304,12 @@ class ConfigDaemon {
foreach($order->attributes() as $key => $value) { foreach($order->attributes() as $key => $value) {
$attributes[(string)$key] = (string)$value; $attributes[(string)$key] = (string)$value;
} }
$order = $this->_parseContent((string)$order); $order = $this->_parseContent((string)$order);
if (!array_key_exists('mode', $attributes)) { if (!array_key_exists('mode', $attributes)) {
throw new \Exception('"<visibility>' . $order . '</visibility>" is missing mode'); throw new \Exception('"<visibility>' . $order . '</visibility>" is missing mode');
} }
switch ($attributes['mode']) { switch ($attributes['mode']) {
case "isfile": if (!is_file($order)) { return -1; }; break; case "isfile": if (!is_file($order)) { return -1; }; break;
case "isdir": if (!is_dir($order)) { return -1; }; break; case "isdir": if (!is_dir($order)) { return -1; }; break;
@@ -268,10 +321,4 @@ class ConfigDaemon {
} }
return 0; return 0;
} }
public function getConfig() {
$this->_parse();
return $this->orders;
}
} }

View File

@@ -76,6 +76,7 @@ class ConfigParser {
if (!is_readable($filename)) { if (!is_readable($filename)) {
throw new Exception('File not readable'); throw new Exception('File not readable');
} }
$this->xml = simplexml_load_file($filename); $this->xml = simplexml_load_file($filename);
if ($this->xml === false) { if ($this->xml === false) {
$error = ''; $error = '';
@@ -105,12 +106,14 @@ class ConfigParser {
/** /**
* Parse the XML and populate $this->services * Parse the XML and populate $this->services
* @return void * @return bool
*/ */
private function _parse() { private function _parse() {
// We only want to parse the stuff one time
if ($this->isparsed == true) { if ($this->isparsed == true) {
return true; return true;
} }
// Get all services // Get all services
$services = $this->xml->xpath('//services/service'); $services = $this->xml->xpath('//services/service');
foreach ($services as $service) { foreach ($services as $service) {
@@ -126,7 +129,7 @@ class ConfigParser {
} }
} }
// Switch parsed - indicator // Switch flag to indicate we parsed our data
$this->isparsed = true; $this->isparsed = true;
return true; return true;
} }

View File

@@ -31,28 +31,56 @@
* @package Classes * @package Classes
*/ */
class ConfigService { class ConfigService {
/**
* Holding the available daemons in this service
* @var array
*/
private $daemons = array(); private $daemons = array();
/**
* Store the parsed SimpleXMLElement for usage
* @var SimpleXMLElement
*/
private $fullxml; 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; private $xpath;
/**
* Human - readable title of this service
* @var string
*/
public $title; public $title;
public function __construct($xml, $xpath) { public function __construct($xml, $xpath) {
$this->fullxml = $xml; $this->fullxml = $xml;
$this->xpath = $xpath; $this->xpath = $xpath;
$this->service = $this->fullxml->xpath($this->xpath); $service = $this->fullxml->xpath($this->xpath);
$attributes = $this->service[0]->attributes(); $attributes = $service[0]->attributes();
if ($attributes['title'] != '') { if ($attributes['title'] != '') {
$this->title = (string)$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'); $daemons = $this->fullxml->xpath($this->xpath . '/daemon');
foreach ($daemons as $daemon) { foreach ($daemons as $daemon) {
if ($daemon->getName() == 'comment') { if ($daemon->getName() == 'comment') {
@@ -81,9 +109,14 @@ class ConfigService {
} }
$this->daemons[$name] = new ConfigDaemon($this->fullxml, $this->xpath . "/daemon" . $nametag . $versiontag); $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() { public function getDaemons() {
$this->_parse();
return $this->daemons; return $this->daemons;
} }
} }