/
FilesystemCache.php
103 lines (90 loc) · 2.44 KB
/
FilesystemCache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/*
* This file is part of Twig.
*
* (c) 2014 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* Class Twig_Cache_FilesystemCache - the one and only officially supported cache for Twig
*
* @author Andrew Tch <andrew@noop.lv>
*/
class Twig_Cache_FilesystemCache implements Twig_Cache_CacheInterface
{
/**
* Root cache directory
*
* @var string
*/
protected $directory;
public function __construct($directory)
{
$this->directory = $directory;
}
/**
* {@inheritdoc}
*/
public function getCacheKey($className, $prefix)
{
$class = substr($className, strlen($prefix));
return $this->directory.'/'.substr($class, 0, 2).'/'.substr($class, 2, 2).'/'.substr($class, 4).'.php';
}
/**
* {@inheritdoc}
*/
public function clear()
{
foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($this->directory), RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
if ($file->isFile()) {
@unlink($file->getPathname());
}
}
}
/**
* {@inheritdoc}
*/
public function has($key)
{
return is_readable($key);
}
/**
* {@inheritdoc}
*/
public function load($key)
{
require_once $key;
}
/**
* {@inheritdoc}
*/
public function write($key, $content)
{
$dir = dirname($key);
if (!is_dir($dir)) {
if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
throw new RuntimeException(sprintf("Unable to create the cache directory (%s).", $dir));
}
} elseif (!is_writable($dir)) {
throw new RuntimeException(sprintf("Unable to write in the cache directory (%s).", $dir));
}
$tmpFile = tempnam($dir, basename($key));
if (false !== @file_put_contents($tmpFile, $content)) {
// rename does not work on Win32 before 5.2.6
if (@rename($tmpFile, $key) || (@copy($tmpFile, $key) && unlink($tmpFile))) {
@chmod($key, 0666 & ~umask());
return;
}
}
throw new RuntimeException(sprintf('Failed to write cache file "%s".', $key));
}
/**
* {@inheritdoc}
*/
public function getTimestamp($key)
{
return filemtime($key);
}
}