DoctrineTrait.php 2.2 KB

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