Set up return structure.

This commit is contained in:
Josh Sherman 2014-09-24 22:28:34 -04:00
parent 1718bc27a6
commit eba206556c
3 changed files with 77 additions and 129 deletions

View file

@ -317,11 +317,11 @@ class Controller extends Object
if (!is_array($module_return)) if (!is_array($module_return))
{ {
$module_return = $module->return; $module_return = $module->response;
} }
else else
{ {
$module_return = array_merge($module_return, $module->return); $module_return = array_merge($module_return, $module->response);
} }
} }
@ -353,27 +353,7 @@ class Controller extends Object
} }
} }
if (!isset($module_return)) $display = new Display($module);
{
$module_return = [
'status' => 'error',
'message' => $error_message,
];
}
// @todo Should simplify this, give Display direct acess to
// $module instead of all these variable assignment
$display = new Display();
$display->output = $module->output;
$display->templates = $module->template;
$display->module = $module_return;
// @todo Check for $module->meta variable first, then remove entirely when sites are updated
$display->meta = [
'title' => $module->title,
'description' => $module->description,
'keywords' => $module->keywords
];
} }
// Starts a timer for the display rendering // Starts a timer for the display rendering

View file

@ -23,55 +23,20 @@
class Display extends Object class Display extends Object
{ {
/** /**
* Return Type * Module
* *
* This class supports loading a PHP template, displaying JSON, XML and an * This is the module we are attempting to display output for.
* RSS flavored XML. Inside your modules you can specify either a string or
* array. Possible values include "template", "json", "xml" and "rss".
* Default behavior is to try to load a template and fallback to displaying
* JSON. The "template" option always takes precedence when used with the
* other types.
*
* @var mixed string or array to determine how to return
*/
public $return = ['template', 'json'];
/**
* Templates
*
* Templates are found in the ./templates directory of your site. The
* template workflow is to load ./templates/__shared/index.phtml and you
* would set that template up to require $this->template, the path and
* filename for the module template (named based on the structure of the
* requested URI. Inside your module you can specify the basename of the
* parent template you would like to use or false to not use a parent
* template.
*
* @var string or boolean false the basename of the parent template
*/
public $templates = false;
/**
* Meta Data
*
* An array of meta data that you want exposed to the template. Currently
* you set the meta data from inside your module using the class variables
* title, description and keywords. The newer [preferred] method is to
* set an array in your module using the meta variable using title,
* description and keywords as the keys. You can also specify any other
* meta keys in the array that you would like to be exposed to your
* templates. The meta data is only used by TEMPLATE and RSS return types.
*/
public $meta = [];
/**
* Module Data
*
* Any data the module returns or is assigned inside of the module will
* be available here and exposed to the template.
*/ */
public $module = null; public $module = null;
public function __construct($module = null)
{
if ($module && $module instanceof Module)
{
$this->module = $module;
}
}
public function render() public function render()
{ {
try try
@ -79,21 +44,23 @@ class Display extends Object
// Starts up the buffer so we can capture it // Starts up the buffer so we can capture it
ob_start(); ob_start();
if (!is_array($this->return)) if (!is_array($this->module->response))
{ {
$this->return = [$this->return]; $this->module->response = [$this->module->response];
} }
$return_json = $return_rss = $return_template = $return_xml = false; $return_json = false;
$return_template = false;
$return_xml = false;
foreach ($this->return as $return) foreach ($this->module->output as $return)
{ {
$variable = 'return_' . $return; $variable = 'return_' . $return;
$$variable = true; $$variable = true;
} }
// Makes sure the return type is valid // Makes sure the return type is valid
if (!$return_json && !$return_rss && !$return_template && !$return_xml) if (!$return_json && !$return_template && !$return_xml)
{ {
throw new Exception('Invalid return type.'); throw new Exception('Invalid return type.');
} }
@ -118,8 +85,6 @@ class Display extends Object
throw new Exception('Requested URI contains PHPSESSID, redirecting.'); throw new Exception('Requested URI contains PHPSESSID, redirecting.');
} }
// @todo Derrive CSS and JS from _REQUEST['request'] no need to pass around
$loaded = false; $loaded = false;
if ($return_template) if ($return_template)
@ -131,22 +96,14 @@ class Display extends Object
// Exposes some objects and variables to the local scope of the template // Exposes some objects and variables to the local scope of the template
$this->request = $this->js_file = $_REQUEST['request']; $this->request = $this->js_file = $_REQUEST['request'];
// @todo replace _ with - as it's more appropriate for CSS naming $this->css_class = strtr($this->request, '/', '-');
$this->css_class = strtr($this->request, '/', '_');
// @todo Remove the magic $__variable when all sites are ported $this->dynamic = new $dynamic_class();
$__config = $this->config; $this->form = new $form_class();
$__css_class = $this->css_class; $this->html = new $html_class();
$__js_file = $this->js_file;
$__meta = $this->meta;
$__module = $this->module;
$__dynamic = $this->dynamic = new $dynamic_class();
$__form = $this->form = new $form_class();
$__html = $this->html = new $html_class();
// Checks for the parent template and tries to load it // Checks for the parent template and tries to load it
if ($this->templates) if ($this->module->template)
{ {
$profiler = $this->config->pickles['profiler']; $profiler = $this->config->pickles['profiler'];
$profiler = $profiler === true || stripos($profiler, 'timers') !== false; $profiler = $profiler === true || stripos($profiler, 'timers') !== false;
@ -158,9 +115,9 @@ class Display extends Object
} }
// Assigns old variable // Assigns old variable
$required_template = $this->templates[0]; $required_template = $this->module->templates[0];
$__template = $this->template = end($this->templates); $this->module->template = end($this->module->templates);
$loaded = require_once $required_template; $loaded = require_once $required_template;
// Stops the template loading timer // Stops the template loading timer
if ($profiler) if ($profiler)
@ -170,16 +127,26 @@ class Display extends Object
} }
} }
$meta = [
'status' => $this->module->status,
'message' => $this->module->message,
];
$response = [
'meta' => $meta,
'response' => $this->module->response,
];
if (!$loaded) if (!$loaded)
{ {
if ($return_json) if ($return_json)
{ {
$pretty = isset($_REQUEST['pretty']) ? JSON_PRETTY_PRINT : false; $pretty = isset($_REQUEST['pretty']) ? JSON_PRETTY_PRINT : false;
echo json_encode($this->module, $pretty); echo json_encode($response, $pretty);
} }
elseif ($return_xml) elseif ($return_xml)
{ {
echo Convert::arrayToXML($this->module, isset($_REQUEST['pretty'])); echo Convert::arrayToXML($response, isset($_REQUEST['pretty']));
} }
} }
@ -189,7 +156,8 @@ class Display extends Object
// Kills any whitespace and HTML comments in templates // Kills any whitespace and HTML comments in templates
if ($loaded) if ($loaded)
{ {
// The BSA exception is because their system sucks and demands there be comments present // The BSA exception is because their system sucks and demands
// there be comments present
$buffer = preg_replace(['/^[\s]+/m', '/<!--(?:(?!BuySellAds).)+-->/U'], '', $buffer); $buffer = preg_replace(['/^[\s]+/m', '/<!--(?:(?!BuySellAds).)+-->/U'], '', $buffer);
} }

