vendor/symfony/cache/Traits/FilesystemTrait.php line 57

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