PhpFileCache.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use function is_object;
  4. use function method_exists;
  5. use function restore_error_handler;
  6. use function serialize;
  7. use function set_error_handler;
  8. use function sprintf;
  9. use function time;
  10. use function var_export;
  11. /**
  12. * Php file cache driver.
  13. *
  14. * @deprecated Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0
  15. */
  16. class PhpFileCache extends FileCache
  17. {
  18. public const EXTENSION = '.doctrinecache.php';
  19. /**
  20. * @var callable
  21. *
  22. * This is cached in a local static variable to avoid instantiating a closure each time we need an empty handler
  23. */
  24. private static $emptyErrorHandler;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
  29. {
  30. parent::__construct($directory, $extension, $umask);
  31. self::$emptyErrorHandler = static function () {
  32. };
  33. }
  34. /**
  35. * {@inheritdoc}
  36. */
  37. protected function doFetch($id)
  38. {
  39. $value = $this->includeFileForId($id);
  40. if ($value === null) {
  41. return false;
  42. }
  43. if ($value['lifetime'] !== 0 && $value['lifetime'] < time()) {
  44. return false;
  45. }
  46. return $value['data'];
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function doContains($id)
  52. {
  53. $value = $this->includeFileForId($id);
  54. if ($value === null) {
  55. return false;
  56. }
  57. return $value['lifetime'] === 0 || $value['lifetime'] > time();
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. protected function doSave($id, $data, $lifeTime = 0)
  63. {
  64. if ($lifeTime > 0) {
  65. $lifeTime = time() + $lifeTime;
  66. }
  67. $filename = $this->getFilename($id);
  68. $value = [
  69. 'lifetime' => $lifeTime,
  70. 'data' => $data,
  71. ];
  72. if (is_object($data) && method_exists($data, '__set_state')) {
  73. $value = var_export($value, true);
  74. $code = sprintf('<?php return %s;', $value);
  75. } else {
  76. $value = var_export(serialize($value), true);
  77. $code = sprintf('<?php return unserialize(%s);', $value);
  78. }
  79. return $this->writeFile($filename, $code);
  80. }
  81. /**
  82. * @return mixed[]|null
  83. */
  84. private function includeFileForId(string $id): ?array
  85. {
  86. $fileName = $this->getFilename($id);
  87. // note: error suppression is still faster than `file_exists`, `is_file` and `is_readable`
  88. set_error_handler(self::$emptyErrorHandler);
  89. $value = include $fileName;
  90. restore_error_handler();
  91. if (! isset($value['lifetime'])) {
  92. return null;
  93. }
  94. return $value;
  95. }
  96. }