<?php
/**
* Implements an updater for CBoxManager
*
*
* @version 0.0.1 - dev
* @author Edoardo Tenani <hide@address.com>
* @license GNU Public License 2.0 or greater
* <http://opensource.org/licenses/gpl-2.0.php>
* @copyright Edoardo Tenani
*/
class Updater {
/**
* The xml file containing the update
*
* @var object
*/
protected $xml_file;
/**
* The SimpleXMLObject with the updates
*
* @var object
*/
protected $xml;
/**
* The code to be executed to update CBoxManager
*
* @var array
*/
protected $code;
function __construct($xml_file) {
$this->xml_file = $xml_file;
$this->xml = simplexml_load_file($xml_file);
$this->load_update();
}
/* OBJECT FUNCTION OVERRIDE
************************************************/
/**
* When the class is destroyed this function is called.
*
* @return void
*/
public function __destruct() { }
/**
* When an inaccesible method of the class is invoked this function
* is called.
*
* @return void
*/
public function __call($name, $arguments) {
echo get_class($this)."::Error in calling object method <b>'$name'</b>"."<br>\n";
}
/**
* When an inaccesible static method of the class is invoked this
* function is called.
*
* @return void
*/
public static function __callStatic($name, $arguments) {
echo get_class($this)."::Error in calling object static method <b>'$name'</b>"."<br>\n";
}
/**
* When the object is converted to a string this function is used to
* format the result string.
* Values are separated by "|". If the class var is an array, values
* in it will be separated by ",", or NULL if is not set.
* The string starts and ends with "-".
*
* @access public
* @return a string with all class vars values separated by |
*/
public function __toString() {
return NULL;
}
/* SET & GET FUNCTION
************************************************/
/**
* The function set a specified class var.
* Is not case sensitive.
*
* @access public
* @param string $var the name of the var to be set
* @param mixed $value the new value to set
*
* @return true
*/
public function set($var, $value = NULL) {
$this->{$var} = $value;
return true;
}
/**
* The function get a specified class var.
* Is case sensitive.
*
* @access public
* @param string $var the name of the var to be get
*
* @return the class var
*/
public function get($var) {
return $this->{$var};
}
/* PUBLIC FUNCTION
************************************************/
/**
* Get attribute's value of xml root element
*
* @param string $name the name of the attribute'value to be
* retrieved
* @return the value on success, false on fail
*
* @link http://it.php.net/manual/en/simplexmlelement.attributes.php
* @link http://php.net/strcmp
*/
public function get_attr($name) {
foreach ( $this->xml->attributes() as $k => $v ) {
if ( strcmp($k, $name) == 0 )
return $this->xml["$k"];
}
return false;
}
/**
* Run update
*
* Run update based on $this->code var. Is the main update function,
* to be called to perform the update.
*
* Rename the update file to {filename}.old to disable update if
* done.
*
* Actually "file" is not disposable.
*
* @todo Maybe is better to remove the file?? how to check if the
* update goes well?
*
* @access public
* @param object $db a valid istance of the Database class
* @param string $section can be "sql" or "file", to specify which
* part of the update must be run
* @return bool true if code returs true, false if fails
*
* @internal if $section is not specified, recursive calls with "sql"
* and "file" as parameter
*
* @link http://php.net/eval
*/
public function run($db, $section = "") {
switch ( $section ) {
case "sql" :
foreach ( $this->code["sql"]["query"] as $sql ) {
$sql_results[] = $db->query("$sql") ? true : false;
}
$return = (in_array(false, $sql_results) ) ? false : true;
break;
case "file" :
$return = false;
break;
default:
// to be used when file section is completed
//return ( $this->run($db, "sql") && $this->run($db, "file") ) ? true : false;
$return = ( $this->run($db, "sql") ) ? true : false;
break;
}
@unlink($this->xml_file);
return $return;
}
public function debug() {
echo "<pre><b>\$this->xml</b>: <br>", var_dump($this->xml), "</pre>";
echo "<pre><b>\$this->code</b>: <br>", var_dump($this->code), "</pre>";
}
public function _debug($var) {
echo "<pre><b>var</b>: <br>", var_dump($var), "</pre>";
}
/* PRIVATE FUNCTION
************************************************/
/**
* Loads code from xml
*
* Loads code to be executed as update inside $this->code
*
* @access private
* @return void
*
* @link http://php.net/trim
*/
private function load_update() {
foreach ( $this->xml as $v ) {
for ( $i = 0, $count = count($v); $i < $count; $i++ ) {
$this->code[$v->getName()][$v->children()->getName()][$i] = trim($v->children());
}
}
}
}
?>