Annotations
3 years ago
EntityTraits
2 years ago
EventListeners
2 years ago
Types
3 years ago
Validator
3 years ago
ArrayCache.php
3 years ago
CacheOnlyMappingDriver.php
3 years ago
ConfigurationFactory.php
3 years ago
ConnectionFactory.php
3 years ago
EntityManagerFactory.php
2 years ago
MetadataCache.php
2 years ago
PSRArrayCache.php
3 years ago
PSRCacheInvalidArgumentException.php
3 years ago
PSRCacheItem.php
3 years ago
PSRMetadataCache.php
3 years ago
ProxyClassNameResolver.php
3 years ago
Repository.php
2 years ago
SerializableConnection.php
3 years ago
TablePrefixMetadataFactory.php
2 years ago
index.php
3 years ago
PSRMetadataCache.php
112 lines
| 1 | <?php // phpcs:ignore SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing |
| 2 | |
| 3 | namespace MailPoet\Doctrine; |
| 4 | |
| 5 | if (!defined('ABSPATH')) exit; |
| 6 | |
| 7 | |
| 8 | use MailPoetVendor\Psr\Cache\CacheItemInterface; |
| 9 | use MailPoetVendor\Psr\Cache\CacheItemPoolInterface; |
| 10 | |
| 11 | class PSRMetadataCache implements CacheItemPoolInterface { |
| 12 | /** @var MetadataCache */ |
| 13 | private $metadataCache; |
| 14 | |
| 15 | public function __construct( |
| 16 | string $dir, |
| 17 | bool $isReadOnly |
| 18 | ) { |
| 19 | $this->metadataCache = new MetadataCache($dir, $isReadOnly); |
| 20 | } |
| 21 | |
| 22 | /** |
| 23 | * @inheritDoc |
| 24 | */ |
| 25 | public function getItem($key): CacheItemInterface { |
| 26 | if (!$this->hasItem($key)) { |
| 27 | return new PSRCacheItem($key, false); |
| 28 | } |
| 29 | $item = new PSRCacheItem($key, true); |
| 30 | $item->set($this->metadataCache->fetch($key)); |
| 31 | return $item; |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * @inheritDoc |
| 36 | */ |
| 37 | public function getItems(array $keys = []) { |
| 38 | if (empty($keys)) { |
| 39 | return []; |
| 40 | } |
| 41 | $foundItems = []; |
| 42 | // no internal array function supports this sort of mapping: needs to be iterative |
| 43 | // this filters and combines keys in one pass |
| 44 | foreach ($keys as $key) { |
| 45 | if (!is_string($key)) { |
| 46 | throw new PSRCacheInvalidArgumentException('Invalid key'); |
| 47 | } |
| 48 | $foundItems[$key] = $this->getItem($key); |
| 49 | } |
| 50 | return $foundItems; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * @inheritDoc |
| 55 | */ |
| 56 | public function hasItem($key): bool { |
| 57 | return $this->metadataCache->contains($key); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * @inheritDoc |
| 62 | */ |
| 63 | public function clear(): bool { |
| 64 | return $this->metadataCache->flushAll(); |
| 65 | } |
| 66 | |
| 67 | /** |
| 68 | * @inheritDoc |
| 69 | */ |
| 70 | public function deleteItem($key): bool { |
| 71 | return $this->metadataCache->delete($key); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * @inheritDoc |
| 76 | */ |
| 77 | public function deleteItems(array $keys): bool { |
| 78 | if (empty($keys)) { |
| 79 | return true; |
| 80 | } |
| 81 | foreach ($keys as $key) { |
| 82 | $this->deleteItem($key); |
| 83 | } |
| 84 | return true; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * @inheritDoc |
| 89 | */ |
| 90 | public function save(CacheItemInterface $item) { |
| 91 | try { |
| 92 | return $this->metadataCache->save($item->getKey(), $item->get()); |
| 93 | } catch (\RuntimeException $e) { |
| 94 | throw new PSRCacheInvalidArgumentException($e->getMessage(), $e->getCode(), $e); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * @inheritDoc |
| 100 | */ |
| 101 | public function saveDeferred(CacheItemInterface $item) { |
| 102 | return $this->save($item); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * @inheritDoc |
| 107 | */ |
| 108 | public function commit(): bool { |
| 109 | return true; |
| 110 | } |
| 111 | } |
| 112 |