SimpleAnnotationReader.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. // @codingStandardsIgnoreFile
  3. /**
  4. * @file
  5. *
  6. * This class is a near-copy of
  7. * Doctrine\Common\Annotations\SimpleAnnotationReader, which is part of the
  8. * Doctrine project: <http://www.doctrine-project.org>. It was copied from
  9. * version 1.2.7.
  10. *
  11. * Original copyright:
  12. *
  13. * Copyright (c) 2006-2013 Doctrine Project
  14. *
  15. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  16. * this software and associated documentation files (the "Software"), to deal in
  17. * the Software without restriction, including without limitation the rights to
  18. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  19. * of the Software, and to permit persons to whom the Software is furnished to do
  20. * so, subject to the following conditions:
  21. *
  22. * The above copyright notice and this permission notice shall be included in all
  23. * copies or substantial portions of the Software.
  24. */
  25. namespace Drupal\Component\Annotation\Doctrine;
  26. use Doctrine\Common\Annotations\Reader;
  27. /**
  28. * Simple Annotation Reader.
  29. *
  30. * Drupal adds its own version of DocParser and allows for ignoring common
  31. * annotations.
  32. *
  33. * @internal
  34. */
  35. final class SimpleAnnotationReader implements Reader
  36. {
  37. protected $ignoredAnnotations = [
  38. 'addtogroup' => TRUE,
  39. 'code' => TRUE,
  40. 'defgroup' => TRUE,
  41. 'deprecated' => TRUE,
  42. 'endcode' => TRUE,
  43. 'endlink' => TRUE,
  44. 'file' => TRUE,
  45. 'ingroup' => TRUE,
  46. 'group' => TRUE,
  47. 'link' => TRUE,
  48. 'mainpage' => TRUE,
  49. 'param' => TRUE,
  50. 'ref' => TRUE,
  51. 'return' => TRUE,
  52. 'section' => TRUE,
  53. 'see' => TRUE,
  54. 'subsection' => TRUE,
  55. 'throws' => TRUE,
  56. 'todo' => TRUE,
  57. 'var' => TRUE,
  58. '{' => TRUE,
  59. '}' => TRUE,
  60. ];
  61. /**
  62. * @var DocParser
  63. */
  64. private $parser;
  65. /**
  66. * Constructor.
  67. *
  68. * Initializes a new SimpleAnnotationReader.
  69. */
  70. public function __construct()
  71. {
  72. $this->parser = new DocParser();
  73. $this->parser->setIgnoreNotImportedAnnotations(true);
  74. $this->parser->setIgnoredAnnotationNames($this->ignoredAnnotations);
  75. }
  76. /**
  77. * Adds a namespace in which we will look for annotations.
  78. *
  79. * @param string $namespace
  80. *
  81. * @return void
  82. */
  83. public function addNamespace($namespace)
  84. {
  85. $this->parser->addNamespace($namespace);
  86. }
  87. /**
  88. * {@inheritDoc}
  89. */
  90. public function getClassAnnotations(\ReflectionClass $class)
  91. {
  92. return $this->parser->parse($class->getDocComment(), 'class '.$class->getName());
  93. }
  94. /**
  95. * {@inheritDoc}
  96. */
  97. public function getMethodAnnotations(\ReflectionMethod $method)
  98. {
  99. return $this->parser->parse($method->getDocComment(), 'method '.$method->getDeclaringClass()->name.'::'.$method->getName().'()');
  100. }
  101. /**
  102. * {@inheritDoc}
  103. */
  104. public function getPropertyAnnotations(\ReflectionProperty $property)
  105. {
  106. return $this->parser->parse($property->getDocComment(), 'property '.$property->getDeclaringClass()->name.'::$'.$property->getName());
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. public function getClassAnnotation(\ReflectionClass $class, $annotationName)
  112. {
  113. foreach ($this->getClassAnnotations($class) as $annot) {
  114. if ($annot instanceof $annotationName) {
  115. return $annot;
  116. }
  117. }
  118. return null;
  119. }
  120. /**
  121. * {@inheritDoc}
  122. */
  123. public function getMethodAnnotation(\ReflectionMethod $method, $annotationName)
  124. {
  125. foreach ($this->getMethodAnnotations($method) as $annot) {
  126. if ($annot instanceof $annotationName) {
  127. return $annot;
  128. }
  129. }
  130. return null;
  131. }
  132. /**
  133. * {@inheritDoc}
  134. */
  135. public function getPropertyAnnotation(\ReflectionProperty $property, $annotationName)
  136. {
  137. foreach ($this->getPropertyAnnotations($property) as $annot) {
  138. if ($annot instanceof $annotationName) {
  139. return $annot;
  140. }
  141. }
  142. return null;
  143. }
  144. }