From ad49db7a1e9c781ed427c2e0e28e2996fec023ee Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 15 Jan 2005 10:31:06 +0000 Subject: [PATCH] Added debug information notices. Created iff() wrapper. Template system done. --- kernel.php | 36 +++- template.php | 440 ++++++++++++++++++++++++++++++++++++++++++++++++ template_fs.php | 105 ++++++++++++ 3 files changed, 580 insertions(+), 1 deletion(-) create mode 100644 template.php create mode 100644 template_fs.php diff --git a/kernel.php b/kernel.php index 17c29c2..84a1cee 100644 --- a/kernel.php +++ b/kernel.php @@ -37,12 +37,16 @@ class Shared_Object_Framework * @var sourcepath The location of the framework sources * @var appath The path to the application's location * @var application The name of the application that is using the framework + * @var debug Variable for debug mode + * @var debuginfo Listing of all debug notices * @var modules An array of loaded framework modules */ var $version = '[#]version[#]'; var $sourcepath = ''; var $apppath = ''; var $application = ''; + var $debug = false; + var $debuginfo = array(); var $modules = array(); /** @@ -212,7 +216,7 @@ class Shared_Object_Framework // Error case ERR_ERROR: - $title = 'Error'; + $title = 'Alert'; break; // Warning @@ -226,10 +230,40 @@ class Shared_Object_Framework $this->_message($title, $errstr, 3); } + + /** + * Logs a debug message for verbose output + * + * @param str Message + */ + function debug($message) + { + if ($this->debug) + { + $this->debuginfo[] = $message; + } + } } +/** +* Global callback used for module calls back to the kernel +*/ $_isso = new Shared_Object_Framework(); +/** +* Wrapper for ternary operator that has to be in the global scope +* +* @param expr Expression +* @param mixed If the expression is true +* @param mixed If the expression is false +* +* @return mixed True or false data +*/ +function iff($condition, $iftrue, $iffalse = null) +{ + return ($condition) ? ($iftrue) : ($iffalse); +} + /*=====================================================================*\ || ################################################################### || # $HeadURL$ diff --git a/template.php b/template.php new file mode 100644 index 0000000..c3307cd --- /dev/null +++ b/template.php @@ -0,0 +1,440 @@ +cache) > 0) + { + trigger_error('You cannot cache templates more than once per initialization', ERR_WARNING); + } + else + { + $templates = $_isso->db->query("SELECT * FROM " . $this->tablename . " WHERE " . $this->namecolumn . " IN ('" . implode("', '", $namearray) . "')" . iff($this->extrawhere, $this->extrawhere)); + while ($template = $_isso->db->fetch_array($templates)) + { + $template = $this->_parse($template); + $this->cache[ $template[ $this->namecolumn ] ] = $template[ $this->datacolumn ]; + $this->usage["$name"] = 0; + } + } + } + + /** + * Loads a template from the cache or the _load function and + * stores the parsed version of it + * + * @param str The name of the template + * + * @return str A parsed and loaded template + */ + function fetch($name) + { + global $_isso; + + if (isset($this->cache["$name"])) + { + $template = $this->cache["$name"]; + } + else + { + $this->uncached[] = $name; + $_isso->debug("Manually loading template `$name`"); + $template = $this->_load($name); + $template = $this->_parse($template); + } + + if (!isset($this->usage["$name"])) + { + $this->usage["$name"] = 0; + } + + $this->usage["$name"]++; + + return $template; + } + + /** + * Output a template fully compiled to the browser + * + * @param str Compiled and ready template + */ + function flush($template) + { + global $_isso; + + ob_start(); + + if (empty($template)) + { + trigger_error('There was no output to print', ERR_FATAL); + exit; + } + + if ($_isso->debug AND isset($_GET['query'])) + { + if (is_array($_isso->db->history)) + { + echo '
';
+				foreach ($_isso->db->history AS $query)
+				{
+					echo $query . "\n\n
\n\n"; + } + echo '
'; + } + exit; + } + + if ($this->doneflush) + { + trigger_error('A template has already been sent to the output buffer', ERR_FATAL); + exit; + } + + if ($_isso->debug) + { + $optlist = array(); + foreach ($this->usage AS $name => $count) + { + if (isset($this->uncached["$name"])) + { + $optlist[] = $name . '[' . $count . ']'; + } + } + + // --- START + $debug = "\n"; + + $debug = "\n
\n" . $_isso->_message('Debug Information', $debug, 1, true); + $template = str_replace('', "\n\n$debug\n\n\n", $template); + } + + print(stripslashes($template)); + } + + /** + * Loads an additional template from the database + * + * @param str The name of the template + * + * @return str Template data from the database + */ + function _load($name) + { + global $_isso; + if ($template = $_isso->db->query("SELECT * FROM " . $this->tablename . " WHERE " . $this->namecolumn . " = '$name'" . iff($this->extrawhere, $this->extrawhere))) + { + return $template[ $this->datacolumn ]; + } + else + { + trigger_error("The template '$name' could not be loaded", ERR_FATAL); + exit; + } + } + + /** + * A wrapper for all the parsing functions and compiling functins + * + * @param str Unparsed template data + * + * @return str Parsed template data + */ + function _parse($template) + { + $template = stripslashes($template); + $template = str_replace('"', '\"', $template); + $template = $this->_parse_phrases($template); + $template = $this->_parse_conditionals($template); + return $template; + } + + /** + * Prepares language and locale information inside templates + * + * @param str Template data to be processed + * + * @return str Language-ready template data + */ + function _parse_phrases($template) + { + $tag_start = 'langcall . '(\'$1\') . "', $template); + + while (1) + { + // Find the start language object tag + $location_start = strpos($template, $tag_start, $location_end + 1); + if ($location_start === false) + { + break; + } + + // Find the end tag + $location_end = strpos($template, $tag_end, $location_end + strlen($tag_end)); + if ($location_end === false) + { + break; + } + + // Extract the language object + $phrase_bunch = substr($template, $location_start, ($location_end + strlen($tag_end)) - $location_start); + + // Find the close to the opening + $close_of_open = strpos($phrase_bunch, $tag_start_end); + if ($close_of_open === false) + { + break; + } + + // Extract the opening tag so it can be parsed + $init_tag = substr($phrase_bunch, 0, ($close_of_open + strlen($tag_start_end))); + $init_tag = str_replace($tag_start, '', $init_tag); + $init_tag = substr($init_tag, 0, strlen($init_tag) - 1); + + // Get the args out of the tag + $args = preg_split('#([0-9].*?)=#', $init_tag); + foreach ($args AS $arg) + { + if ($arg AND $arg != ' ') + { + $arg = trim($arg); + $arg = substr($arg, 2); + $arg = substr($arg, 0, strlen($arg) - 2); + $arglist[] = $arg; + } + } + + // Just get the phrase name + $phrase_name = preg_replace('#(.*?)#i', '$2', $phrase_bunch); + + // Wrap the parsed data into the build function + $function_wrap = '" . ' . $this->langconst . '(\'' . $phrase_name . '\', "' . implode('", "', $arglist) . '") . "'; + + // Replace the fully-parsed string back into the template + $template = substr_replace($template, $function_wrap, $location_start, $location_end + strlen($tag_end) - $location_start); + + unset($arglist); + } + + return $template; + } + + /** + * Parser for in-line template conditionals + * + * @param str Template data awaiting processing + * + * @return str Parsed template data + */ + function _parse_conditionals($template) + { + // Tag locations + $location_start = -1; + $location_end = -1; + + // Tag data + $tag_start = ' 1) + { + $location_start = $location_start + 1; + $location_end = $location_start + 2; + } + + // Strip out the opening ' 2) + { + trigger_error('Multiple else statements encountered while parsing conditionals in template', ERR_ERROR); + } + + // Set the data + $iftrue = $data[0]; + $iffalse = $data[1]; + } + // Nope, reassign variables + else + { + $iftrue = $parsed; + $iffalse = null; + } + + // Put the condition and iftrue in the iff() function + $parsed_expression = '" . iff(' . stripslashes($expression_condition) . ',"' . $iftrue . '","' . $iffalse . '") . "'; + + // Parse the iff()'d expression back into the template data + $template = substr_replace($template, $parsed_expression, $location_start, $location_end + strlen($tag_end) - $location_start); + } + + // Repeat this process until it can't find any more + // expressions, then send back the parsed template data + // for final output + return $template; + } +} + +/*=====================================================================*\ +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file diff --git a/template_fs.php b/template_fs.php new file mode 100644 index 0000000..541b1b0 --- /dev/null +++ b/template_fs.php @@ -0,0 +1,105 @@ +is_loaded('template')) +{ + $this->locate('template'); +} + +$OBJECT = 'File-Based Template System'; +$CLASS = 'FS_Template'; +$OBJ = 'template'; + +/** +* File System Template System +* +* This framework merely replaces the template loading functions with +* file-system based ones. +* +* @author Iris Studios, Inc. +* @copyright Copyright ©2003 - [#]year[#], Iris Studios, Inc. +* @version $Revision$ +* +*/ +class FS_Template extends DB_Template +{ + /** + * Global environment variables + * + * @var templatedir Directory name that houses the templates + * @var extension The extension used on template files + */ + var $templatedir = ''; + var $extension = 'tpl'; + + /** + * Takes an array of template names, loads them, and then stores + * a parsed version for optimum speed. + * + * @var array List of template names to be cached + */ + function cache($namearray) + { + if (sizeof($this->cache) > 0) + { + trigger_error('You cannot cache templates more than once per initialization', ERR_WARNING); + } + else + { + foreach ($namearray AS $name) + { + $template = $this->_load($name); + $template = $this->_parse($template); + $this->cache["$name"] = $template; + $this->usage["$name"] = 0; + } + } + } + + /** + * Loads a template from the file system from the specified $templatedir + * with the file extension $extension + * + * @var name The name of the template call + */ + function _load($name) + { + global $_isso; + + $path = $_isso->apppath . $this->templatedir . $name . '.' . $this->extension; + if (is_file($path)) + { + if (($template = @file_get_contents($path)) !== false) + { + return $template; + } + else + { + trigger_error("Could not load the template '$path'", ERR_FATAL); + exit; + } + } + else + { + trigger_error("Could not load the template '$path'", ERR_FATAL); + exit; + } + } +} + +/*=====================================================================*\ +|| ################################################################### +|| # $HeadURL$ +|| # $Id$ +|| ################################################################### +\*=====================================================================*/ +?> \ No newline at end of file -- 2.43.5