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

Open in your IDE?
  1. <?php
  2. /*
  3.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  4.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  5.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  6.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  7.  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  8.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  9.  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  10.  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  11.  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  12.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  13.  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * This software consists of voluntary contributions made by many individuals
  16.  * and is licensed under the MIT license. For more information, see
  17.  * <http://www.doctrine-project.org>.
  18.  */
  19. namespace Doctrine\Common\Annotations;
  20. use Doctrine\Common\Cache\Cache;
  21. use ReflectionClass;
  22. /**
  23.  * A cache aware annotation reader.
  24.  *
  25.  * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  26.  * @author Benjamin Eberlei <kontakt@beberlei.de>
  27.  */
  28. final class CachedReader implements Reader
  29. {
  30.     /**
  31.      * @var Reader
  32.      */
  33.     private $delegate;
  34.     /**
  35.      * @var Cache
  36.      */
  37.     private $cache;
  38.     /**
  39.      * @var boolean
  40.      */
  41.     private $debug;
  42.     /**
  43.      * @var array
  44.      */
  45.     private $loadedAnnotations = array();
  46.     /**
  47.      * Constructor.
  48.      *
  49.      * @param Reader $reader
  50.      * @param Cache  $cache
  51.      * @param bool   $debug
  52.      */
  53.     public function __construct(Reader $readerCache $cache$debug false)
  54.     {
  55.         $this->delegate $reader;
  56.         $this->cache $cache;
  57.         $this->debug = (boolean) $debug;
  58.     }
  59.     /**
  60.      * {@inheritDoc}
  61.      */
  62.     public function getClassAnnotations(ReflectionClass $class)
  63.     {
  64.         $cacheKey $class->getName();
  65.         if (isset($this->loadedAnnotations[$cacheKey])) {
  66.             return $this->loadedAnnotations[$cacheKey];
  67.         }
  68.         if (false === ($annots $this->fetchFromCache($cacheKey$class))) {
  69.             $annots $this->delegate->getClassAnnotations($class);
  70.             $this->saveToCache($cacheKey$annots);
  71.         }
  72.         return $this->loadedAnnotations[$cacheKey] = $annots;
  73.     }
  74.     /**
  75.      * {@inheritDoc}
  76.      */
  77.     public function getClassAnnotation(ReflectionClass $class$annotationName)
  78.     {
  79.         foreach ($this->getClassAnnotations($class) as $annot) {
  80.             if ($annot instanceof $annotationName) {
  81.                 return $annot;
  82.             }
  83.         }
  84.         return null;
  85.     }
  86.     /**
  87.      * {@inheritDoc}
  88.      */
  89.     public function getPropertyAnnotations(\ReflectionProperty $property)
  90.     {
  91.         $class $property->getDeclaringClass();
  92.         $cacheKey $class->getName().'$'.$property->getName();
  93.         if (isset($this->loadedAnnotations[$cacheKey])) {
  94.             return $this->loadedAnnotations[$cacheKey];
  95.         }
  96.         if (false === ($annots $this->fetchFromCache($cacheKey$class))) {
  97.             $annots $this->delegate->getPropertyAnnotations($property);
  98.             $this->saveToCache($cacheKey$annots);
  99.         }
  100.         return $this->loadedAnnotations[$cacheKey] = $annots;
  101.     }
  102.     /**
  103.      * {@inheritDoc}
  104.      */
  105.     public function getPropertyAnnotation(\ReflectionProperty $property$annotationName)
  106.     {
  107.         foreach ($this->getPropertyAnnotations($property) as $annot) {
  108.             if ($annot instanceof $annotationName) {
  109.                 return $annot;
  110.             }
  111.         }
  112.         return null;
  113.     }
  114.     /**
  115.      * {@inheritDoc}
  116.      */
  117.     public function getMethodAnnotations(\ReflectionMethod $method)
  118.     {
  119.         $class $method->getDeclaringClass();
  120.         $cacheKey $class->getName().'#'.$method->getName();
  121.         if (isset($this->loadedAnnotations[$cacheKey])) {
  122.             return $this->loadedAnnotations[$cacheKey];
  123.         }
  124.         if (false === ($annots $this->fetchFromCache($cacheKey$class))) {
  125.             $annots $this->delegate->getMethodAnnotations($method);
  126.             $this->saveToCache($cacheKey$annots);
  127.         }
  128.         return $this->loadedAnnotations[$cacheKey] = $annots;
  129.     }
  130.     /**
  131.      * {@inheritDoc}
  132.      */
  133.     public function getMethodAnnotation(\ReflectionMethod $method$annotationName)
  134.     {
  135.         foreach ($this->getMethodAnnotations($method) as $annot) {
  136.             if ($annot instanceof $annotationName) {
  137.                 return $annot;
  138.             }
  139.         }
  140.         return null;
  141.     }
  142.     /**
  143.      * Clears loaded annotations.
  144.      *
  145.      * @return void
  146.      */
  147.     public function clearLoadedAnnotations()
  148.     {
  149.         $this->loadedAnnotations = array();
  150.     }
  151.     /**
  152.      * Fetches a value from the cache.
  153.      *
  154.      * @param string          $cacheKey The cache key.
  155.      * @param ReflectionClass $class    The related class.
  156.      *
  157.      * @return mixed The cached value or false when the value is not in cache.
  158.      */
  159.     private function fetchFromCache($cacheKeyReflectionClass $class)
  160.     {
  161.         if (($data $this->cache->fetch($cacheKey)) !== false) {
  162.             if (!$this->debug || $this->isCacheFresh($cacheKey$class)) {
  163.                 return $data;
  164.             }
  165.         }
  166.         return false;
  167.     }
  168.     /**
  169.      * Saves a value to the cache.
  170.      *
  171.      * @param string $cacheKey The cache key.
  172.      * @param mixed  $value    The value.
  173.      *
  174.      * @return void
  175.      */
  176.     private function saveToCache($cacheKey$value)
  177.     {
  178.         $this->cache->save($cacheKey$value);
  179.         if ($this->debug) {
  180.             $this->cache->save('[C]'.$cacheKeytime());
  181.         }
  182.     }
  183.     /**
  184.      * Checks if the cache is fresh.
  185.      *
  186.      * @param string           $cacheKey
  187.      * @param ReflectionClass $class
  188.      *
  189.      * @return boolean
  190.      */
  191.     private function isCacheFresh($cacheKeyReflectionClass $class)
  192.     {
  193.         if (null === $lastModification $this->getLastModification($class)) {
  194.             return true;
  195.         }
  196.         return $this->cache->fetch('[C]'.$cacheKey) >= $lastModification;
  197.     }
  198.     /**
  199.      * Returns the time the class was last modified, testing traits and parents
  200.      *
  201.      * @param ReflectionClass $class
  202.      * @return int
  203.      */
  204.     private function getLastModification(ReflectionClass $class)
  205.     {
  206.         $filename $class->getFileName();
  207.         $parent   $class->getParentClass();
  208.         return max(array_merge(
  209.             [$filename filemtime($filename) : 0],
  210.             array_map([$this'getTraitLastModificationTime'], $class->getTraits()),
  211.             array_map([$this'getLastModification'], $class->getInterfaces()),
  212.             $parent ? [$this->getLastModification($parent)] : []
  213.         ));
  214.     }
  215.     /**
  216.      * @param ReflectionClass $reflectionTrait
  217.      * @return int
  218.      */
  219.     private function getTraitLastModificationTime(ReflectionClass $reflectionTrait)
  220.     {
  221.         $fileName $reflectionTrait->getFileName();
  222.         return max(array_merge(
  223.             [$fileName filemtime($fileName) : 0],
  224.             array_map([$this'getTraitLastModificationTime'], $reflectionTrait->getTraits())
  225.         ));
  226.     }
  227. }