Refactored a bunch of the display logic, added handling to avoid the infinite redirect loop in the controller.

This commit is contained in:
Josh Sherman 2010-09-19 19:13:11 -04:00
parent 4307594ab5
commit 6bd87d270d
5 changed files with 171 additions and 110 deletions

View file

@ -71,33 +71,19 @@ class Controller extends Object
$request[key($request)] = $last_part;
}
unset($last_part);
list($basename, $module_class, $module_filename, $template_basename, $css_class, $js_basename) = $this->prepareVariables(implode('/', $request));
$request = implode('/', $request);
$basename = strtr($request, '-', '_');
$module_class = strtr($basename, '/', '_');
$module_filename = SITE_MODULE_PATH . $basename . '.php';
$template_basename = $basename;
$css_class = str_replace(array('_', '/', ' '), '-', $basename);
$js_basename = $basename;
unset($request);
unset($last_part, $request);
}
// Loads the default module information (if any)
else
{
$basename = $this->config->module['default'];
$module_class = strtr($basename, '/', '_');
$module_filename = SITE_MODULE_PATH . $basename . '.php';
$template_basename = $basename;
$css_class = str_replace(array('_', '/', ' '), '-', $basename);
$js_basename = $basename;
list($basename, $module_class, $module_filename, $template_basename, $css_class, $js_basename) = $this->prepareVariables($this->config->module['default']);
}
unset($basename);
$module_exists = (isset($module_filename) && $module_filename != null && file_exists($module_filename));
$module_exists = (isset($module_filename) && $module_filename != null && file_exists($module_filename));
// Instantiates an instance of the module
if ($module_exists)
@ -135,6 +121,9 @@ class Controller extends Object
}
// Validates the rendering engine
// @todo Need to validate against the module's return type(s)
$engine = $module->engine;
if (isset($return_type))
{
if (in_array(strtolower($return_type), array('json', 'rss', 'xml')))
@ -145,21 +134,35 @@ class Controller extends Object
unset($return_type);
}
// Defaults the rendering engine
if (!isset($engine))
{
$engine = $module->engine;
}
// Starts up the display engine
$display_class = 'Display_' . $engine;
$display = new $display_class($module->template, $template_basename);
$display = new $display_class();
// Assigns the template / template variables
$display->setTemplateVariables($module->template, $template_basename, $css_class, $js_basename);
// Checks the templates
$template_exists = $display->templateExists();
// If there's no valid module or template redirect
// @todo The == 1 portion needs to be refactored as it's not mandatory to use a parent template
if (!$module_exists && (!$display->templateExists() || $display->templateExists() == 1))
if (!$module_exists && !$template_exists)
{
header('Location: /', 404);
if (!isset($_REQUEST['request']))
{
Error::fatal('Way to go, you\'ve successfully created an infinite redirect loop. Good thing I was here or you would have been served with a pretty ugly browser error.<br /><br />So here\'s the deal, no templates were able to be loaded. Make sure your parent and child templates actually exist and if you\'re using non-default values, make sure they\'re defined correctly in your config.');
}
else
{
$redirect_url = '/';
if (isset($this->config->site['404']) && $_REQUEST['request'] != $this->config->site['404'])
{
$redirect_url .= $this->config->site['404'];
}
header('Location: ' . $redirect, 404);
exit;
}
}
$module_return = null;
@ -177,14 +180,25 @@ class Controller extends Object
* module know to use the cache, either passing in a variable
* or setting it on the object
*/
$module_return = $module->__default();
}
$display->setModuleReturn($module->__default());
$display->prepare($css_class, $js_basename, $module_return);
}
// Renders the content
$display->render();
}
function prepareVariables($request)
{
$basename = strtr($request, '-', '_');
$module_class = strtr($basename, '/', '_');
$module_filename = SITE_MODULE_PATH . $basename . '.php';
$template_basename = $basename;
$css_class = str_replace(array('_', '/', ' '), '-', $basename);
$js_basename = $basename;
return array($basename, $module_class, $module_filename, $template_basename, $css_class, $js_basename);
}
}
?>

View file

