SimpleAnnotationReader.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Doctrine\Common\Annotations;
  3. use ReflectionClass;
  4. use ReflectionMethod;
  5. use ReflectionProperty;
  6. /**
  7. * Simple Annotation Reader.
  8. *
  9. * This annotation reader is intended to be used in projects where you have
  10. * full-control over all annotations that are available.
  11. *
  12. * @deprecated Deprecated in favour of using AnnotationReader
  13. */
  14. class SimpleAnnotationReader implements Reader
  15. {
  16. /** @var DocParser */
  17. private $parser;
  18. /**
  19. * Initializes a new SimpleAnnotationReader.
  20. */
  21. public function __construct()
  22. {
  23. $this->parser = new DocParser();
  24. $this->parser->setIgnoreNotImportedAnnotations(true);
  25. }
  26. /**
  27. * Adds a namespace in which we will look for annotations.
  28. *
  29. * @param string $namespace
  30. *
  31. * @return void
  32. */
  33. public function addNamespace($namespace)
  34. {
  35. $this->parser->addNamespace($namespace);
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function getClassAnnotations(ReflectionClass $class)
  41. {
  42. return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function getMethodAnnotations(ReflectionMethod $method)
  48. {
  49. return $this->parser->parse(
  50. $method->getDocComment(),
  51. 'method ' . $method->getDeclaringClass()->name . '::' . $method->getName() . '()'
  52. );
  53. }
  54. /**
  55. * {@inheritDoc}
  56. */
  57. public function getPropertyAnnotations(ReflectionProperty $property)
  58. {
  59. return $this->parser->parse(
  60. $property->getDocComment(),
  61. 'property ' . $property->getDeclaringClass()->name . '::$' . $property->getName()
  62. );
  63. }
  64. /**
  65. * {@inheritDoc}
  66. */
  67. public function getClassAnnotation(ReflectionClass $class, $annotationName)
  68. {
  69. foreach ($this->getClassAnnotations($class) as $annot) {
  70. if ($annot instanceof $annotationName) {
  71. return $annot;
  72. }
  73. }
  74. return null;
  75. }
  76. /**
  77. * {@inheritDoc}
  78. */
  79. public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
  80. {
  81. foreach ($this->getMethodAnnotations($method) as $annot) {
  82. if ($annot instanceof $annotationName) {
  83. return $annot;
  84. }
  85. }
  86. return null;
  87. }
  88. /**
  89. * {@inheritDoc}
  90. */
  91. public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
  92. {
  93. foreach ($this->getPropertyAnnotations($property) as $annot) {
  94. if ($annot instanceof $annotationName) {
  95. return $annot;
  96. }
  97. }
  98. return null;
  99. }
  100. }