web/lib/Zend/Cache/Frontend/File.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Cache
       
    17  * @subpackage Zend_Cache_Frontend
       
    18  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    19  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    20  * @version    $Id: File.php 23330 2010-11-14 20:08:09Z mabe $
       
    21  */
       
    22 
       
    23 
       
    24 /**
       
    25  * @see Zend_Cache_Core
       
    26  */
       
    27 require_once 'Zend/Cache/Core.php';
       
    28 
       
    29 
       
    30 /**
       
    31  * @package    Zend_Cache
       
    32  * @subpackage Zend_Cache_Frontend
       
    33  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    34  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    35  */
       
    36 class Zend_Cache_Frontend_File extends Zend_Cache_Core
       
    37 {
       
    38 
       
    39     /**
       
    40      * Consts for master_files_mode
       
    41      */
       
    42     const MODE_AND = 'AND';
       
    43     const MODE_OR  = 'OR';
       
    44 
       
    45     /**
       
    46      * Available options
       
    47      *
       
    48      * ====> (string) master_file :
       
    49      * - a complete path of the master file
       
    50      * - deprecated (see master_files)
       
    51      *
       
    52      * ====> (array) master_files :
       
    53      * - an array of complete path of master files
       
    54      * - this option has to be set !
       
    55      *
       
    56      * ====> (string) master_files_mode :
       
    57      * - Zend_Cache_Frontend_File::MODE_AND or Zend_Cache_Frontend_File::MODE_OR
       
    58      * - if MODE_AND, then all master files have to be touched to get a cache invalidation
       
    59      * - if MODE_OR (default), then a single touched master file is enough to get a cache invalidation
       
    60      *
       
    61      * ====> (boolean) ignore_missing_master_files
       
    62      * - if set to true, missing master files are ignored silently
       
    63      * - if set to false (default), an exception is thrown if there is a missing master file
       
    64      * @var array available options
       
    65      */
       
    66     protected $_specificOptions = array(
       
    67         'master_file' => null,
       
    68         'master_files' => null,
       
    69         'master_files_mode' => 'OR',
       
    70         'ignore_missing_master_files' => false
       
    71     );
       
    72 
       
    73     /**
       
    74      * Master file mtimes
       
    75      *
       
    76      * Array of int
       
    77      *
       
    78      * @var array
       
    79      */
       
    80     private $_masterFile_mtimes = null;
       
    81 
       
    82     /**
       
    83      * Constructor
       
    84      *
       
    85      * @param  array $options Associative array of options
       
    86      * @throws Zend_Cache_Exception
       
    87      * @return void
       
    88      */
       
    89     public function __construct(array $options = array())
       
    90     {
       
    91         while (list($name, $value) = each($options)) {
       
    92             $this->setOption($name, $value);
       
    93         }
       
    94         if (!isset($this->_specificOptions['master_files'])) {
       
    95             Zend_Cache::throwException('master_files option must be set');
       
    96         }
       
    97     }
       
    98 
       
    99     /**
       
   100      * Change the master_files option
       
   101      *
       
   102      * @param array $masterFiles the complete paths and name of the master files
       
   103      */
       
   104     public function setMasterFiles(array $masterFiles)
       
   105     {
       
   106         $this->_specificOptions['master_file']  = null; // to keep a compatibility
       
   107         $this->_specificOptions['master_files'] = null;
       
   108         $this->_masterFile_mtimes = array();
       
   109 
       
   110         clearstatcache();
       
   111         $i = 0;
       
   112         foreach ($masterFiles as $masterFile) {
       
   113             $mtime = @filemtime($masterFile);
       
   114 
       
   115             if (!$this->_specificOptions['ignore_missing_master_files'] && !$mtime) {
       
   116                 Zend_Cache::throwException('Unable to read master_file : ' . $masterFile);
       
   117             }
       
   118 
       
   119             $this->_masterFile_mtimes[$i] = $mtime;
       
   120             $this->_specificOptions['master_files'][$i] = $masterFile;
       
   121             if ($i === 0) { // to keep a compatibility
       
   122                 $this->_specificOptions['master_files'] = $masterFile;
       
   123             }
       
   124 
       
   125             $i++;
       
   126         }
       
   127     }
       
   128 
       
   129     /**
       
   130      * Change the master_file option
       
   131      *
       
   132      * To keep the compatibility
       
   133      *
       
   134      * @deprecated
       
   135      * @param string $masterFile the complete path and name of the master file
       
   136      */
       
   137     public function setMasterFile($masterFile)
       
   138     {
       
   139           $this->setMasterFiles(array($masterFile));
       
   140     }
       
   141 
       
   142     /**
       
   143      * Public frontend to set an option
       
   144      *
       
   145      * Just a wrapper to get a specific behaviour for master_file
       
   146      *
       
   147      * @param  string $name  Name of the option
       
   148      * @param  mixed  $value Value of the option
       
   149      * @throws Zend_Cache_Exception
       
   150      * @return void
       
   151      */
       
   152     public function setOption($name, $value)
       
   153     {
       
   154         if ($name == 'master_file') {
       
   155             $this->setMasterFile($value);
       
   156         } else if ($name == 'master_files') {
       
   157             $this->setMasterFiles($value);
       
   158         } else {
       
   159             parent::setOption($name, $value);
       
   160         }
       
   161     }
       
   162 
       
   163     /**
       
   164      * Test if a cache is available for the given id and (if yes) return it (false else)
       
   165      *
       
   166      * @param  string  $id                     Cache id
       
   167      * @param  boolean $doNotTestCacheValidity If set to true, the cache validity won't be tested
       
   168      * @param  boolean $doNotUnserialize       Do not serialize (even if automatic_serialization is true) => for internal use
       
   169      * @return mixed|false Cached datas
       
   170      */
       
   171     public function load($id, $doNotTestCacheValidity = false, $doNotUnserialize = false)
       
   172     {
       
   173         if (!$doNotTestCacheValidity) {
       
   174             if ($this->test($id)) {
       
   175                 return parent::load($id, true, $doNotUnserialize);
       
   176             }
       
   177             return false;
       
   178         }
       
   179         return parent::load($id, true, $doNotUnserialize);
       
   180     }
       
   181 
       
   182     /**
       
   183      * Test if a cache is available for the given id
       
   184      *
       
   185      * @param  string $id Cache id
       
   186      * @return int|false Last modified time of cache entry if it is available, false otherwise
       
   187      */
       
   188     public function test($id)
       
   189     {
       
   190         $lastModified = parent::test($id);
       
   191         if ($lastModified) {
       
   192             if ($this->_specificOptions['master_files_mode'] == self::MODE_AND) {
       
   193                 // MODE_AND
       
   194                 foreach($this->_masterFile_mtimes as $masterFileMTime) {
       
   195                     if ($masterFileMTime) {
       
   196                         if ($lastModified > $masterFileMTime) {
       
   197                             return $lastModified;
       
   198                         }
       
   199                     }
       
   200                 }
       
   201             } else {
       
   202                 // MODE_OR
       
   203                 $res = true;
       
   204                 foreach($this->_masterFile_mtimes as $masterFileMTime) {
       
   205                     if ($masterFileMTime) {
       
   206                         if ($lastModified <= $masterFileMTime) {
       
   207                             return false;
       
   208                         }
       
   209                     }
       
   210                 }
       
   211                 return $lastModified;
       
   212             }
       
   213         }
       
   214         return false;
       
   215     }
       
   216 
       
   217 }
       
   218