-
Notifications
You must be signed in to change notification settings - Fork 70
/
FileCache.php
81 lines (69 loc) · 2.29 KB
/
FileCache.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
<?php
namespace Metadata\Cache;
use Metadata\ClassMetadata;
class FileCache implements CacheInterface
{
private $dir;
public function __construct($dir)
{
if (!is_dir($dir)) {
throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir));
}
if (!is_writable($dir)) {
throw new \InvalidArgumentException(sprintf('The directory "%s" is not writable.', $dir));
}
$this->dir = rtrim($dir, '\\/');
}
/**
* {@inheritDoc}
*/
public function loadClassMetadataFromCache(\ReflectionClass $class)
{
$path = $this->dir.'/'.strtr($class->name, '\\', '-').'.cache.php';
if (!file_exists($path)) {
return null;
}
return include $path;
}
/**
* {@inheritDoc}
*/
public function putClassMetadataInCache(ClassMetadata $metadata)
{
$path = $this->dir.'/'.strtr($metadata->name, '\\', '-').'.cache.php';
$tmpFile = tempnam($this->dir, 'metadata-cache');
file_put_contents($tmpFile, '<?php return unserialize('.var_export(serialize($metadata), true).');');
chmod($tmpFile, 0666 & ~umask());
$this->renameFile($tmpFile, $path);
}
/**
* Renames a file with fallback for windows
*
* @param string $source
* @param string $target
*/
private function renameFile($source, $target) {
if (false === @rename($source, $target)) {
if (defined('PHP_WINDOWS_VERSION_BUILD')) {
if (false === copy($source, $target)) {
throw new \RuntimeException(sprintf('(WIN) Could not write new cache file to %s.', $target));
}
if (false === unlink($source)) {
throw new \RuntimeException(sprintf('(WIN) Could not delete temp cache file to %s.', $source));
}
} else {
throw new \RuntimeException(sprintf('Could not write new cache file to %s.', $target));
}
}
}
/**
* {@inheritDoc}
*/
public function evictClassMetadataFromCache(\ReflectionClass $class)
{
$path = $this->dir.'/'.strtr($class->name, '\\', '-').'.cache.php';
if (file_exists($path)) {
unlink($path);
}
}
}