| 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 |
|
| 18 |
namespace Google\Auth\Cache; |
| 19 |
|
| 20 |
use ErrorException; |
| 21 |
use Psr\Cache\CacheItemInterface; |
| 22 |
use Psr\Cache\CacheItemPoolInterface; |
| 23 |
|
| 24 |
class 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 |
// Suppress the error for when the directory already exists because of a |
| 50 |
// race condition |
| 51 |
if (!@mkdir($this->cachePath, 0777, true) && !is_dir($this->cachePath)) { |
| 52 |
throw new ErrorException("Cache folder couldn't be created."); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* {@inheritdoc} |
| 58 |
*/ |
| 59 |
public function getItem(string $key): CacheItemInterface |
| 60 |
{ |
| 61 |
if (!$this->validKey($key)) { |
| 62 |
throw new InvalidArgumentException("The key '$key' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|"); |
| 63 |
} |
| 64 |
|
| 65 |
$item = new TypedItem($key); |
| 66 |
|
| 67 |
$itemPath = $this->cacheFilePath($key); |
| 68 |
|
| 69 |
if (!file_exists($itemPath)) { |
| 70 |
return $item; |
| 71 |
} |
| 72 |
|
| 73 |
$serializedItem = file_get_contents($itemPath); |
| 74 |
|
| 75 |
if ($serializedItem === false) { |
| 76 |
return $item; |
| 77 |
} |
| 78 |
|
| 79 |
$item->set(unserialize($serializedItem)); |
| 80 |
|
| 81 |
return $item; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* {@inheritdoc} |
| 86 |
* |
| 87 |
* @return iterable<CacheItemInterface> An iterable object containing all the |
| 88 |
* A traversable collection of Cache Items keyed by the cache keys of |
| 89 |
* each item. A Cache item will be returned for each key, even if that |
| 90 |
* key is not found. However, if no keys are specified then an empty |
| 91 |
* traversable MUST be returned instead. |
| 92 |
*/ |
| 93 |
public function getItems(array $keys = []): iterable |
| 94 |
{ |
| 95 |
$result = []; |
| 96 |
|
| 97 |
foreach ($keys as $key) { |
| 98 |
$result[$key] = $this->getItem($key); |
| 99 |
} |
| 100 |
|
| 101 |
return $result; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* {@inheritdoc} |
| 106 |
*/ |
| 107 |
public function save(CacheItemInterface $item): bool |
| 108 |
{ |
| 109 |
if (!$this->validKey($item->getKey())) { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
$itemPath = $this->cacheFilePath($item->getKey()); |
| 114 |
$serializedItem = serialize($item->get()); |
| 115 |
|
| 116 |
$result = file_put_contents($itemPath, $serializedItem, LOCK_EX); |
| 117 |
|
| 118 |
// 0 bytes write is considered a successful operation |
| 119 |
if ($result === false) { |
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* {@inheritdoc} |
| 128 |
*/ |
| 129 |
public function hasItem(string $key): bool |
| 130 |
{ |
| 131 |
return $this->getItem($key)->isHit(); |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* {@inheritdoc} |
| 136 |
*/ |
| 137 |
public function clear(): bool |
| 138 |
{ |
| 139 |
$this->buffer = []; |
| 140 |
|
| 141 |
if (!is_dir($this->cachePath)) { |
| 142 |
return false; |
| 143 |
} |
| 144 |
|
| 145 |
$files = scandir($this->cachePath); |
| 146 |
if (!$files) { |
| 147 |
return false; |
| 148 |
} |
| 149 |
|
| 150 |
foreach ($files as $fileName) { |
| 151 |
if ($fileName === '.' || $fileName === '..') { |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
if (!unlink($this->cachePath . '/' . $fileName)) { |
| 156 |
return false; |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
return true; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* {@inheritdoc} |
| 165 |
*/ |
| 166 |
public function deleteItem(string $key): bool |
| 167 |
{ |
| 168 |
if (!$this->validKey($key)) { |
| 169 |
throw new InvalidArgumentException("The key '$key' is not valid. The key should follow the pattern |^[a-zA-Z0-9_\.! ]+$|"); |
| 170 |
} |
| 171 |
|
| 172 |
$itemPath = $this->cacheFilePath($key); |
| 173 |
|
| 174 |
if (!file_exists($itemPath)) { |
| 175 |
return true; |
| 176 |
} |
| 177 |
|
| 178 |
return unlink($itemPath); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* {@inheritdoc} |
| 183 |
*/ |
| 184 |
public function deleteItems(array $keys): bool |
| 185 |
{ |
| 186 |
$result = true; |
| 187 |
|
| 188 |
foreach ($keys as $key) { |
| 189 |
if (!$this->deleteItem($key)) { |
| 190 |
$result = false; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
return $result; |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* {@inheritdoc} |
| 199 |
*/ |
| 200 |
public function saveDeferred(CacheItemInterface $item): bool |
| 201 |
{ |
| 202 |
array_push($this->buffer, $item); |
| 203 |
|
| 204 |
return true; |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* {@inheritdoc} |
| 209 |
*/ |
| 210 |
public function commit(): bool |
| 211 |
{ |
| 212 |
$result = true; |
| 213 |
|
| 214 |
foreach ($this->buffer as $item) { |
| 215 |
if (!$this->save($item)) { |
| 216 |
$result = false; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
return $result; |
| 221 |
} |
| 222 |
|
| 223 |
private function cacheFilePath(string $key): string |
| 224 |
{ |
| 225 |
return $this->cachePath . '/' . $key; |
| 226 |
} |
| 227 |
|
| 228 |
private function validKey(string $key): bool |
| 229 |
{ |
| 230 |
return (bool) preg_match('|^[a-zA-Z0-9_\.]+$|', $key); |
| 231 |
} |
| 232 |
} |
| 233 |
|