[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/config/ -> ResourceCheckerConfigCache.php (source)

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


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1