DoctrineAdapter.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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 Symfony\Component\Cache\Adapter;
  11. use Doctrine\Common\Cache\CacheProvider;
  12. use Doctrine\Common\Cache\Psr6\CacheAdapter;
  13. /**
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. *
  16. * @deprecated Since Symfony 5.4, use Doctrine\Common\Cache\Psr6\CacheAdapter instead
  17. */
  18. class DoctrineAdapter extends AbstractAdapter
  19. {
  20. private $provider;
  21. public function __construct(CacheProvider $provider, string $namespace = '', int $defaultLifetime = 0)
  22. {
  23. trigger_deprecation('symfony/cache', '5.4', '"%s" is deprecated, use "%s" instead.', __CLASS__, CacheAdapter::class);
  24. parent::__construct('', $defaultLifetime);
  25. $this->provider = $provider;
  26. $provider->setNamespace($namespace);
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function reset()
  32. {
  33. parent::reset();
  34. $this->provider->setNamespace($this->provider->getNamespace());
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. protected function doFetch(array $ids)
  40. {
  41. $unserializeCallbackHandler = ini_set('unserialize_callback_func', parent::class.'::handleUnserializeCallback');
  42. try {
  43. return $this->provider->fetchMultiple($ids);
  44. } catch (\Error $e) {
  45. $trace = $e->getTrace();
  46. if (isset($trace[0]['function']) && !isset($trace[0]['class'])) {
  47. switch ($trace[0]['function']) {
  48. case 'unserialize':
  49. case 'apcu_fetch':
  50. case 'apc_fetch':
  51. throw new \ErrorException($e->getMessage(), $e->getCode(), \E_ERROR, $e->getFile(), $e->getLine());
  52. }
  53. }
  54. throw $e;
  55. } finally {
  56. ini_set('unserialize_callback_func', $unserializeCallbackHandler);
  57. }
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function doHave(string $id)
  63. {
  64. return $this->provider->contains($id);
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. protected function doClear(string $namespace)
  70. {
  71. $namespace = $this->provider->getNamespace();
  72. return isset($namespace[0])
  73. ? $this->provider->deleteAll()
  74. : $this->provider->flushAll();
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function doDelete(array $ids)
  80. {
  81. $ok = true;
  82. foreach ($ids as $id) {
  83. $ok = $this->provider->delete($id) && $ok;
  84. }
  85. return $ok;
  86. }
  87. /**
  88. * {@inheritdoc}
  89. */
  90. protected function doSave(array $values, int $lifetime)
  91. {
  92. return $this->provider->saveMultiple($values, $lifetime);
  93. }
  94. }