Source for file class.confighandler.php
Documentation is available at class.confighandler.php
* Configuration storage handler classes.
* Includes following classes user for configuration handling:
* - interface IConfigBase
* - class ConfigBaseAbstract
* Abstract base configuration class, other childs must extend this
* - class ConfigFileStorage
* Handles configuration data in files
* - class ConfigDBStorage
* Handles configuration data in a table.
* NOTE: the table must be created before
* Factory class should used to get a instance of a configuration class
* @author Murat Purc <murat@purc.de>
* @copyright © Murat Purc 2008
* @subpackage Configuration
defined('CON_FRAMEWORK') or die('Illegal call');
* Interface definition for misc configuration handler.
* @author Murat Purc <murat@purc.de>
* @subpackage Configuration
* Method to get configuration, must be overwritten by child.
* Method to set configuration, must be overwritten by child.
* @param mixed $param Assoziative array or another type of variable
public function set($param);
* Method to remove configuration, must be overwritten by child.
* Abstract base config class. Extended classes must overwrite existing Methods.
* @author Murat Purc <murat@purc.de>
* @subpackage Configuration
* Flag about occured error
* Key of configuration (could be a file or a md5 hash, depends on child class)
* Lifetime of cached data in seconds
* Constructor of ConfigBaseAbstract, does some checks, stops further script execution if validation of
* @param array $options Assoziative options array as follows:
* $options['key'] = Configuration key, depending on child class
* $options['lifetime'] = Lifetime of data in seconds, optional
* @throws InvalidArgumentException If option key 'key' is missing or empty
if (!isset
($options['key'])) {
throw
new InvalidArgumentException('ConfigBaseAbstract->ConfigBaseAbstract: Missing argument $options["key"]');
} elseif ((string)
$options['key'] ===
'') {
throw
new InvalidArgumentException('ConfigBaseAbstract->ConfigBaseAbstract: Empty argument $options["key"]');
$this->_key =
$options['key'];
if (isset
($options['lifetime']) && (int)
$options['lifetime'] >
0) {
$this->_lifetime = (int)
$options['lifetime'];
* Returns remained lifetime
* @return mixed Remained lifetime or null
* ConfigFileStorage to store serialized configuration in files.
* $options['key'] = '/full/path/to/config.php';
* $config = ConfigFactory::get('filestorage', $options);
* @author Murat Purc <murat@purc.de>
* @subpackage Configuration
* Constructor of ConfigFileStorage, delegates parameter to parent.
* @param array $options Assoziative options array as follows:
* $options['key'] = Full path to configuration file
* $options['lifetime'] = Lifetime of data in seconds, optional
parent::__construct($options);
* Reads content of configuration file and returns the content as a unserialized PHP variable.
* @return mixed PHP variable or nul on error
* Serializes passed configuration and stores it into the configuation file
* @return bool True on success otherwhise false
public function set($content){
// use LOCK_EX if script is running under PHP5
$flag =
(defined('LOCK_EX')) ?
LOCK_EX :
null;
return ($result) ?
true :
false;
* Removes (unlink) configuration file.
* @return bool True on success otherwhise false
* Class ConfigDBStorage to store serialized configuration in a table.
* Needs a table, create it if not exists using following statement:
* -- {prefix} ist almost con by default
* CREATE TABLE {prefix}_configdbstorage (
* `id` varchar(32) NOT NULL default '' COMMENT 'md5 hash as PK',
* `value` text COMMENT 'Serialized content of configuration',
* // md5 hash of $options['key'] will be used as id in table {prefix}_configdbstorage
* $options['key'] = '/full/path/to/config.php';
* $config = ConfigFactory::get('dbstorage', $options);
* @todo Add caching lifetime to table
* @author Murat Purc <murat@purc.de>
* @subpackage Configuration
* Name of table where the configuration is stored
* Constructor of ConfigDBStorage, delegates parameter to parent and sets some properties
* @param array $options Assoziative options array as follows:
* $options['key'] = Normally full path to configuration file, where the md5 hash will be calculated
parent::__construct($options);
$this->_table =
$GLOBALS['cfg']['sql']['sqlprefix'] .
'_configdbstorage';
$this->_oDB =
new DB_Contenido();
* Returns the configuration from db table
* @return mixed PHP variable or nul on error
$sql =
'SELECT value FROM ' .
$this->_table .
' WHERE id="' .
$this->_key .
'"';
$this->_oDB->query($sql);
if ($this->_oDB->next_record()) {
* Serializes and writes passed configuration into the table.
* @return bool True on success otherwhise false
public function set($content){
$sql =
'UPDATE ' .
$this->_table .
' SET value="' .
$content .
'" WHERE id="' .
$this->_key .
'"';
$sql =
'INSERT INTO ' .
$this->_table .
' (`id`, `value`) VALUES ("' .
$this->_key .
'", "' .
$content .
'")';
$result =
$this->_oDB->query($sql);
return ($result) ?
true :
false;
* Removes the configuration from table.
* @return bool True on success otherwhise false
$sql =
'DELETE FROM ' .
$this->_table .
' WHERE id="' .
$this->_key .
'"';
$result =
$this->_oDB->query($sql);
return ($result) ?
true :
false;
* Factory class to get an instance of one of configuration classes
* // ConfigFileStorage example
* $options['key'] = '/full/path/to/config.php';
* $config = ConfigFactory::get('filestorage', $options);
* // ConfigDBStorage example
* $options['key'] = '/full/path/to/config.php';
* $config = ConfigFactory::get('dbstorage', $options);
* @author Murat Purc <murat@purc.de>
* @subpackage Configuration
* Returns a instance of desired configuration class
* @param string $name Type of configuration ('filestorage', 'serializer' or 'dbstorage')
* Note: 'serializer' is deprecated, use 'filestorage' instead
* @param array $options Options array
* @return IConfigBase A object based on a IConfigBase implementation
public static function get($name, $options) {
if ($name ==
'filestorage') {
} elseif ($name ==
'serializer') {
// @deprecated: use 'filestorage'
} elseif ($name ==
'dbstorage') {
* Emulate file_put_contents for PHP 4
* File append option for file_put_contents()
* This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.
* @param string $filename Path to the file where to write the data
* @param mixed $data The data to write. Can be either a string or a array
* @param int $flag Flag, supported is only FILE_APPEND (=1)
* @return mixed Number of written bytes or false on error
if (!$f =
@fopen($filename, $mode)) {
$bytes_written =
fwrite($f, $data);
Documentation generated on Sun, 08 Feb 2009 22:00:24 +0100 by phpDocumentor 1.4.1