Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
ResourceCheckerConfigCache.php
001 <?php
002
003 /*
004 * This file is part of the Symfony package.
005 *
006 * (c) Fabien Potencier <fabien@symfony.com>
007 *
008 * For the full copyright and license information, please view the LICENSE
009 * file that was distributed with this source code.
010 */
011
012 namespace Symfony\Component\Config;
013
014 use Symfony\Component\Config\Resource\ResourceInterface;
015 use Symfony\Component\Filesystem\Exception\IOException;
016 use Symfony\Component\Filesystem\Filesystem;
017
018 /**
019 * ResourceCheckerConfigCache uses instances of ResourceCheckerInterface
020 * to check whether cached data is still fresh.
021 *
022 * @author Matthias Pigulla <mp@webfactory.de>
023 */
024 class ResourceCheckerConfigCache implements ConfigCacheInterface
025 {
026 /**
027 * @var string
028 */
029 private $file;
030
031 /**
032 * @var iterable|ResourceCheckerInterface[]
033 */
034 private $resourceCheckers;
035
036 /**
037 * @param string $file The absolute cache path
038 * @param iterable|ResourceCheckerInterface[] $resourceCheckers The ResourceCheckers to use for the freshness check
039 */
040 public function __construct($file, $resourceCheckers = [])
041 {
042 $this->file = $file;
043 $this->resourceCheckers = $resourceCheckers;
044 }
045
046 /**
047 * {@inheritdoc}
048 */
049 public function getPath()
050 {
051 return $this->file;
052 }
053
054 /**
055 * Checks if the cache is still fresh.
056 *
057 * This implementation will make a decision solely based on the ResourceCheckers
058 * passed in the constructor.
059 *
060 * The first ResourceChecker that supports a given resource is considered authoritative.
061 * Resources with no matching ResourceChecker will silently be ignored and considered fresh.
062 *
063 * @return bool true if the cache is fresh, false otherwise
064 */
065 public function isFresh()
066 {
067 if (!is_file($this->file)) {
068 return false;
069 }
070
071 if ($this->resourceCheckers instanceof \Traversable && !$this->resourceCheckers instanceof \Countable) {
072 $this->resourceCheckers = iterator_to_array($this->resourceCheckers);
073 }
074
075 if (!\count($this->resourceCheckers)) {
076 return true; // shortcut - if we don't have any checkers we don't need to bother with the meta file at all
077 }
078
079 $metadata = $this->getMetaFile();
080
081 if (!is_file($metadata)) {
082 return false;
083 }
084
085 $meta = $this->safelyUnserialize($metadata);
086
087 if (false === $meta) {
088 return false;
089 }
090
091 $time = filemtime($this->file);
092
093 foreach ($meta as $resource) {
094 /* @var ResourceInterface $resource */
095 foreach ($this->resourceCheckers as $checker) {
096 if (!$checker->supports($resource)) {
097 continue; // next checker
098 }
099 if ($checker->isFresh($resource, $time)) {
100 break; // no need to further check this resource
101 }
102
103 return false; // cache is stale
104 }
105 // no suitable checker found, ignore this resource
106 }
107
108 return true;
109 }
110
111 /**
112 * Writes cache.
113 *
114 * @param string $content The content to write in the cache
115 * @param ResourceInterface[] $metadata An array of metadata
116 *
117 * @throws \RuntimeException When cache file can't be written
118 */
119 public function write($content, array $metadata = null)
120 {
121 $mode = 0666;
122 $umask = umask();
123 $filesystem = new Filesystem();
124 $filesystem->dumpFile($this->file, $content);
125 try {
126 $filesystem->chmod($this->file, $mode, $umask);
127 } catch (IOException $e) {
128 // discard chmod failure (some filesystem may not support it)
129 }
130
131 if (null !== $metadata) {
132 $filesystem->dumpFile($this->getMetaFile(), serialize($metadata));
133 try {
134 $filesystem->chmod($this->getMetaFile(), $mode, $umask);
135 } catch (IOException $e) {
136 // discard chmod failure (some filesystem may not support it)
137 }
138 }
139
140 if (\function_exists('opcache_invalidate') && filter_var(ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN)) {
141 @opcache_invalidate($this->file, true);
142 }
143 }
144
145 /**
146 * Gets the meta file path.
147 *
148 * @return string The meta file path
149 */
150 private function getMetaFile()
151 {
152 return $this->file.'.meta';
153 }
154
155 private function safelyUnserialize($file)
156 {
157 $e = null;
158 $meta = false;
159 $content = file_get_contents($file);
160 $signalingException = new \UnexpectedValueException();
161 $prevUnserializeHandler = ini_set('unserialize_callback_func', '');
162 $prevErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$prevErrorHandler, $signalingException) {
163 if (__FILE__ === $file) {
164 throw $signalingException;
165 }
166
167 return $prevErrorHandler ? $prevErrorHandler($type, $msg, $file, $line, $context) : false;
168 });
169
170 try {
171 $meta = unserialize($content);
172 } catch (\Error $e) {
173 } catch (\Exception $e) {
174 }
175 restore_error_handler();
176 ini_set('unserialize_callback_func', $prevUnserializeHandler);
177 if (null !== $e && $e !== $signalingException) {
178 throw $e;
179 }
180
181 return $meta;
182 }
183 }
184