PageRenderTime 33ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/doctrine/orm/lib/Doctrine/ORM/Cache/DefaultCacheFactory.php

https://gitlab.com/mario.uriarte/doctrine2.5-tutorial
PHP | 254 lines | 139 code | 39 blank | 76 comment | 11 complexity | bb1f968059c046b480af3d749fd2bc15 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;
  20. use Doctrine\Common\Cache\Cache as CacheAdapter;
  21. use Doctrine\Common\Cache\CacheProvider;
  22. use Doctrine\Common\Cache\MultiGetCache;
  23. use Doctrine\ORM\Cache;
  24. use Doctrine\ORM\Cache\Persister\Collection\NonStrictReadWriteCachedCollectionPersister;
  25. use Doctrine\ORM\Cache\Persister\Collection\ReadOnlyCachedCollectionPersister;
  26. use Doctrine\ORM\Cache\Persister\Collection\ReadWriteCachedCollectionPersister;
  27. use Doctrine\ORM\Cache\Persister\Entity\NonStrictReadWriteCachedEntityPersister;
  28. use Doctrine\ORM\Cache\Persister\Entity\ReadOnlyCachedEntityPersister;
  29. use Doctrine\ORM\Cache\Persister\Entity\ReadWriteCachedEntityPersister;
  30. use Doctrine\ORM\Cache\Region;
  31. use Doctrine\ORM\Cache\Region\DefaultMultiGetRegion;
  32. use Doctrine\ORM\Cache\Region\DefaultRegion;
  33. use Doctrine\ORM\Cache\Region\FileLockRegion;
  34. use Doctrine\ORM\Cache\Region\UpdateTimestampCache;
  35. use Doctrine\ORM\EntityManagerInterface;
  36. use Doctrine\ORM\Mapping\ClassMetadata;
  37. use Doctrine\ORM\Persisters\Collection\CollectionPersister;
  38. use Doctrine\ORM\Persisters\Entity\EntityPersister;
  39. /**
  40. * @since 2.5
  41. * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
  42. */
  43. class DefaultCacheFactory implements CacheFactory
  44. {
  45. /**
  46. * @var CacheAdapter
  47. */
  48. private $cache;
  49. /**
  50. * @var \Doctrine\ORM\Cache\RegionsConfiguration
  51. */
  52. private $regionsConfig;
  53. /**
  54. * @var \Doctrine\ORM\Cache\TimestampRegion|null
  55. */
  56. private $timestampRegion;
  57. /**
  58. * @var \Doctrine\ORM\Cache\Region[]
  59. */
  60. private $regions = array();
  61. /**
  62. * @var string|null
  63. */
  64. private $fileLockRegionDirectory;
  65. /**
  66. * @param RegionsConfiguration $cacheConfig
  67. * @param CacheAdapter $cache
  68. */
  69. public function __construct(RegionsConfiguration $cacheConfig, CacheAdapter $cache)
  70. {
  71. $this->cache = $cache;
  72. $this->regionsConfig = $cacheConfig;
  73. }
  74. /**
  75. * @param string $fileLockRegionDirectory
  76. */
  77. public function setFileLockRegionDirectory($fileLockRegionDirectory)
  78. {
  79. $this->fileLockRegionDirectory = (string) $fileLockRegionDirectory;
  80. }
  81. /**
  82. * @return string
  83. */
  84. public function getFileLockRegionDirectory()
  85. {
  86. return $this->fileLockRegionDirectory;
  87. }
  88. /**
  89. * @param \Doctrine\ORM\Cache\Region $region
  90. */
  91. public function setRegion(Region $region)
  92. {
  93. $this->regions[$region->getName()] = $region;
  94. }
  95. /**
  96. * @param \Doctrine\ORM\Cache\TimestampRegion $region
  97. */
  98. public function setTimestampRegion(TimestampRegion $region)
  99. {
  100. $this->timestampRegion = $region;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function buildCachedEntityPersister(EntityManagerInterface $em, EntityPersister $persister, ClassMetadata $metadata)
  106. {
  107. $region = $this->getRegion($metadata->cache);
  108. $usage = $metadata->cache['usage'];
  109. if ($usage === ClassMetadata::CACHE_USAGE_READ_ONLY) {
  110. return new ReadOnlyCachedEntityPersister($persister, $region, $em, $metadata);
  111. }
  112. if ($usage === ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE) {
  113. return new NonStrictReadWriteCachedEntityPersister($persister, $region, $em, $metadata);
  114. }
  115. if ($usage === ClassMetadata::CACHE_USAGE_READ_WRITE) {
  116. return new ReadWriteCachedEntityPersister($persister, $region, $em, $metadata);
  117. }
  118. throw new \InvalidArgumentException(sprintf("Unrecognized access strategy type [%s]", $usage));
  119. }
  120. /**
  121. * {@inheritdoc}
  122. */
  123. public function buildCachedCollectionPersister(EntityManagerInterface $em, CollectionPersister $persister, array $mapping)
  124. {
  125. $usage = $mapping['cache']['usage'];
  126. $region = $this->getRegion($mapping['cache']);
  127. if ($usage === ClassMetadata::CACHE_USAGE_READ_ONLY) {
  128. return new ReadOnlyCachedCollectionPersister($persister, $region, $em, $mapping);
  129. }
  130. if ($usage === ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE) {
  131. return new NonStrictReadWriteCachedCollectionPersister($persister, $region, $em, $mapping);
  132. }
  133. if ($usage === ClassMetadata::CACHE_USAGE_READ_WRITE) {
  134. return new ReadWriteCachedCollectionPersister($persister, $region, $em, $mapping);
  135. }
  136. throw new \InvalidArgumentException(sprintf("Unrecognized access strategy type [%s]", $usage));
  137. }
  138. /**
  139. * {@inheritdoc}
  140. */
  141. public function buildQueryCache(EntityManagerInterface $em, $regionName = null)
  142. {
  143. return new DefaultQueryCache(
  144. $em,
  145. $this->getRegion(
  146. array(
  147. 'region' => $regionName ?: Cache::DEFAULT_QUERY_REGION_NAME,
  148. 'usage' => ClassMetadata::CACHE_USAGE_NONSTRICT_READ_WRITE
  149. )
  150. )
  151. );
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function buildCollectionHydrator(EntityManagerInterface $em, array $mapping)
  157. {
  158. return new DefaultCollectionHydrator($em);
  159. }
  160. /**
  161. * {@inheritdoc}
  162. */
  163. public function buildEntityHydrator(EntityManagerInterface $em, ClassMetadata $metadata)
  164. {
  165. return new DefaultEntityHydrator($em);
  166. }
  167. /**
  168. * {@inheritdoc}
  169. */
  170. public function getRegion(array $cache)
  171. {
  172. if (isset($this->regions[$cache['region']])) {
  173. return $this->regions[$cache['region']];
  174. }
  175. $cacheAdapter = clone $this->cache;
  176. if ($cacheAdapter instanceof CacheProvider) {
  177. $cacheAdapter->setNamespace($cache['region']);
  178. }
  179. $name = $cache['region'];
  180. $lifetime = $this->regionsConfig->getLifetime($cache['region']);
  181. $region = ($cacheAdapter instanceof MultiGetCache)
  182. ? new DefaultMultiGetRegion($name, $cacheAdapter, $lifetime)
  183. : new DefaultRegion($name, $cacheAdapter, $lifetime);
  184. if ($cache['usage'] === ClassMetadata::CACHE_USAGE_READ_WRITE) {
  185. if ( ! $this->fileLockRegionDirectory) {
  186. throw new \LogicException(
  187. 'If you what to use a "READ_WRITE" cache an implementation of "Doctrine\ORM\Cache\ConcurrentRegion" is required, ' .
  188. 'The default implementation provided by doctrine is "Doctrine\ORM\Cache\Region\FileLockRegion" if you what to use it please provide a valid directory, DefaultCacheFactory#setFileLockRegionDirectory(). '
  189. );
  190. }
  191. $directory = $this->fileLockRegionDirectory . DIRECTORY_SEPARATOR . $cache['region'];
  192. $region = new FileLockRegion($region, $directory, $this->regionsConfig->getLockLifetime($cache['region']));
  193. }
  194. return $this->regions[$cache['region']] = $region;
  195. }
  196. /**
  197. * {@inheritdoc}
  198. */
  199. public function getTimestampRegion()
  200. {
  201. if ($this->timestampRegion === null) {
  202. $name = Cache::DEFAULT_TIMESTAMP_REGION_NAME;
  203. $lifetime = $this->regionsConfig->getLifetime($name);
  204. $this->timestampRegion = new UpdateTimestampCache($name, clone $this->cache, $lifetime);
  205. }
  206. return $this->timestampRegion;
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function createCache(EntityManagerInterface $em)
  212. {
  213. return new DefaultCache($em);
  214. }
  215. }