Made INI the new PICKLES config standard
* SimpleXML was becoming a pain to maintain as there was differences between versions of PHP 5.x that prohibited me from being truely 5.0.0+ compliant. * The move to INI is because it's still good enough for PHP to use php.ini as a standard, and the support in PHP is built in and hasn't changed in a while * XML was also a bit bloated, and the point behind PICKLES is to kick the bloat. * May add support for a config file in pure PHP and/or JSON. * Added logic to prohibit the overriding of config variables.
This commit is contained in:
parent
a191a99561
commit
1eff5c3db7
1 changed files with 37 additions and 55 deletions
|
@ -13,16 +13,14 @@
|
|||
* @copyright Copyright 2007-2010, Gravity Boulevard, LLC
|
||||
* @license http://www.gnu.org/licenses/gpl.html GPL v3
|
||||
* @link http://phpwithpickles.org
|
||||
* @todo Add support for .php and .ini config files (auto scan directory?)
|
||||
*/
|
||||
|
||||
/**
|
||||
* Config Class
|
||||
*
|
||||
* Handles loading a configuration file and parsing the data for
|
||||
* any public nodes that need to be made available to the viewer.
|
||||
* Handles loading the site's configuration file (if available).
|
||||
*
|
||||
* @usage <code>$config = new Config($filename); // $filename is optional, default = ../config.xml</code>
|
||||
* @usage <code>$config = new Config($filename);</code>
|
||||
*/
|
||||
class Config
|
||||
{
|
||||
|
@ -35,16 +33,24 @@ class Config
|
|||
*/
|
||||
private static $instance;
|
||||
|
||||
/**
|
||||
* Config data
|
||||
*
|
||||
* @access private
|
||||
* @var array
|
||||
*/
|
||||
private $data = array();
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* Calls the parent constructor and loads the pass file
|
||||
*
|
||||
* @param string $file Filename of the config file (optional)
|
||||
* @param string $filename optional Filename of the config
|
||||
*/
|
||||
public function __construct($file = '../config.xml')
|
||||
public function __construct($filename = '../config.ini')
|
||||
{
|
||||
$this->load($file);
|
||||
$this->load($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -53,47 +59,18 @@ class Config
|
|||
* Handles the potential loading of the configuration file and
|
||||
* sanitizing the boolean strings into actual boolean values.
|
||||
*
|
||||
* @param string $file Filename of the XML file to be loaded
|
||||
* @param string $filename filename of the config file
|
||||
* @return boolean Success of the load process
|
||||
* @todo Add the ability to load in multiple configuration files.
|
||||
*/
|
||||
public function load($file)
|
||||
public function load($filename)
|
||||
{
|
||||
// Makes sure the file is legit on the surface
|
||||
if (file_exists($file) && is_file($file) && is_readable($file))
|
||||
// Sanity checks the config file
|
||||
if (file_exists($filename) && is_file($filename) && is_readable($filename))
|
||||
{
|
||||
// Pulls the contents of the file to allow for validation
|
||||
$contents = trim(file_get_contents($file));
|
||||
$this->data = parse_ini_file($filename, true);
|
||||
|
||||
// Checks that the file contents isn't empty and that the config node is present
|
||||
if ($contents != '' && substr($contents, 0, 8) == '<config>' && substr($contents, -9) == '</config>')
|
||||
{
|
||||
/**
|
||||
* @todo LIBXML_NOCDATA is 5.1+ and I want PICKLES to
|
||||
* be 5.0+ compatible. Potential fix is to read
|
||||
* the file in as a string, and if it has CDATA,
|
||||
* throw an internal warning.
|
||||
*/
|
||||
$data = simplexml_load_string($contents, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
|
||||
// Loops through the top level nodes to find public nodes
|
||||
$variables = get_object_vars($data);
|
||||
|
||||
if (is_array($variables))
|
||||
{
|
||||
foreach ($variables as $key => $value)
|
||||
{
|
||||
if (is_object($value) && isset($value->attributes()->public) && $value->attributes()->public == true)
|
||||
{
|
||||
$this->_public[$key] = $value;
|
||||
}
|
||||
|
||||
$this->$key = $value;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -117,32 +94,37 @@ class Config
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets status of site
|
||||
* Magic Setter Method
|
||||
*
|
||||
* Checks the site->disabled variable for truth.
|
||||
* Prohibits the direct modification of module variables.
|
||||
*
|
||||
* @return boolean whether or not the site is disabled
|
||||
* @param string $name name of the variable to be set
|
||||
* @param mixed $value value of the variable to be set
|
||||
* @return boolean false
|
||||
*/
|
||||
public function disabled()
|
||||
public function __set($name, $value)
|
||||
{
|
||||
return (isset($this->site->disabled) && $this->site->disabled == 'true');
|
||||
trigger_error('Cannot set config variables directly', E_USER_ERROR);
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets default module
|
||||
* Magic Getter Method
|
||||
*
|
||||
* @return string default module or null
|
||||
* Attempts to load the config variable. If it's not set, will override
|
||||
* the variable with boolean false.
|
||||
*
|
||||
* @param string $name name of the variable requested
|
||||
* @return mixed value of the variable or boolean false
|
||||
*/
|
||||
public function module()
|
||||
public function __get($name)
|
||||
{
|
||||
$module = null;
|
||||
|
||||
if (isset($this->site->module) && trim($this->site->module) != '')
|
||||
if (!isset($this->data[$name]))
|
||||
{
|
||||
$module = (string)$this->site->module;
|
||||
$this->data[$name] = false;
|
||||
}
|
||||
|
||||
return $module;
|
||||
return $this->data[$name];
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue