vendor/symfony/config/ResourceCheckerConfigCache.php line 93

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