|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Metadata\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Metadata\ClassMetadata; |
|
|
|
|
|
|
6
|
|
|
|
|
7
|
|
|
class FileCache implements CacheInterface |
|
|
|
|
|
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
|
|
|
|
|
10
|
|
|
* @var string |
|
|
|
|
|
|
11
|
|
|
*/ |
|
|
|
|
|
|
12
|
|
|
private $dir; |
|
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
|
|
|
|
|
15
|
|
|
* FileCache constructor. |
|
|
|
|
|
|
16
|
|
|
* |
|
|
|
|
|
|
17
|
|
|
* @param string $dir |
|
|
|
|
|
|
18
|
|
|
*/ |
|
|
|
|
|
|
19
|
|
|
public function __construct($dir) |
|
|
|
|
|
|
20
|
|
|
{ |
|
|
|
|
|
|
21
|
|
|
if (!is_dir($dir)) { |
|
|
|
|
|
|
22
|
|
|
throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir)); |
|
|
|
|
|
|
23
|
|
|
} |
|
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
$this->dir = rtrim($dir, '\\/'); |
|
|
|
|
|
|
26
|
|
|
} |
|
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
/** |
|
|
|
|
|
|
29
|
|
|
* {@inheritDoc} |
|
|
|
|
|
|
30
|
|
|
*/ |
|
|
|
|
|
|
31
|
|
View Code Duplication |
public function loadClassMetadataFromCache(\ReflectionClass $class) |
|
|
|
|
|
|
32
|
|
|
{ |
|
|
|
|
|
|
33
|
|
|
$path = $this->dir.'/'.strtr($class->name, '\\', '-').'.cache.php'; |
|
|
|
|
|
|
34
|
|
|
if (!file_exists($path)) { |
|
|
|
|
|
|
35
|
|
|
return null; |
|
|
|
|
|
|
36
|
|
|
} |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
return include $path; |
|
|
|
|
|
|
39
|
|
|
} |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
/** |
|
|
|
|
|
|
42
|
|
|
* {@inheritDoc} |
|
|
|
|
|
|
43
|
|
|
*/ |
|
|
|
|
|
|
44
|
|
|
public function putClassMetadataInCache(ClassMetadata $metadata) |
|
|
|
|
|
|
45
|
|
|
{ |
|
|
|
|
|
|
46
|
|
|
$path = $this->getFileName($metadata); |
|
|
|
|
|
|
47
|
|
|
if (file_exists($path) && !is_writable($path)) { |
|
|
|
|
|
|
48
|
|
|
throw new \RuntimeException("Cache file {$path} is not writable."); |
|
|
|
|
|
|
49
|
|
|
} |
|
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
if (false === (@file_put_contents($path, |
|
|
|
|
|
|
52
|
|
|
'<?php return unserialize(' . var_export(serialize($metadata), true) . ');') |
|
|
|
|
|
|
53
|
|
|
)) { |
|
|
|
|
|
|
54
|
|
|
throw new \RuntimeException("Can't not write new cache file to {$path}"); |
|
|
|
|
|
|
55
|
|
|
}; |
|
|
|
|
|
|
56
|
|
|
|
|
57
|
|
|
// Let's not break filesystems which do not support chmod. |
|
|
|
|
|
|
58
|
|
|
@chmod($path, 0666 & ~umask()); |
|
|
|
|
|
|
59
|
|
|
} |
|
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
/** |
|
|
|
|
|
|
62
|
|
|
* {@inheritDoc} |
|
|
|
|
|
|
63
|
|
|
*/ |
|
|
|
|
|
|
64
|
|
View Code Duplication |
public function evictClassMetadataFromCache(\ReflectionClass $class) |
|
|
|
|
|
|
65
|
|
|
{ |
|
|
|
|
|
|
66
|
|
|
$path = $this->dir.'/'.strtr($class->name, '\\', '-').'.cache.php'; |
|
|
|
|
|
|
67
|
|
|
if (file_exists($path)) { |
|
|
|
|
|
|
68
|
|
|
unlink($path); |
|
|
|
|
|
|
69
|
|
|
} |
|
|
|
|
|
|
70
|
|
|
} |
|
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
/** |
|
|
|
|
|
|
73
|
|
|
* @param ClassMetadata $metadata |
|
|
|
|
|
|
74
|
|
|
* |
|
|
|
|
|
|
75
|
|
|
* @return string |
|
|
|
|
|
|
76
|
|
|
*/ |
|
|
|
|
|
|
77
|
|
|
private function getFileName(ClassMetadata $metadata) |
|
|
|
|
|
|
78
|
|
|
{ |
|
|
|
|
|
|
79
|
|
|
return $this->dir . '/' . strtr($metadata->name, '\\', '-') . '.cache.php'; |
|
|
|
|
|
|
80
|
|
|
} |
|
|
|
|
|
|
81
|
|
|
} |
|
82
|
|
|
|