FilesystemTrait.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. use Symfony\Component\Cache\Exception\CacheException;
  12. /**
  13. * @author Nicolas Grekas <p@tchwork.com>
  14. * @author Rob Frawley 2nd <rmf@src.run>
  15. *
  16. * @internal
  17. */
  18. trait FilesystemTrait
  19. {
  20. use FilesystemCommonTrait;
  21. private $marshaller;
  22. /**
  23. * @return bool
  24. */
  25. public function prune()
  26. {
  27. $time = time();
  28. $pruned = true;
  29. foreach ($this->scanHashDir($this->directory) as $file) {
  30. if (!$h = @fopen($file, 'r')) {
  31. continue;
  32. }
  33. if (($expiresAt = (int) fgets($h)) && $time >= $expiresAt) {
  34. fclose($h);
  35. $pruned = @unlink($file) && !file_exists($file) && $pruned;
  36. } else {
  37. fclose($h);
  38. }
  39. }
  40. return $pruned;
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. protected function doFetch(array $ids)
  46. {
  47. $values = [];
  48. $now = time();
  49. foreach ($ids as $id) {
  50. $file = $this->getFile($id);
  51. if (!is_file($file) || !$h = @fopen($file, 'r')) {
  52. continue;
  53. }
  54. if (($expiresAt = (int) fgets($h)) && $now >= $expiresAt) {
  55. fclose($h);
  56. @unlink($file);
  57. } else {
  58. $i = rawurldecode(rtrim(fgets($h)));
  59. $value = stream_get_contents($h);
  60. fclose($h);
  61. if ($i === $id) {
  62. $values[$id] = $this->marshaller->unmarshall($value);
  63. }
  64. }
  65. }
  66. return $values;
  67. }
  68. /**
  69. * {@inheritdoc}
  70. */
  71. protected function doHave(string $id)
  72. {
  73. $file = $this->getFile($id);
  74. return is_file($file) && (@filemtime($file) > time() || $this->doFetch([$id]));
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function doSave(array $values, int $lifetime)
  80. {
  81. $expiresAt = $lifetime ? (time() + $lifetime) : 0;
  82. $values = $this->marshaller->marshall($values, $failed);
  83. foreach ($values as $id => $value) {
  84. if (!$this->write($this->getFile($id, true), $expiresAt."\n".rawurlencode($id)."\n".$value, $expiresAt)) {
  85. $failed[] = $id;
  86. }
  87. }
  88. if ($failed && !is_writable($this->directory)) {
  89. throw new CacheException(sprintf('Cache directory is not writable (%s).', $this->directory));
  90. }
  91. return $failed;
  92. }
  93. private function getFileKey(string $file): string
  94. {
  95. if (!$h = @fopen($file, 'r')) {
  96. return '';
  97. }
  98. fgets($h); // expiry
  99. $encodedKey = fgets($h);
  100. fclose($h);
  101. return rawurldecode(rtrim($encodedKey));
  102. }
  103. }