SimpleAnnotationReader.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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\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. *
  62. * @return array An array of Annotations.
  63. */
  64. public function getClassAnnotations(\ReflectionClass $class)
  65. {
  66. return $this->parser->parse($class->getDocComment(), 'class '.$class->getName());
  67. }
  68. /**
  69. * Gets the annotations applied to a method.
  70. *
  71. * @param \ReflectionMethod $method The ReflectionMethod of the method from which
  72. * the annotations should be read.
  73. *
  74. * @return array An array of Annotations.
  75. */
  76. public function getMethodAnnotations(\ReflectionMethod $method)
  77. {
  78. return $this->parser->parse($method->getDocComment(), 'method '.$method->getDeclaringClass()->name.'::'.$method->getName().'()');
  79. }
  80. /**
  81. * Gets the annotations applied to a property.
  82. *
  83. * @param \ReflectionProperty $property The ReflectionProperty of the property
  84. * from which the annotations should be read.
  85. *
  86. * @return array An array of Annotations.
  87. */
  88. public function getPropertyAnnotations(\ReflectionProperty $property)
  89. {
  90. return $this->parser->parse($property->getDocComment(), 'property '.$property->getDeclaringClass()->name.'::$'.$property->getName());
  91. }
  92. /**
  93. * Gets a class annotation.
  94. *
  95. * @param \ReflectionClass $class The ReflectionClass of the class from which
  96. * the class annotations should be read.
  97. * @param string $annotationName The name of the annotation.
  98. *
  99. * @return mixed The Annotation or NULL, if the requested annotation does not exist.
  100. */
  101. public function getClassAnnotation(\ReflectionClass $class, $annotationName)
  102. {
  103. foreach ($this->getClassAnnotations($class) as $annot) {
  104. if ($annot instanceof $annotationName) {
  105. return $annot;
  106. }
  107. }
  108. return null;
  109. }
  110. /**
  111. * Gets a method annotation.
  112. *
  113. * @param \ReflectionMethod $method
  114. * @param string $annotationName The name of the annotation.
  115. *
  116. * @return mixed The Annotation or NULL, if the requested annotation does not exist.
  117. */
  118. public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
  119. {
  120. foreach ($this->getMethodAnnotations($method) as $annot) {
  121. if ($annot instanceof $annotationName) {
  122. return $annot;
  123. }
  124. }
  125. return null;
  126. }
  127. /**
  128. * Gets a property annotation.
  129. *
  130. * @param \ReflectionProperty $property
  131. * @param string $annotationName The name of the annotation.
  132. * @return mixed The Annotation or NULL, if the requested annotation does not exist.
  133. */
  134. public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
  135. {
  136. foreach ($this->getPropertyAnnotations($property) as $annot) {
  137. if ($annot instanceof $annotationName) {
  138. return $annot;
  139. }
  140. }
  141. return null;
  142. }
  143. }