DoctrineProvider.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Doctrine\Common\Cache\Psr6;
  11. use Doctrine\Common\Cache\Cache;
  12. use Doctrine\Common\Cache\CacheProvider;
  13. use Psr\Cache\CacheItemPoolInterface;
  14. use Symfony\Component\Cache\Adapter\DoctrineAdapter as SymfonyDoctrineAdapter;
  15. use Symfony\Contracts\Service\ResetInterface;
  16. use function rawurlencode;
  17. /**
  18. * This class was copied from the Symfony Framework, see the original copyright
  19. * notice above. The code is distributed subject to the license terms in
  20. * https://github.com/symfony/symfony/blob/ff0cf61278982539c49e467db9ab13cbd342f76d/LICENSE
  21. */
  22. final class DoctrineProvider extends CacheProvider
  23. {
  24. /** @var CacheItemPoolInterface */
  25. private $pool;
  26. public static function wrap(CacheItemPoolInterface $pool): Cache
  27. {
  28. if ($pool instanceof CacheAdapter) {
  29. return $pool->getCache();
  30. }
  31. if ($pool instanceof SymfonyDoctrineAdapter) {
  32. $getCache = function () {
  33. // phpcs:ignore Squiz.Scope.StaticThisUsage.Found
  34. return $this->provider;
  35. };
  36. return $getCache->bindTo($pool, SymfonyDoctrineAdapter::class)();
  37. }
  38. return new self($pool);
  39. }
  40. private function __construct(CacheItemPoolInterface $pool)
  41. {
  42. $this->pool = $pool;
  43. }
  44. /** @internal */
  45. public function getPool(): CacheItemPoolInterface
  46. {
  47. return $this->pool;
  48. }
  49. public function reset(): void
  50. {
  51. if ($this->pool instanceof ResetInterface) {
  52. $this->pool->reset();
  53. }
  54. $this->setNamespace($this->getNamespace());
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. protected function doFetch($id)
  60. {
  61. $item = $this->pool->getItem(rawurlencode($id));
  62. return $item->isHit() ? $item->get() : false;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. *
  67. * @return bool
  68. */
  69. protected function doContains($id)
  70. {
  71. return $this->pool->hasItem(rawurlencode($id));
  72. }
  73. /**
  74. * {@inheritdoc}
  75. *
  76. * @return bool
  77. */
  78. protected function doSave($id, $data, $lifeTime = 0)
  79. {
  80. $item = $this->pool->getItem(rawurlencode($id));
  81. if (0 < $lifeTime) {
  82. $item->expiresAfter($lifeTime);
  83. }
  84. return $this->pool->save($item->set($data));
  85. }
  86. /**
  87. * {@inheritdoc}
  88. *
  89. * @return bool
  90. */
  91. protected function doDelete($id)
  92. {
  93. return $this->pool->deleteItem(rawurlencode($id));
  94. }
  95. /**
  96. * {@inheritdoc}
  97. *
  98. * @return bool
  99. */
  100. protected function doFlush()
  101. {
  102. return $this->pool->clear();
  103. }
  104. /**
  105. * {@inheritdoc}
  106. *
  107. * @return array|null
  108. */
  109. protected function doGetStats()
  110. {
  111. return null;
  112. }
  113. }