FilesystemTrait.php 2.9 KB

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