1 | <?php |
||
11 | class FileAdapter implements CacheAdapterInterface |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | private $directory; |
||
17 | /** |
||
18 | * @var null|int |
||
19 | */ |
||
20 | private $chmod; |
||
21 | /** |
||
22 | * @var null|string |
||
23 | */ |
||
24 | private $directorySeparator; |
||
25 | /** |
||
26 | * @var null|int |
||
27 | */ |
||
28 | private $ttl; |
||
29 | /** |
||
30 | * @var SerializerInterface |
||
31 | */ |
||
32 | private $serializer; |
||
33 | |||
34 | /** |
||
35 | * @param $directory |
||
36 | * @param SerializerInterface $serializer |
||
37 | * @param null $chmod |
||
38 | * @param null $directorySeparator |
||
39 | * @param null $ttl |
||
40 | */ |
||
41 | 9 | public function __construct( |
|
54 | |||
55 | /** |
||
56 | * @param $key |
||
57 | * @param $value |
||
58 | */ |
||
59 | 8 | public function set($key, $value) |
|
60 | { |
||
61 | 8 | $file = $this->getFilename($key); |
|
62 | 8 | file_put_contents($file, $this->serializer->serialize($value)); |
|
63 | |||
64 | 8 | if ($this->chmod !== null) { |
|
65 | 5 | chmod($file, $this->chmod); |
|
66 | } |
||
67 | 8 | } |
|
68 | |||
69 | /** |
||
70 | * @param $key |
||
71 | * @return null|mixed |
||
72 | */ |
||
73 | 7 | public function get($key) |
|
81 | |||
82 | /** |
||
83 | * @param $key |
||
84 | */ |
||
85 | 4 | public function delete($key) |
|
86 | { |
||
87 | 4 | if (strpos($key, '*') !== false) { |
|
88 | 3 | $this->deleteGlob($key); |
|
89 | 3 | return; |
|
90 | } |
||
91 | |||
92 | 1 | $file = $this->getFilename($key); |
|
93 | 1 | if ($this->exists($file)) { |
|
94 | 1 | unlink($file); |
|
95 | } |
||
96 | 1 | } |
|
97 | |||
98 | /** |
||
99 | * @param $pattern |
||
100 | */ |
||
101 | 3 | private function deleteGlob($pattern) |
|
102 | { |
||
103 | 3 | list($directory, $file) = $this->getDirectoryAndFile($pattern); |
|
104 | 3 | $list = new \GlobIterator($directory . '/' . $file); |
|
105 | |||
106 | 3 | foreach ($list as $cacheItem) { |
|
107 | 3 | if ($cacheItem->isDir()) { |
|
108 | continue; |
||
109 | } |
||
110 | 3 | unlink($cacheItem->getPathName()); |
|
111 | } |
||
112 | 3 | } |
|
113 | |||
114 | /** |
||
115 | * @param $file |
||
116 | * @return bool |
||
117 | */ |
||
118 | 7 | private function exists($file) |
|
122 | |||
123 | /** |
||
124 | * @param $file |
||
125 | * @return bool |
||
126 | */ |
||
127 | 6 | private function valid($file) |
|
131 | |||
132 | /** |
||
133 | * @param $key |
||
134 | * @return string |
||
135 | */ |
||
136 | 9 | private function getFilename($key) |
|
141 | |||
142 | /** |
||
143 | * @param $key |
||
144 | * @return array |
||
145 | */ |
||
146 | 9 | private function getDirectoryAndFile($key) |
|
161 | |||
162 | /** |
||
163 | * @param $directory |
||
164 | */ |
||
165 | 2 | private function createSubDirectoryIfNotExists($directory) |
|
174 | } |
||
175 |