mailpoet
/
vendor-prefixed
/
doctrine
/
orm
/
src
/
Cache
/
Persister
/
Collection
/
ReadWriteCachedCollectionPersister.php
AbstractCollectionPersister.php
9 months ago
CachedCollectionPersister.php
9 months ago
NonStrictReadWriteCachedCollectionPersister.php
9 months ago
ReadOnlyCachedCollectionPersister.php
9 months ago
ReadWriteCachedCollectionPersister.php
9 months ago
index.php
9 months ago
ReadWriteCachedCollectionPersister.php
74 lines
| 1 | <?php |
| 2 | declare (strict_types=1); |
| 3 | namespace MailPoetVendor\Doctrine\ORM\Cache\Persister\Collection; |
| 4 | if (!defined('ABSPATH')) exit; |
| 5 | use MailPoetVendor\Doctrine\ORM\Cache\CollectionCacheKey; |
| 6 | use MailPoetVendor\Doctrine\ORM\Cache\ConcurrentRegion; |
| 7 | use MailPoetVendor\Doctrine\ORM\EntityManagerInterface; |
| 8 | use MailPoetVendor\Doctrine\ORM\Mapping\ClassMetadata; |
| 9 | use MailPoetVendor\Doctrine\ORM\PersistentCollection; |
| 10 | use MailPoetVendor\Doctrine\ORM\Persisters\Collection\CollectionPersister; |
| 11 | use function spl_object_id; |
| 12 | class ReadWriteCachedCollectionPersister extends AbstractCollectionPersister |
| 13 | { |
| 14 | public function __construct(CollectionPersister $persister, ConcurrentRegion $region, EntityManagerInterface $em, array $association) |
| 15 | { |
| 16 | parent::__construct($persister, $region, $em, $association); |
| 17 | } |
| 18 | public function afterTransactionComplete() |
| 19 | { |
| 20 | if (isset($this->queuedCache['update'])) { |
| 21 | foreach ($this->queuedCache['update'] as $item) { |
| 22 | $this->region->evict($item['key']); |
| 23 | } |
| 24 | } |
| 25 | if (isset($this->queuedCache['delete'])) { |
| 26 | foreach ($this->queuedCache['delete'] as $item) { |
| 27 | $this->region->evict($item['key']); |
| 28 | } |
| 29 | } |
| 30 | $this->queuedCache = []; |
| 31 | } |
| 32 | public function afterTransactionRolledBack() |
| 33 | { |
| 34 | if (isset($this->queuedCache['update'])) { |
| 35 | foreach ($this->queuedCache['update'] as $item) { |
| 36 | $this->region->evict($item['key']); |
| 37 | } |
| 38 | } |
| 39 | if (isset($this->queuedCache['delete'])) { |
| 40 | foreach ($this->queuedCache['delete'] as $item) { |
| 41 | $this->region->evict($item['key']); |
| 42 | } |
| 43 | } |
| 44 | $this->queuedCache = []; |
| 45 | } |
| 46 | public function delete(PersistentCollection $collection) |
| 47 | { |
| 48 | $ownerId = $this->uow->getEntityIdentifier($collection->getOwner()); |
| 49 | $key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId, $this->filters->getHash()); |
| 50 | $lock = $this->region->lock($key); |
| 51 | $this->persister->delete($collection); |
| 52 | if ($lock === null) { |
| 53 | return; |
| 54 | } |
| 55 | $this->queuedCache['delete'][spl_object_id($collection)] = ['key' => $key, 'lock' => $lock]; |
| 56 | } |
| 57 | public function update(PersistentCollection $collection) |
| 58 | { |
| 59 | $isInitialized = $collection->isInitialized(); |
| 60 | $isDirty = $collection->isDirty(); |
| 61 | if (!$isInitialized && !$isDirty) { |
| 62 | return; |
| 63 | } |
| 64 | $this->persister->update($collection); |
| 65 | $ownerId = $this->uow->getEntityIdentifier($collection->getOwner()); |
| 66 | $key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId, $this->filters->getHash()); |
| 67 | $lock = $this->region->lock($key); |
| 68 | if ($lock === null) { |
| 69 | return; |
| 70 | } |
| 71 | $this->queuedCache['update'][spl_object_id($collection)] = ['key' => $key, 'lock' => $lock]; |
| 72 | } |
| 73 | } |
| 74 |