SimpleAnnotationReader.php 5.0 KB

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