|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
namespace Metadata\Cache; |
|
|
4 |
|
|
|
5 |
use Metadata\ClassMetadata; |
|
|
6 |
|
|
|
7 |
class FileCache implements CacheInterface |
|
|
8 |
{ |
|
|
9 |
private $dir; |
|
|
10 |
|
|
|
11 |
public function __construct($dir) |
|
|
12 |
{ |
|
|
13 |
if (!is_dir($dir)) { |
|
|
14 |
throw new \InvalidArgumentException(sprintf('The directory "%s" does not exist.', $dir)); |
|
|
15 |
} |
|
|
16 |
if (!is_writable($dir)) { |
|
|
17 |
throw new \InvalidArgumentException(sprintf('The directory "%s" is not writable.', $dir)); |
|
|
18 |
} |
|
|
19 |
|
|
|
20 |
$this->dir = rtrim($dir, '\\/'); |
|
|
21 |
} |
|
|
22 |
|
|
|
23 |
public function loadClassMetadataFromCache(\ReflectionClass $class) |
|
|
24 |
{ |
|
|
25 |
$path = $this->dir.'/'.strtr($class->getName(), '\\', '-').'.cache.php'; |
|
|
26 |
if (!file_exists($path)) { |
|
|
27 |
return null; |
|
|
28 |
} |
|
|
29 |
|
|
|
30 |
return include $path; |
|
|
31 |
} |
|
|
32 |
|
|
|
33 |
public function putClassMetadataInCache(ClassMetadata $metadata) |
|
|
34 |
{ |
|
|
35 |
$path = $this->dir.'/'.strtr($metadata->name, '\\', '-').'.cache.php'; |
|
|
36 |
file_put_contents($path, '<?php return unserialize('.var_export(serialize($metadata), true).');'); |
|
|
37 |
} |
|
|
38 |
|
|
|
39 |
public function evictClassMetadataFromCache(\ReflectionClass $class) |
|
|
40 |
{ |
|
|
41 |
$path = $this->dir.'/'.strtr($class->getName(), '\\', '-').'.cache.php'; |
|
|
42 |
if (file_exists($path)) { |
|
|
43 |
unlink($path); |
|
|
44 |
} |
|
|
45 |
} |
|
|
46 |
} |