PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/App/Library/vendor/doctrine/orm/lib/Doctrine/ORM/Cache/Persister/Collection/AbstractCollectionPersister.php

https://gitlab.com/aleksbenmaza/PPE_NEW
PHP | 313 lines | 152 code | 48 blank | 113 comment | 12 complexity | 86fed9b12feaa83c4d5ed423c2608040 MD5 | raw file
  1. <?php
  2. /*
  3. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. *
  15. * This software consists of voluntary contributions made by many individuals
  16. * and is licensed under the MIT license. For more information, see
  17. * <http://www.doctrine-project.org>.
  18. */
  19. namespace Doctrine\ORM\Cache\Persister\Collection;
  20. use Doctrine\Common\Collections\Criteria;
  21. use Doctrine\ORM\Cache\EntityCacheKey;
  22. use Doctrine\ORM\Cache\CollectionCacheKey;
  23. use Doctrine\ORM\Cache\Persister\Entity\CachedEntityPersister;
  24. use Doctrine\ORM\Persisters\Collection\CollectionPersister;
  25. use Doctrine\ORM\PersistentCollection;
  26. use Doctrine\ORM\EntityManagerInterface;
  27. use Doctrine\ORM\Cache\Region;
  28. use Doctrine\Common\Util\ClassUtils;
  29. /**
  30. * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  31. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  32. * @since 2.5
  33. */
  34. abstract class AbstractCollectionPersister implements CachedCollectionPersister
  35. {
  36. /**
  37. * @var \Doctrine\ORM\UnitOfWork
  38. */
  39. protected $uow;
  40. /**
  41. * @var \Doctrine\ORM\Mapping\ClassMetadataFactory
  42. */
  43. protected $metadataFactory;
  44. /**
  45. * @var \Doctrine\ORM\Persisters\Collection\CollectionPersister
  46. */
  47. protected $persister;
  48. /**
  49. * @var \Doctrine\ORM\Mapping\ClassMetadata
  50. */
  51. protected $sourceEntity;
  52. /**
  53. * @var \Doctrine\ORM\Mapping\ClassMetadata
  54. */
  55. protected $targetEntity;
  56. /**
  57. * @var array
  58. */
  59. protected $association;
  60. /**
  61. * @var array
  62. */
  63. protected $queuedCache = array();
  64. /**
  65. * @var \Doctrine\ORM\Cache\Region
  66. */
  67. protected $region;
  68. /**
  69. * @var string
  70. */
  71. protected $regionName;
  72. /**
  73. * @var \Doctrine\ORM\Cache\CollectionHydrator
  74. */
  75. protected $hydrator;
  76. /**
  77. * @var \Doctrine\ORM\Cache\Logging\CacheLogger
  78. */
  79. protected $cacheLogger;
  80. /**
  81. * @param \Doctrine\ORM\Persisters\Collection\CollectionPersister $persister The collection persister that will be cached.
  82. * @param \Doctrine\ORM\Cache\Region $region The collection region.
  83. * @param \Doctrine\ORM\EntityManagerInterface $em The entity manager.
  84. * @param array $association The association mapping.
  85. */
  86. public function __construct(CollectionPersister $persister, Region $region, EntityManagerInterface $em, array $association)
  87. {
  88. $configuration = $em->getConfiguration();
  89. $cacheConfig = $configuration->getSecondLevelCacheConfiguration();
  90. $cacheFactory = $cacheConfig->getCacheFactory();
  91. $this->region = $region;
  92. $this->persister = $persister;
  93. $this->association = $association;
  94. $this->regionName = $region->getName();
  95. $this->uow = $em->getUnitOfWork();
  96. $this->metadataFactory = $em->getMetadataFactory();
  97. $this->cacheLogger = $cacheConfig->getCacheLogger();
  98. $this->hydrator = $cacheFactory->buildCollectionHydrator($em, $association);
  99. $this->sourceEntity = $em->getClassMetadata($association['sourceEntity']);
  100. $this->targetEntity = $em->getClassMetadata($association['targetEntity']);
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getCacheRegion()
  106. {
  107. return $this->region;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getSourceEntityMetadata()
  113. {
  114. return $this->sourceEntity;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getTargetEntityMetadata()
  120. {
  121. return $this->targetEntity;
  122. }
  123. /**
  124. * @param \Doctrine\ORM\PersistentCollection $collection
  125. * @param \Doctrine\ORM\Cache\CollectionCacheKey $key
  126. *
  127. * @return \Doctrine\ORM\PersistentCollection|null
  128. */
  129. public function loadCollectionCache(PersistentCollection $collection, CollectionCacheKey $key)
  130. {
  131. if (($cache = $this->region->get($key)) === null) {
  132. return null;
  133. }
  134. if (($cache = $this->hydrator->loadCacheEntry($this->sourceEntity, $key, $cache, $collection)) === null) {
  135. return null;
  136. }
  137. return $cache;
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function storeCollectionCache(CollectionCacheKey $key, $elements)
  143. {
  144. /* @var $targetPersister CachedEntityPersister */
  145. $associationMapping = $this->sourceEntity->associationMappings[$key->association];
  146. $targetPersister = $this->uow->getEntityPersister($this->targetEntity->rootEntityName);
  147. $targetRegion = $targetPersister->getCacheRegion();
  148. $targetHydrator = $targetPersister->getEntityHydrator();
  149. // Only preserve ordering if association configured it
  150. if ( ! (isset($associationMapping['indexBy']) && $associationMapping['indexBy'])) {
  151. // Elements may be an array or a Collection
  152. $elements = array_values(is_array($elements) ? $elements : $elements->getValues());
  153. }
  154. $entry = $this->hydrator->buildCacheEntry($this->targetEntity, $key, $elements);
  155. foreach ($entry->identifiers as $index => $entityKey) {
  156. if ($targetRegion->contains($entityKey)) {
  157. continue;
  158. }
  159. $class = $this->targetEntity;
  160. $className = ClassUtils::getClass($elements[$index]);
  161. if ($className !== $this->targetEntity->name) {
  162. $class = $this->metadataFactory->getMetadataFor($className);
  163. }
  164. $entity = $elements[$index];
  165. $entityEntry = $targetHydrator->buildCacheEntry($class, $entityKey, $entity);
  166. $targetRegion->put($entityKey, $entityEntry);
  167. }
  168. $cached = $this->region->put($key, $entry);
  169. if ($this->cacheLogger && $cached) {
  170. $this->cacheLogger->collectionCachePut($this->regionName, $key);
  171. }
  172. }
  173. /**
  174. * {@inheritdoc}
  175. */
  176. public function contains(PersistentCollection $collection, $element)
  177. {
  178. return $this->persister->contains($collection, $element);
  179. }
  180. /**
  181. * {@inheritdoc}
  182. */
  183. public function containsKey(PersistentCollection $collection, $key)
  184. {
  185. return $this->persister->containsKey($collection, $key);
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function count(PersistentCollection $collection)
  191. {
  192. $ownerId = $this->uow->getEntityIdentifier($collection->getOwner());
  193. $key = new CollectionCacheKey($this->sourceEntity->rootEntityName, $this->association['fieldName'], $ownerId);
  194. $entry = $this->region->get($key);
  195. if ($entry !== null) {
  196. return count($entry->identifiers);
  197. }
  198. return $this->persister->count($collection);
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function get(PersistentCollection $collection, $index)
  204. {
  205. return $this->persister->get($collection, $index);
  206. }
  207. /**
  208. * {@inheritdoc}
  209. */
  210. public function removeElement(PersistentCollection $collection, $element)
  211. {
  212. if ($persisterResult = $this->persister->removeElement($collection, $element)) {
  213. $this->evictCollectionCache($collection);
  214. $this->evictElementCache($this->sourceEntity->rootEntityName, $collection->getOwner());
  215. $this->evictElementCache($this->targetEntity->rootEntityName, $element);
  216. }
  217. return $persisterResult;
  218. }
  219. /**
  220. * {@inheritdoc}
  221. */
  222. public function slice(PersistentCollection $collection, $offset, $length = null)
  223. {
  224. return $this->persister->slice($collection, $offset, $length);
  225. }
  226. /**
  227. * {@inheritDoc}
  228. */
  229. public function loadCriteria(PersistentCollection $collection, Criteria $criteria)
  230. {
  231. return $this->persister->loadCriteria($collection, $criteria);
  232. }
  233. /**
  234. * Clears cache entries related to the current collection
  235. *
  236. * @param PersistentCollection $collection
  237. */
  238. protected function evictCollectionCache(PersistentCollection $collection)
  239. {
  240. $key = new CollectionCacheKey(
  241. $this->sourceEntity->rootEntityName,
  242. $this->association['fieldName'],
  243. $this->uow->getEntityIdentifier($collection->getOwner())
  244. );
  245. $this->region->evict($key);
  246. if ($this->cacheLogger) {
  247. $this->cacheLogger->collectionCachePut($this->regionName, $key);
  248. }
  249. }
  250. /**
  251. * @param string $targetEntity
  252. * @param object $element
  253. */
  254. protected function evictElementCache($targetEntity, $element)
  255. {
  256. /* @var $targetPersister CachedEntityPersister */
  257. $targetPersister = $this->uow->getEntityPersister($targetEntity);
  258. $targetRegion = $targetPersister->getCacheRegion();
  259. $key = new EntityCacheKey($targetEntity, $this->uow->getEntityIdentifier($element));
  260. $targetRegion->evict($key);
  261. if ($this->cacheLogger) {
  262. $this->cacheLogger->entityCachePut($targetRegion->getName(), $key);
  263. }
  264. }
  265. }