| 1 |
<?php |
| 2 |
/* |
| 3 |
* Copyright 2016 Google Inc. |
| 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 Psr\Cache\CacheItemInterface; |
| 21 |
use Psr\Cache\CacheItemPoolInterface; |
| 22 |
|
| 23 |
/** |
| 24 |
* Simple in-memory cache implementation. |
| 25 |
*/ |
| 26 |
final class MemoryCacheItemPool implements CacheItemPoolInterface |
| 27 |
{ |
| 28 |
/** |
| 29 |
* @var CacheItemInterface[] |
| 30 |
*/ |
| 31 |
private $items; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var CacheItemInterface[] |
| 35 |
*/ |
| 36 |
private $deferredItems; |
| 37 |
|
| 38 |
/** |
| 39 |
* {@inheritdoc} |
| 40 |
* |
| 41 |
* @return CacheItemInterface The corresponding Cache Item. |
| 42 |
*/ |
| 43 |
public function getItem($key): CacheItemInterface |
| 44 |
{ |
| 45 |
return current($this->getItems([$key])); // @phpstan-ignore-line |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* {@inheritdoc} |
| 50 |
* |
| 51 |
* @return iterable<CacheItemInterface> |
| 52 |
* A traversable collection of Cache Items keyed by the cache keys of |
| 53 |
* each item. A Cache item will be returned for each key, even if that |
| 54 |
* key is not found. However, if no keys are specified then an empty |
| 55 |
* traversable MUST be returned instead. |
| 56 |
*/ |
| 57 |
public function getItems(array $keys = []): iterable |
| 58 |
{ |
| 59 |
$items = []; |
| 60 |
foreach ($keys as $key) { |
| 61 |
$items[$key] = $this->hasItem($key) ? clone $this->items[$key] : new TypedItem($key); |
| 62 |
} |
| 63 |
|
| 64 |
return $items; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* {@inheritdoc} |
| 69 |
* |
| 70 |
* @return bool |
| 71 |
* True if item exists in the cache, false otherwise. |
| 72 |
*/ |
| 73 |
public function hasItem($key): bool |
| 74 |
{ |
| 75 |
$this->isValidKey($key); |
| 76 |
|
| 77 |
return isset($this->items[$key]) && $this->items[$key]->isHit(); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* {@inheritdoc} |
| 82 |
* |
| 83 |
* @return bool |
| 84 |
* True if the pool was successfully cleared. False if there was an error. |
| 85 |
*/ |
| 86 |
public function clear(): bool |
| 87 |
{ |
| 88 |
$this->items = []; |
| 89 |
$this->deferredItems = []; |
| 90 |
|
| 91 |
return true; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* {@inheritdoc} |
| 96 |
* |
| 97 |
* @return bool |
| 98 |
* True if the item was successfully removed. False if there was an error. |
| 99 |
*/ |
| 100 |
public function deleteItem($key): bool |
| 101 |
{ |
| 102 |
return $this->deleteItems([$key]); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* {@inheritdoc} |
| 107 |
* |
| 108 |
* @return bool |
| 109 |
* True if the items were successfully removed. False if there was an error. |
| 110 |
*/ |
| 111 |
public function deleteItems(array $keys): bool |
| 112 |
{ |
| 113 |
array_walk($keys, [$this, 'isValidKey']); |
| 114 |
|
| 115 |
foreach ($keys as $key) { |
| 116 |
unset($this->items[$key]); |
| 117 |
} |
| 118 |
|
| 119 |
return true; |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* {@inheritdoc} |
| 124 |
* |
| 125 |
* @return bool |
| 126 |
* True if the item was successfully persisted. False if there was an error. |
| 127 |
*/ |
| 128 |
public function save(CacheItemInterface $item): bool |
| 129 |
{ |
| 130 |
$this->items[$item->getKey()] = $item; |
| 131 |
|
| 132 |
return true; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* {@inheritdoc} |
| 137 |
* |
| 138 |
* @return bool |
| 139 |
* False if the item could not be queued or if a commit was attempted and failed. True otherwise. |
| 140 |
*/ |
| 141 |
public function saveDeferred(CacheItemInterface $item): bool |
| 142 |
{ |
| 143 |
$this->deferredItems[$item->getKey()] = $item; |
| 144 |
|
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* {@inheritdoc} |
| 150 |
* |
| 151 |
* @return bool |
| 152 |
* True if all not-yet-saved items were successfully saved or there were none. False otherwise. |
| 153 |
*/ |
| 154 |
public function commit(): bool |
| 155 |
{ |
| 156 |
foreach ($this->deferredItems as $item) { |
| 157 |
$this->save($item); |
| 158 |
} |
| 159 |
|
| 160 |
$this->deferredItems = []; |
| 161 |
|
| 162 |
return true; |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* Determines if the provided key is valid. |
| 167 |
* |
| 168 |
* @param string $key |
| 169 |
* @return bool |
| 170 |
* @throws InvalidArgumentException |
| 171 |
*/ |
| 172 |
private function isValidKey($key) |
| 173 |
{ |
| 174 |
$invalidCharacters = '{}()/\\\\@:'; |
| 175 |
|
| 176 |
if (!is_string($key) || preg_match("#[$invalidCharacters]#", $key)) { |
| 177 |
throw new InvalidArgumentException('The provided key is not valid: ' . var_export($key, true)); |
| 178 |
} |
| 179 |
|
| 180 |
return true; |
| 181 |
} |
| 182 |
} |
| 183 |
|