1<?php
2
3/**
4* This class extends Cache_Lite and offers a cache system driven by a master file
5*
6* With this class, cache validity is only dependent of a given file. Cache files
7* are valid only if they are older than the master file. It's a perfect way for
8* caching templates results (if the template file is newer than the cache, cache
9* must be rebuild...) or for config classes...
10* There are some examples in the 'docs/examples' file
11* Technical choices are described in the 'docs/technical' file
12*
13* @package Cache_Lite
14* @author Fabien MARTY <fab@php.net>
15*/
16
17require_once('Cache/Lite.php');
18
19class Cache_Lite_File extends Cache_Lite
20{
21
22    // --- Private properties ---
23
24    /**
25    * Complete path of the file used for controlling the cache lifetime
26    *
27    * @var string $_masterFile
28    */
29    var $_masterFile = '';
30
31    /**
32    * Masterfile mtime
33    *
34    * @var int $_masterFile_mtime
35    */
36    var $_masterFile_mtime = 0;
37
38    // --- Public methods ----
39
40    /**
41    * Constructor
42    *
43    * $options is an assoc. To have a look at availables options,
44    * see the constructor of the Cache_Lite class in 'Cache_Lite.php'
45    *
46    * Comparing to Cache_Lite constructor, there is another option :
47    * $options = array(
48    *     (...) see Cache_Lite constructor
49    *     'masterFile' => complete path of the file used for controlling the cache lifetime(string)
50    * );
51    *
52    * @param array $options options
53    * @access public
54    */
55    function __construct($options = array(NULL))
56    {
57        $options['lifetime'] = 0;
58        parent::__construct($options);
59        if (isset($options['masterFile'])) {
60            $this->_masterFile = $options['masterFile'];
61        } else {
62            return $this->raiseError('Cache_Lite_File : masterFile option must be set !');
63        }
64        if (!($this->_masterFile_mtime = @filemtime($this->_masterFile))) {
65            return $this->raiseError('Cache_Lite_File : Unable to read masterFile : '.$this->_masterFile, -3);
66        }
67    }
68
69    /**
70    * Test if a cache is available and (if yes) return it
71    *
72    * @param string $id cache id
73    * @param string $group name of the cache group
74    * @param boolean $doNotTestCacheValidity if set to true, the cache validity won't be tested
75    * @return string data of the cache (else : false)
76    * @access public
77    */
78    function get($id, $group = 'default', $doNotTestCacheValidity = false)
79    {
80        if ($data = parent::get($id, $group, true)) {
81            if ($filemtime = $this->lastModified()) {
82                if ($filemtime > $this->_masterFile_mtime) {
83                    return $data;
84                }
85            }
86        }
87        return false;
88    }
89
90}
91