[345] | 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 LGPL. For more information, see |
---|
| 17 | * <http://www.doctrine-project.org>. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | namespace Doctrine\Common\Annotations; |
---|
| 21 | |
---|
| 22 | use Doctrine\Common\Annotations\Annotation\Target; |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * Simple Annotation Reader. |
---|
| 26 | * |
---|
| 27 | * This annotation reader is intended to be used in projects where you have |
---|
| 28 | * full-control over all annotations that are available. |
---|
| 29 | * |
---|
| 30 | * @since 2.2 |
---|
| 31 | * @author Johannes M. Schmitt <schmittjoh@gmail.com> |
---|
| 32 | * @author Fabio B. Silva <fabio.bat.silva@gmail.com> |
---|
| 33 | */ |
---|
| 34 | class SimpleAnnotationReader implements Reader |
---|
| 35 | { |
---|
| 36 | /** |
---|
| 37 | * @var DocParser |
---|
| 38 | */ |
---|
| 39 | private $parser; |
---|
| 40 | |
---|
| 41 | /** |
---|
| 42 | * Constructor. |
---|
| 43 | * |
---|
| 44 | * Initializes a new SimpleAnnotationReader. |
---|
| 45 | */ |
---|
| 46 | public function __construct() |
---|
| 47 | { |
---|
| 48 | $this->parser = new DocParser(); |
---|
| 49 | $this->parser->setIgnoreNotImportedAnnotations(true); |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * Adds a namespace in which we will look for annotations. |
---|
| 54 | * |
---|
| 55 | * @param string $namespace |
---|
| 56 | */ |
---|
| 57 | public function addNamespace($namespace) |
---|
| 58 | { |
---|
| 59 | $this->parser->addNamespace($namespace); |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * Gets the annotations applied to a class. |
---|
| 64 | * |
---|
| 65 | * @param ReflectionClass $class The ReflectionClass of the class from which |
---|
| 66 | * the class annotations should be read. |
---|
| 67 | * @return array An array of Annotations. |
---|
| 68 | */ |
---|
| 69 | public function getClassAnnotations(\ReflectionClass $class) |
---|
| 70 | { |
---|
| 71 | return $this->parser->parse($class->getDocComment(), 'class '.$class->getName()); |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * Gets the annotations applied to a method. |
---|
| 76 | * |
---|
| 77 | * @param ReflectionMethod $property The ReflectionMethod of the method from which |
---|
| 78 | * the annotations should be read. |
---|
| 79 | * @return array An array of Annotations. |
---|
| 80 | */ |
---|
| 81 | public function getMethodAnnotations(\ReflectionMethod $method) |
---|
| 82 | { |
---|
| 83 | return $this->parser->parse($method->getDocComment(), 'method '.$method->getDeclaringClass()->name.'::'.$method->getName().'()'); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | /** |
---|
| 87 | * Gets the annotations applied to a property. |
---|
| 88 | * |
---|
| 89 | * @param ReflectionProperty $property The ReflectionProperty of the property |
---|
| 90 | * from which the annotations should be read. |
---|
| 91 | * @return array An array of Annotations. |
---|
| 92 | */ |
---|
| 93 | public function getPropertyAnnotations(\ReflectionProperty $property) |
---|
| 94 | { |
---|
| 95 | return $this->parser->parse($property->getDocComment(), 'property '.$property->getDeclaringClass()->name.'::$'.$property->getName()); |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | /** |
---|
| 99 | * Gets a class annotation. |
---|
| 100 | * |
---|
| 101 | * @param ReflectionClass $class The ReflectionClass of the class from which |
---|
| 102 | * the class annotations should be read. |
---|
| 103 | * @param string $annotationName The name of the annotation. |
---|
| 104 | * @return The Annotation or NULL, if the requested annotation does not exist. |
---|
| 105 | */ |
---|
| 106 | public function getClassAnnotation(\ReflectionClass $class, $annotationName) |
---|
| 107 | { |
---|
| 108 | foreach ($this->getClassAnnotations($class) as $annot) { |
---|
| 109 | if ($annot instanceof $annotationName) { |
---|
| 110 | return $annot; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | return null; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * Gets a method annotation. |
---|
| 119 | * |
---|
| 120 | * @param ReflectionMethod $method |
---|
| 121 | * @param string $annotationName The name of the annotation. |
---|
| 122 | * @return The Annotation or NULL, if the requested annotation does not exist. |
---|
| 123 | */ |
---|
| 124 | public function getMethodAnnotation(\ReflectionMethod $method, $annotationName) |
---|
| 125 | { |
---|
| 126 | foreach ($this->getMethodAnnotations($method) as $annot) { |
---|
| 127 | if ($annot instanceof $annotationName) { |
---|
| 128 | return $annot; |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | return null; |
---|
| 133 | } |
---|
| 134 | |
---|
| 135 | /** |
---|
| 136 | * Gets a property annotation. |
---|
| 137 | * |
---|
| 138 | * @param ReflectionProperty $property |
---|
| 139 | * @param string $annotationName The name of the annotation. |
---|
| 140 | * @return The Annotation or NULL, if the requested annotation does not exist. |
---|
| 141 | */ |
---|
| 142 | public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName) |
---|
| 143 | { |
---|
| 144 | foreach ($this->getPropertyAnnotations($property) as $annot) { |
---|
| 145 | if ($annot instanceof $annotationName) { |
---|
| 146 | return $annot; |
---|
| 147 | } |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | return null; |
---|
| 151 | } |
---|
| 152 | } |
---|