Exception
9 months ago
Logging
9 months ago
Persister
9 months ago
Region
9 months ago
AssociationCacheEntry.php
9 months ago
CacheConfiguration.php
9 months ago
CacheEntry.php
9 months ago
CacheException.php
9 months ago
CacheFactory.php
9 months ago
CacheKey.php
9 months ago
CollectionCacheEntry.php
9 months ago
CollectionCacheKey.php
9 months ago
CollectionHydrator.php
9 months ago
ConcurrentRegion.php
9 months ago
DefaultCache.php
9 months ago
DefaultCacheFactory.php
9 months ago
DefaultCollectionHydrator.php
9 months ago
DefaultEntityHydrator.php
9 months ago
DefaultQueryCache.php
9 months ago
EntityCacheEntry.php
9 months ago
EntityCacheKey.php
9 months ago
EntityHydrator.php
9 months ago
Lock.php
9 months ago
LockException.php
9 months ago
MultiGetRegion.php
9 months ago
QueryCache.php
9 months ago
QueryCacheEntry.php
9 months ago
QueryCacheKey.php
9 months ago
QueryCacheValidator.php
9 months ago
Region.php
9 months ago
RegionsConfiguration.php
9 months ago
TimestampCacheEntry.php
9 months ago
TimestampCacheKey.php
9 months ago
TimestampQueryCacheValidator.php
9 months ago
TimestampRegion.php
9 months ago
index.php
9 months ago
DefaultCacheFactory.php
146 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Cache; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\Common\Cache\Cache as LegacyCache; |
| 6 | use MailPoetVendor\Doctrine\Common\Cache\Psr6\CacheAdapter; |
| 7 | use MailPoetVendor\Doctrine\Deprecations\Deprecation; |
| 8 | use MailPoetVendor\Doctrine\ORM\Cache; |
| 9 | use MailPoetVendor\Doctrine\ORM\Cache\Persister\Collection\NonStrictReadWriteCachedCollectionPersister; |
| 10 | use MailPoetVendor\Doctrine\ORM\Cache\Persister\Collection\ReadOnlyCachedCollectionPersister; |
| 11 | use MailPoetVendor\Doctrine\ORM\Cache\Persister\Collection\ReadWriteCachedCollectionPersister; |
| 12 | use MailPoetVendor\Doctrine\ORM\Cache\Persister\Entity\NonStrictReadWriteCachedEntityPersister; |
| 13 | use MailPoetVendor\Doctrine\ORM\Cache\Persister\Entity\ReadOnlyCachedEntityPersister; |
| 14 | use MailPoetVendor\Doctrine\ORM\Cache\Persister\Entity\ReadWriteCachedEntityPersister; |
| 15 | use MailPoetVendor\Doctrine\ORM\Cache\Region\DefaultRegion; |
| 16 | use MailPoetVendor\Doctrine\ORM\Cache\Region\FileLockRegion; |
| 17 | use MailPoetVendor\Doctrine\ORM\Cache\Region\UpdateTimestampCache; |
| 18 | use MailPoetVendor\Doctrine\ORM\EntityManagerInterface; |
| 19 | use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadata; |
| 20 | use MailPoetVendor\Doctrine\ORM\Persisters\Collection\CollectionPersister; |
| 21 | use MailPoetVendor\Doctrine\ORM\Persisters\Entity\EntityPersister; |
| 22 | use InvalidArgumentException; |
| 23 | use LogicException; |
| 24 | use MailPoetVendor\Psr\Cache\CacheItemPoolInterface; |
| 25 | use TypeError; |
| 26 | use function assert; |
| 27 | use function get_debug_type; |
| 28 | use function sprintf; |
| 29 | use const DIRECTORY_SEPARATOR; |
| 30 | class DefaultCacheFactory implements CacheFactory |
| 31 | { |
| 32 | private $cacheItemPool; |
| 33 | private $regionsConfig; |
| 34 | private $timestampRegion; |
| 35 | private $regions = []; |
| 36 | private $fileLockRegionDirectory; |
| 37 | public function __construct(RegionsConfiguration $cacheConfig, $cacheItemPool) |
| 38 | { |
| 39 | if ($cacheItemPool instanceof LegacyCache) { |
| 40 | Deprecation::trigger('doctrine/orm', 'https://github.com/doctrine/orm/pull/9322', 'Passing an instance of %s to %s is deprecated, pass a %s instead.', get_debug_type($cacheItemPool), __METHOD__, CacheItemPoolInterface::class); |
| 41 | $this->cacheItemPool = CacheAdapter::wrap($cacheItemPool); |
| 42 | } elseif (!$cacheItemPool instanceof CacheItemPoolInterface) { |
| 43 | throw new TypeError(sprintf('%s: Parameter #2 is expected to be an instance of %s, got %s.', __METHOD__, CacheItemPoolInterface::class, get_debug_type($cacheItemPool))); |
| 44 | } else { |
| 45 | $this->cacheItemPool = $cacheItemPool; |
| 46 | } |
| 47 | $this->regionsConfig = $cacheConfig; |
| 48 | } |
| 49 | public function setFileLockRegionDirectory($fileLockRegionDirectory) |
| 50 | { |
| 51 | $this->fileLockRegionDirectory = (string) $fileLockRegionDirectory; |
| 52 | } |
| 53 | public function getFileLockRegionDirectory() |
| 54 | { |
| 55 | return $this->fileLockRegionDirectory; |
| 56 | } |
| 57 | public function setRegion(Region $region) |
| 58 | { |
| 59 | $this->regions[$region->getName()] = $region; |
| 60 | } |
| 61 | public function setTimestampRegion(TimestampRegion $region) |
| 62 | { |
| 63 | $this->timestampRegion = $region; |
| 64 | } |
| 65 | public function buildCachedEntityPersister(EntityManagerInterface $em, EntityPersister $persister, ClassMetadata $metadata) |
| 66 | { |
| 67 | assert($metadata->cache !== null); |
| 68 | $region = $this->getRegion($metadata->cache); |
| 69 | $usage = $metadata->cache['usage']; |
| 70 | if ($usage === ClassMetadata::CACHE_USAGE_READ_ONLY) { |
| 71 | return new ReadOnlyCachedEntityPersister($persister, $region, $em, $metadata); |
| 72 | } |
| 73 | if ($usage === ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE) { |
| 74 | return new NonStrictReadWriteCachedEntityPersister($persister, $region, $em, $metadata); |
| 75 | } |
| 76 | if ($usage === ClassMetadata::CACHE_USAGE_READ_WRITE) { |
| 77 | if (!$region instanceof ConcurrentRegion) { |
| 78 | throw new InvalidArgumentException(sprintf('Unable to use access strategy type of [%s] without a ConcurrentRegion', $usage)); |
| 79 | } |
| 80 | return new ReadWriteCachedEntityPersister($persister, $region, $em, $metadata); |
| 81 | } |
| 82 | throw new InvalidArgumentException(sprintf('Unrecognized access strategy type [%s]', $usage)); |
| 83 | } |
| 84 | public function buildCachedCollectionPersister(EntityManagerInterface $em, CollectionPersister $persister, array $mapping) |
| 85 | { |
| 86 | assert(isset($mapping['cache'])); |
| 87 | $usage = $mapping['cache']['usage']; |
| 88 | $region = $this->getRegion($mapping['cache']); |
| 89 | if ($usage === ClassMetadata::CACHE_USAGE_READ_ONLY) { |
| 90 | return new ReadOnlyCachedCollectionPersister($persister, $region, $em, $mapping); |
| 91 | } |
| 92 | if ($usage === ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE) { |
| 93 | return new NonStrictReadWriteCachedCollectionPersister($persister, $region, $em, $mapping); |
| 94 | } |
| 95 | if ($usage === ClassMetadata::CACHE_USAGE_READ_WRITE) { |
| 96 | if (!$region instanceof ConcurrentRegion) { |
| 97 | throw new InvalidArgumentException(sprintf('Unable to use access strategy type of [%s] without a ConcurrentRegion', $usage)); |
| 98 | } |
| 99 | return new ReadWriteCachedCollectionPersister($persister, $region, $em, $mapping); |
| 100 | } |
| 101 | throw new InvalidArgumentException(sprintf('Unrecognized access strategy type [%s]', $usage)); |
| 102 | } |
| 103 | public function buildQueryCache(EntityManagerInterface $em, $regionName = null) |
| 104 | { |
| 105 | return new DefaultQueryCache($em, $this->getRegion(['region' => $regionName ?: Cache::DEFAULT_QUERY_REGION_NAME, 'usage' => ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE])); |
| 106 | } |
| 107 | public function buildCollectionHydrator(EntityManagerInterface $em, array $mapping) |
| 108 | { |
| 109 | return new DefaultCollectionHydrator($em); |
| 110 | } |
| 111 | public function buildEntityHydrator(EntityManagerInterface $em, ClassMetadata $metadata) |
| 112 | { |
| 113 | return new DefaultEntityHydrator($em); |
| 114 | } |
| 115 | public function getRegion(array $cache) |
| 116 | { |
| 117 | if (isset($this->regions[$cache['region']])) { |
| 118 | return $this->regions[$cache['region']]; |
| 119 | } |
| 120 | $name = $cache['region']; |
| 121 | $lifetime = $this->regionsConfig->getLifetime($cache['region']); |
| 122 | $region = new DefaultRegion($name, $this->cacheItemPool, $lifetime); |
| 123 | if ($cache['usage'] === ClassMetadata::CACHE_USAGE_READ_WRITE) { |
| 124 | if ($this->fileLockRegionDirectory === '' || $this->fileLockRegionDirectory === null) { |
| 125 | throw new LogicException('If you want to use a "READ_WRITE" cache an implementation of "MailPoetVendor\\Doctrine\\ORM\\Cache\\ConcurrentRegion" is required, ' . 'The default implementation provided by doctrine is "MailPoetVendor\\Doctrine\\ORM\\Cache\\Region\\FileLockRegion" if you want to use it please provide a valid directory, DefaultCacheFactory#setFileLockRegionDirectory(). '); |
| 126 | } |
| 127 | $directory = $this->fileLockRegionDirectory . DIRECTORY_SEPARATOR . $cache['region']; |
| 128 | $region = new FileLockRegion($region, $directory, (string) $this->regionsConfig->getLockLifetime($cache['region'])); |
| 129 | } |
| 130 | return $this->regions[$cache['region']] = $region; |
| 131 | } |
| 132 | public function getTimestampRegion() |
| 133 | { |
| 134 | if ($this->timestampRegion === null) { |
| 135 | $name = Cache::DEFAULT_TIMESTAMP_REGION_NAME; |
| 136 | $lifetime = $this->regionsConfig->getLifetime($name); |
| 137 | $this->timestampRegion = new UpdateTimestampCache($name, $this->cacheItemPool, $lifetime); |
| 138 | } |
| 139 | return $this->timestampRegion; |
| 140 | } |
| 141 | public function createCache(EntityManagerInterface $entityManager) |
| 142 | { |
| 143 | return new DefaultCache($entityManager); |
| 144 | } |
| 145 | } |
| 146 |