Merge branch 'Issue_#70' into 'master'
Config file ORM for issue #70 Solves #70 Please mind that this will break some serious things when directly reading from the config object. See merge request !37
This commit is contained in:
commit
d9807c87ce
|
@ -29,11 +29,15 @@
|
|||
*/
|
||||
|
||||
namespace FuzeWorks;
|
||||
use \Iterator;
|
||||
use \PDOException;
|
||||
|
||||
|
||||
/**
|
||||
* Config Class
|
||||
*
|
||||
* This class gives access to the config files. Can read and write .php files with an array in a file
|
||||
* This class gives access to the config files. It allows you to open configurations and edit them.
|
||||
* This class is extensible, but not yet from the outside
|
||||
* @package net.techfuze.fuzeworks.core
|
||||
* @author Abel Hoogeveen <abel@techfuze.net>
|
||||
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
|
||||
|
@ -57,13 +61,13 @@ class Config extends Bus{
|
|||
}
|
||||
|
||||
/**
|
||||
* Get's called when the class get's loaded. Does nothing
|
||||
* @access public
|
||||
* All loaded Config files
|
||||
* @var Array of ConfigORM
|
||||
*/
|
||||
public function onLoad() {}
|
||||
private $cfg = array();
|
||||
|
||||
/**
|
||||
* Reads a config file and returns it as object
|
||||
* Loads a config file and returns it as an object
|
||||
* @access public
|
||||
* @param String config file name
|
||||
* @param String directory, default is Application/Config
|
||||
|
@ -74,69 +78,33 @@ class Config extends Bus{
|
|||
$dir = (isset($directory) ? $directory : "Application/Config/");
|
||||
$file = $dir . 'config.' . strtolower($name).".php";
|
||||
|
||||
if (file_exists($file)) {
|
||||
$DECODED = (object) require($file);
|
||||
return $DECODED;
|
||||
} else {
|
||||
$this->core->loadMod('techfuze/database');
|
||||
if ($this->dbActive) {
|
||||
// Fetch me a query of 5
|
||||
$prefix = $this->mods->database->getPrefix();
|
||||
$query = "SELECT * FROM ".$prefix."config WHERE `file` = ?";
|
||||
$binds = array($name);
|
||||
try{
|
||||
$sth = $this->mods->database->prepare($query);
|
||||
$sth->execute($binds);
|
||||
}catch (\PDOException $e){
|
||||
throw new Exception('Could not execute SQL-query due PDO-exception '.$e->getMessage());
|
||||
}
|
||||
|
||||
// Fetch results
|
||||
$result = $sth->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$return = array();
|
||||
for ($i=0; $i < count($result); $i++) {
|
||||
$return[ $result[$i]['key'] ] = $result[$i]['value'];
|
||||
}
|
||||
|
||||
// Return if found in DB
|
||||
if (!empty($return)) {
|
||||
return (object) $return;
|
||||
}
|
||||
|
||||
}
|
||||
throw new Exception("Config file '".strtolower($name)."' was not found", 1);
|
||||
return false;
|
||||
// If already loaded, return a reference to the ORM
|
||||
if (isset($this->cfg[$name])) {
|
||||
return $cfg = &$this->cfg[$name];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Change a value in the config, wherever this is saved
|
||||
* @access public
|
||||
* @param String filename
|
||||
* @param String config key
|
||||
* @param String/Array config value
|
||||
* @param String directory, default is Application/Config
|
||||
*/
|
||||
public function set($name, $key, $value, $directory = null) {
|
||||
$dir = (isset($directory) ? $directory : "Application/config/");
|
||||
$file = $dir . 'config.' . strtolower($name).".php";
|
||||
// Is this the real file?
|
||||
if (file_exists($file)) {
|
||||
$DECODED = require($file);
|
||||
if (!is_array($DECODED)) {
|
||||
$DECODED = array();
|
||||
}
|
||||
if (is_null($value)) {
|
||||
unset($DECODED[$key]);
|
||||
} else {
|
||||
$DECODED[$key] = $value;
|
||||
// Is it just reference?
|
||||
return $cfg = $this->cfg[$name] = new ConfigFileORM($file);
|
||||
} else {
|
||||
// Caught in a datastream
|
||||
$this->core->loadMod('techfuze/database');
|
||||
// No escape from dbactive
|
||||
if ($this->dbActive) {
|
||||
// Open your stream
|
||||
$dborm = new ConfigDatabaseORM($this->mods->database, $name);
|
||||
// Lookup for the success
|
||||
if ($dborm->success) {
|
||||
// And see
|
||||
return $cfg = $this->cfg[$name] = $dborm;
|
||||
}
|
||||
}
|
||||
|
||||
if (is_writable($file)) {
|
||||
$config = var_export($DECODED, true);
|
||||
file_put_contents($file, "<?php return $config ;");
|
||||
}
|
||||
} else {
|
||||
throw new Exception("Config file '".strtolower($name)."' was not found", 1);
|
||||
// I'm just a poor exception
|
||||
throw new ConfigException("Config file '".strtolower($name)."' was not found", 1);
|
||||
|
||||
// I need no returnee
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
@ -152,5 +120,320 @@ class Config extends Bus{
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Abstract ConfigORM class.
|
||||
*
|
||||
* This class implements the iterator, so a config file can be accessed using foreach.
|
||||
* A file can also be returned using toArray(), so it will be converted to an array
|
||||
* @package net.techfuze.fuzeworks.core
|
||||
* @author Abel Hoogeveen <abel@techfuze.net>
|
||||
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
|
||||
*/
|
||||
abstract class ConfigORM implements Iterator {
|
||||
|
||||
/**
|
||||
* The original state of a config file. Can be reverted to using revert()
|
||||
* @var StdObject Config file
|
||||
*/
|
||||
protected $originalCfg;
|
||||
|
||||
/**
|
||||
* The current state of a config file.
|
||||
* @var StdObject Config file
|
||||
*/
|
||||
protected $cfg;
|
||||
|
||||
/**
|
||||
* Revert to the original conditions of the config file.
|
||||
*/
|
||||
public function revert() {
|
||||
$this->cfg = $this->originalCfg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a requested key is set in the config file
|
||||
* @param String $name Parameter name
|
||||
* @return boolean true on isset, false on not
|
||||
*/
|
||||
public function __isset($name) {
|
||||
return isset($this->cfg[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a value from a config file
|
||||
* @param String $name Key of the requested entry
|
||||
* @return Mixed Value of the requested entry
|
||||
*/
|
||||
public function __get($name) {
|
||||
return $this->cfg[$name];
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets an entry in the config file
|
||||
* @param String $name Key of the entry
|
||||
* @param Mixed $value Value of the entry
|
||||
*/
|
||||
public function __set($name, $value) {
|
||||
$this->cfg[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unset a value in a config file
|
||||
* @param String Key of the entry
|
||||
*/
|
||||
public function __unset($name) {
|
||||
unset($this->cfg[$name]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator method
|
||||
*/
|
||||
public function rewind() {
|
||||
return reset($this->cfg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator method
|
||||
*/
|
||||
public function current() {
|
||||
return current($this->cfg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator method
|
||||
*/
|
||||
public function key() {
|
||||
return key($this->cfg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator method
|
||||
*/
|
||||
public function next() {
|
||||
return next($this->cfg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator method
|
||||
*/
|
||||
public function valid() {
|
||||
return key($this->cfg) !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the config file as an array
|
||||
* @return Array Config file
|
||||
*/
|
||||
public function toArray() {
|
||||
return $this->cfg;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* ORM class for config files in a database
|
||||
*
|
||||
* Handles entries in the database of FuzeWorks and is able to dynamically update them when requested
|
||||
* @package net.techfuze.fuzeworks.core
|
||||
* @author Abel Hoogeveen <abel@techfuze.net>
|
||||
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
|
||||
*/
|
||||
class ConfigDatabaseORM extends ConfigORM {
|
||||
|
||||
/**
|
||||
* The current connection to the database
|
||||
* @var \FuzeWorks\Database Database Connection
|
||||
*/
|
||||
private $dbh;
|
||||
|
||||
/**
|
||||
* Wether the database connection has been successfully established
|
||||
* @var boolean true on success
|
||||
*/
|
||||
public $success = false;
|
||||
|
||||
/**
|
||||
* The current filename
|
||||
* @var String filename
|
||||
*/
|
||||
private $file;
|
||||
|
||||
/**
|
||||
* Sets up the class and the connection to the database
|
||||
* @param \FuzeWorks\Database $db The Database connection
|
||||
* @param String $filename The current filename
|
||||
* @throws ConfigException on fatal error
|
||||
*/
|
||||
public function __construct($db, $filename) {
|
||||
$this->dbh = $db;
|
||||
$this->cfg = $this->openDb($filename);
|
||||
$this->originalCfg = $this->cfg;
|
||||
$this->file = $filename;
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens up a database connection with the requested filename
|
||||
* @param String $name Name of the file
|
||||
* @return Array Content of the file
|
||||
* @throws ConfigException on fatal error
|
||||
*/
|
||||
private function openDb($name) {
|
||||
$prefix = $this->dbh->getPrefix();
|
||||
try{
|
||||
$stmnt = $this->dbh->prepare("SELECT * FROM ".$prefix."config WHERE `file` = ?");
|
||||
$stmnt->execute(array($name));
|
||||
}catch (PDOException $e){
|
||||
throw new ConfigException('Could not execute SQL-query due PDO-exception '.$e->getMessage());
|
||||
}
|
||||
|
||||
// Fetch results
|
||||
$result = $stmnt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$return = array();
|
||||
for ($i=0; $i < count($result); $i++) {
|
||||
$return[ $result[$i]['key'] ] = $result[$i]['value'];
|
||||
}
|
||||
|
||||
// Return if found in DB
|
||||
if (!empty($return)) {
|
||||
$this->success = true;
|
||||
return (array) $return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Write config updates to the database
|
||||
* @throws ConfigException on fatal error
|
||||
*/
|
||||
private function writeDb() {
|
||||
// First arrays of all the fields that need to change
|
||||
$changed_fields = array();
|
||||
$removed_fields = array();
|
||||
$new_fields = array();
|
||||
|
||||
// First check for changed and new feeds
|
||||
foreach ($this->cfg as $key => $value) {
|
||||
if (isset($this->originalCfg[$key])) {
|
||||
if ($this->originalCfg[$key] != $value) {
|
||||
// Changed field
|
||||
$changed_fields[$key] = $value;
|
||||
}
|
||||
} else {
|
||||
// New field
|
||||
$new_fields[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// Then check for removed fields
|
||||
foreach ($this->originalCfg as $key => $value) {
|
||||
if (!isset($this->cfg[$key])) {
|
||||
$removed_fields[$key] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
// First for the removed values
|
||||
$prefix = $this->dbh->getPrefix();
|
||||
try {
|
||||
$stmnt = $this->dbh->prepare("DELETE FROM ".$prefix."config WHERE `file` = :file AND `key` = :key");
|
||||
foreach ($removed_fields as $key => $value) {
|
||||
$stmnt->execute(array('file' => $this->file, 'key' => $key));
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new ConfigException("Could not change config due to PDOException: " . $e->getMessage(), 1);
|
||||
}
|
||||
|
||||
// Then for the changed values
|
||||
try {
|
||||
$stmnt = $this->dbh->prepare("UPDATE ".$prefix."config SET `value` = :value WHERE `file` = :file AND `key` = :key");
|
||||
foreach ($changed_fields as $key => $value) {
|
||||
$stmnt->execute(array('file' => $this->file, 'key' => $key, 'value' => $value));
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new ConfigException("Could not change config due to PDOException: " . $e->getMessage(), 1);
|
||||
}
|
||||
|
||||
// And finally for the new values
|
||||
try {
|
||||
$stmnt = $this->dbh->prepare("INSERT INTO ".$prefix."config (`file`,`key`,`value`) VALUES (:file,:key,:value)");
|
||||
foreach ($new_fields as $key => $value) {
|
||||
$stmnt->execute(array('file' => $this->file, 'key' => $key, 'value' => $value));
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
throw new ConfigException("Could not change config due to PDOException: " . $e->getMessage(), 1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Write updates of the config file to the database
|
||||
* @throws ConfigException on fatal error
|
||||
*/
|
||||
public function commit() {
|
||||
$this->writeDb();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* ORM class for config files in PHP files
|
||||
*
|
||||
* Handles entries in the config directory of FuzeWorks and is able to dynamically update them when requested
|
||||
* @package net.techfuze.fuzeworks.core
|
||||
* @author Abel Hoogeveen <abel@techfuze.net>
|
||||
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
|
||||
*/
|
||||
class ConfigFileORM extends ConfigORM {
|
||||
|
||||
/**
|
||||
* The current filename
|
||||
* @var String filename
|
||||
*/
|
||||
private $file;
|
||||
|
||||
/**
|
||||
* Sets up the class and the connection to the PHP file
|
||||
* @param String $filename The current filename
|
||||
* @throws ConfigException on fatal error
|
||||
*/
|
||||
public function __construct($file) {
|
||||
if (file_exists($file)) {
|
||||
$this->file = $file;
|
||||
$this->openFile($file);
|
||||
$this->originalCfg = $this->cfg;
|
||||
} else {
|
||||
throw new ConfigException("Could not load config file. Config file does not exist", 1);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Opens the file and returns the data
|
||||
* @param String $file Name of the config file
|
||||
* @return Array Content of the file
|
||||
*/
|
||||
private function openFile($file) {
|
||||
$this->cfg = (array) require($file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the config file and writes it to the database
|
||||
* @throws ConfigException on fatal error
|
||||
*/
|
||||
private function writeFile() {
|
||||
// Implement writing here
|
||||
if (is_writable($this->file)) {
|
||||
$config = var_export($this->cfg, true);
|
||||
file_put_contents($this->file, "<?php return $config ;");
|
||||
return true;
|
||||
}
|
||||
throw new ConfigException("Could not write config file. $file is not writable", 1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the config file and writes it
|
||||
* @throws ConfigException on fatal error
|
||||
*/
|
||||
public function commit() {
|
||||
$this->writeFile();
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
|
@ -47,6 +47,13 @@ class Exception extends \Exception{}
|
|||
class LayoutException extends Exception{}
|
||||
|
||||
/**
|
||||
* Class ConfigException
|
||||
* @package net.techfuze.fuzeworks.core
|
||||
* @author Abel Hoogeveen <abel@techfuze.net>
|
||||
* @copyright Copyright (c) 2013 - 2015, Techfuze. (http://techfuze.net)
|
||||
*/
|
||||
class ConfigException extends Exception{}
|
||||
|
||||
* Class RouterException
|
||||
* @package net.techfuze.fuzeworks.core
|
||||
* @author Abel Hoogeveen <abel@techfuze.net>
|
||||
|
|
|
@ -124,7 +124,7 @@ class Layout extends Bus {
|
|||
$this->assigned_variables['serverName'] = $this->config->main->SERVER_NAME;
|
||||
$this->assigned_variables['siteDomain'] = $this->config->main->SITE_DOMAIN;
|
||||
$this->assigned_variables['adminMail'] = $this->config->main->administrator_mail;
|
||||
$this->assigned_variables['contact'] = (array) $this->config->contact;
|
||||
$this->assigned_variables['contact'] = $this->config->contact->toArray();
|
||||
|
||||
// Select an engine if one is not already selected
|
||||
if (is_null($this->current_engine)) {
|
||||
|
|
Loading…
Reference in New Issue