@ -23,21 +23,29 @@
*/
abstract class Display_Common extends Object
{
/**
* Templates
*
* @access protected
* @var string
*/
protected $templates = null;
/**
* Template Extension
*
* @access protected
* @var string $extension file extension for the template files
*/
protected $extension = null;
/**
* Parent Template
*
* @access protected
* @var string
*/
protected $extension = false;
protected $parent_template = null;
/**
* Child (sub) Template
*
* @access protected
* @var string
*/
protected $child_template = null;
/**
* CSS Class Name
@ -66,7 +74,7 @@ abstract class Display_Common extends Object
/**
* Constructor
*
* Runs the parent's constructor and adds the module to the object.
* Gets those headers working
*/
public function __construct()
{
@ -75,10 +83,10 @@ abstract class Display_Common extends Object
// Obliterates any passed in PHPSESSID (thanks Google)
if (stripos($_SERVER['REQUEST_URI'], '?PHPSESSID=') !== false)
{
list($request_uri, $phpsessid) = split('\?PHPSESSID=', $_SERVER['REQUEST_URI'], 2);
list($request_uri, $phpsessid) = explode('?PHPSESSID=', $_SERVER['REQUEST_URI'], 2);
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $request_uri);
exit();
exit;
}
else
{
@ -87,42 +95,69 @@ abstract class Display_Common extends Object
ini_set('url_rewriter.tags', 'a=href,area=href,frame=src,input=src,fieldset=');
header('Content-type: text/html; charset=UTF-8');
// Loops through each passed template and variables it
foreach (func_get_args() as $template)
{
$template = SITE_TEMPLATE_PATH . $template . ($this->extension != false ? '.' . $this->extension : '');
if (file_exists($template) && is_file($template) && is_readable($template))
{
$this->templates[] = $template;
}
}
}
}
/**
* Set Template
*
* Sets the template file based on passed template type. Adds path and
* extension if applicable.
*
* @param string $template template file's basename
* @param string $type template file's type (either parent or child)
*/
private function setTemplate($template, $type)
{
if ($template != null)
{
$template_name = $type . '_template';
$this->$template_name = SITE_TEMPLATE_PATH . $template . ($this->extension != false ? '.' . $this->extension : '');
}
}
/**
* Template Exists
* Set Template Variables
*
* @return integer the number of templates defined
* Sets the variables used by the templates
*
* @param string $parent_template parent template
* @param string $child_template child (sub) template
* @param string $css_class name of the CSS class for the module
* @param string $js_basename basename for the javascript file for the module
*/
public function templateExists()
public function setTemplateVariables($parent_template, $child_template, $css_class, $js_basename)
{
return count($this->templates);
$this->setTemplate($parent_template, 'parent');
$this->setTemplate($child_template, 'child');
$this->css_class = $css_class;
$this->js_basename = $js_basename;
}
/**
* Preparation Method
* Set Module Return
*
* @param array $css_class name of the CSS class name for this module
* @param array $js_basename path and basename of the module's JS file
* @param array $module_return data returned by the module
* Sets the return data from the module so the display class can display it
*
* @param array $module_return key / value pairs for the data
*/
public function prepare($css_class, $js_basename, $module_return)
public function setModuleReturn($module_return)
{
$this->css_class = $css_class;
$this->js_basename = $js_basename;
$this->module_return = $module_return;
$this->module_return = $module_return;
}
/**
* Template Exists
*
* Checks the templates for validity, not required by every display type so
* the return defaults to true.
*
* @return boolean whether or not the template exists
*/
public function templateExists()
{
return true;
}
/**

View file

@ -35,44 +35,70 @@ class Display_PHP extends Display_Common
*/
protected $extension = 'phtml';
/**
* Template Exists
*
* @return integer the number of templates defined
*/
public function templateExists()
{
if ($this->parent_template != null)
{
return file_exists($this->parent_template) && file_exists($this->child_template);
}
else
{
return file_exists($this->child_template);
}
}
/**
* Renders the PHP templated pages
*/
public function render()
{
// Assigns the variables and loads the template
if (is_array($this->templates) && isset($this->templates[0]))
// Starts up the buffer
ob_start();
// Puts the class variables in local scope for the template
$config = $this->config;
$module_css_class = $this->css_class;
$module_js_file = $this->js_basename;
$module = $this->module_return;
// Loads the template
if ($this->parent_template != null)
{
// Starts up the buffer
ob_start();
// Puts the class variables in local scope for the template
$config = $this->config;
$module_css_class = $this->css_class;
$module_js_file = $this->js_basename;
$module = $this->module_return;
// Assigns the template variable if there's more than one template
if (isset($this->templates[1]))
if ($this->child_template == null)
{
$template = $this->templates[1];
$template = $this->parent_template;
}
else
{
$template = $this->child_template;
}
// Loads the template
require_once $this->templates[0];
// Grabs the buffer contents and clears it out
$buffer = ob_get_clean();
// Minifies the output
// @todo Need to add logic to not minify blocks of CSS or Javascript
//$buffer = str_replace(array(' ', "\r\n", "\n", "\t"), null, $buffer);
$buffer = str_replace(array(' ', "\t"), null, $buffer);
// Spits out the minified buffer
echo $buffer;
require_once $this->parent_template;
}
elseif ($this->child_template != null)
{
$template = $this->child_template;
require_once $template;
}
// Grabs the buffer contents and clears it out
$buffer = ob_get_clean();
// Minifies the output
// @todo Need to add logic to not minify blocks of CSS or Javascript
//$buffer = str_replace(array(' ', "\r\n", "\n", "\t"), null, $buffer);
$buffer = str_replace(array(' ', "\t"), null, $buffer);
// Spits out the minified buffer
echo $buffer;
// Note, this doesn't exit in case you want to run code after the display of the page
}
}

View file

@ -45,6 +45,7 @@ class Display_XML extends Display_Common
* @access private
* @param array $array array to convert into XML
* @return string generated XML
* @todo Method sucks, should replace
*/
private function arrayToXML($array)
{

View file

@ -206,22 +206,7 @@ class Module extends Object
}
else
{
switch ($name)
{
case 'engine':
$default = DISPLAY_PHP;
break;
case 'template':
$default = 'index';
break;
default:
$default = false;
break;
}
$this->$name = $default;
$this->$name = false;
}
}