Implements: IteratorAggregate | Traversable | ArrayAccess | Serializable | Countable
Database-based configuration loader.
Schema for configuration table:
group_name varchar(128)
config_key varchar(128)
config_value text
primary key (group_name, config_key)
Class declared in MODPATH/database/classes/config/database.php on line 3.
integer 1
integer 2
string
$_configuration_groupConfiguration group name
$_database_instance
$_database_tableLoads an empty array as the initial configuration and enables array keys to be used as properties.
void
public function __construct(array $config = NULL)
{
if (isset($config['instance']))
{
$this->_database_instance = $config['instance'];
}
elseif ($this->_database_instance === NULL)
{
$this->_database_instance = Database::$default;
}
if (isset($config['table']))
{
$this->_database_table = $config['table'];
}
parent::__construct();
}
Query the configuration table for all values for this group and unserialize each of the values.
string
$group
required - Group namearray
$config
= NULL - Configuration array$this
- Clone of the current objectpublic function load($group, array $config = NULL)
{
if ($config === NULL AND $group !== 'database')
{
// Load all of the configuration values for this group
$query = DB::select('config_key', 'config_value')
->from($this->_database_table)
->where('group_name', '=', $group)
->execute($this->_database_instance);
if (count($query) > 0)
{
// Unserialize the configuration values
$config = array_map('unserialize', $query->as_array('config_key', 'config_value'));
}
}
return parent::load($group, $config);
}
Overload setting offsets to insert or update the database values as changes occur.
string
$key
required - Array keymixed
$value
required - New valuemixed
public function offsetSet($key, $value)
{
if ( ! $this->offsetExists($key))
{
// Insert a new value
DB::insert($this->_database_table, array('group_name', 'config_key', 'config_value'))
->values(array($this->_configuration_group, $key, serialize($value)))
->execute($this->_database_instance);
}
elseif ($this->offsetGet($key) !== $value)
{
// Update the value
DB::update($this->_database_table)
->value('config_value', serialize($value))
->where('group_name', '=', $this->_configuration_group)
->where('config_key', '=', $key)
->execute($this->_database_instance);
}
return parent::offsetSet($key, $value);
}
Return the current group in serialized form.
echo $config;
string
public function __toString()
{
return serialize($this->getArrayCopy());
}
Return the raw array that is being used for this object.
$array = $config->as_array();
array
public function as_array()
{
return $this->getArrayCopy();
}
Get a variable from the configuration or return the default value.
$value = $config->get($key);
string
$key
required - Array keymixed
$default
= NULL - Default valuemixed
public function get($key, $default = NULL)
{
return $this->offsetExists($key) ? $this->offsetGet($key) : $default;
}
Sets a value in the configuration array.
$config->set($key, $new_value);
string
$key
required - Array keymixed
$value
required - Array value$this
public function set($key, $value)
{
$this->offsetSet($key, $value);
return $this;
}