]> src.bluestatic.org Git - hoplite.git/blob - file_cache_backend.php
b4c9952714ebb741cc35f1657dd5064afec12c4b
[hoplite.git] / file_cache_backend.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2013 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\views;
18
19 require_once HOPLITE_ROOT . '/views/cache_backend.php';
20
21 /*!
22 An instance of a CacheBackend that writes cached representations to the file
23 system in temporary files.
24 */
25 class FileCacheBackend implements CacheBackend
26 {
27 /*! @var string The cache path for templates. This should be a path, to which
28 the cached template name will be appended. This should not
29 end with a trailing slash.
30 */
31 protected $cache_path = '/tmp/phalanx_views';
32
33 public function cache_path() { return $this->cache_path; }
34
35 public function __construct($cache_path)
36 {
37 $this->cache_path = $cache_path;
38 }
39
40 public function GetTemplateDataForName($name, $tpl_mtime)
41 {
42 $cache_path = $this->_CachePath($name);
43
44 // Make sure the cached file exists and hasn't gotten out-of-date.
45 if (!file_exists($cache_path) || filemtime($cache_path) < $tpl_mtime)
46 return NULL;
47
48 // Load the contents of the cache.
49 $data = @file_get_contents($cache_path);
50 if ($data === FALSE)
51 return NULL;
52
53 return $data;
54 }
55
56 public function StoreCompiledTemplate($name, $mtime, $data)
57 {
58 $cache_path = $this->_CachePath($name);
59
60 // Cache the file.
61 if (file_put_contents($cache_path, $data) === FALSE)
62 throw new TemplateLoaderException('Could not cache ' . $name . ' to ' . $cache_path);
63 }
64
65 public function GetMultipleTemplates(Array $fetch_templates)
66 {
67 return array();
68 }
69
70 /*! Returns the cache path for a given template name. */
71 protected function _CachePath($name)
72 {
73 return $this->cache_path . $name . '.phpi';
74 }
75 }