[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/fs/src/ -> lmbCachingFileLocator.class.php (source)

   1  <?php
   2  /*
   3   * Limb PHP Framework
   4   *
   5   * @link http://limb-project.com 
   6   * @copyright  Copyright &copy; 2004-2007 BIT(http://bit-creative.com)
   7   * @license    LGPL http://www.gnu.org/copyleft/lesser.html 
   8   */
   9  lmb_require('limb/fs/src/lmbFileLocatorDecorator.class.php');
  10  lmb_require('limb/fs/src/lmbFs.class.php');
  11  
  12  /**

  13   * class lmbCachingFileLocator.

  14   *

  15   * @package fs

  16   * @version $Id$

  17   */
  18  class lmbCachingFileLocator extends lmbFileLocatorDecorator
  19  {
  20    protected $_cached_paths = array();
  21    protected $_changed = false;
  22    protected $_cache_name;
  23  
  24    function __construct($locator, $cache_dir, $cache_name = 'default')
  25    {
  26      $this->_cache_name = $cache_name;
  27      $this->_cache_dir = $cache_dir;
  28  
  29      parent :: __construct($locator);
  30  
  31      $this->_loadCache();
  32    }
  33  
  34    function __destruct()
  35    {
  36      $this->saveCache();
  37    }
  38  
  39    function getCacheFile()
  40    {
  41      lmbFs :: mkdir($this->_cache_dir);
  42      $cache_file = $this->_cache_dir . '/' . $this->_cache_name  . '_locator.cache';
  43      return $cache_file;
  44    }
  45  
  46    function flushCache()
  47    {
  48      $this->_cached_paths = array();
  49      $cache_file = $this->getCacheFile();
  50  
  51      if(file_exists($cache_file))
  52        unlink($cache_file);
  53    }
  54  
  55    function _loadCache()
  56    {
  57      $cache_file = $this->getCacheFile();
  58      if(!file_exists($cache_file))
  59        return;
  60  
  61      $this->_cached_paths = unserialize(file_get_contents($cache_file));
  62    }
  63  
  64    function saveCache()
  65    {
  66      if(!$this->_changed)
  67        return;
  68  
  69      $content = serialize($this->_cached_paths);
  70      lmbFs :: safeWrite($this->getCacheFile(), $content);
  71    }
  72  
  73    function locate($alias, $params = array())
  74    {
  75      if($params)
  76        $hash = $alias . '_' . md5(serialize($params));
  77      else
  78        $hash = $alias;
  79  
  80      if(isset($this->_cached_paths[$hash]))
  81        return $this->_cached_paths[$hash];
  82  
  83      $this->_changed = true;
  84      $this->_cached_paths[$hash] = $this->locator->locate($alias, $params);
  85  
  86      return $this->_cached_paths[$hash];
  87    }
  88  }
  89  
  90  ?>


Generated: Tue Oct 7 05:02:03 2008 Cross-referenced by PHPXref 0.7