source: vendor/google/auth/src/Cache/FileSystemCacheItemPool.php@ f9c482b

Last change on this file since f9c482b was f9c482b, checked in by Vlado 222039 <vlado.popovski@…>, 7 days ago

Upload new project files

  • Property mode set to 100644
File size: 5.3 KB
RevLine 
[f9c482b]1<?php
2/**
3 * Copyright 2024 Google Inc. All Rights Reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18namespace Google\Auth\Cache;
19
20use ErrorException;
21use Psr\Cache\CacheItemInterface;
22use Psr\Cache\CacheItemPoolInterface;
23
24class FileSystemCacheItemPool implements CacheItemPoolInterface
25{
26 /**
27 * @var string
28 */
29 private string $cachePath;
30
31 /**
32 * @var array<CacheItemInterface>
33 */
34 private array $buffer = [];
35
36 /**
37 * Creates a FileSystemCacheItemPool cache that stores values in local storage
38 *
39 * @param string $path The string representation of the path where the cache will store the serialized objects.
40 */
41 public function __construct(string $path)
42 {
43 $this->cachePath = $path;
44
45 if (is_dir($this->cachePath)) {
46 return;
47 }
48
49 if (!mkdir($this->cachePath)) {
50 throw new ErrorException("Cache folder couldn't be created.");
51 }
52 }
53
54 /**
55 * {@inheritdoc}
56 */
57 public function getItem(string $key): CacheItemInterface
58 {
59 if (!$this->validKey($key)) {
60 throw new InvalidArgumentException("The key '$key' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|");
61 }
62
63 $item = new TypedItem($key);
64
65 $itemPath = $this->cacheFilePath($key);
66
67 if (!file_exists($itemPath)) {
68 return $item;
69 }
70
71 $serializedItem = file_get_contents($itemPath);
72
73 if ($serializedItem === false) {
74 return $item;
75 }
76
77 $item->set(unserialize($serializedItem));
78
79 return $item;
80 }
81
82 /**
83 * {@inheritdoc}
84 *
85 * @return iterable<CacheItemInterface> An iterable object containing all the
86 * A traversable collection of Cache Items keyed by the cache keys of
87 * each item. A Cache item will be returned for each key, even if that
88 * key is not found. However, if no keys are specified then an empty
89 * traversable MUST be returned instead.
90 */
91 public function getItems(array $keys = []): iterable
92 {
93 $result = [];
94
95 foreach ($keys as $key) {
96 $result[$key] = $this->getItem($key);
97 }
98
99 return $result;
100 }
101
102 /**
103 * {@inheritdoc}
104 */
105 public function save(CacheItemInterface $item): bool
106 {
107 if (!$this->validKey($item->getKey())) {
108 return false;
109 }
110
111 $itemPath = $this->cacheFilePath($item->getKey());
112 $serializedItem = serialize($item->get());
113
114 $result = file_put_contents($itemPath, $serializedItem);
115
116 // 0 bytes write is considered a successful operation
117 if ($result === false) {
118 return false;
119 }
120
121 return true;
122 }
123
124 /**
125 * {@inheritdoc}
126 */
127 public function hasItem(string $key): bool
128 {
129 return $this->getItem($key)->isHit();
130 }
131
132 /**
133 * {@inheritdoc}
134 */
135 public function clear(): bool
136 {
137 $this->buffer = [];
138
139 if (!is_dir($this->cachePath)) {
140 return false;
141 }
142
143 $files = scandir($this->cachePath);
144 if (!$files) {
145 return false;
146 }
147
148 foreach ($files as $fileName) {
149 if ($fileName === '.' || $fileName === '..') {
150 continue;
151 }
152
153 if (!unlink($this->cachePath . '/' . $fileName)) {
154 return false;
155 }
156 }
157
158 return true;
159 }
160
161 /**
162 * {@inheritdoc}
163 */
164 public function deleteItem(string $key): bool
165 {
166 if (!$this->validKey($key)) {
167 throw new InvalidArgumentException("The key '$key' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|");
168 }
169
170 $itemPath = $this->cacheFilePath($key);
171
172 if (!file_exists($itemPath)) {
173 return true;
174 }
175
176 return unlink($itemPath);
177 }
178
179 /**
180 * {@inheritdoc}
181 */
182 public function deleteItems(array $keys): bool
183 {
184 $result = true;
185
186 foreach ($keys as $key) {
187 if (!$this->deleteItem($key)) {
188 $result = false;
189 }
190 }
191
192 return $result;
193 }
194
195 /**
196 * {@inheritdoc}
197 */
198 public function saveDeferred(CacheItemInterface $item): bool
199 {
200 array_push($this->buffer, $item);
201
202 return true;
203 }
204
205 /**
206 * {@inheritdoc}
207 */
208 public function commit(): bool
209 {
210 $result = true;
211
212 foreach ($this->buffer as $item) {
213 if (!$this->save($item)) {
214 $result = false;
215 }
216 }
217
218 return $result;
219 }
220
221 private function cacheFilePath(string $key): string
222 {
223 return $this->cachePath . '/' . $key;
224 }
225
226 private function validKey(string $key): bool
227 {
228 return (bool) preg_match('|^[a-zA-Z0-9_\.]+$|', $key);
229 }
230}
Note: See TracBrowser for help on using the repository browser.