[ Index ]

PHP Cross Reference of Limb3

title

Body

[close]

/config/src/ -> lmbCachedIni.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/config/src/lmbIni.class.php');
  10  lmb_require('limb/fs/src/lmbFs.class.php');
  11  
  12  /**

  13   * class lmbCachedIni.

  14   *

  15   * @package config

  16   * @version $Id: lmbCachedIni.class.php 5945 2007-06-06 08:31:43Z pachanga $

  17   */
  18  class lmbCachedIni extends lmbIni
  19  {
  20    protected $cache_dir;
  21  
  22    function __construct($file, $cache_dir)
  23    {
  24      $this->file_path = $file;
  25      $this->cache_dir = $cache_dir;
  26  
  27      if(!$this->_loadCache())
  28      {
  29        $this->import($this->_createIni($file)->export());
  30        $this->_saveCache();
  31      }
  32    }
  33  
  34    protected function _createIni($file)
  35    {
  36      return new lmbIni($file);
  37    }
  38  
  39    function isCacheEnabled()
  40    {
  41      return (!defined('LIMB_INI_CACHE_ENABLED') || (defined('LIMB_INI_CACHE_ENABLED') &&
  42               constant('LIMB_INI_CACHE_ENABLED')));
  43    }
  44  
  45    function getCacheFile()
  46    {
  47      return $this->cache_dir . md5($this->file_path) . '.cache';
  48    }
  49  
  50    protected function _loadCache()
  51    {
  52      if(!$this->isCacheEnabled())
  53        return false;
  54  
  55      $cache_dir = $this->cache_dir;
  56  
  57      lmbFs :: mkdir($cache_dir);
  58  
  59      $cache_file = $this->getCacheFile();
  60  
  61      if(!$this->_isCacheValid($cache_file))
  62        return false;
  63  
  64      $this->import(unserialize(file_get_contents($cache_file)));
  65  
  66      return true;
  67    }
  68  
  69    protected function _isCacheValid($cache_file)
  70    {
  71      if(!file_exists($cache_file))
  72        return false;
  73  
  74      if(filemtime($cache_file) >= $this->_getIniLastModificationTime())
  75        return true;
  76      else
  77        return false;
  78    }
  79  
  80    protected function _getIniLastModificationTime()
  81    {
  82      if(!$override = $this->getOverrideFile())
  83        return filemtime($this->file_path);
  84  
  85      return filemtime($override);
  86    }
  87  
  88    protected function _saveCache()
  89    {
  90      lmbFs :: safeWrite($this->getCacheFile(),
  91                         serialize($this->export()));
  92    }
  93  
  94    function removeCache()
  95    {
  96      if(file_exists($cache_file = $this->getCacheFile()))
  97        unlink($cache_file);
  98    }
  99  }
 100  
 101  ?>


Generated: Fri Dec 5 04:05:07 2008 Cross-referenced by PHPXref 0.7