vendor/symfony/cache/Traits/FilesystemCommonTrait.php line 91

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\InvalidArgumentException;
  12. /**
  13.  * @author Nicolas Grekas <p@tchwork.com>
  14.  *
  15.  * @internal
  16.  */
  17. trait FilesystemCommonTrait
  18. {
  19.     private $directory;
  20.     private $tmp;
  21.     private function init(string $namespace, ?string $directory)
  22.     {
  23.         if (!isset($directory[0])) {
  24.             $directory sys_get_temp_dir().\DIRECTORY_SEPARATOR.'symfony-cache';
  25.         } else {
  26.             $directory realpath($directory) ?: $directory;
  27.         }
  28.         if (isset($namespace[0])) {
  29.             if (preg_match('#[^-+_.A-Za-z0-9]#'$namespace$match)) {
  30.                 throw new InvalidArgumentException(sprintf('Namespace contains "%s" but only characters in [-+_.A-Za-z0-9] are allowed.'$match[0]));
  31.             }
  32.             $directory .= \DIRECTORY_SEPARATOR.$namespace;
  33.         } else {
  34.             $directory .= \DIRECTORY_SEPARATOR.'@';
  35.         }
  36.         if (!is_dir($directory)) {
  37.             @mkdir($directory0777true);
  38.         }
  39.         $directory .= \DIRECTORY_SEPARATOR;
  40.         // On Windows the whole path is limited to 258 chars
  41.         if ('\\' === \DIRECTORY_SEPARATOR && \strlen($directory) > 234) {
  42.             throw new InvalidArgumentException(sprintf('Cache directory too long (%s).'$directory));
  43.         }
  44.         $this->directory $directory;
  45.     }
  46.     /**
  47.      * {@inheritdoc}
  48.      */
  49.     protected function doClear(string $namespace)
  50.     {
  51.         $ok true;
  52.         foreach ($this->scanHashDir($this->directory) as $file) {
  53.             if ('' !== $namespace && !str_starts_with($this->getFileKey($file), $namespace)) {
  54.                 continue;
  55.             }
  56.             $ok = ($this->doUnlink($file) || !file_exists($file)) && $ok;
  57.         }
  58.         return $ok;
  59.     }
  60.     /**
  61.      * {@inheritdoc}
  62.      */
  63.     protected function doDelete(array $ids)
  64.     {
  65.         $ok true;
  66.         foreach ($ids as $id) {
  67.             $file $this->getFile($id);
  68.             $ok = (!is_file($file) || $this->doUnlink($file) || !file_exists($file)) && $ok;
  69.         }
  70.         return $ok;
  71.     }
  72.     protected function doUnlink(string $file)
  73.     {
  74.         return @unlink($file);
  75.     }
  76.     private function write(string $filestring $data, ?int $expiresAt null)
  77.     {
  78.         $unlink false;
  79.         set_error_handler(__CLASS__.'::throwError');
  80.         try {
  81.             if (null === $this->tmp) {
  82.                 $this->tmp $this->directory.bin2hex(random_bytes(6));
  83.             }
  84.             try {
  85.                 $h fopen($this->tmp'x');
  86.             } catch (\ErrorException $e) {
  87.                 if (!str_contains($e->getMessage(), 'File exists')) {
  88.                     throw $e;
  89.                 }
  90.                 $this->tmp $this->directory.bin2hex(random_bytes(6));
  91.                 $h fopen($this->tmp'x');
  92.             }
  93.             fwrite($h$data);
  94.             fclose($h);
  95.             $unlink true;
  96.             if (null !== $expiresAt) {
  97.                 touch($this->tmp$expiresAt ?: time() + 31556952); // 1 year in seconds
  98.             }
  99.             if ('\\' === \DIRECTORY_SEPARATOR) {
  100.                 $success copy($this->tmp$file);
  101.                 $unlink true;
  102.             } else {
  103.                 $success rename($this->tmp$file);
  104.                 $unlink = !$success;
  105.             }
  106.             return $success;
  107.         } finally {
  108.             restore_error_handler();
  109.             if ($unlink) {
  110.                 @unlink($this->tmp);
  111.             }
  112.         }
  113.     }
  114.     private function getFile(string $idbool $mkdir false, ?string $directory null)
  115.     {
  116.         // Use MD5 to favor speed over security, which is not an issue here
  117.         $hash str_replace('/''-'base64_encode(hash('md5', static::class.$idtrue)));
  118.         $dir = ($directory ?? $this->directory).strtoupper($hash[0].\DIRECTORY_SEPARATOR.$hash[1].\DIRECTORY_SEPARATOR);
  119.         if ($mkdir && !is_dir($dir)) {
  120.             @mkdir($dir0777true);
  121.         }
  122.         return $dir.substr($hash220);
  123.     }
  124.     private function getFileKey(string $file): string
  125.     {
  126.         return '';
  127.     }
  128.     private function scanHashDir(string $directory): \Generator
  129.     {
  130.         if (!is_dir($directory)) {
  131.             return;
  132.         }
  133.         $chars '+-ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  134.         for ($i 0$i 38; ++$i) {
  135.             if (!is_dir($directory.$chars[$i])) {
  136.                 continue;
  137.             }
  138.             for ($j 0$j 38; ++$j) {
  139.                 if (!is_dir($dir $directory.$chars[$i].\DIRECTORY_SEPARATOR.$chars[$j])) {
  140.                     continue;
  141.                 }
  142.                 foreach (@scandir($dir, \SCANDIR_SORT_NONE) ?: [] as $file) {
  143.                     if ('.' !== $file && '..' !== $file) {
  144.                         yield $dir.\DIRECTORY_SEPARATOR.$file;
  145.                     }
  146.                 }
  147.             }
  148.         }
  149.     }
  150.     /**
  151.      * @internal
  152.      */
  153.     public static function throwError(int $typestring $messagestring $fileint $line)
  154.     {
  155.         throw new \ErrorException($message0$type$file$line);
  156.     }
  157.     /**
  158.      * @return array
  159.      */
  160.     public function __sleep()
  161.     {
  162.         throw new \BadMethodCallException('Cannot serialize '.__CLASS__);
  163.     }
  164.     public function __wakeup()
  165.     {
  166.         throw new \BadMethodCallException('Cannot unserialize '.__CLASS__);
  167.     }
  168.     public function __destruct()
  169.     {
  170.         if (method_exists(parent::class, '__destruct')) {
  171.             parent::__destruct();
  172.         }
  173.         if (null !== $this->tmp && is_file($this->tmp)) {
  174.             unlink($this->tmp);
  175.         }
  176.     }
  177. }