View file

@ -78,20 +78,6 @@ class Module extends Object
*/ */
public $security = null; public $security = null;
/**
* AJAX
*
* Whether or not the module is being called via AJAX. This determines if
* errors should be returned as JSON or if it should use the Error class
* which can be interrogated from within a template.
*
* @var boolean, false (not AJAX) by default
* @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.
*/
public $ajax = false;
/** /**
* Method * Method
* *
@ -122,18 +108,15 @@ class Module extends Object
public $template = 'index'; public $template = 'index';
/** /**
* Return * Response
* *
* Array that is returned to the template in the case of the module not * Array of data that will be rendered as part of the display. This is
* returning anything itself. This is somewhat of a one way trip as you * somewhat of a one way trip as you cannot get the variable unless you
* cannot get the variable unless you reference the return array explicitly * reference the response array explicitly, $this->response['variable']
* $this->return['variable']
* *
* @var array * @var array
* @todo Rename __return so it's kinda obscured
* @todo Will need to update leaderbin and sndcrd to use new variable
*/ */
public $return = []; public $response = [];
/** /**
* Output * Output
@ -146,6 +129,16 @@ class Module extends Object
*/ */
public $output = ['template', 'json']; public $output = ['template', 'json'];
// @todo
public $status = 200;
public $message = 'OK';
public $echo = false;
public $limit = false;
public $offset = false;
public $errors = [];
// @todo if $status != 200 && $message == 'OK' ...
/** /**
* Constructor * Constructor
* *
@ -155,19 +148,26 @@ class Module extends Object
* controller (the registration page calls the login page in this manner. * controller (the registration page calls the login page in this manner.
* *
* @param boolean $autorun optional flag to autorun __default() * @param boolean $autorun optional flag to autorun __default()
@ @param boolean $filter optional flag to disable autorun filtering
* @param boolean $valiate optional flag to disable autorun validation * @param boolean $valiate optional flag to disable autorun validation
*/ */
public function __construct($autorun = false, $validate = true) public function __construct($autorun = false, $filter = true, $validate = true)
{ {
parent::__construct(['cache', 'db']); parent::__construct(['cache', 'db']);
if ($autorun === true) if ($autorun)
{ {
if ($validate === true) if ($filter)
{
// @todo
//$this->__filter();
}
if ($validate)
{ {
$errors = $this->__validate(); $errors = $this->__validate();
if ($errors !== false) if (!$errors)
{ {
// @todo Fatal error perhaps? // @todo Fatal error perhaps?
exit('Errors encountered, this is a @todo for form validation when calling modules from inside of modules'); exit('Errors encountered, this is a @todo for form validation when calling modules from inside of modules');
@ -192,35 +192,35 @@ class Module extends Object
/** /**
* Magic Setter Method * Magic Setter Method
* *
* Places undefined properties into the return array as part of the * Places undefined properties into the response array as part of the
* module's payload. * module's payload.
* *
* @param string $name name of the variable to be set * @param string $variable name of the variable to be set
* @param mixed $value value of the variable to be set * @param mixed $value value of the variable to be set
*/ */
public function __set($name, $value) public function __set($variable, $value)
{ {
$this->return[$name] = $value; $this->response[$variable] = $value;
} }
/** /**
* Magic Getter Method * Magic Getter Method
* *
* Any variables not defined in this class are set in the return array and * Any variables not defined in this class are set in the response array
* default to false if not defined there. * and default to false if not defined there.
* *
* @param string $name name of the variable requested * @param string $name name of the variable requested
* @return mixed value of the variable or boolean false * @return mixed value of the variable or boolean false
*/ */
public function __get($name) public function __get($name)
{ {
if (!isset($this->return[$name])) if (!isset($this->response[$name]))
{ {
return false; return false;
} }
else else
{ {
return $this->return[$name]; return $this->response[$name];
} }
} }