BSTemplate now uses the entire file path when caching templates in the database
authorRobert Sesek <rsesek@bluestatic.org>
Tue, 6 Jan 2009 07:31:04 +0000 (23:31 -0800)
committerRobert Sesek <rsesek@bluestatic.org>
Tue, 6 Jan 2009 07:31:04 +0000 (23:31 -0800)
* Template.php

CHANGES
Template.php

diff --git a/CHANGES b/CHANGES
index abaf646f98b959983d58e05d988c3ebafc73cca4..ab99cec2334030de016946bc349452609052eb28 100644 (file)
--- 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
 ===================
index 76b8fb35471ccb35fc8327973ebd1b1e5dfa8995..aeb8a72b1b96ab0ddfd3aa8efd9d4a45a1723575 100644 (file)
@@ -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