You are here

final class CachedReader in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/CachedReader.php \Doctrine\Common\Annotations\CachedReader

A cache aware annotation reader.

@author Johannes M. Schmitt <schmittjoh@gmail.com> @author Benjamin Eberlei <kontakt@beberlei.de>

Hierarchy

Expanded class hierarchy of CachedReader

1 file declares its use of CachedReader
ValidatorBuilder.php in vendor/symfony/validator/ValidatorBuilder.php

File

vendor/doctrine/annotations/lib/Doctrine/Common/Annotations/CachedReader.php, line 30

Namespace

Doctrine\Common\Annotations
View source
final class CachedReader implements Reader {

  /**
   * @var string
   */
  private static $CACHE_SALT = '@[Annot]';

  /**
   * @var Reader
   */
  private $delegate;

  /**
   * @var Cache
   */
  private $cache;

  /**
   * @var boolean
   */
  private $debug;

  /**
   * @var array
   */
  private $loadedAnnotations = array();

  /**
   * Constructor.
   *
   * @param Reader $reader
   * @param Cache  $cache
   * @param bool   $debug
   */
  public function __construct(Reader $reader, Cache $cache, $debug = false) {
    $this->delegate = $reader;
    $this->cache = $cache;
    $this->debug = (bool) $debug;
  }

  /**
   * {@inheritDoc}
   */
  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;
  }

  /**
   * {@inheritDoc}
   */
  public function getClassAnnotation(\ReflectionClass $class, $annotationName) {
    foreach ($this
      ->getClassAnnotations($class) as $annot) {
      if ($annot instanceof $annotationName) {
        return $annot;
      }
    }
    return null;
  }

  /**
   * {@inheritDoc}
   */
  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;
  }

  /**
   * {@inheritDoc}
   */
  public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName) {
    foreach ($this
      ->getPropertyAnnotations($property) as $annot) {
      if ($annot instanceof $annotationName) {
        return $annot;
      }
    }
    return null;
  }

  /**
   * {@inheritDoc}
   */
  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;
  }

  /**
   * {@inheritDoc}
   */
  public function getMethodAnnotation(\ReflectionMethod $method, $annotationName) {
    foreach ($this
      ->getMethodAnnotations($method) as $annot) {
      if ($annot instanceof $annotationName) {
        return $annot;
      }
    }
    return null;
  }

  /**
   * Clears loaded annotations.
   *
   * @return void
   */
  public function clearLoadedAnnotations() {
    $this->loadedAnnotations = array();
  }

  /**
   * Fetches a value from the cache.
   *
   * @param string           $rawCacheKey The cache key.
   * @param \ReflectionClass $class       The related class.
   *
   * @return mixed The cached value or false when the value is not in cache.
   */
  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;
  }

  /**
   * Saves a value to the cache.
   *
   * @param string $rawCacheKey The cache key.
   * @param mixed  $value       The value.
   *
   * @return void
   */
  private function saveToCache($rawCacheKey, $value) {
    $cacheKey = $rawCacheKey . self::$CACHE_SALT;
    $this->cache
      ->save($cacheKey, $value);
    if ($this->debug) {
      $this->cache
        ->save('[C]' . $cacheKey, time());
    }
  }

  /**
   * Checks if the cache is fresh.
   *
   * @param string           $cacheKey
   * @param \ReflectionClass $class
   *
   * @return boolean
   */
  private function isCacheFresh($cacheKey, \ReflectionClass $class) {
    if (false === ($filename = $class
      ->getFilename())) {
      return true;
    }
    return $this->cache
      ->fetch('[C]' . $cacheKey) >= filemtime($filename);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CachedReader::$cache private property
CachedReader::$CACHE_SALT private static property
CachedReader::$debug private property
CachedReader::$delegate private property
CachedReader::$loadedAnnotations private property
CachedReader::clearLoadedAnnotations public function Clears loaded annotations.
CachedReader::fetchFromCache private function Fetches a value from the cache.
CachedReader::getClassAnnotation public function Gets a class annotation. Overrides Reader::getClassAnnotation
CachedReader::getClassAnnotations public function Gets the annotations applied to a class. Overrides Reader::getClassAnnotations
CachedReader::getMethodAnnotation public function Gets a method annotation. Overrides Reader::getMethodAnnotation
CachedReader::getMethodAnnotations public function Gets the annotations applied to a method. Overrides Reader::getMethodAnnotations
CachedReader::getPropertyAnnotation public function Gets a property annotation. Overrides Reader::getPropertyAnnotation
CachedReader::getPropertyAnnotations public function Gets the annotations applied to a property. Overrides Reader::getPropertyAnnotations
CachedReader::isCacheFresh private function Checks if the cache is fresh.
CachedReader::saveToCache private function Saves a value to the cache.
CachedReader::__construct public function Constructor.