vendor/symfony/config/ResourceCheckerConfigCache.php line 96

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