Updated the Config class to use objects instead of an array (overriding __get() saved some effort). Also refactored some code, and updated the controller (partially) to handle shared models.
git-svn-id: http://svn.cleancode.org/svn/pickles@60 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
parent
d38196073a
commit
fcf1ffde4e
12 changed files with 164 additions and 246 deletions
|
@ -3,11 +3,8 @@
|
|||
/**
|
||||
* Configuration class
|
||||
*
|
||||
* Handles loading and caching of the configuration into a Singleton object.
|
||||
* Contains logic to determine if the configuration has already been loaded
|
||||
* and will opt to use a "frozen" object rather than instantiate a new one.
|
||||
* Also contains logic to reload configurations if the configuration file
|
||||
* had been modified since it was originally instantiated (smart caching).
|
||||
* Handles loading a configuration file and parsing the data for any public
|
||||
* nodes that need to be made available to the viewer.
|
||||
*
|
||||
* @package PICKLES
|
||||
* @author Joshua Sherman <josh@phpwithpickles.org>
|
||||
|
@ -55,44 +52,30 @@ class Config extends Singleton {
|
|||
* Handles the potential loading of the configuration file and
|
||||
* sanitizing the boolean strings into actual boolean values.
|
||||
*
|
||||
* @param string $file File name of the configuration file to be loaded
|
||||
* @param string $file File name of the config file to be loaded
|
||||
* @return boolean Based on the success or failure of the file load
|
||||
* @todo Either get rid of or make better use of the Error object
|
||||
* @todo Some of this code seems screwy, (bool) on an array??
|
||||
*/
|
||||
public function load($file) {
|
||||
if (file_exists($file)) {
|
||||
$this->file = $file;
|
||||
|
||||
|
||||
/**
|
||||
* @todo LIBXML_NOCDATA is 5.1+ and I want PICKLES to be 5.0+ compatible
|
||||
* @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.
|
||||
*/
|
||||
$config_array = ArrayUtils::object2array(simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA));
|
||||
$this->data = simplexml_load_file($file, 'SimpleXMLElement', LIBXML_NOCDATA);
|
||||
|
||||
/**
|
||||
* @todo Loop through the object and deal with it accordingly
|
||||
*/
|
||||
if (is_array($config_array)) {
|
||||
foreach ($config_array as $variable => $value) {
|
||||
if ($value == 'true' || $value == array()) {
|
||||
$value = (bool)$value;
|
||||
// Loops through the top level nodes to find public nodes
|
||||
$variables = get_object_vars($this->data);
|
||||
|
||||
if (is_array($variables)) {
|
||||
foreach ($variables as $key => $value) {
|
||||
if (is_object($value) && isset($value->attributes()->public) && $value->attributes()->public == true) {
|
||||
$this->viewer_data[$key] = $value;
|
||||
}
|
||||
|
||||
if (isset($value['@attributes']['public'])) {
|
||||
if ($value['@attributes']['public'] == true) {
|
||||
|
||||
if (count($value['@attributes']) == 1) {
|
||||
unset($value['@attributes']);
|
||||
}
|
||||
else {
|
||||
unset($value['@attributes']['public']);
|
||||
}
|
||||
|
||||
$this->viewer_data[$variable] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
$this->data[$variable] = $value == array() ? (bool)$value : $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -105,34 +88,73 @@ class Config extends Singleton {
|
|||
}
|
||||
|
||||
/**
|
||||
* Gets the default model
|
||||
* Alias for $config->models->default
|
||||
*
|
||||
* @return Returns the default model as set
|
||||
* @return Returns the default model set or null
|
||||
* @todo Need to add a PICKLES fallback model just in case
|
||||
*/
|
||||
public function getDefaultModel() {
|
||||
if (isset($this->data['models']['default'])) {
|
||||
return $this->data['models']['default'];
|
||||
if (isset($this->data->models->default)) {
|
||||
return (string)$this->data->models->default;
|
||||
}
|
||||
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the viewer data
|
||||
*
|
||||
* @return Returns either the variable value or false if no variable.
|
||||
* @todo Need better checking if the passed variable is an array when
|
||||
* the array element value is present
|
||||
* @todo Returning false could be misleading, especially if you're
|
||||
* expecting a boolean value to begin with. Perhaps an error
|
||||
* should be thrown?
|
||||
*/
|
||||
public function getSharedModel($requested_model) {
|
||||
|
||||
$additional = null;
|
||||
|
||||
if (strpos($requested_model, '/') !== false) {
|
||||
list($requested_model, $additional) = split('/', $requested_model, 2);
|
||||
$additional = '/' . $additional;
|
||||
}
|
||||
|
||||
if (isset($this->data->models->shared->model)) {
|
||||
foreach ($this->data->models->shared->model as $shared_model) {
|
||||
if (isset($shared_model->alias)) {
|
||||
if ($requested_model == $shared_model->alias) {
|
||||
return (string)$shared_model->name . $additional;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if ($requested_model == $shared_model->name) {
|
||||
return (string)$shared_model->name . $additional;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Alias for $config->viewer_data
|
||||
*
|
||||
* @return Returns either the variable value or null if no variable.
|
||||
*/
|
||||
public function getViewerData() {
|
||||
if (isset($this->viewer_data)) {
|
||||
return $this->viewer_data;
|
||||
}
|
||||
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
public function __get($node) {
|
||||
if (isset($this->data->$node) && $this->data->$node != '') {
|
||||
if (in_array($this->data->$node, array('true', 'false'))) {
|
||||
return (bool)$this->data->$node;
|
||||
}
|
||||
else if (is_object($this->data->$node)) {
|
||||
return $this->data->$node;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue