DoctrineAdapter.php 2.5 KB

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