query("SELECT * FROM " . self::$dbCacheTable . " WHERE filename IN ('" . implode("', '", $namearray) . "')"); while ($tpl = $cache->fetchArray()) { self::$cache[$tpl['filename']] = $tpl; } } /** * Fluent interface-compatible constructor */ public static function fetch() { $obj = new ReflectionClass(__CLASS__); $args = func_get_args(); return $obj->newInstanceArgs($args); } /** * Constructor * * @param string File name */ public function __construct($name) { $this->name = $name; $this->path = self::_path($name); // checks to see if the template has been cached if (isset(self::$cache[$this->path])) { if (!self::$dbCacheTable || filemtime($this->path) <= self::$cache[$this->path]['timestamp']) { $this->template = self::$cache[$this->path]['template']; return; } } // it hasn't been cached if (!is_file($this->path) || !is_readable($this->path)) { throw new Exception("Could not load the template {$this->path}"); } $this->template = $this->_parseTemplate(file_get_contents($this->path)); self::$cache[$this->path]['template'] = $this->template; // store the template in the database if (self::$dbCacheTable) { BSApp::$db->query("REPLACE INTO " . self::$dbCacheTable . " SET template = '" . BSApp::$db->escapeString($this->template) . "', timestamp = " . TIMENOW . ", filename = '" . $this->path . "'"); self::$cache[$this->path]['time'] = TIMENOW; } } /** * Evaluates and returns the template. This is equivalent to calling: * $tpl->evaluate()->getTemplate() * * @return string */ public function __toString() { return $this->evaluate()->getTemplate(); } /** * Returns the template data * * @return string Final template data */ public function getTemplate() { return $this->template; } /** * This function globalizes/extracts the assigned variables and then * returns the output buffer * * @param string Unevaluated template * * @return fluent interface */ public function evaluate() { extract($this->vars); extract(self::$globalVars); ob_start(); $this->template = str_replace(array('$this->', 'self::'), 'null', $this->template); // don't want internal access coming from a template $this->template = '?>' . $this->template; $test = eval($this->template); $output = ob_get_clean(); if ($output === false) { throw new Exception('A parse error was encountered while evaluating the template'); } $this->template = $output; return $this; } /** * Output a template fully compiled to the browser */ public function flush() { ob_start(); if (empty($this->template)) { throw new Exception('There is no output to print'); } echo $this->template; } /** * Returns the debug block * * @return string */ public static function get_debug_block() { if (!BSApp::get_debug()) { return; } $debugBlock = "\n
Query Debug |
";
$debugBlock .= "\n\t\t\t$query[query]\n\n\t\t\t ($query[time]) \n\n\t\t | \n\t