composer package updates
[openemr.git] / vendor / symfony / config / ResourceCheckerConfigCache.php
blob52ae833d441821b5117a4d57b04fea8d2be0eb0f
1 <?php
3 /*
4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\Config;
14 use Symfony\Component\Config\Resource\ResourceInterface;
15 use Symfony\Component\Filesystem\Exception\IOException;
16 use Symfony\Component\Filesystem\Filesystem;
18 /**
19 * ResourceCheckerConfigCache uses instances of ResourceCheckerInterface
20 * to check whether cached data is still fresh.
22 * @author Matthias Pigulla <mp@webfactory.de>
24 class ResourceCheckerConfigCache implements ConfigCacheInterface
26 /**
27 * @var string
29 private $file;
31 /**
32 * @var ResourceCheckerInterface[]
34 private $resourceCheckers;
36 /**
37 * @param string $file The absolute cache path
38 * @param ResourceCheckerInterface[] $resourceCheckers The ResourceCheckers to use for the freshness check
40 public function __construct($file, array $resourceCheckers = array())
42 $this->file = $file;
43 $this->resourceCheckers = $resourceCheckers;
46 /**
47 * {@inheritdoc}
49 public function getPath()
51 return $this->file;
54 /**
55 * Checks if the cache is still fresh.
57 * This implementation will make a decision solely based on the ResourceCheckers
58 * passed in the constructor.
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.
63 * @return bool true if the cache is fresh, false otherwise
65 public function isFresh()
67 if (!is_file($this->file)) {
68 return false;
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
75 $metadata = $this->getMetaFile();
76 if (!is_file($metadata)) {
77 return false;
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;
90 return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
91 });
93 try {
94 $meta = unserialize(file_get_contents($metadata));
95 } catch (\Error $e) {
96 } catch (\Exception $e) {
98 restore_error_handler();
99 ini_set('unserialize_callback_func', $prevUnserializeHandler);
100 if (null !== $e && $e !== $signalingException) {
101 throw $e;
103 if (false === $meta) {
104 return false;
107 foreach ($meta as $resource) {
108 /* @var ResourceInterface $resource */
109 foreach ($this->resourceCheckers as $checker) {
110 if (!$checker->supports($resource)) {
111 continue; // next checker
113 if ($checker->isFresh($resource, $time)) {
114 break; // no need to further check this resource
117 return false; // cache is stale
119 // no suitable checker found, ignore this resource
122 return true;
126 * Writes cache.
128 * @param string $content The content to write in the cache
129 * @param ResourceInterface[] $metadata An array of metadata
131 * @throws \RuntimeException When cache file can't be written
133 public function write($content, array $metadata = null)
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)
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)
154 if (\function_exists('opcache_invalidate') && ini_get('opcache.enable')) {
155 @opcache_invalidate($this->file, true);
160 * Gets the meta file path.
162 * @return string The meta file path
164 private function getMetaFile()
166 return $this->file.'.meta';