vendor/symfony/cache/Adapter/PhpFilesAdapter.php line 118

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\Adapter;
  11. use Symfony\Component\Cache\Exception\CacheException;
  12. use Symfony\Component\Cache\Exception\InvalidArgumentException;
  13. use Symfony\Component\Cache\PruneableInterface;
  14. use Symfony\Component\Cache\Traits\FilesystemCommonTrait;
  15. use Symfony\Component\VarExporter\VarExporter;
  16. /**
  17.  * @author Piotr Stankowski <git@trakos.pl>
  18.  * @author Nicolas Grekas <p@tchwork.com>
  19.  * @author Rob Frawley 2nd <rmf@src.run>
  20.  */
  21. class PhpFilesAdapter extends AbstractAdapter implements PruneableInterface
  22. {
  23.     use FilesystemCommonTrait {
  24.         doClear as private doCommonClear;
  25.         doDelete as private doCommonDelete;
  26.     }
  27.     private \Closure $includeHandler;
  28.     private bool $appendOnly;
  29.     private array $values = [];
  30.     private array $files = [];
  31.     private static int $startTime;
  32.     private static array $valuesCache = [];
  33.     /**
  34.      * @param $appendOnly Set to `true` to gain extra performance when the items stored in this pool never expire.
  35.      *                    Doing so is encouraged because it fits perfectly OPcache's memory model.
  36.      *
  37.      * @throws CacheException if OPcache is not enabled
  38.      */
  39.     public function __construct(string $namespace ''int $defaultLifetime 0, ?string $directory nullbool $appendOnly false)
  40.     {
  41.         $this->appendOnly $appendOnly;
  42.         self::$startTime ??= $_SERVER['REQUEST_TIME'] ?? time();
  43.         parent::__construct(''$defaultLifetime);
  44.         $this->init($namespace$directory);
  45.         $this->includeHandler = static function ($type$msg$file$line) {
  46.             throw new \ErrorException($msg0$type$file$line);
  47.         };
  48.     }
  49.     /**
  50.      * @return bool
  51.      */
  52.     public static function isSupported()
  53.     {
  54.         self::$startTime ??= $_SERVER['REQUEST_TIME'] ?? time();
  55.         return \function_exists('opcache_invalidate') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOL) && (!\in_array(\PHP_SAPI, ['cli''phpdbg''embed'], true) || filter_var(\ini_get('opcache.enable_cli'), \FILTER_VALIDATE_BOOL));
  56.     }
  57.     public function prune(): bool
  58.     {
  59.         $time time();
  60.         $pruned true;
  61.         $getExpiry true;
  62.         set_error_handler($this->includeHandler);
  63.         try {
  64.             foreach ($this->scanHashDir($this->directory) as $file) {
  65.                 try {
  66.                     if (\is_array($expiresAt = include $file)) {
  67.                         $expiresAt $expiresAt[0];
  68.                     }
  69.                 } catch (\ErrorException $e) {
  70.                     $expiresAt $time;
  71.                 }
  72.                 if ($time >= $expiresAt) {
  73.                     $pruned = ($this->doUnlink($file) || !file_exists($file)) && $pruned;
  74.                 }
  75.             }
  76.         } finally {
  77.             restore_error_handler();
  78.         }
  79.         return $pruned;
  80.     }
  81.     protected function doFetch(array $ids): iterable
  82.     {
  83.         if ($this->appendOnly) {
  84.             $now 0;
  85.             $missingIds = [];
  86.         } else {
  87.             $now time();
  88.             $missingIds $ids;
  89.             $ids = [];
  90.         }
  91.         $values = [];
  92.         begin:
  93.         $getExpiry false;
  94.         foreach ($ids as $id) {
  95.             if (null === $value $this->values[$id] ?? null) {
  96.                 $missingIds[] = $id;
  97.             } elseif ('N;' === $value) {
  98.                 $values[$id] = null;
  99.             } elseif (!\is_object($value)) {
  100.                 $values[$id] = $value;
  101.             } elseif (!$value instanceof LazyValue) {
  102.                 $values[$id] = $value();
  103.             } elseif (false === $values[$id] = include $value->file) {
  104.                 unset($values[$id], $this->values[$id]);
  105.                 $missingIds[] = $id;
  106.             }
  107.             if (!$this->appendOnly) {
  108.                 unset($this->values[$id]);
  109.             }
  110.         }
  111.         if (!$missingIds) {
  112.             return $values;
  113.         }
  114.         set_error_handler($this->includeHandler);
  115.         try {
  116.             $getExpiry true;
  117.             foreach ($missingIds as $k => $id) {
  118.                 try {
  119.                     $file $this->files[$id] ??= $this->getFile($id);
  120.                     if (isset(self::$valuesCache[$file])) {
  121.                         [$expiresAt$this->values[$id]] = self::$valuesCache[$file];
  122.                     } elseif (\is_array($expiresAt = include $file)) {
  123.                         if ($this->appendOnly) {
  124.                             self::$valuesCache[$file] = $expiresAt;
  125.                         }
  126.                         [$expiresAt$this->values[$id]] = $expiresAt;
  127.                     } elseif ($now $expiresAt) {
  128.                         $this->values[$id] = new LazyValue($file);
  129.                     }
  130.                     if ($now >= $expiresAt) {
  131.                         unset($this->values[$id], $missingIds[$k], self::$valuesCache[$file]);
  132.                     }
  133.                 } catch (\ErrorException $e) {
  134.                     unset($missingIds[$k]);
  135.                 }
  136.             }
  137.         } finally {
  138.             restore_error_handler();
  139.         }
  140.         $ids $missingIds;
  141.         $missingIds = [];
  142.         goto begin;
  143.     }
  144.     protected function doHave(string $id): bool
  145.     {
  146.         if ($this->appendOnly && isset($this->values[$id])) {
  147.             return true;
  148.         }
  149.         set_error_handler($this->includeHandler);
  150.         try {
  151.             $file $this->files[$id] ??= $this->getFile($id);
  152.             $getExpiry true;
  153.             if (isset(self::$valuesCache[$file])) {
  154.                 [$expiresAt$value] = self::$valuesCache[$file];
  155.             } elseif (\is_array($expiresAt = include $file)) {
  156.                 if ($this->appendOnly) {
  157.                     self::$valuesCache[$file] = $expiresAt;
  158.                 }
  159.                 [$expiresAt$value] = $expiresAt;
  160.             } elseif ($this->appendOnly) {
  161.                 $value = new LazyValue($file);
  162.             }
  163.         } catch (\ErrorException) {
  164.             return false;
  165.         } finally {
  166.             restore_error_handler();
  167.         }
  168.         if ($this->appendOnly) {
  169.             $now 0;
  170.             $this->values[$id] = $value;
  171.         } else {
  172.             $now time();
  173.         }
  174.         return $now $expiresAt;
  175.     }
  176.     protected function doSave(array $valuesint $lifetime): array|bool
  177.     {
  178.         $ok true;
  179.         $expiry $lifetime time() + $lifetime 'PHP_INT_MAX';
  180.         $allowCompile self::isSupported();
  181.         foreach ($values as $key => $value) {
  182.             unset($this->values[$key]);
  183.             $isStaticValue true;
  184.             if (null === $value) {
  185.                 $value "'N;'";
  186.             } elseif (\is_object($value) || \is_array($value)) {
  187.                 try {
  188.                     $value VarExporter::export($value$isStaticValue);
  189.                 } catch (\Exception $e) {
  190.                     throw new InvalidArgumentException(\sprintf('Cache key "%s" has non-serializable "%s" value.'$keyget_debug_type($value)), 0$e);
  191.                 }
  192.             } elseif (\is_string($value)) {
  193.                 // Wrap "N;" in a closure to not confuse it with an encoded `null`
  194.                 if ('N;' === $value) {
  195.                     $isStaticValue false;
  196.                 }
  197.                 $value var_export($valuetrue);
  198.             } elseif (!\is_scalar($value)) {
  199.                 throw new InvalidArgumentException(\sprintf('Cache key "%s" has non-serializable "%s" value.'$keyget_debug_type($value)));
  200.             } else {
  201.                 $value var_export($valuetrue);
  202.             }
  203.             $encodedKey rawurlencode($key);
  204.             if ($isStaticValue) {
  205.                 $value "return [{$expiry}{$value}];";
  206.             } elseif ($this->appendOnly) {
  207.                 $value "return [{$expiry}, static fn () => {$value}];";
  208.             } else {
  209.                 // We cannot use a closure here because of https://bugs.php.net/76982
  210.                 $value str_replace('\Symfony\Component\VarExporter\Internal\\'''$value);
  211.                 $value "namespace Symfony\Component\VarExporter\Internal;\n\nreturn \$getExpiry ? {$expiry} : {$value};";
  212.             }
  213.             $file $this->files[$key] = $this->getFile($keytrue);
  214.             // Since OPcache only compiles files older than the script execution start, set the file's mtime in the past
  215.             $ok $this->write($file"<?php //{$encodedKey}\n\n{$value}\n"self::$startTime 10) && $ok;
  216.             if ($allowCompile) {
  217.                 @opcache_invalidate($filetrue);
  218.                 @opcache_compile_file($file);
  219.             }
  220.             unset(self::$valuesCache[$file]);
  221.         }
  222.         if (!$ok && !is_writable($this->directory)) {
  223.             throw new CacheException(\sprintf('Cache directory is not writable (%s).'$this->directory));
  224.         }
  225.         return $ok;
  226.     }
  227.     protected function doClear(string $namespace): bool
  228.     {
  229.         $this->values = [];
  230.         return $this->doCommonClear($namespace);
  231.     }
  232.     protected function doDelete(array $ids): bool
  233.     {
  234.         foreach ($ids as $id) {
  235.             unset($this->values[$id]);
  236.         }
  237.         return $this->doCommonDelete($ids);
  238.     }
  239.     /**
  240.      * @return bool
  241.      */
  242.     protected function doUnlink(string $file)
  243.     {
  244.         unset(self::$valuesCache[$file]);
  245.         if (self::isSupported()) {
  246.             @opcache_invalidate($filetrue);
  247.         }
  248.         return @unlink($file);
  249.     }
  250.     private function getFileKey(string $file): string
  251.     {
  252.         if (!$h = @fopen($file'r')) {
  253.             return '';
  254.         }
  255.         $encodedKey substr(fgets($h), 8);
  256.         fclose($h);
  257.         return rawurldecode(rtrim($encodedKey));
  258.     }
  259. }
  260. /**
  261.  * @internal
  262.  */
  263. class LazyValue
  264. {
  265.     public string $file;
  266.     public function __construct(string $file)
  267.     {
  268.         $this->file $file;
  269.     }
  270. }