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();
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; 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) { 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,9 +124,27 @@ 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;
}
/** /**
* Parse a single order and return it in a format for easier usage * Parse a single order and return it in a format for easier usage
* *
@@ -119,7 +167,7 @@ class ConfigDaemon {
return $parsedorder; return $parsedorder;
} }
/** /**
* Parse a install - order and return it in a format for easier usage * Parse a install - order and return it in a format for easier usage
* *
@@ -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 = '';
@@ -140,14 +189,14 @@ class ConfigDaemon {
case "content": $content = $this->_parseContent((string)$child); break; case "content": $content = $this->_parseContent((string)$child); break;
} }
} }
if ($visibility > 0) { if ($visibility > 0) {
return array('type' => 'install', 'content' => $content); return array('type' => 'install', 'content' => $content);
} else { } else {
return ''; return '';
} }
} }
/** /**
* Parse a command - order and return it in a format for easier usage * Parse a command - order and return it in a format for easier usage
* *
@@ -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 = '';
@@ -168,7 +218,7 @@ class ConfigDaemon {
case "content": $content = $this->_parseContent((string)$child); break; case "content": $content = $this->_parseContent((string)$child); break;
} }
} }
if ($visibility > 0) { if ($visibility > 0) {
return array('type' => 'command', 'content' => $content); return array('type' => 'command', 'content' => $content);
} else { } else {
@@ -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 = '';
@@ -204,10 +255,10 @@ class ConfigDaemon {
if (array_key_exists('backup', $attributes)) { if (array_key_exists('backup', $attributes)) {
$return[] = array('type' => 'command', 'content' => 'mv "' . $attributes['name'] . '" "' . $attributes['name'] . '.frx.bak"'); $return[] = array('type' => 'command', 'content' => 'mv "' . $attributes['name'] . '" "' . $attributes['name'] . '.frx.bak"');
} }
// Now the content of the file can be written // Now the content of the file can be written
$return[] = array('type' => 'file', 'content' => $content, 'name' => $attributes['name']); $return[] = array('type' => 'file', 'content' => $content, 'name' => $attributes['name']);
// Let's check if the mode of the file should be changed // Let's check if the mode of the file should be changed
if (array_key_exists('chmod', $attributes)) { if (array_key_exists('chmod', $attributes)) {
$return[] = array('type' => 'command', 'content' => 'chmod ' . $attributes['chmod'] . ' "' . $attributes['name'] . '"'); $return[] = array('type' => 'command', 'content' => 'chmod ' . $attributes['chmod'] . ' "' . $attributes['name'] . '"');
@@ -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

@@ -34,13 +34,13 @@ class ConfigParser {
* @var array * @var array
*/ */
private $services = array(); private $services = array();
/** /**
* Store the parsed SimpleXMLElement for usage * Store the parsed SimpleXMLElement for usage
* @var SimpleXMLElement * @var SimpleXMLElement
*/ */
private $xml; private $xml;
/** /**
* Memorize if we already parsed the XML * Memorize if we already parsed the XML
* @var bool * @var bool
@@ -64,7 +64,7 @@ class ConfigParser {
* @var bool * @var bool
*/ */
public $deprecated = false; public $deprecated = false;
/** /**
* Constructor * Constructor
* *
@@ -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 = '';
@@ -102,15 +103,17 @@ 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) {
@@ -125,12 +128,12 @@ class ConfigParser {
} }
} }
} }
// Switch parsed - indicator // Switch flag to indicate we parsed our data
$this->isparsed = true; $this->isparsed = true;
return true; return true;
} }
/** /**
* Return all services defined by the XML * Return all services defined by the XML
* *
@@ -140,8 +143,8 @@ class ConfigParser {
public function getServices() { public function getServices() {
// Let's parse this shit(!) // Let's parse this shit(!)
$this->_parse(); $this->_parse();
// Return our carefully searched for services // Return our carefully searched for services
return $this->services; return $this->services;
} }
} }

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;
} }
} }