Default to JSON if there are no PHP templates

This commit is contained in:
Josh Sherman 2010-11-14 16:47:06 -05:00
parent d157b9ad52
commit eccb07e786
4 changed files with 106 additions and 79 deletions

View file

@ -171,7 +171,7 @@ class Controller extends Object
if (isset($return_type))
{
if (in_array(strtolower($return_type), array('json', 'rss', 'xml')))
if (in_array(strtolower($return_type), array('json', 'xml'))) // @todo add back rss and possibly atom
{
$engine = strtoupper($return_type);
}
@ -189,7 +189,7 @@ class Controller extends Object
// Checks the templates
$template_exists = $display->templateExists();
// If there's no valid module or template redirect
// If there is no valid module or template, then redirect
if (!$module_exists && !$template_exists)
{
if (!isset($_REQUEST['request']))

View file

@ -185,6 +185,33 @@ abstract class Display_Common extends Object
{
return true;
}
/**
* JSON Encode
*
* Encodes module return data as JSON.
*
* Requires PHP 5 >= 5.2.0 or PECL json >= 1.2.0
* Note: PECL json 1.2.1 is included /vendors
*
* @link http://json.org/
* @link http://us.php.net/json_encode
* @link http://pecl.php.net/package/json
*
* @return JSON encoded string
* @todo This really should be moved into some sort of converter class. I couldn't settle on a name, so it will live here for now.
*/
public function jsonEncode()
{
if (JSON_AVAILABLE)
{
echo json_encode($this->module_return);
}
else
{
echo '{ "status": "error", "message": "json_encode() not found" }';
}
}
/**
* Rendering Method

View file

@ -34,14 +34,7 @@ class Display_JSON extends Display_Common
*/
public function render()
{
if (JSON_AVAILABLE)
{
echo json_encode($this->module_return);
}
else
{
echo '{ "status": "error", "message": "json_encode() not found" }';
}
return $this->jsonEncode();
}
}

View file

@ -57,89 +57,96 @@ class Display_PHP extends Display_Common
*/
public function render()
{
// Starts up the buffer
ob_start();
// Puts the class variables in local scope of the template
$__config = $this->config;
$__meta = $this->meta_data;
$__module = $this->module_return;
$__css_class = $this->css_class;
$__js_file = $this->js_basename;
// Creates (possibly overwritten) objects
$form_class = (class_exists('CustomForm') ? 'CustomForm' : 'Form');
$dynamic_class = (class_exists('CustomDynamic') ? 'CustomDynamic' : 'Dynamic');
$__form = new $form_class();
$__dynamic = new $dynamic_class();
// Loads the template
if ($this->parent_template != null)
if ($this->templateExists())
{
if ($this->child_template == null)
// Starts up the buffer
ob_start();
// Puts the class variables in local scope of the template
$__config = $this->config;
$__meta = $this->meta_data;
$__module = $this->module_return;
$__css_class = $this->css_class;
$__js_file = $this->js_basename;
// Creates (possibly overwritten) objects
$form_class = (class_exists('CustomForm') ? 'CustomForm' : 'Form');
$dynamic_class = (class_exists('CustomDynamic') ? 'CustomDynamic' : 'Dynamic');
$__form = new $form_class();
$__dynamic = new $dynamic_class();
// Loads the template
if ($this->parent_template != null)
{
$__template = $this->parent_template;
if ($this->child_template == null)
{
$__template = $this->parent_template;
}
else
{
$__template = $this->child_template;
}
require_once $this->parent_template;
}
else
elseif ($this->child_template != null)
{
$__template = $this->child_template;
require_once $__template;
}
require_once $this->parent_template;
}
elseif ($this->child_template != null)
{
$__template = $this->child_template;
// Grabs the buffer contents and clears it out
$buffer = ob_get_clean();
require_once $__template;
}
/*
@todo this wasn't working as well as I'd like:
// Grabs the buffer contents and clears it out
$buffer = ob_get_clean();
$regex = array(
'PRE' => '!<pre>.+?</pre>!is',
'SCRIPT' => '!<script[^>]+>.+?</script>!is',
'TEXTAREA' => '!<textarea[^>]+>.+?</textarea>!is',
);
/*
@todo this wasn't working as well as I'd like:
$search_replace = array();
$regex = array(
'PRE' => '!<pre>.+?</pre>!is',
'SCRIPT' => '!<script[^>]+>.+?</script>!is',
'TEXTAREA' => '!<textarea[^>]+>.+?</textarea>!is',
);
$search_replace = array();
// Cleans up the stuff we don't want to minify
foreach ($regex as $type => $pattern)
{
$hash = '@@@PICKLES::' . $type . '@@@';
preg_match_all($pattern, $buffer, $matches);
$search_replace[$hash] = $matches[0];
$buffer = preg_replace($pattern, $hash, $buffer);
}
// Strips the whitespace
$buffer = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $buffer));
// Kills any HTML comments
$buffer = preg_replace('/<!--.*-->/U', '', $buffer);
// Injects the stuff we stripped earlier
foreach ($search_replace as $search => $replacements)
{
foreach ($replacements as $replacement)
// Cleans up the stuff we don't want to minify
foreach ($regex as $type => $pattern)
{
$buffer = preg_replace('/'.$search.'/', $replacement, $buffer, 1);
$hash = '@@@PICKLES::' . $type . '@@@';
preg_match_all($pattern, $buffer, $matches);
$search_replace[$hash] = $matches[0];
$buffer = preg_replace($pattern, $hash, $buffer);
}
// Strips the whitespace
$buffer = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $buffer));
// Kills any HTML comments
$buffer = preg_replace('/<!--.*-->/U', '', $buffer);
// Injects the stuff we stripped earlier
foreach ($search_replace as $search => $replacements)
{
foreach ($replacements as $replacement)
{
$buffer = preg_replace('/'.$search.'/', $replacement, $buffer, 1);
}
}
*/
// Kills any whitespace and HTML comments
$buffer = preg_replace(array('/^[\s]+/m', '/<!--.*-->/U'), '', $buffer);
// Note, this doesn't exit in case you want to run code after the display of the page
echo $buffer;
}
else
{
$this->jsonEncode();
}
*/
// Kills any whitespace and HTML comments
$buffer = preg_replace(array('/^[\s]+/m', '/<!--.*-->/U'), '', $buffer);
// Note, this doesn't exit in case you want to run code after the display of the page
echo $buffer;
}
}