From 64dc006b5f6a86b15ec7c20b3a08d65726c8f2ab Mon Sep 17 00:00:00 2001 From: Joshua Sherman Date: Mon, 30 Dec 2013 15:47:13 -0500 Subject: [PATCH] Documented some stuff, refactoring some other stuff Moved re-used object instances to the Object class and added object loader logic to the constructor. --- classes/Controller.php | 10 +++++--- classes/Display.php | 2 +- classes/Model.php | 24 +++-------------- classes/Module.php | 58 ++++++++++++++++++++++-------------------- classes/Object.php | 40 ++++++++++++++++++++++++++--- classes/Session.php | 11 -------- 6 files changed, 77 insertions(+), 68 deletions(-) diff --git a/classes/Controller.php b/classes/Controller.php index df3cccb..bcdb746 100644 --- a/classes/Controller.php +++ b/classes/Controller.php @@ -295,11 +295,11 @@ class Controller extends Object if (!is_array($module_return)) { - $module_return = $module->return_data; + $module_return = $module->return; } else { - $module_return = array_merge($module_return, $module->return_data); + $module_return = array_merge($module_return, $module->return); } } @@ -310,7 +310,7 @@ class Controller extends Object } // @todo Set this in the module and use $module->return and rename module->return to module->data? - $module->return = ['template', 'json']; + $module->display = ['template', 'json']; // Checks if we have any templates $parent_template = $module->template; @@ -334,8 +334,10 @@ class Controller extends Object } } + // @todo Should simplify this, give Display direct acess to + // $module instead of all these variable assignment $display = new Display(); - $display->return = $module->return; + $display->output = $module->output; $display->templates = $module->template; $display->module = isset($module_return) ? $module_return : ['status' => 'error', 'message' => $error_message]; diff --git a/classes/Display.php b/classes/Display.php index db9e439..23ad71d 100644 --- a/classes/Display.php +++ b/classes/Display.php @@ -81,7 +81,7 @@ class Display extends Object if (!is_array($this->return)) { - $this->return = [ $this->return ]; + $this->return = [$this->return]; } $return_json = $return_rss = $return_template = $return_xml = false; diff --git a/classes/Model.php b/classes/Model.php index fee8815..a5f9a83 100644 --- a/classes/Model.php +++ b/classes/Model.php @@ -34,14 +34,6 @@ class Model extends Object */ private $model = null; - /** - * Database Object - * - * @access protected - * @var object - */ - protected $db = null; - /** * Columns * @@ -52,14 +44,6 @@ class Model extends Object */ protected $columns = null; - /** - * Cache Object - * - * @access - * @var object - */ - protected $cache = null; - /** * Whether or not to use cache * @@ -327,16 +311,14 @@ class Model extends Object } // Runs the parent constructor so we have the config - parent::__construct(); + parent::__construct(['cache', 'db']); - // Gets an instance of the database and check which it is - $this->db = Database::getInstance(); + // Interrogates our database object $this->use_cache = $this->db->cache; $this->mysql = ($this->db->driver == 'pdo_mysql'); $this->postgresql = ($this->db->driver == 'pdo_pgsql'); - // Sets up the cache object and grabs the class name to use in our cache keys - $this->cache = Cache::getInstance(); + // Grabs the class name to use in our cache keys $this->model = get_class($this); // Default column mapping diff --git a/classes/Module.php b/classes/Module.php index de7208b..1122ec3 100644 --- a/classes/Module.php +++ b/classes/Module.php @@ -27,28 +27,12 @@ */ class Module extends Object { - /** - * Cache Object - * - * @access protected - * @var object - */ - protected $cache = null; - - /** - * Database Object - * - * @access protected - * @var object - */ - protected $db = null; - /** * Page Title * * @access protected * @var string, null by default - * @todo Move to public scope + * @todo Move to public scope, then abandon for $this->meta */ protected $title = null; @@ -57,7 +41,7 @@ class Module extends Object * * @access protected * @var string, null by default - * @todo Move to public scope + * @todo Move to public scope, then abandon for $this->meta */ protected $description = null; @@ -66,10 +50,21 @@ class Module extends Object * * @access protected * @var string, null by default - * @todo Move to public scope + * @todo Move to public scope, then abandon for $this->meta */ protected $keywords = null; + /** + * Meta Data + * + * @var array + */ + public $meta = [ + 'title' => '', + 'description' => '', + 'keywords' => '', + ]; + /** * Secure * @@ -98,6 +93,9 @@ class Module extends Object * @access protected * @var boolean, false (not AJAX) by default * @todo Move to public scope + * @todo Doesn't seem to be in use, but I have it defined on Clipinary + * don't want to remove until I drop it else it would end up in the + * module return array. */ protected $ajax = false; @@ -146,11 +144,20 @@ class Module extends Object * @access protected * @var array * @todo Move to public scope and rename __return so it's kinda obscured + * @todo Will need to update leaderbin and sndcrd to use new variable */ - protected $return_data = array(); + protected $return = array(); - // @todo Document me - public $return = array(); + /** + * Output + * + * What should the class render as output? This can be a string or an array + * containing either 'json', 'rss', 'template' or 'xml'. Default is to use + * templates and if the template is not present, fall back to JSON. + * + * @var mixed string or array + */ + public $output = ['template', 'json']; /** * Constructor @@ -165,10 +172,7 @@ class Module extends Object */ public function __construct($autorun = false, $validate = true) { - parent::__construct(); - - $this->cache = Cache::getInstance(); - $this->db = Database::getInstance(); + parent::__construct(['cache', 'db']); if ($autorun === true) { @@ -217,7 +221,7 @@ class Module extends Object } else { - $this->return_data[$name] = $value; + $this->return[$name] = $value; } } diff --git a/classes/Object.php b/classes/Object.php index 4abd617..5ff13ce 100644 --- a/classes/Object.php +++ b/classes/Object.php @@ -29,9 +29,9 @@ class Object * * @static * @access private - * @var mixed + * @var array */ - protected static $instances = array(); + protected static $instances = []; /** * Instance of the Config object @@ -42,8 +42,23 @@ class Object protected $config = null; /** - * Profiler flag + * Instance of the Cache object * + * @access protected + * @var object + */ + protected $cache = null; + + /** + * Instance of the Database object + * + * @access protected + * @var object + */ + protected $db = null; + + /** + * Profiler flag * * @access private * @var mixed @@ -55,7 +70,7 @@ class Object * * Establishes a Config instance for all children to enjoy */ - public function __construct() + public function __construct($objects = null) { // Gets an instance of the config, unless we ARE the config if (get_class($this) == 'Config') @@ -67,6 +82,23 @@ class Object $this->config = Config::getInstance(); } + if ($objects) + { + if (!is_array($objects)) + { + $objects = [$objects]; + } + + foreach ($objects as $object) + { + switch ($object) + { + case 'cache': $this->cache = Cache::getInstance(); break; + case 'db': $this->db = Database::getInstance(); break; + } + } + } + // Assigns the profiler flag $this->profiler = (isset($this->config->pickles['profiler']) && $this->config->pickles['profiler'] != '' ? $this->config->pickles['profiler'] : false); diff --git a/classes/Session.php b/classes/Session.php index ead03c8..e5218b5 100644 --- a/classes/Session.php +++ b/classes/Session.php @@ -105,17 +105,6 @@ class Session extends Object */ private $table = null; - /** - * Database - * - * Our database object to interact with the aforementioned datasource - * and table. This object is shared with other PICKLES internals. - * - * @access private - * @var object - */ - private $db = null; - /** * Constructor *