Deprecated: Assigning the return value of new by reference is deprecated in /home/bluestat/public_html/source/index.php on line 477
ISSO - Commitdiff - ViewGit - Blue Static

BSTemplate now uses the entire file path when caching templates in the database

Robert Sesek [2009-01-06 07:31]
BSTemplate now uses the entire file path when caching templates in the database

* Template.php
diff --git a/CHANGES b/CHANGES
index abaf646..ab99cec 100644
--- a/CHANGES
+++ b/CHANGES
@@ -2,6 +2,7 @@
 ===================
 - New: BSFunctions::bool_to_string() to convert a boolean value into human-readable Yes/No
 - Change: BSTemplate::flush() will no longer insert debug blocks, but BSTemplate::get_debug_block() can be used to retrieve it
+- Change: BSTemplate now uses the full file path when caching templates, rather than just the name

 3.2.0
 ===================
diff --git a/Template.php b/Template.php
index 76b8fb3..aeb8a72 100644
--- a/Template.php
+++ b/Template.php
@@ -94,10 +94,16 @@ class BSTemplate
 	public static $globalVars = array();

 	/**
-	 * The file name of the template
+	 * The path of the template file
 	 * @var string
 	 */
-	protected $filename;
+	protected $path;
+
+	/**
+	 * The name of the template
+	 * @var string
+	 */
+	protected $name;

 	/**
 	 * Template contents
@@ -118,6 +124,7 @@ class BSTemplate
 			return; // there's no point in pre-caching file templates
 		}

+		$namearray = array_map(array('self', '_path'), $namearray);
 		$cache = BSApp::$db->query("SELECT * FROM " . self::$dbCacheTable . " WHERE filename IN ('" . implode("', '", $namearray) . "')");
 		while ($tpl = $cache->fetchArray())
 		{
@@ -140,34 +147,34 @@ class BSTemplate
 	 *
 	 * @param	string	File name
 	 */
-	public function __construct($path)
+	public function __construct($name)
 	{
-		$this->file = $path;
+		$this->name = $name;
+		$this->path = self::_path($name);

 		// checks to see if the template has been cached
-		if (isset(self::$cache[$this->file]))
+		if (isset(self::$cache[$this->path]))
 		{
-			if (!self::$dbCacheTable || filemtime(sprintf(self::$templatePath, $this->file)) <= self::$cache[$this->file]['timestamp'])
+			if (!self::$dbCacheTable || filemtime($this->path) <= self::$cache[$this->path]['timestamp'])
 			{
-				$this->template = self::$cache[$this->file]['template'];
+				$this->template = self::$cache[$this->path]['template'];
 				return;
 			}
 		}

 		// it hasn't been cached
-		$path = sprintf(self::$templatePath, $this->file);
-		if (!is_file($path) || !is_readable($path))
+		if (!is_file($this->path) || !is_readable($this->path))
 		{
-			throw new Exception("Could not load the template $path");
+			throw new Exception("Could not load the template {$this->path}");
 		}
-		$this->template = $this->_parseTemplate(file_get_contents($path));
-		self::$cache[$this->file]['template'] = $this->template;
+		$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->file . "'");
-			self::$cache[$this->file]['time'] = TIMENOW;
+			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;
 		}
 	}

@@ -321,6 +328,18 @@ class BSTemplate

 		return $template;
 	}
+
+	/**
+	 * Returns the full path of a template given a name
+	 *
+	 * @param	string	Template name
+	 *
+	 * @return	string	Template path
+	 */
+	protected static function _path($name)
+	{
+		return sprintf(self::$templatePath, $name);
+	}
 }

 ?>
\ No newline at end of file