Refactoring Controller and Module

This commit is contained in:
Josh Sherman 2010-03-11 23:16:14 -05:00
parent 6c0b28b82f
commit 3c75861e9c
2 changed files with 148 additions and 307 deletions

View file

@ -26,76 +26,82 @@
*
* @usage <code>new Controller($config);</code>
*/
class Controller extends Object {
private $execute_tests = false;
class Controller extends Object
{
/**
* Constructor
*
* To make life a bit easier when using PICKLES, the Controller logici
* To make life a bit easier when using PICKLES, the Controller logic
* is executed automatically via use of a constructor.
*
* @param mixed Config object or filename (optional)
*/
public function __construct($config = null) {
public function __construct()
{
parent::__construct();
// Creates the core objects that don't need a Config object
$error = new Error();
// Check the passed config variables object type
if (is_object($config)) {
if ($config instanceof Config === false) {
$error->addWarning('Passed object is not an instance of Config');
$config = null;
}
}
// Config filename to be loaded
$filename = null;
// Checks if the config value is a filename
if (is_string($config)) {
if (file_exists($config)) {
$filename = $config;
}
else {
$error->addWarning('Passed config filename does not exist');
}
}
else {
$config = null;
}
// If no Config object is passed (or it's cleared), create a new one from assumptions
if ($config == null) {
$config = new Config();
}
else {
$config = new Config($filename);
}
unset($filename);
// Creates all the other core objects we need to pass around
$db = new DB($config, $error);
$mailer = new Mailer($config, $error);
// Generate a generic "site down" message
if ($config->getDisabled()) {
if ($this->config->disabled()) {
exit("<h2><em>{$_SERVER['SERVER_NAME']} is currently down for maintenance</em></h2>");
}
// Loads the default module
$module['requested']['name'] = $config->getDefaultModule();
// Loads the default module information (if any)
$basename = $this->config->module();
// Attempts to override the default module
if (isset($_REQUEST['module'])) {
if (strpos($config->templates->main, $_REQUEST['module']) !== 0) {
$module['requested']['name'] = $_REQUEST['module'];
if ($basename != null)
{
$module_class = strtr($basename, '/', '_');
$module_filename = '../modules/' . $basename . '.php';
$css_class = strtr($basename, '_', '-');
$js_filename = $basename;
unset($basename);
}
// Attempts to override the defaults with passed information (if any)
if (isset($_REQUEST['module']) && trim($_REQUEST['module']) != '')
{
$new_basename = strtr($_REQUEST['module'], '-', '_');
$new_module_class = strtr($new_basename, '/', '_');
$new_module_filename = '../modules/' . $new_basename . '.php';
$new_css_class = strtr($new_basename, '_', '-');
$new_js_filename = $new_basename;
// File exists, proceed with override
if (file_exists($new_module_filename))
{
$module_class = $new_module_class;
$module_filename = $new_module_filename;
$css_class = $new_css_class;
$js_filename = $new_js_filename;
}
unset($new_basename, $new_module_class, $new_module_filename, $new_css_class, $new_js_filename);
}
// Loads the module or errors out
if (isset($module_filename) && $module_filename != null && file_exists($module_filename))
{
require_once $module_filename;
// Checks that our class exists
if (class_exists($module_class))
{
$module = new $module_class;
// Checks that our default method exists
if (method_exists($module, '__default'))
{
var_dump($module->__default());
}
}
}
else
{
// @todo Error handling
// @todo Should we be creating a new generic Module?
}
exit('EOF');
// Checks if we have a display type passed in
if (strpos($module['requested']['name'], '.') !== false) {
@ -120,37 +126,7 @@ class Controller extends Object {
}
}
// Loads the requested module's information
$module['requested']['filename'] = strtr($module['requested']['name'], '-', '_');
$module['requested']['php_file'] = '../modules/' . $module['requested']['filename'] . '.php';
$module['requested']['class_name'] = strtr($module['requested']['filename'], '/', '_');
// Establishes the shared module information
$module['shared']['name'] = $config->getSharedModule($module['requested']['name']);
$module['shared']['filename'] = strtr($module['shared']['name'], '-', '_');
$module['shared']['php_file'] = PICKLES_PATH . 'common/modules/' . $module['shared']['filename'] . '.php';
$module['shared']['class_name'] = strtr($module['shared']['filename'], '/', '_');
// Tries to load the site level module
if (file_exists($module['requested']['php_file'])) {
require_once $module['requested']['php_file'];
if (class_exists($module['requested']['class_name'])) {
$module['object'] = new $module['requested']['class_name']($config, $db, $mailer, $error);
}
}
// Tries to load the shared module
else if (file_exists($module['shared']['php_file']) && $module['shared']['name'] != false) {
require_once $module['shared']['php_file'];
if (class_exists($module['shared']['class_name'])) {
$module['object'] = new $module['shared']['class_name']($config, $db, $mailer, $error);
}
}
// Loads the stock module
else {
$module['object'] = new Module($config, $db, $mailer, $error);
}
// Checks if we loaded a module file and no class was present
if ($module['object'] != null) {

View file

@ -3,261 +3,126 @@
/**
* Module Class File for PICKLES
*
* PICKLES is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
* PHP version 5
*
* PICKLES is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
* Licensed under the GNU General Public License Version 3
* Redistribution of these files must retain the above copyright notice.
*
* You should have received a copy of the GNU Lesser General Public
* License along with PICKLES. If not, see
* <http://www.gnu.org/licenses/>.
*
* @author Joshua John Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007, 2008, 2009 Joshua John Sherman
* @package pickles
* @author Josh Sherman <josh@phpwithpickles.org>
* @copyright Copyright 2007-2010, Gravity Boulevard, LLC
* @license http://www.gnu.org/licenses/gpl.html GPL v3
* @link http://phpwithpickles.org
* @license http://www.gnu.org/copyleft/lesser.html
* @package PICKLES
*/
/**
* Module Class
*
* Every module (page) in PICKLES at both the core and site levels should
* extend this class. It handles the getting of common module variables
* (auth, data and view) as well as making sure that every module has a
* database object available.
* This is a parent class that all PICKLES modules should be extending.
* Each module can specify it's own meta data and whether or not a user
* must be properly authenticated to view the page. Currently any pages
* without a template are treated as pages being requested via AJAX and the
* return will be JSON encoded. In the future this may need to be changed
* out for logic that allows the requested module to specify what display
* type(s) it can use.
*/
class Module extends Object {
class Module extends Object
{
/**
* Page title
*
* @var string
*/
public $title;
/**
* Array of public variables to be available by the display
* Meta description
*
* @var string
*/
protected $public = array();
public $description;
/**
* Passed objects
* Meta keywords (comma separated)
*
* @var string
*/
protected $config = null;
protected $db = null;
protected $mailer = null;
protected $error = null;
public $keywords;
/**
* Template file for the module
* Access level of the page
*
* Defaults to false which is everybody, even anonymous
*
* @access protected
* @var boolean
*/
protected $template = null;
protected $name = null;
/**
* Module defaults
*/
protected $authentication = false;
protected $caching = false;
protected $display = false;
protected $session = false;
protected $access = false;
private $smarty;
private $cache_id;
/**
* Secure
*
* Whether or not the page should be loaded via SSL. Not currently
* being used. Defaults to false, non-SSL.
*
* @access protected
* @var boolean
*/
protected $secure = false;
/**
* AJAX
*
* Whether or not the page must be loaded via AJAX and if so, what
* pages are allowed to access it and the request method.
*
* @access protected
* @var array
*/
protected $ajax = false;
/**
* Default template
*
* Defaults to index.tpl but could be set to any valid template.
*
* @var string
*/
public $template = 'index';
/**
* Constructor
*
* Handles calling the parent constructor and sets up the module's
* internal config and database object
* The constructor does nothing by default but can be passed a boolean
* variable to tell it to automatically run the __default() method.
* This is typically used when a module is called outside of the scope
* of the controller (the registration page calls the login page in
* this manner.
*
* @param object $config Instance of the Config class
* @param object $db Instance of the DB class
* @param object $mailer Instance of the Mailer class
* @param boolean $autorun optional flag to autorun __default()
*/
public function __construct(Config $config, DB $db, Mailer $mailer, Error $error) {
public function __construct($autorun = false)
{
parent::__construct();
$this->config = $config;
$this->db = $db;
$this->mailer = $mailer;
$this->error = $error;
}
/**
* Gets the authentication value
*
* Order of precedence:
* Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not user authentication is required
*/
public function getAuthentication() {
if ($this->authentication != null) {
return $this->authentication;
}
else if (is_bool($this->config->getAuthentication())) {
return $this->config->getAuthentication();
}
return false;
}
/**
* Gets the caching value
*
* Order of precedence:
* POSTed, Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not user authentication is required
*/
public function getCaching() {
/*
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
return false;
}
*/
if ($this->caching != null) {
return $this->caching;
}
else if ($this->config->getCaching()) {
return $this->config->getCaching();
}
return false;
}
/**
* Gets the session value
*
* Order of precedence:
* Auth On, Module, Config, Guess (guess is always false)
*
* @return boolean Whether or not the session needs to be started
*/
public function getSession() {
if ($this->authentication === true) {
return true;
}
else if ($this->session != null) {
return $this->session;
}
else if (is_bool($this->config->getSession())) {
return $this->config->getSession();
}
return false;
}
/**
* Gets the requested Display
*
* Order of precedence:
* Module, Config, Guess (guess is always Smarty)
*
* @return string The display that the module has requested to be used
*/
public function getDisplay() {
// Checks if the module has a display tyoe
if (isset($this->display)) {
// Checks if multiple display types are supported
if (is_array($this->display)) {
$display = $this->display[0];
}
else {
$display = $this->display;
}
if (in_array($display, array(DISPLAY_JSON, DISPLAY_PHP, DISPLAY_RSS, DISPLAY_SMARTY))) {
return $display;
}
}
// Checks for a display type in the config
if (isset($this->config->modules->display)) {
return (string)$this->config->modules->display;
}
else {
$this->error->addWarning('Invalid display specified, DISPLAY_PHP used by default (' . $this->display . ')');
return DISPLAY_PHP;
if ($autorun === true)
{
$this->__default();
}
}
/**
* Alias for $module->data
* Default "Magic" Method
*
* @return array Associative array of data that was set by the module
* This function is overloaded by the module. The __default() method is
* where you want to place any code that needs to be executed at
* runtime. The reason the code isn't in the constructor is because the
* module must be instantiated before the code is executed so that the
* controller script is aware of the authentication requirements.
*/
public function getData() {
if (isset($this->data)) {
return $this->data;
}
public function __default()
{
return null;
}
public function setPublic($variable, $value) {
$this->public[$variable] = $value;
return true;
}
public function getPublic($variable) {
if (isset($this->public[$variable])) {
return $this->public[$variable];
}
else {
return null;
}
}
public function setSmartyObject(Smarty $smarty) {
$this->smarty = $smarty;
}
public function isCached($id = null) {
if ($id == null) {
$id = get_class($this);
}
switch ($this->getDisplay()) {
case DISPLAY_PHP:
break;
case DISPLAY_SMARTY:
if (is_object($this->smarty)) {
if ($this->smarty->template_exists('index.tpl')) {
return $this->smarty->is_cached('index.tpl', $id);
}
else {
return $this->smarty->is_cached($template, $id);
}
}
break;
}
return false;
}
public function setCacheID($id) {
$this->cache_id = $id;
}
public function getCacheID() {
return $this->cache_id;
}
/**
* Default function
*
* This function is overloaded by the module. The __default() function
* is where any code that needs to be executed at run time needs to be
* placed. It's not in the constructor as the module needs to be
* instantiated first so that the authorization requirements can be
* checked without running code it's potentially not supposed to have
* been executed.
*/
public function __default() {
}
}