FilesystemCache.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. namespace Doctrine\Common\Cache;
  3. use function fclose;
  4. use function fgets;
  5. use function fopen;
  6. use function is_file;
  7. use function serialize;
  8. use function time;
  9. use function unserialize;
  10. use const PHP_EOL;
  11. /**
  12. * Filesystem cache driver.
  13. *
  14. * @deprecated Deprecated without replacement in doctrine/cache 1.11. This class will be dropped in 2.0
  15. */
  16. class FilesystemCache extends FileCache
  17. {
  18. public const EXTENSION = '.doctrinecache.data';
  19. /**
  20. * {@inheritdoc}
  21. */
  22. public function __construct($directory, $extension = self::EXTENSION, $umask = 0002)
  23. {
  24. parent::__construct($directory, $extension, $umask);
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. protected function doFetch($id)
  30. {
  31. $data = '';
  32. $lifetime = -1;
  33. $filename = $this->getFilename($id);
  34. if (! is_file($filename)) {
  35. return false;
  36. }
  37. $resource = fopen($filename, 'r');
  38. $line = fgets($resource);
  39. if ($line !== false) {
  40. $lifetime = (int) $line;
  41. }
  42. if ($lifetime !== 0 && $lifetime < time()) {
  43. fclose($resource);
  44. return false;
  45. }
  46. while (($line = fgets($resource)) !== false) {
  47. $data .= $line;
  48. }
  49. fclose($resource);
  50. return unserialize($data);
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. protected function doContains($id)
  56. {
  57. $lifetime = -1;
  58. $filename = $this->getFilename($id);
  59. if (! is_file($filename)) {
  60. return false;
  61. }
  62. $resource = fopen($filename, 'r');
  63. $line = fgets($resource);
  64. if ($line !== false) {
  65. $lifetime = (int) $line;
  66. }
  67. fclose($resource);
  68. return $lifetime === 0 || $lifetime > time();
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. protected function doSave($id, $data, $lifeTime = 0)
  74. {
  75. if ($lifeTime > 0) {
  76. $lifeTime = time() + $lifeTime;
  77. }
  78. $data = serialize($data);
  79. $filename = $this->getFilename($id);
  80. return $this->writeFile($filename, $lifeTime . PHP_EOL . $data);
  81. }
  82. }