comparison vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/CachedReader.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children
comparison
equal deleted inserted replaced
11:bfffd8d7479a 12:7a779792577d
18 */ 18 */
19 19
20 namespace Doctrine\Common\Annotations; 20 namespace Doctrine\Common\Annotations;
21 21
22 use Doctrine\Common\Cache\Cache; 22 use Doctrine\Common\Cache\Cache;
23 use ReflectionClass;
23 24
24 /** 25 /**
25 * A cache aware annotation reader. 26 * A cache aware annotation reader.
26 * 27 *
27 * @author Johannes M. Schmitt <schmittjoh@gmail.com> 28 * @author Johannes M. Schmitt <schmittjoh@gmail.com>
28 * @author Benjamin Eberlei <kontakt@beberlei.de> 29 * @author Benjamin Eberlei <kontakt@beberlei.de>
29 */ 30 */
30 final class CachedReader implements Reader 31 final class CachedReader implements Reader
31 { 32 {
32 /** 33 /**
33 * @var string
34 */
35 private static $CACHE_SALT = '@[Annot]';
36
37 /**
38 * @var Reader 34 * @var Reader
39 */ 35 */
40 private $delegate; 36 private $delegate;
41 37
42 /** 38 /**
69 } 65 }
70 66
71 /** 67 /**
72 * {@inheritDoc} 68 * {@inheritDoc}
73 */ 69 */
74 public function getClassAnnotations(\ReflectionClass $class) 70 public function getClassAnnotations(ReflectionClass $class)
75 { 71 {
76 $cacheKey = $class->getName(); 72 $cacheKey = $class->getName();
77 73
78 if (isset($this->loadedAnnotations[$cacheKey])) { 74 if (isset($this->loadedAnnotations[$cacheKey])) {
79 return $this->loadedAnnotations[$cacheKey]; 75 return $this->loadedAnnotations[$cacheKey];
88 } 84 }
89 85
90 /** 86 /**
91 * {@inheritDoc} 87 * {@inheritDoc}
92 */ 88 */
93 public function getClassAnnotation(\ReflectionClass $class, $annotationName) 89 public function getClassAnnotation(ReflectionClass $class, $annotationName)
94 { 90 {
95 foreach ($this->getClassAnnotations($class) as $annot) { 91 foreach ($this->getClassAnnotations($class) as $annot) {
96 if ($annot instanceof $annotationName) { 92 if ($annot instanceof $annotationName) {
97 return $annot; 93 return $annot;
98 } 94 }
180 } 176 }
181 177
182 /** 178 /**
183 * Fetches a value from the cache. 179 * Fetches a value from the cache.
184 * 180 *
185 * @param string $rawCacheKey The cache key. 181 * @param string $cacheKey The cache key.
186 * @param \ReflectionClass $class The related class. 182 * @param ReflectionClass $class The related class.
187 * 183 *
188 * @return mixed The cached value or false when the value is not in cache. 184 * @return mixed The cached value or false when the value is not in cache.
189 */ 185 */
190 private function fetchFromCache($rawCacheKey, \ReflectionClass $class) 186 private function fetchFromCache($cacheKey, ReflectionClass $class)
191 { 187 {
192 $cacheKey = $rawCacheKey . self::$CACHE_SALT;
193 if (($data = $this->cache->fetch($cacheKey)) !== false) { 188 if (($data = $this->cache->fetch($cacheKey)) !== false) {
194 if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) { 189 if (!$this->debug || $this->isCacheFresh($cacheKey, $class)) {
195 return $data; 190 return $data;
196 } 191 }
197 } 192 }
200 } 195 }
201 196
202 /** 197 /**
203 * Saves a value to the cache. 198 * Saves a value to the cache.
204 * 199 *
205 * @param string $rawCacheKey The cache key. 200 * @param string $cacheKey The cache key.
206 * @param mixed $value The value. 201 * @param mixed $value The value.
207 * 202 *
208 * @return void 203 * @return void
209 */ 204 */
210 private function saveToCache($rawCacheKey, $value) 205 private function saveToCache($cacheKey, $value)
211 { 206 {
212 $cacheKey = $rawCacheKey . self::$CACHE_SALT;
213 $this->cache->save($cacheKey, $value); 207 $this->cache->save($cacheKey, $value);
214 if ($this->debug) { 208 if ($this->debug) {
215 $this->cache->save('[C]'.$cacheKey, time()); 209 $this->cache->save('[C]'.$cacheKey, time());
216 } 210 }
217 } 211 }
218 212
219 /** 213 /**
220 * Checks if the cache is fresh. 214 * Checks if the cache is fresh.
221 * 215 *
222 * @param string $cacheKey 216 * @param string $cacheKey
223 * @param \ReflectionClass $class 217 * @param ReflectionClass $class
224 * 218 *
225 * @return boolean 219 * @return boolean
226 */ 220 */
227 private function isCacheFresh($cacheKey, \ReflectionClass $class) 221 private function isCacheFresh($cacheKey, ReflectionClass $class)
228 { 222 {
229 if (false === $filename = $class->getFilename()) { 223 if (null === $lastModification = $this->getLastModification($class)) {
230 return true; 224 return true;
231 } 225 }
232 226
233 return $this->cache->fetch('[C]'.$cacheKey) >= filemtime($filename); 227 return $this->cache->fetch('[C]'.$cacheKey) >= $lastModification;
228 }
229
230 /**
231 * Returns the time the class was last modified, testing traits and parents
232 *
233 * @param ReflectionClass $class
234 * @return int
235 */
236 private function getLastModification(ReflectionClass $class)
237 {
238 $filename = $class->getFileName();
239 $parent = $class->getParentClass();
240
241 return max(array_merge(
242 [$filename ? filemtime($filename) : 0],
243 array_map([$this, 'getTraitLastModificationTime'], $class->getTraits()),
244 array_map([$this, 'getLastModification'], $class->getInterfaces()),
245 $parent ? [$this->getLastModification($parent)] : []
246 ));
247 }
248
249 /**
250 * @param ReflectionClass $reflectionTrait
251 * @return int
252 */
253 private function getTraitLastModificationTime(ReflectionClass $reflectionTrait)
254 {
255 $fileName = $reflectionTrait->getFileName();
256
257 return max(array_merge(
258 [$fileName ? filemtime($fileName) : 0],
259 array_map([$this, 'getTraitLastModificationTime'], $reflectionTrait->getTraits())
260 ));
234 } 261 }
235 } 262 }