Made a bunch of changes, as per usual.

git-svn-id: http://svn.cleancode.org/svn/pickles@59 4d10bc64-7434-11dc-a737-d2d0f8310089
This commit is contained in:
Josh Sherman 2008-10-03 01:37:45 +00:00
parent e02ded351d
commit d38196073a
7 changed files with 134 additions and 91 deletions

View file

@ -26,17 +26,27 @@ class ArrayUtils {
* @return array Resulting array formed from the passed object
*/
public static function object2array($object) {
if (is_object($object)) {
$object = (array)$object;
$array = null;
if (is_array($object)) {
foreach ($object as $key => $value) {
$array[$key] = self::object2array($value);
}
}
foreach ($object as $key => $value) {
if (is_object($value)) {
$object[$key] = self::object2array($value);
else {
$variables = get_object_vars($object);
if (is_array($variables)) {
foreach ($variables as $key => $value) {
$array[$key] = ($key && !$value) ? null : self::object2array($value);
}
}
else {
return $object;
}
}
return $object;
return $array;
}
}

View file

@ -21,6 +21,11 @@ class Config extends Singleton {
*/
private static $instance;
/**
* Private collection of data to be loaded by the viewer
*/
private $viewer_data;
/**
* Private constructor
*/
@ -53,48 +58,44 @@ class Config extends Singleton {
* @param string $file File name of the configuration 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;
$config_array = ArrayUtils::object2array(simplexml_load_file($file));
/**
* @todo LIBXML_NOCDATA is 5.1+ and I want PICKLES to be 5.0+ compatible
*/
$config_array = ArrayUtils::object2array(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;
}
if (isset($value['@attributes']['public'])) {
if ($value['@attributes']['public'] == true) {
if (count($value['@attributes']) == 1) {
unset($value['@attributes']);
}
else {
unset($value['@attributes']['public']);
}
$this->$variable = $value == array() ? (bool)$value : $value;
$this->viewer_data[$variable] = $value;
}
}
$this->data[$variable] = $value == array() ? (bool)$value : $value;
}
}
/**
* @todo Okay, now if no default section is specified, we'll
* default to the first section listed. But what
* should be done if no sections are specified?
* Perhaps force just the index to load, or perhaps
* error out? I have to keep in mind that single
* page sites exist where no navigation will exist.
* So yeah, I suppose just specifying the default
* would combat against that, or should I drill down
* further, and see if any site level models exist and
* load the first one, or even better I'm thinking
* that a shared model / template would be good when
* nothing is available. That would in turn tell the
* user to fix the issue to be able to get it all
* working again. Damn, this ended up being a very
* long @todo.
* @todo This may be better suited in the loop above since
* we're already looping through all the values, we
* could snag it in passing.
*/
if (!isset($this->navigation['default']) || $this->navigation['default'] == '') {
$this->navigation['default'] = key($this->navigation['sections']);
}
return true;
}
else {
@ -102,6 +103,37 @@ class Config extends Singleton {
return false;
}
}
/**
* Gets the default model
*
* @return Returns the default model as set
*/
public function getDefaultModel() {
if (isset($this->data['models']['default'])) {
return $this->data['models']['default'];
}
return false;
}
/**
* 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 getViewerData() {
if (isset($this->viewer_data)) {
return $this->viewer_data;
}
return false;
}
}
?>

View file

@ -50,7 +50,7 @@ class Controller extends Object {
}
// Grab the passed in model or use the default
$name = isset($_REQUEST['model']) ? str_replace('-', '_', $_REQUEST['model']) : $this->config->get('navigation', 'default');
$name = isset($_REQUEST['model']) ? str_replace('-', '_', $_REQUEST['model']) : $this->config->getDefaultModel();
if ($name == 'logout') {
Security::logout();
@ -77,7 +77,10 @@ class Controller extends Object {
$this->model = new $class;
}
}
elseif (file_exists($shared_file)) {
/**
* @todo Fix the shared stuff
*/
elseif (file_exists($shared_file) && $this->config->get('models', 'shared') != false) {
if (class_exists($class)) {
$this->model = new $class;
}
@ -86,33 +89,34 @@ class Controller extends Object {
$this->model = new Model();
}
// Start the session if it's not started already
/**
* @todo Need to make the session not so mandatory.
*/
if ($this->model->getSession() === true) {
if (ini_get('session.auto_start') == 0) {
session_start();
if ($this->model != null) {
// Start the session if it's not started already
if ($this->model->getSession() === true) {
if (ini_get('session.auto_start') == 0) {
session_start();
}
}
if ($this->model->getAuthentication() === true && $controller != 'CLI') {
Security::authenticate();
}
/**
* @todo are any of these relevant any more?
*/
$this->model->set('name', $name);
$this->model->set('section', $section);
//$this->model->set('event', $event);
// Execute the model's logic
if (method_exists($this->model, '__default')) {
$this->model->__default();
}
// Load the viewer
$this->viewer = Viewer::factory($this->model);
$this->viewer->display();
}
if ($this->model->getAuthenticate() === true && $controller != 'CLI') {
Security::authenticate();
}
/**
* @todo are any of these relevant any more?
*/
$this->model->set('name', $name);
$this->model->set('section', $section);
$this->model->set('event', $event);
// Execute the model's logic
$this->model->__default();
// Load the viewer
$this->viewer = Viewer::factory($this->model);
$this->viewer->display();
}
}

View file

@ -28,9 +28,9 @@ class Model extends Object {
*/
protected $name = null;
protected $authenticate = null;
protected $viewer = null;
protected $session = null;
protected $authentication = null;
protected $viewer = null;
protected $session = null;
/**
* Constructor
@ -50,13 +50,13 @@ class Model extends Object {
* @todo Add in configuration level override
* @return boolean Whether or not the model requires user authentication to use
*/
public function getAuthenticate() {
public function getAuthentication() {
// Order of precedence: Model, Config, Guess
if ($this->authenticate == null) {
if ($this->authentication == null) {
return false;
}
else {
return $this->authenticate;
return $this->authentication;
}
}
@ -68,7 +68,7 @@ class Model extends Object {
*/
public function getSession() {
// Order of precedence: Auth On, Model, Config, Guess
if ($this->authenticate === true) {
if ($this->authentication === true) {
return true;
}
else {

View file

@ -14,6 +14,11 @@
*/
class Singleton {
/**
* Protected collection of data
*/
protected $data;
/**
* Private constructor
*/
@ -40,16 +45,16 @@ class Singleton {
* should be thrown?
*/
public function get($variable, $array_element = null) {
if (isset($this->$variable)) {
if (isset($this->data[$variable])) {
if (isset($array_element)) {
$array = $this->$variable;
$array = $this->data[$variable];
if (isset($array[$array_element])) {
return $array[$array_element];
}
}
else {
return $this->$variable;
return $this->data[$variable];
}
}
@ -63,7 +68,7 @@ class Singleton {
* @param mixed $value Value to be assigned to the passed variable
*/
public function set($variable, $value) {
$this->$$variable = $value;
$this->data[$variable] = $value;
}
}

View file

@ -24,8 +24,6 @@ class Viewer {
*
* @param object $model The model to be displayed
* @return object An instance of the viewer, loaded with the passed model
* @todo Add some checking to determine if the passed object is really a
* valid instance of Model.
* @todo Create constants to correspond with each viewer type so it's
* potentially easier to reference from the model (since the
* constants would each be uppercase instead of mixedcase.

View file

@ -9,7 +9,7 @@
* @subpackage Viewer
* @author Joshua Sherman <josh@phpwithpickles.org>
* @copyright 2007-2008 Joshua Sherman
* @link http://smart.net/
* @link http://smarty.net/
*/
class Viewer_Smarty extends Viewer_Common {
@ -69,19 +69,6 @@ class Viewer_Smarty extends Viewer_Common {
}
}
$navigation = $this->config->get('navigation', 'sections');
// Add the admin section if we're authenticated
/**
* @todo Add logic to check if the user is already logged in. Currently
* it is always assumed that they are not.
*/
if (false) {
if ($this->config->get('admin', 'menu') == true) {
$navigation['admin'] = 'Admin';
}
}
/**
* @todo Maybe the template path should be part of the configuration?
*/
@ -95,7 +82,6 @@ class Viewer_Smarty extends Viewer_Common {
}
// Pass all of our controller values to Smarty
$smarty->assign('navigation', $navigation);
$smarty->assign('section', $this->model->get('section'));
$smarty->assign('model', $this->model->get('name'));
/**
@ -103,8 +89,8 @@ class Viewer_Smarty extends Viewer_Common {
* @todo I'm not entirely sure that these values are necessary at all due
* to new naming conventions.
*/
$smarty->assign('action', $this->model->get('action'));
$smarty->assign('event', $this->model->get('action'));
//$smarty->assign('action', $this->model->get('action'));
//$smarty->assign('event', $this->model->get('action'));
// Thanks to new naming conventions
$smarty->assign('admin', $this->config->get('admin', 'sections'));
@ -121,6 +107,14 @@ class Viewer_Smarty extends Viewer_Common {
}
*/
// Loads the data from the config
$data = $this->config->getViewerData();
if (isset($data) && is_array($data)) {
$smarty->assign('config', $data);
}
// Loads the data from the model
$data = $this->model->getData();
if (isset($data) && is_array($data)) {