1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Cache; |
4
|
|
|
|
5
|
|
|
use const PHP_EOL; |
6
|
|
|
use function fclose; |
7
|
|
|
use function fgets; |
8
|
|
|
use function fopen; |
9
|
|
|
use function is_file; |
10
|
|
|
use function serialize; |
11
|
|
|
use function time; |
12
|
|
|
use function unserialize; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Filesystem cache driver. |
16
|
|
|
*/ |
17
|
|
|
class FilesystemCache extends FileCache |
18
|
|
|
{ |
19
|
|
|
public const EXTENSION = '.doctrinecache.data'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
|
|
public function __construct($directory, $extension = self::EXTENSION, $umask = 0002) |
25
|
|
|
{ |
26
|
|
|
parent::__construct($directory, $extension, $umask); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
|
|
protected function doFetch($id) |
33
|
|
|
{ |
34
|
|
|
$data = ''; |
35
|
83 |
|
$lifetime = -1; |
36
|
|
|
$filename = $this->getFilename($id); |
37
|
83 |
|
|
38
|
83 |
|
if (! is_file($filename)) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$resource = fopen($filename, 'r'); |
43
|
81 |
|
$line = fgets($resource); |
|
|
|
|
44
|
|
|
|
45
|
81 |
|
if ($line !== false) { |
46
|
81 |
|
$lifetime = (int) $line; |
47
|
81 |
|
} |
48
|
|
|
|
49
|
81 |
|
if ($lifetime !== 0 && $lifetime < time()) { |
50
|
81 |
|
fclose($resource); |
|
|
|
|
51
|
|
|
|
52
|
|
|
return false; |
53
|
68 |
|
} |
54
|
|
|
|
55
|
68 |
|
while (($line = fgets($resource)) !== false) { |
56
|
68 |
|
$data .= $line; |
57
|
|
|
} |
58
|
|
|
|
59
|
68 |
|
fclose($resource); |
60
|
|
|
|
61
|
|
|
return unserialize($data); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
68 |
|
* {@inheritdoc} |
66
|
68 |
|
*/ |
67
|
|
|
protected function doContains($id) |
68
|
|
|
{ |
69
|
68 |
|
$lifetime = -1; |
70
|
|
|
$filename = $this->getFilename($id); |
71
|
68 |
|
|
72
|
|
|
if (! is_file($filename)) { |
73
|
|
|
return false; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$resource = fopen($filename, 'r'); |
77
|
72 |
|
$line = fgets($resource); |
|
|
|
|
78
|
|
|
|
79
|
72 |
|
if ($line !== false) { |
80
|
72 |
|
$lifetime = (int) $line; |
81
|
|
|
} |
82
|
72 |
|
|
83
|
51 |
|
fclose($resource); |
|
|
|
|
84
|
|
|
|
85
|
|
|
return $lifetime === 0 || $lifetime > time(); |
86
|
67 |
|
} |
87
|
|
|
|
88
|
67 |
|
/** |
89
|
67 |
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
protected function doSave($id, $data, $lifeTime = 0) |
92
|
67 |
|
{ |
93
|
|
|
if ($lifeTime > 0) { |
94
|
67 |
|
$lifeTime = time() + $lifeTime; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
$data = serialize($data); |
98
|
|
|
$filename = $this->getFilename($id); |
99
|
|
|
|
100
|
79 |
|
return $this->writeFile($filename, $lifeTime . PHP_EOL . $data); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|