CachedReader.php in Service Container 7
File
modules/providers/service_container_annotation_discovery/lib/Doctrine/annotations/lib/Doctrine/Common/Annotations/CachedReader.php
View source
<?php
namespace Doctrine\Common\Annotations;
use Doctrine\Common\Cache\Cache;
final class CachedReader implements Reader {
private static $CACHE_SALT = '@[Annot]';
private $delegate;
private $cache;
private $debug;
private $loadedAnnotations = array();
public function __construct(Reader $reader, Cache $cache, $debug = false) {
$this->delegate = $reader;
$this->cache = $cache;
$this->debug = (bool) $debug;
}
public function getClassAnnotations(\ReflectionClass $class) {
$cacheKey = $class
->getName();
if (isset($this->loadedAnnotations[$cacheKey])) {
return $this->loadedAnnotations[$cacheKey];
}
if (false === ($annots = $this
->fetchFromCache($cacheKey, $class))) {
$annots = $this->delegate
->getClassAnnotations($class);
$this
->saveToCache($cacheKey, $annots);
}
return $this->loadedAnnotations[$cacheKey] = $annots;
}
public function getClassAnnotation(\ReflectionClass $class, $annotationName) {
foreach ($this
->getClassAnnotations($class) as $annot) {
if ($annot instanceof $annotationName) {
return $annot;
}
}
return null;
}
public function getPropertyAnnotations(\ReflectionProperty $property) {
$class = $property
->getDeclaringClass();
$cacheKey = $class
->getName() . '$' . $property
->getName();
if (isset($this->loadedAnnotations[$cacheKey])) {
return $this->loadedAnnotations[$cacheKey];
}
if (false === ($annots = $this
->fetchFromCache($cacheKey, $class))) {
$annots = $this->delegate
->getPropertyAnnotations($property);
$this
->saveToCache($cacheKey, $annots);
}
return $this->loadedAnnotations[$cacheKey] = $annots;
}
public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName) {
foreach ($this
->getPropertyAnnotations($property) as $annot) {
if ($annot instanceof $annotationName) {
return $annot;
}
}
return null;
}
public function getMethodAnnotations(\ReflectionMethod $method) {
$class = $method
->getDeclaringClass();
$cacheKey = $class
->getName() . '#' . $method
->getName();
if (isset($this->loadedAnnotations[$cacheKey])) {
return $this->loadedAnnotations[$cacheKey];
}
if (false === ($annots = $this
->fetchFromCache($cacheKey, $class))) {
$annots = $this->delegate
->getMethodAnnotations($method);
$this
->saveToCache($cacheKey, $annots);
}
return $this->loadedAnnotations[$cacheKey] = $annots;
}
public function getMethodAnnotation(\ReflectionMethod $method, $annotationName) {
foreach ($this
->getMethodAnnotations($method) as $annot) {
if ($annot instanceof $annotationName) {
return $annot;
}
}
return null;
}
public function clearLoadedAnnotations() {
$this->loadedAnnotations = array();
}
private function fetchFromCache($rawCacheKey, \ReflectionClass $class) {
$cacheKey = $rawCacheKey . self::$CACHE_SALT;
if (($data = $this->cache
->fetch($cacheKey)) !== false) {
if (!$this->debug || $this
->isCacheFresh($cacheKey, $class)) {
return $data;
}
}
return false;
}
private function saveToCache($rawCacheKey, $value) {
$cacheKey = $rawCacheKey . self::$CACHE_SALT;
$this->cache
->save($cacheKey, $value);
if ($this->debug) {
$this->cache
->save('[C]' . $cacheKey, time());
}
}
private function isCacheFresh($cacheKey, \ReflectionClass $class) {
if (false === ($filename = $class
->getFilename())) {
return true;
}
return $this->cache
->fetch('[C]' . $cacheKey) >= filemtime($filename);
}
}