Commit f7fff511 authored by romanb's avatar romanb

[2.0] Improved AnnotationReader implementation. Fixes #2345.

parent a3d58e7b
...@@ -35,7 +35,6 @@ class AnnotationReader ...@@ -35,7 +35,6 @@ class AnnotationReader
private static $CACHE_SALT = "@<Annot>"; private static $CACHE_SALT = "@<Annot>";
private $_parser; private $_parser;
private $_cache; private $_cache;
private $_annotations = array();
/** /**
* Initiaizes a new AnnotationReader that uses the given Cache provider to cache annotations. * Initiaizes a new AnnotationReader that uses the given Cache provider to cache annotations.
...@@ -49,9 +48,10 @@ class AnnotationReader ...@@ -49,9 +48,10 @@ class AnnotationReader
} }
/** /**
* Sets the default namespace that the AnnotationReader should assume for annotations
* with not fully qualified names.
* *
* @param $defaultNamespace * @param string $defaultNamespace
* @return unknown_type
*/ */
public function setDefaultAnnotationNamespace($defaultNamespace) public function setDefaultAnnotationNamespace($defaultNamespace)
{ {
...@@ -67,25 +67,24 @@ class AnnotationReader ...@@ -67,25 +67,24 @@ class AnnotationReader
*/ */
public function getClassAnnotations(ReflectionClass $class) public function getClassAnnotations(ReflectionClass $class)
{ {
$className = $class->getName(); $cacheKey = $class->getName() . self::$CACHE_SALT;
if (isset($this->_annotations[$className])) { if ($this->_cache->contains($cacheKey)) {
return $this->_annotations[$className]; return $this->_cache->fetch($cacheKey);
} else if ($this->_cache->contains($className . self::$CACHE_SALT)) {
$this->_annotations[$className] = $this->_cacheDriver->get($className . self::$CACHE_SALT);
return $this->_annotations[$className];
} }
$this->_annotations[$className] = $this->_parser->parse($class->getDocComment()); $annotations = $this->_parser->parse($class->getDocComment());
$this->_cache->save($cacheKey, $annotations, null);
return $this->_annotations[$className]; return $annotations;
} }
/** /**
* Gets a class annotation.
* *
* @param $class * @param $class
* @param $annotation * @param string $annotation The name of the annotation.
* @return unknown_type * @return The Annotation or NULL, if the requested annotation does not exist.
*/ */
public function getClassAnnotation(ReflectionClass $class, $annotation) public function getClassAnnotation(ReflectionClass $class, $annotation)
{ {
...@@ -103,25 +102,24 @@ class AnnotationReader ...@@ -103,25 +102,24 @@ class AnnotationReader
*/ */
public function getPropertyAnnotations(ReflectionProperty $property) public function getPropertyAnnotations(ReflectionProperty $property)
{ {
$propertyName = $property->getDeclaringClass()->getName() . '$' . $property->getName(); $cacheKey = $property->getDeclaringClass()->getName() . '$' . $property->getName() . self::$CACHE_SALT;
if (isset($this->_annotations[$propertyName])) { if ($this->_cache->contains($cacheKey)) {
return $this->_annotations[$propertyName]; return $this->_cache->fetch($cacheKey);
} else if ($this->_cache->contains($propertyName . self::$CACHE_SALT)) {
$this->_annotations[$propertyName] = $this->_cacheDriver->get($propertyName . self::$CACHE_SALT);
return $this->_annotations[$propertyName];
} }
$this->_annotations[$propertyName] = $this->_parser->parse($property->getDocComment()); $annotations = $this->_parser->parse($property->getDocComment());
$this->_cache->save($cacheKey, $annotations, null);
return $this->_annotations[$propertyName]; return $annotations;
} }
/** /**
* Gets a property annotation.
* *
* @param $property * @param ReflectionProperty $property
* @param $annotation * @param string $annotation The name of the annotation.
* @return unknown_type * @return The Annotation or NULL, if the requested annotation does not exist.
*/ */
public function getPropertyAnnotation(ReflectionProperty $property, $annotation) public function getPropertyAnnotation(ReflectionProperty $property, $annotation)
{ {
...@@ -139,25 +137,24 @@ class AnnotationReader ...@@ -139,25 +137,24 @@ class AnnotationReader
*/ */
public function getMethodAnnotations(ReflectionMethod $method) public function getMethodAnnotations(ReflectionMethod $method)
{ {
$methodName = $method->getDeclaringClass()->getName() . '#' . $method->getName(); $cacheKey = $method->getDeclaringClass()->getName() . '#' . $method->getName() . self::$CACHE_SALT;
if (isset($this->_annotations[$methodName])) { if ($this->_cache->contains($cacheKey)) {
return $this->_annotations[$methodName]; return $this->_cache->fetch($cacheKey);
} else if ($this->_cache->contains($methodName . self::$CACHE_SALT)) {
$this->_annotations[$methodName] = $this->_cacheDriver->get($methodName . self::$CACHE_SALT);
return $this->_annotations[$methodName];
} }
$this->_annotations[$methodName] = $this->_parser->parse($method->getDocComment()); $annotations = $this->_parser->parse($method->getDocComment());
$this->_cache->save($cacheKey, $annotations, null);
return $this->_annotations[$methodName]; return $annotations;
} }
/** /**
* Gets a method annotation.
* *
* @param $method * @param ReflectionMethod $method
* @param $annotation * @param string $annotation The name of the annotation.
* @return unknown_type * @return The Annotation or NULL, if the requested annotation does not exist.
*/ */
public function getMethodAnnotation(ReflectionMethod $method, $annotation) public function getMethodAnnotation(ReflectionMethod $method, $annotation)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment