AnnotationReader.php 6.52 KB
Newer Older
1
<?php
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 *  $Id$
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * This software consists of voluntary contributions made by many individuals
 * and is licensed under the LGPL. For more information, see
 * <http://www.doctrine-project.org>.
 */
21 22 23

namespace Doctrine\Common\Annotations;

24 25 26 27
use \ReflectionClass, 
    \ReflectionMethod, 
    \ReflectionProperty,
    Doctrine\Common\Cache\Cache;
28

29 30 31
/**
 * A reader for docblock annotations.
 * 
32 33 34 35 36 37 38
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @link    www.doctrine-project.org
 * @since   2.0
 * @version $Revision: 3938 $
 * @author  Guilherme Blanco <guilhermeblanco@hotmail.com>
 * @author  Jonathan Wage <jonwage@gmail.com>
 * @author  Roman Borschel <roman@code-factory.org>
39
 */
40 41
class AnnotationReader
{
42 43 44 45 46 47
    /**
     * Cache salt
     *
     * @var string
     * @static
     */
48
    private static $CACHE_SALT = "@<Annot>";
49 50 51 52 53 54
    
    /**
     * Annotations Parser
     *
     * @var Doctrine\Common\Annotations\Parser
     */
55
    private $_parser;
56 57 58 59 60 61
    
    /**
     * Cache machanism to store processed Annotations
     *
     * @var Doctrine\Common\Cache\Cache
     */
62 63
    private $_cache;
    
64
    /**
65 66
     * Constructor. Initializes a new AnnotationReader that uses the given 
     * Cache provider.
67 68 69
     * 
     * @param Cache $cache The cache provider to use.
     */
70 71 72 73 74 75
    public function __construct(Cache $cache)
    {
        $this->_parser = new Parser;
        $this->_cache = $cache;
    }
    
76
    /**
77 78
     * Sets the default namespace that the AnnotationReader should assume for annotations
     * with not fully qualified names.
79
     * 
80
     * @param string $defaultNamespace
81
     */
82 83 84 85 86 87 88 89 90 91 92 93
    public function setDefaultAnnotationNamespace($defaultNamespace)
    {
        $this->_parser->setDefaultAnnotationNamespace($defaultNamespace);
    }
    
    /**
     * Gets the annotations applied to a class.
     * 
     * @param string|ReflectionClass $class The name or ReflectionClass of the class from which
     * the class annotations should be read.
     * @return array An array of Annotations.
     */
94
    public function getClassAnnotations(ReflectionClass $class)
95
    {
96
        $cacheKey = $class->getName() . self::$CACHE_SALT;
97
        
98 99
        if ($this->_cache->contains($cacheKey)) {
            return $this->_cache->fetch($cacheKey);
100 101
        }
        
102
        $annotations = $this->_parser->parse($class->getDocComment(), "class ".$class->getName());
103
        $this->_cache->save($cacheKey, $annotations, null);
104
        
105
        return $annotations;
106 107
    }
    
108
    /**
109
     * Gets a class annotation.
110 111
     * 
     * @param $class
112 113
     * @param string $annotation The name of the annotation.
     * @return The Annotation or NULL, if the requested annotation does not exist.
114 115
     */
    public function getClassAnnotation(ReflectionClass $class, $annotation)
116 117 118 119 120 121 122 123 124 125 126 127 128
    {
        $annotations = $this->getClassAnnotations($class);
        return isset($annotations[$annotation]) ? $annotations[$annotation] : null;
    }
    
    /**
     * Gets the annotations applied to a property.
     * 
     * @param string|ReflectionClass $class The name or ReflectionClass of the class that owns the property.
     * @param string|ReflectionProperty $property The name or ReflectionProperty of the property
     * from which the annotations should be read.
     * @return array An array of Annotations.
     */
129
    public function getPropertyAnnotations(ReflectionProperty $property)
130
    {
131
        $cacheKey = $property->getDeclaringClass()->getName() . '$' . $property->getName() . self::$CACHE_SALT;
132
        
133 134
        if ($this->_cache->contains($cacheKey)) {
            return $this->_cache->fetch($cacheKey);
135
        }
136 137 138

        $context = "property ".$property->getDeclaringClass()->getName()."::\$".$property->getName();
        $annotations = $this->_parser->parse($property->getDocComment(), $context);
139
        $this->_cache->save($cacheKey, $annotations, null);
140
        
141
        return $annotations;
142 143
    }
    
144
    /**
145
     * Gets a property annotation.
146
     * 
147 148 149
     * @param ReflectionProperty $property
     * @param string $annotation The name of the annotation.
     * @return The Annotation or NULL, if the requested annotation does not exist.
150 151
     */
    public function getPropertyAnnotation(ReflectionProperty $property, $annotation)
152
    {
153
        $annotations = $this->getPropertyAnnotations($property);
154 155 156 157 158 159 160 161 162 163 164
        return isset($annotations[$annotation]) ? $annotations[$annotation] : null;
    }
    
    /**
     * Gets the annotations applied to a method.
     * 
     * @param string|ReflectionClass $class The name or ReflectionClass of the class that owns the method.
     * @param string|ReflectionMethod $property The name or ReflectionMethod of the method from which
     * the annotations should be read.
     * @return array An array of Annotations.
     */
165
    public function getMethodAnnotations(ReflectionMethod $method)
166
    {
167
        $cacheKey = $method->getDeclaringClass()->getName() . '#' . $method->getName() . self::$CACHE_SALT;
168
        
169 170
        if ($this->_cache->contains($cacheKey)) {
            return $this->_cache->fetch($cacheKey);
171
        }
172 173 174

        $context = "method ".$method->getDeclaringClass()->getName()."::".$method->getName()."()";
        $annotations = $this->_parser->parse($method->getDocComment(), $context);
175
        $this->_cache->save($cacheKey, $annotations, null);
176
        
177
        return $annotations;
178 179
    }
    
180
    /**
181
     * Gets a method annotation.
182
     * 
183 184 185
     * @param ReflectionMethod $method
     * @param string $annotation The name of the annotation.
     * @return The Annotation or NULL, if the requested annotation does not exist.
186 187
     */
    public function getMethodAnnotation(ReflectionMethod $method, $annotation)
188
    {
189
        $annotations = $this->getMethodAnnotations($method);
190 191 192
        return isset($annotations[$annotation]) ? $annotations[$annotation] : null;
    }
}