smarty_internal_cacheresource_file.php
Current file: C:\DATA\Viktor\Diplomka\XSLT-Benchmarking/Libs/Smarty/sysplugins/smarty_internal_cacheresource_file.php
Legend: executed not executed dead code

  Coverage
  Classes Functions / Methods Lines
Total
0.00% 0 / 1
0.00% 0 / 9 CRAP
0.00% 0 / 116
Smarty_Internal_CacheResource_File
0.00% 0 / 1
0.00% 0 / 9 2352
0.00% 0 / 116
 populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
0.00% 0 / 1 90
0.00% 0 / 31
 populateTimestamp(Smarty_Template_Cached $cached)
0.00% 0 / 1 2
0.00% 0 / 3
 process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)
0.00% 0 / 1 2
0.00% 0 / 2
 writeCachedContent(Smarty_Internal_Template $_template, $content)
0.00% 0 / 1 6
0.00% 0 / 5
 clearAll(Smarty $smarty, $exp_time = null)
0.00% 0 / 1 2
0.00% 0 / 1
 clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
0.00% 0 / 1 870
0.00% 0 / 62
 hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
0.00% 0 / 1 12
0.00% 0 / 6
 acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
0.00% 0 / 1 2
0.00% 0 / 3
 releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
0.00% 0 / 1 2
0.00% 0 / 3



       1                 : <?php                                                                                                                                                                       
       2                 : /**                                                                                                                                                                         
       3                 :  * Smarty Internal Plugin CacheResource File                                                                                                                                
       4                 :  *                                                                                                                                                                          
       5                 :  * @package Smarty                                                                                                                                                          
       6                 :  * @subpackage Cacher                                                                                                                                                       
       7                 :  * @author Uwe Tews                                                                                                                                                         
       8                 :  * @author Rodney Rehm                                                                                                                                                      
       9                 :  */                                                                                                                                                                         
      10                 :                                                                                                                                                                             
      11                 : /**                                                                                                                                                                         
      12                 :  * This class does contain all necessary methods for the HTML cache on file system                                                                                          
      13                 :  *                                                                                                                                                                          
      14                 :  * Implements the file system as resource for the HTML cache Version ussing nocache inserts.                                                                                
      15                 :  *                                                                                                                                                                          
      16                 :  * @package Smarty                                                                                                                                                          
      17                 :  * @subpackage Cacher                                                                                                                                                       
      18                 :  */                                                                                                                                                                         
      19                 : class Smarty_Internal_CacheResource_File extends Smarty_CacheResource {                                                                                                     
      20                 :                                                                                                                                                                             
      21                 :     /**                                                                                                                                                                     
      22                 :      * populate Cached Object with meta data from Resource                                                                                                                  
      23                 :      *                                                                                                                                                                      
      24                 :      * @param Smarty_Template_Cached   $cached    cached object                                                                                                             
      25                 :      * @param Smarty_Internal_Template $_template template object                                                                                                           
      26                 :      * @return void                                                                                                                                                         
      27                 :      */                                                                                                                                                                     
      28                 :     public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)                                                                           
      29                 :     {                                                                                                                                                                       
      30               0 :         $_source_file_path = str_replace(':', '.', $_template->source->filepath);                                                                                           
      31               0 :         $_cache_id = isset($_template->cache_id) ? preg_replace('![^\w\|]+!', '_', $_template->cache_id) : null;                                                            
      32               0 :         $_compile_id = isset($_template->compile_id) ? preg_replace('![^\w\|]+!', '_', $_template->compile_id) : null;                                                      
      33               0 :         $_filepath = $_template->source->uid;                                                                                                                               
      34                 :         // if use_sub_dirs, break file into directories                                                                                                                     
      35               0 :         if ($_template->smarty->use_sub_dirs) {                                                                                                                             
      36               0 :             $_filepath = substr($_filepath, 0, 2) . DS                                                                                                                      
      37               0 :                 . substr($_filepath, 2, 2) . DS                                                                                                                             
      38               0 :                 . substr($_filepath, 4, 2) . DS                                                                                                                             
      39               0 :                 . $_filepath;                                                                                                                                               
      40               0 :         }                                                                                                                                                                   
      41               0 :         $_compile_dir_sep = $_template->smarty->use_sub_dirs ? DS : '^';                                                                                                    
      42               0 :         if (isset($_cache_id)) {                                                                                                                                            
      43               0 :             $_cache_id = str_replace('|', $_compile_dir_sep, $_cache_id) . $_compile_dir_sep;                                                                               
      44               0 :         } else {                                                                                                                                                            
      45               0 :             $_cache_id = '';                                                                                                                                                
      46                 :         }                                                                                                                                                                   
      47               0 :         if (isset($_compile_id)) {                                                                                                                                          
      48               0 :             $_compile_id = $_compile_id . $_compile_dir_sep;                                                                                                                
      49               0 :         } else {                                                                                                                                                            
      50               0 :             $_compile_id = '';                                                                                                                                              
      51                 :         }                                                                                                                                                                   
      52               0 :         $_cache_dir = $_template->smarty->getCacheDir();                                                                                                                    
      53               0 :         if ($_template->smarty->cache_locking) {                                                                                                                            
      54                 :             // create locking file name                                                                                                                                     
      55                 :             // relative file name?                                                                                                                                          
      56               0 :             if (!preg_match('/^([\/\\\\]|[a-zA-Z]:[\/\\\\])/', $_cache_dir)) {                                                                                              
      57               0 :                 $_lock_dir = rtrim(getcwd(), '/\\') . DS . $_cache_dir;                                                                                                     
      58               0 :             } else {                                                                                                                                                        
      59               0 :                 $_lock_dir = $_cache_dir;                                                                                                                                   
      60                 :             }                                                                                                                                                               
      61               0 :             $cached->lock_id = $_lock_dir.sha1($_cache_id.$_compile_id.$_template->source->uid).'.lock';                                                                    
      62               0 :         }                                                                                                                                                                   
      63               0 :         $cached->filepath = $_cache_dir . $_cache_id . $_compile_id . $_filepath . '.' . basename($_source_file_path) . '.php';                                             
      64               0 :         $cached->timestamp = @filemtime($cached->filepath);                                                                                                                 
      65               0 :         $cached->exists = !!$cached->timestamp;                                                                                                                             
      66               0 :     }                                                                                                                                                                       
      67                 :                                                                                                                                                                             
      68                 :     /**                                                                                                                                                                     
      69                 :      * populate Cached Object with timestamp and exists from Resource                                                                                                       
      70                 :      *                                                                                                                                                                      
      71                 :      * @param Smarty_Template_Cached $cached cached object                                                                                                                  
      72                 :      * @return void                                                                                                                                                         
      73                 :      */                                                                                                                                                                     
      74                 :     public function populateTimestamp(Smarty_Template_Cached $cached)                                                                                                       
      75                 :     {                                                                                                                                                                       
      76               0 :         $cached->timestamp = @filemtime($cached->filepath);                                                                                                                 
      77               0 :         $cached->exists = !!$cached->timestamp;                                                                                                                             
      78               0 :     }                                                                                                                                                                       
      79                 :                                                                                                                                                                             
      80                 :     /**                                                                                                                                                                     
      81                 :      * Read the cached template and process its header                                                                                                                      
      82                 :      *                                                                                                                                                                      
      83                 :      * @param Smarty_Internal_Template $_template template object                                                                                                           
      84                 :      * @param Smarty_Template_Cached $cached cached object                                                                                                                  
      85                 :      * @return booelan true or false if the cached content does not exist                                                                                                   
      86                 :      */                                                                                                                                                                     
      87                 :     public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached=null)                                                                       
      88                 :     {                                                                                                                                                                       
      89               0 :         $_smarty_tpl = $_template;                                                                                                                                          
      90               0 :         return @include $_template->cached->filepath;                                                                                                                       
      91                 :     }                                                                                                                                                                       
      92                 :                                                                                                                                                                             
      93                 :     /**                                                                                                                                                                     
      94                 :      * Write the rendered template output to cache                                                                                                                          
      95                 :      *                                                                                                                                                                      
      96                 :      * @param Smarty_Internal_Template $_template template object                                                                                                           
      97                 :      * @param string                   $content   content to cache                                                                                                          
      98                 :      * @return boolean success                                                                                                                                              
      99                 :      */                                                                                                                                                                     
     100                 :     public function writeCachedContent(Smarty_Internal_Template $_template, $content)                                                                                       
     101                 :     {                                                                                                                                                                       
     102               0 :         if (Smarty_Internal_Write_File::writeFile($_template->cached->filepath, $content, $_template->smarty) === true) {                                                   
     103               0 :             $_template->cached->timestamp = filemtime($_template->cached->filepath);                                                                                        
     104               0 :             $_template->cached->exists = !!$_template->cached->timestamp;                                                                                                   
     105               0 :             return true;                                                                                                                                                    
     106                 :         }                                                                                                                                                                   
     107               0 :         return false;                                                                                                                                                       
     108                 :     }                                                                                                                                                                       
     109                 :                                                                                                                                                                             
     110                 :     /**                                                                                                                                                                     
     111                 :      * Empty cache                                                                                                                                                          
     112                 :      *                                                                                                                                                                      
     113                 :      * @param Smarty_Internal_Template $_template template object                                                                                                           
     114                 :      * @param integer                  $exp_time  expiration time (number of seconds, not timestamp)                                                                        
     115                 :      * @return integer number of cache files deleted                                                                                                                        
     116                 :      */                                                                                                                                                                     
     117                 :     public function clearAll(Smarty $smarty, $exp_time = null)                                                                                                              
     118                 :     {                                                                                                                                                                       
     119               0 :         return $this->clear($smarty, null, null, null, $exp_time);                                                                                                          
     120                 :     }                                                                                                                                                                       
     121                 :                                                                                                                                                                             
     122                 :     /**                                                                                                                                                                     
     123                 :      * Empty cache for a specific template                                                                                                                                  
     124                 :      *                                                                                                                                                                      
     125                 :      * @param Smarty  $_template     template object                                                                                                                        
     126                 :      * @param string  $resource_name template name                                                                                                                          
     127                 :      * @param string  $cache_id      cache id                                                                                                                               
     128                 :      * @param string  $compile_id    compile id                                                                                                                             
     129                 :      * @param integer $exp_time      expiration time (number of seconds, not timestamp)                                                                                     
     130                 :      * @return integer number of cache files deleted                                                                                                                        
     131                 :     */                                                                                                                                                                      
     132                 :     public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)                                                                                
     133                 :     {                                                                                                                                                                       
     134               0 :         $_cache_id = isset($cache_id) ? preg_replace('![^\w\|]+!', '_', $cache_id) : null;                                                                                  
     135               0 :         $_compile_id = isset($compile_id) ? preg_replace('![^\w\|]+!', '_', $compile_id) : null;                                                                            
     136               0 :         $_dir_sep = $smarty->use_sub_dirs ? '/' : '^';                                                                                                                      
     137               0 :         $_compile_id_offset = $smarty->use_sub_dirs ? 3 : 0;                                                                                                                
     138               0 :         $_dir = $smarty->getCacheDir();                                                                                                                                     
     139               0 :         $_dir_length = strlen($_dir);                                                                                                                                       
     140               0 :         if (isset($_cache_id)) {                                                                                                                                            
     141               0 :             $_cache_id_parts = explode('|', $_cache_id);                                                                                                                    
     142               0 :             $_cache_id_parts_count = count($_cache_id_parts);                                                                                                               
     143               0 :             if ($smarty->use_sub_dirs) {                                                                                                                                    
     144               0 :                 foreach ($_cache_id_parts as $id_part) {                                                                                                                    
     145               0 :                     $_dir .= $id_part . DS;                                                                                                                                 
     146               0 :                 }                                                                                                                                                           
     147               0 :             }                                                                                                                                                               
     148               0 :         }                                                                                                                                                                   
     149               0 :         if (isset($resource_name)) {                                                                                                                                        
     150               0 :             $_save_stat = $smarty->caching;                                                                                                                                 
     151               0 :             $smarty->caching = true;                                                                                                                                        
     152               0 :             $tpl = new $smarty->template_class($resource_name, $smarty);                                                                                                    
     153               0 :             $smarty->caching = $_save_stat;                                                                                                                                 
     154                 :                                                                                                                                                                             
     155                 :             // remove from template cache                                                                                                                                   
     156               0 :             $tpl->source; // have the template registered before unset()                                                                                                    
     157               0 :             $_templateId = sha1($tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id);                                                                         
     158               0 :             unset($smarty->template_objects[$_templateId]);                                                                                                                 
     159                 :                                                                                                                                                                             
     160               0 :             if ($tpl->source->exists) {                                                                                                                                     
     161               0 :                 $_resourcename_parts = basename(str_replace('^', '/', $tpl->cached->filepath));                                                                             
     162               0 :             } else {                                                                                                                                                        
     163               0 :                 return 0;                                                                                                                                                   
     164                 :             }                                                                                                                                                               
     165               0 :         }                                                                                                                                                                   
     166               0 :         $_count = 0;                                                                                                                                                        
     167               0 :         $_time = time();                                                                                                                                                    
     168               0 :         if (file_exists($_dir)) {                                                                                                                                           
     169               0 :             $_cacheDirs = new RecursiveDirectoryIterator($_dir);                                                                                                            
     170               0 :             $_cache = new RecursiveIteratorIterator($_cacheDirs, RecursiveIteratorIterator::CHILD_FIRST);                                                                   
     171               0 :             foreach ($_cache as $_file) {                                                                                                                                   
     172               0 :                 if (substr($_file->getBasename(),0,1) == '.' || strpos($_file, '.svn') !== false) continue;                                                                 
     173                 :                 // directory ?                                                                                                                                              
     174               0 :                 if ($_file->isDir()) {                                                                                                                                      
     175               0 :                     if (!$_cache->isDot()) {                                                                                                                                
     176                 :                         // delete folder if empty                                                                                                                           
     177               0 :                         @rmdir($_file->getPathname());                                                                                                                      
     178               0 :                     }                                                                                                                                                       
     179               0 :                 } else {                                                                                                                                                    
     180               0 :                     $_parts = explode($_dir_sep, str_replace('\\', '/', substr((string)$_file, $_dir_length)));                                                             
     181               0 :                     $_parts_count = count($_parts);                                                                                                                         
     182                 :                     // check name                                                                                                                                           
     183               0 :                     if (isset($resource_name)) {                                                                                                                            
     184               0 :                         if ($_parts[$_parts_count-1] != $_resourcename_parts) {                                                                                             
     185               0 :                             continue;                                                                                                                                       
     186                 :                         }                                                                                                                                                   
     187               0 :                     }                                                                                                                                                       
     188                 :                     // check compile id                                                                                                                                     
     189               0 :                     if (isset($_compile_id) && (!isset($_parts[$_parts_count-2 - $_compile_id_offset]) || $_parts[$_parts_count-2 - $_compile_id_offset] != $_compile_id)) {
     190               0 :                         continue;                                                                                                                                           
     191                 :                     }                                                                                                                                                       
     192                 :                     // check cache id                                                                                                                                       
     193               0 :                     if (isset($_cache_id)) {                                                                                                                                
     194                 :                         // count of cache id parts                                                                                                                          
     195               0 :                         $_parts_count = (isset($_compile_id)) ? $_parts_count - 2 - $_compile_id_offset : $_parts_count - 1 - $_compile_id_offset;                          
     196               0 :                         if ($_parts_count < $_cache_id_parts_count) {                                                                                                       
     197               0 :                             continue;                                                                                                                                       
     198                 :                         }                                                                                                                                                   
     199               0 :                         for ($i = 0; $i < $_cache_id_parts_count; $i++) {                                                                                                   
     200               0 :                             if ($_parts[$i] != $_cache_id_parts[$i]) continue 2;                                                                                            
     201               0 :                         }                                                                                                                                                   
     202               0 :                     }                                                                                                                                                       
     203                 :                     // expired ?                                                                                                                                            
     204               0 :                     if (isset($exp_time) && $_time - @filemtime($_file) < $exp_time) {                                                                                      
     205               0 :                         continue;                                                                                                                                           
     206                 :                     }                                                                                                                                                       
     207               0 :                     $_count += @unlink((string) $_file) ? 1 : 0;                                                                                                            
     208                 :                 }                                                                                                                                                           
     209               0 :             }                                                                                                                                                               
     210               0 :         }                                                                                                                                                                   
     211               0 :         return $_count;                                                                                                                                                     
     212                 :     }                                                                                                                                                                       
     213                 :                                                                                                                                                                             
     214                 :     /**                                                                                                                                                                     
     215                 :      * Check is cache is locked for this template                                                                                                                           
     216                 :      *                                                                                                                                                                      
     217                 :      * @param Smarty $smarty Smarty object                                                                                                                                  
     218                 :      * @param Smarty_Template_Cached $cached cached object                                                                                                                  
     219                 :      * @return booelan true or false if cache is locked                                                                                                                     
     220                 :      */                                                                                                                                                                     
     221                 :     public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)                                                                                                 
     222                 :     {                                                                                                                                                                       
     223               0 :         if (version_compare(PHP_VERSION, '5.3.0', '>=')) {                                                                                                                  
     224               0 :             clearstatcache(true, $cached->lock_id);                                                                                                                         
     225               0 :         } else {                                                                                                                                                            
     226               0 :             clearstatcache();                                                                                                                                               
     227                 :         }                                                                                                                                                                   
     228               0 :         $t = @filemtime($cached->lock_id);                                                                                                                                  
     229               0 :         return $t && (time() - $t < $smarty->locking_timeout);                                                                                                              
     230                 :     }                                                                                                                                                                       
     231                 :                                                                                                                                                                             
     232                 :     /**                                                                                                                                                                     
     233                 :      * Lock cache for this template                                                                                                                                         
     234                 :      *                                                                                                                                                                      
     235                 :      * @param Smarty $smarty Smarty object                                                                                                                                  
     236                 :      * @param Smarty_Template_Cached $cached cached object                                                                                                                  
     237                 :      */                                                                                                                                                                     
     238                 :     public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)                                                                                             
     239                 :     {                                                                                                                                                                       
     240               0 :         $cached->is_locked = true;                                                                                                                                          
     241               0 :         touch($cached->lock_id);                                                                                                                                            
     242               0 :     }                                                                                                                                                                       
     243                 :                                                                                                                                                                             
     244                 :     /**                                                                                                                                                                     
     245                 :      * Unlock cache for this template                                                                                                                                       
     246                 :      *                                                                                                                                                                      
     247                 :      * @param Smarty $smarty Smarty object                                                                                                                                  
     248                 :      * @param Smarty_Template_Cached $cached cached object                                                                                                                  
     249                 :      */                                                                                                                                                                     
     250                 :     public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)                                                                                             
     251                 :     {                                                                                                                                                                       
     252               0 :         $cached->is_locked = false;                                                                                                                                         
     253               0 :         @unlink($cached->lock_id);                                                                                                                                          
     254               0 :     }                                                                                                                                                                       
     255                 : }                                                                                                                                                                           
     256                 :                                                                                                                                                                             


Generated by PHP_CodeCoverage @package_version@ using PHP 5.3.6 and PHPUnit @package_version@ at Tue Jun 26 15:06:55 CEST 2012.