These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /** |
||
3 | * This file is part of phpDocumentor. |
||
4 | * |
||
5 | * For the full copyright and license information, please view the LICENSE |
||
6 | * file that was distributed with this source code. |
||
7 | * |
||
8 | * @copyright 2010-${YEAR} Mike van Riel<[email protected]> |
||
9 | * @license http://www.opensource.org/licenses/mit-license.php MIT |
||
10 | * @link http://phpdoc.org |
||
11 | */ |
||
12 | |||
13 | namespace phpDocumentor\Infrastructure\JmsSerializer; |
||
14 | |||
15 | use Metadata\Cache\CacheInterface; |
||
16 | use Metadata\ClassMetadata; |
||
17 | |||
18 | class FileCache implements CacheInterface |
||
19 | { |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $dir; |
||
24 | |||
25 | /** |
||
26 | * FileCache constructor. |
||
27 | */ |
||
28 | public function __construct(string $dir) |
||
29 | { |
||
30 | if (!is_dir($dir)) { |
||
31 | $this->createDirectory($dir); |
||
32 | } |
||
33 | |||
34 | $this->dir = rtrim($dir, '\\/'); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * {@inheritDoc} |
||
39 | */ |
||
40 | public function loadClassMetadataFromCache(\ReflectionClass $class) |
||
41 | { |
||
42 | $path = $this->dir . '/' . strtr($class->name, '\\', '-') . '.cache.php'; |
||
43 | if (!file_exists($path)) { |
||
44 | return null; |
||
45 | } |
||
46 | |||
47 | return include $path; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * {@inheritDoc} |
||
52 | */ |
||
53 | public function putClassMetadataInCache(ClassMetadata $metadata) |
||
54 | { |
||
55 | $path = $this->getFileName($metadata); |
||
56 | |||
57 | if (!is_writable(dirname($path))) { |
||
58 | throw new \RuntimeException("Cache file {$path} is not writable."); |
||
59 | } |
||
60 | |||
61 | if (false === (@file_put_contents( |
||
62 | $path, |
||
63 | '<?php return unserialize(' . var_export(serialize($metadata), true) . ');' |
||
64 | ) |
||
65 | )) { |
||
66 | throw new \RuntimeException("Can't not write new cache file to {$path}"); |
||
67 | } |
||
68 | |||
69 | // Let's not break filesystems which do not support chmod. |
||
70 | @chmod($path, 0666 & ~umask()); |
||
0 ignored issues
–
show
|
|||
71 | } |
||
72 | |||
73 | /** |
||
74 | * {@inheritDoc} |
||
75 | */ |
||
76 | public function evictClassMetadataFromCache(\ReflectionClass $class) |
||
77 | { |
||
78 | $path = $this->dir . '/' . strtr($class->name, '\\', '-') . '.cache.php'; |
||
79 | if (file_exists($path)) { |
||
80 | unlink($path); |
||
81 | } |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @return string |
||
86 | */ |
||
87 | private function getFileName(ClassMetadata $metadata) |
||
88 | { |
||
89 | return $this->dir . '/' . strtr($metadata->name, '\\', '-') . '.cache.php'; |
||
90 | } |
||
91 | |||
92 | private function createDirectory(string $dir) |
||
93 | { |
||
94 | if (!mkdir($dir, 0775, true) && !is_dir($dir)) { |
||
95 | throw new \RuntimeException("Can't create directory for cache at {$dir}"); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 |
If you suppress an error, we recommend checking for the error condition explicitly: