Added new Kohana_Config and Kohana_Config_Loader, an extensible and poweful ArrayObject configuration loader, getter, and setter. Caching using Kohana::cache() is built in
This commit is contained in:
parent
4a8faa646c
commit
be1df5257a
163
system/classes/kohana/config.php
Normal file
163
system/classes/kohana/config.php
Normal file
|
@ -0,0 +1,163 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Kohana_Config extends ArrayObject {
|
||||||
|
|
||||||
|
// Has this object been changed?
|
||||||
|
protected $changed = FALSE;
|
||||||
|
|
||||||
|
// The upper parent of this object
|
||||||
|
protected $parent;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a new configuration object.
|
||||||
|
*
|
||||||
|
* @param array array to convert
|
||||||
|
* @param object parent of this object
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct($array, $parent = NULL)
|
||||||
|
{
|
||||||
|
if ($parent === NULL)
|
||||||
|
{
|
||||||
|
// The parent object is this object
|
||||||
|
$parent = $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the parent object
|
||||||
|
$this->parent = $parent;
|
||||||
|
|
||||||
|
parent::__construct($this->array_to_config($array), ArrayObject::ARRAY_AS_PROPS);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Acts as a getter and setter for the changed status of this object.
|
||||||
|
*
|
||||||
|
* @param boolean new status
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function changed($status = NULL)
|
||||||
|
{
|
||||||
|
if ($status === TRUE OR $status === FALSE)
|
||||||
|
{
|
||||||
|
$this->changed = $status;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->changed;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Recursively converts all of the arrays within an array to config objects.
|
||||||
|
*
|
||||||
|
* @param array array to convert
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function array_to_config($array)
|
||||||
|
{
|
||||||
|
foreach ($array as $key => $value)
|
||||||
|
{
|
||||||
|
if (is_array($value))
|
||||||
|
{
|
||||||
|
$array[$key] = new Kohana_Config($value, $this->parent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ArrayObject::getArrayCopy, recursively convert config objects to arrays.
|
||||||
|
*
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public function getArrayCopy()
|
||||||
|
{
|
||||||
|
$array = array();
|
||||||
|
foreach ($this as $key => $value)
|
||||||
|
{
|
||||||
|
if (is_object($value) AND $value instanceof Kohana_Config)
|
||||||
|
{
|
||||||
|
// Convert the value to an array, recursion
|
||||||
|
$value = $value->getArrayCopy();
|
||||||
|
}
|
||||||
|
|
||||||
|
$array[$key] = $value;
|
||||||
|
}
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ArrayObject::exchangeArray, recursively converts arrays to config objects.
|
||||||
|
*/
|
||||||
|
public function exchangeArray($array)
|
||||||
|
{
|
||||||
|
return parent::exchangeArray($this->array_to_config($array));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ArrayObject::offsetExists, forces missing indexes to filled with NULL.
|
||||||
|
*
|
||||||
|
* @param string array key name
|
||||||
|
* @return boolean
|
||||||
|
*/
|
||||||
|
public function offsetExists($index)
|
||||||
|
{
|
||||||
|
if ( ! parent::offsetExists($index))
|
||||||
|
{
|
||||||
|
$this->offsetSet($index, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ArrayObject::offsetGet, checks if offsets exists before returning them.
|
||||||
|
*
|
||||||
|
* @param string array key name
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function offsetGet($index)
|
||||||
|
{
|
||||||
|
// This will force missing values to
|
||||||
|
$this->offsetExists($index);
|
||||||
|
|
||||||
|
return parent::offsetGet($index);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ArrayObject::offsetSet, converts array values to config objects.
|
||||||
|
*
|
||||||
|
* @param string array key name
|
||||||
|
* @param mixed new value
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function offsetSet($index, $newval)
|
||||||
|
{
|
||||||
|
if (is_object($newval) AND $newval instanceof Kohana_Config)
|
||||||
|
{
|
||||||
|
// Simplify the object back to an array
|
||||||
|
$newval = $newval->getArrayCopy();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_array($newval))
|
||||||
|
{
|
||||||
|
// Convert the array into a config object
|
||||||
|
$newval = new Kohana_Config($newval, $this->parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Notify the parent that values have changed
|
||||||
|
$this->parent->changed(TRUE);
|
||||||
|
|
||||||
|
return parent::offsetSet($index, $newval);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetUnset($index)
|
||||||
|
{
|
||||||
|
if (parent::offsetExists($index))
|
||||||
|
{
|
||||||
|
// Notify the parent that values have changed
|
||||||
|
$this->parent->changed(TRUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
return parent::offsetUnset($index);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // End Kohana_Config
|
68
system/classes/kohana/config/loader.php
Normal file
68
system/classes/kohana/config/loader.php
Normal file
|
@ -0,0 +1,68 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class Kohana_Config_Loader_Core extends ArrayObject {
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
ArrayObject::__construct(array(), ArrayObject::ARRAY_AS_PROPS);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function save()
|
||||||
|
{
|
||||||
|
foreach ($this as $group => $config)
|
||||||
|
{
|
||||||
|
if ($config->changed())
|
||||||
|
{
|
||||||
|
echo Kohana::debug("$group config was changed during the request");
|
||||||
|
|
||||||
|
// Cache the group
|
||||||
|
Kohana::cache('kohana_config_'.$group, $config->getArrayCopy());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function load($group)
|
||||||
|
{
|
||||||
|
// Find all the configuration files
|
||||||
|
$files = Kohana::find_file('config', $group);
|
||||||
|
|
||||||
|
$config = array();
|
||||||
|
foreach ($files as $file)
|
||||||
|
{
|
||||||
|
// Merge the config files together
|
||||||
|
$config = array_merge($config, Kohana::load_file($file));
|
||||||
|
}
|
||||||
|
|
||||||
|
return $config;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetExists($group)
|
||||||
|
{
|
||||||
|
if ( ! parent::offsetExists($group))
|
||||||
|
{
|
||||||
|
if (($config = Kohana::cache('kohana_config_'.$group)) === NULL)
|
||||||
|
{
|
||||||
|
// Load the configuration group
|
||||||
|
$config = $this->load($group);
|
||||||
|
|
||||||
|
// Cache the configuration
|
||||||
|
Kohana::cache('kohana_config_'.$group, $config);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the missing configuration
|
||||||
|
$this->offsetSet($group, new Kohana_Config($config));
|
||||||
|
}
|
||||||
|
|
||||||
|
return TRUE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetGet($index)
|
||||||
|
{
|
||||||
|
$this->offsetExists($index);
|
||||||
|
|
||||||
|
return parent::offsetGet($index);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // End Kohana_Config_Loader
|
Loading…
Reference in a new issue