Hallo!
Da ich oftmals mehrmals auf Datenbanken zugreifen muss, Teile wie einen Seitentitel oder einen Namen des öfteren verwende und auch ändere, lege ich solche Informationen gerne in einer Konfigurationsdatei ab. Nun habe ich eine Klasse geschrieben, die diese Datei verarbeiten soll. Ich würde mich freuen, wenn ihr mal einen Blick auf diese Klasse werfen könntet und mir eure Meinung dazu sagen könntet. Besonders würde mich interessieren, ob die Klasse eurer Meinung nach alle Aufgaben erfüllt und wie ihr dieses Verfahren findet.
MfG, Andy
Da ich oftmals mehrmals auf Datenbanken zugreifen muss, Teile wie einen Seitentitel oder einen Namen des öfteren verwende und auch ändere, lege ich solche Informationen gerne in einer Konfigurationsdatei ab. Nun habe ich eine Klasse geschrieben, die diese Datei verarbeiten soll. Ich würde mich freuen, wenn ihr mal einen Blick auf diese Klasse werfen könntet und mir eure Meinung dazu sagen könntet. Besonders würde mich interessieren, ob die Klasse eurer Meinung nach alle Aufgaben erfüllt und wie ihr dieses Verfahren findet.
MfG, Andy
PHP-Code:
<?php
/***
* Class Configuration
*
* The Configuration class allows reading a configuration-file.
* Furthermore you are able to add new entries to the configuration.
* Sure it is also possible to add, remove, edit, query and to save
* the new configuration. Finally it is important to
* mention that this class supports the structuring of the
* configuration in different sections, which sure can also
* be handled. Warning! This class can just handle existing
* configuration files.
*
* @package Configuration
* @version 0.3
* @author Andreas Wilhelm <Andreas2209@web.de>
* @copyright Andreas Wilhelm
**/
class Configuration
{
// instances
static private $instances = array();
// private class-variables
private $config = array();
private $file;
/**
* getInstance - Creates a new instance
*
* @access public
* @param Str $file
* @return NONE
*/
static public function getInstance($file)
{
if (!isset(self::$instances[$file])
{
self::$instances[$file] = new self($file);
}
return self::$instances[$file];
}
/**
* loadFile() - Loads configuration from path
*
* @access protected
* @param Str $file
* @return NONE
*/
protected function loadFile($file)
{
if( file_exists($file) )
{
// set path to config-file
$this->file = $file;
// save configuration to an array
$this->config = parse_ini_file($file, true);
}
else
{
throw new Exception("{$file} cannot be found.");
}
}
/**
* Constructor - Is called when the class is instanced
*
* @access protected
* @param Str $file
* @return NONE
*/
protected function __construct($file)
{
$this->loadFile($file);
}
/**
* __clone() - Is blocked
*
* @access private
* @return NONE
*/
private function __clone() {}
/**
* delItem() - Removes an item
*
* @access: public
* @param Str $section
* @param Str $key
* @return NONE
*/
public function delItem($section, $key)
{
if( isset($this->config[$section][$key]) )
{
unset($this->config[$section][$key]);
}
else
{
throw new Exception("Cannot remove {$section} - {$key}.");
}
}
/**
* setItem() - Assigns a new value to an entry of the config
*
* @access: public
* @param Str $section
* @param Str $key
* @param Str $value
* @param Bool $overwrite
* @return NONE
*/
public function setItem($section, $key, $value, $overwrite = false)
{
if( $overwrite === true )
{
$this->config[$section][$key] = $value;
}
elseif( !isset($this->config[$section][$key]) )
{
$this->config[$section][$key] = $value;
}
else
{
throw new Exception("Item {$section} - {$key} already exists.");
}
}
/**
* getItem() - Gets the value of an config-item
*
* @access: public
* @param Str $section
* @param Str $key
* @return String
*/
public function getItem($section, $key)
{
if( !isset($this->config[$section][$key]) )
{
throw new Exception("Cannot get item {$section} - {$key}.");
}
return $this->config[$section][$key];
}
/**
* rnItem() - Renames an item
*
* @access: public
* @param Str $section
* @param Str $from
* @param Str $to
* @return NONE
*/
public function rnItem($section, $from, $to)
{
if( isset($this->config[$section][$from]) && !isset($this->config[$section][$to]))
{
// move data to new section
$this->config[$section][$to] = $this->config[$section][$from];
// remove old section
$this->delItem($section, $from);
}
else
{
throw new Exception("Cannot rename item {$section} - {$from}.");
}
}
/**
* addSection() - Adds a section
*
* @access: public
* @param Str $name
* @return NONE
*/
public function addSection($name)
{
if( !isset($this->config[$name]) )
{
$this->config[$name] = array();
}
else
{
throw new Exception("Section {$name} already exists.");
}
}
/**
* delSection() - Deletes a section
*
* @access: public
* @param Str $name
* @param Boo $ifEmpty
* @return NONE
*/
public function delSection($name, $ifEmpty = true)
{
if( isset($this->config[$name]) )
{
if( ($ifEmpty == true) && (count($this->config[$name]) > 0) )
{
throw new Exception("Section {$name} is not empty.");
}
else
{
unset($this->config[$name]);
}
}
else
{
throw new Exception("Cannot found section {$name}.");
}
}
/**
* getSection() - Returns all items of a section
*
* @access: public
* @param Str $name
* @return Array
*/
public function getSection($name)
{
$items = array();
foreach( $this->config[$name] as $key => $value )
{
$items[$key] = $value;
}
return $items;
}
/**
* getSections() - Returns all sections
*
* @access: public
* @return Array
*/
public function getSections()
{
$sections = array();
foreach( $this->config as $key => $value )
{
if( is_array($value) )
{
$sections[] = $key;
}
}
return $sections;
}
/**
* rnSection() - Renames a section
*
* @access: public
* @param Str $from
* @param Str $to
* @return NONE
*/
public function rnSection($from, $to)
{
if( isset($this->config[$from]) && !isset($this->config[$to]))
{
// move data to new section
$this->config[$to] = $this->config[$from];
// remove old section
$this->delSection($from, false);
}
else
{
throw new Exception("Cannot rename section {$from}.");
}
}
/**
* getConfig() - Returns the whole configuration in an array
*
* @access: public
* @return Array
*/
public function getConfig()
{
return $this->config;
}
/**
* setConfig() - Creates a new Configuration from array
*
* @access: public
* @param Arr $new
* @return Boolean
*/
public function setConfig($new)
{
$this->config = $new;
}
/**
* save() - Save Configuration to file
*
* @access: public
* @return Boolean
*/
public function save()
{
$config = "; <?php die('Blocked unwanted request.'); ?>\n";
foreach( $this->config as $key => $value)
{
if( is_array($value) )
{
// save section name
$config .= "[$key]\n";
// save section items
foreach( $value as $k => $v)
{
$config .= "$k = \"$v\"\n";
}
}
else
{
// save item
$config .= "$key = \"$value\"\n";
}
}
echo $config;
if( !file_put_contents($this->file, $config) )
{
throw new Exception('Cannot save configuration.');
}
}
}
?>


Kommentar