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

Open in your IDE?
  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.     /**
  22.      * @return bool
  23.      */
  24.     public function prune()
  25.     {
  26.         $time time();
  27.         $pruned true;
  28.         foreach (new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->directory, \FilesystemIterator::SKIP_DOTS), \RecursiveIteratorIterator::LEAVES_ONLY) as $file) {
  29.             if (!$h = @fopen($file'rb')) {
  30.                 continue;
  31.             }
  32.             if (($expiresAt = (int) fgets($h)) && $time >= $expiresAt) {
  33.                 fclose($h);
  34.                 $pruned = @unlink($file) && !file_exists($file) && $pruned;
  35.             } else {
  36.                 fclose($h);
  37.             }
  38.         }
  39.         return $pruned;
  40.     }
  41.     /**
  42.      * {@inheritdoc}
  43.      */
  44.     protected function doFetch(array $ids)
  45.     {
  46.         $values = [];
  47.         $now time();
  48.         foreach ($ids as $id) {
  49.             $file $this->getFile($id);
  50.             if (!file_exists($file) || !$h = @fopen($file'rb')) {
  51.                 continue;
  52.             }
  53.             if (($expiresAt = (int) fgets($h)) && $now >= $expiresAt) {
  54.                 fclose($h);
  55.                 @unlink($file);
  56.             } else {
  57.                 $i rawurldecode(rtrim(fgets($h)));
  58.                 $value stream_get_contents($h);
  59.                 fclose($h);
  60.                 if ($i === $id) {
  61.                     $values[$id] = parent::unserialize($value);
  62.                 }
  63.             }
  64.         }
  65.         return $values;
  66.     }
  67.     /**
  68.      * {@inheritdoc}
  69.      */
  70.     protected function doHave($id)
  71.     {
  72.         $file $this->getFile($id);
  73.         return file_exists($file) && (@filemtime($file) > time() || $this->doFetch([$id]));
  74.     }
  75.     /**
  76.      * {@inheritdoc}
  77.      */
  78.     protected function doSave(array $values$lifetime)
  79.     {
  80.         $ok true;
  81.         $expiresAt $lifetime ? (time() + $lifetime) : 0;
  82.         foreach ($values as $id => $value) {
  83.             $ok $this->write($this->getFile($idtrue), $expiresAt."\n".rawurlencode($id)."\n".serialize($value), $expiresAt) && $ok;
  84.         }
  85.         if (!$ok && !is_writable($this->directory)) {
  86.             throw new CacheException(sprintf('Cache directory is not writable (%s)'$this->directory));
  87.         }
  88.         return $ok;
  89.     }
  90. }