vendor/symfony/config/ResourceCheckerConfigCache.php line 57

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\Config;
  11. use Symfony\Component\Config\Resource\ResourceInterface;
  12. use Symfony\Component\Filesystem\Exception\IOException;
  13. use Symfony\Component\Filesystem\Filesystem;
  14. /**
  15.  * ResourceCheckerConfigCache uses instances of ResourceCheckerInterface
  16.  * to check whether cached data is still fresh.
  17.  *
  18.  * @author Matthias Pigulla <mp@webfactory.de>
  19.  */
  20. class ResourceCheckerConfigCache implements ConfigCacheInterface
  21. {
  22.     private string $file;
  23.     /**
  24.      * @var iterable<mixed, ResourceCheckerInterface>
  25.      */
  26.     private iterable $resourceCheckers;
  27.     /**
  28.      * @param string                                    $file             The absolute cache path
  29.      * @param iterable<mixed, ResourceCheckerInterface> $resourceCheckers The ResourceCheckers to use for the freshness check
  30.      */
  31.     public function __construct(string $file, iterable $resourceCheckers = [])
  32.     {
  33.         $this->file = $file;
  34.         $this->resourceCheckers = $resourceCheckers;
  35.     }
  36.     public function getPath(): string
  37.     {
  38.         return $this->file;
  39.     }
  40.     /**
  41.      * Checks if the cache is still fresh.
  42.      *
  43.      * This implementation will make a decision solely based on the ResourceCheckers
  44.      * passed in the constructor.
  45.      *
  46.      * The first ResourceChecker that supports a given resource is considered authoritative.
  47.      * Resources with no matching ResourceChecker will silently be ignored and considered fresh.
  48.      */
  49.     public function isFresh(): bool
  50.     {
  51.         if (!is_file($this->file)) {
  52.             return false;
  53.         }
  54.         if ($this->resourceCheckers instanceof \Traversable && !$this->resourceCheckers instanceof \Countable) {
  55.             $this->resourceCheckers = iterator_to_array($this->resourceCheckers);
  56.         }
  57.         if (!\count($this->resourceCheckers)) {
  58.             return true; // shortcut - if we don't have any checkers we don't need to bother with the meta file at all
  59.         }
  60.         $metadata = $this->getMetaFile();
  61.         if (!is_file($metadata)) {
  62.             return false;
  63.         }
  64.         $meta = $this->safelyUnserialize($metadata);
  65.         if (false === $meta) {
  66.             return false;
  67.         }
  68.         $time = filemtime($this->file);
  69.         foreach ($meta as $resource) {
  70.             foreach ($this->resourceCheckers as $checker) {
  71.                 if (!$checker->supports($resource)) {
  72.                     continue; // next checker
  73.                 }
  74.                 if ($checker->isFresh($resource, $time)) {
  75.                     break; // no need to further check this resource
  76.                 }
  77.                 return false; // cache is stale
  78.             }
  79.             // no suitable checker found, ignore this resource
  80.         }
  81.         return true;
  82.     }
  83.     /**
  84.      * Writes cache.
  85.      *
  86.      * @param string              $content  The content to write in the cache
  87.      * @param ResourceInterface[] $metadata An array of metadata
  88.      *
  89.      * @return void
  90.      *
  91.      * @throws \RuntimeException When cache file can't be written
  92.      */
  93.     public function write(string $content, ?array $metadata = null)
  94.     {
  95.         $mode = 0666;
  96.         $umask = umask();
  97.         $filesystem = new Filesystem();
  98.         $filesystem->dumpFile($this->file, $content);
  99.         try {
  100.             $filesystem->chmod($this->file, $mode, $umask);
  101.         } catch (IOException) {
  102.             // discard chmod failure (some filesystem may not support it)
  103.         }
  104.         if (null !== $metadata) {
  105.             $filesystem->dumpFile($this->getMetaFile(), serialize($metadata));
  106.             try {
  107.                 $filesystem->chmod($this->getMetaFile(), $mode, $umask);
  108.             } catch (IOException) {
  109.                 // discard chmod failure (some filesystem may not support it)
  110.             }
  111.         }
  112.         if (\function_exists('opcache_invalidate') && filter_var(\ini_get('opcache.enable'), \FILTER_VALIDATE_BOOL)) {
  113.             @opcache_invalidate($this->file, true);
  114.         }
  115.     }
  116.     /**
  117.      * Gets the meta file path.
  118.      */
  119.     private function getMetaFile(): string
  120.     {
  121.         return $this->file.'.meta';
  122.     }
  123.     private function safelyUnserialize(string $file): mixed
  124.     {
  125.         $meta = false;
  126.         $content = file_get_contents($file);
  127.         $signalingException = new \UnexpectedValueException();
  128.         $prevUnserializeHandler = ini_set('unserialize_callback_func', self::class.'::handleUnserializeCallback');
  129.         $prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler, $signalingException) {
  130.             if (__FILE__ === $file && !\in_array($type, [\E_DEPRECATED, \E_USER_DEPRECATED], true)) {
  131.                 throw $signalingException;
  132.             }
  133.             return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
  134.         });
  135.         try {
  136.             $meta = unserialize($content);
  137.         } catch (\Throwable $e) {
  138.             if ($e !== $signalingException) {
  139.                 throw $e;
  140.             }
  141.         } finally {
  142.             restore_error_handler();
  143.             ini_set('unserialize_callback_func', $prevUnserializeHandler);
  144.         }
  145.         return $meta;
  146.     }
  147.     /**
  148.      * @internal
  149.      */
  150.     public static function handleUnserializeCallback(string $class): void
  151.     {
  152.         trigger_error('Class not found: '.$class);
  153.     }
  154. }