AnnotationReader.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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\IgnoreAnnotation;
  21. use Doctrine\Common\Annotations\Annotation\Target;
  22. use Closure;
  23. use ReflectionClass;
  24. use ReflectionMethod;
  25. use ReflectionProperty;
  26. /**
  27. * A reader for docblock annotations.
  28. *
  29. * @author Benjamin Eberlei <kontakt@beberlei.de>
  30. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  31. * @author Jonathan Wage <jonwage@gmail.com>
  32. * @author Roman Borschel <roman@code-factory.org>
  33. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  34. */
  35. class AnnotationReader implements Reader
  36. {
  37. /**
  38. * Global map for imports.
  39. *
  40. * @var array
  41. */
  42. private static $globalImports = array(
  43. 'ignoreannotation' => 'Doctrine\Common\Annotations\Annotation\IgnoreAnnotation',
  44. );
  45. /**
  46. * A list with annotations that are not causing exceptions when not resolved to an annotation class.
  47. *
  48. * The names are case sensitive.
  49. *
  50. * @var array
  51. */
  52. private static $globalIgnoredNames = array(
  53. 'access'=> true, 'author'=> true, 'copyright'=> true, 'deprecated'=> true,
  54. 'example'=> true, 'ignore'=> true, 'internal'=> true, 'link'=> true, 'see'=> true,
  55. 'since'=> true, 'tutorial'=> true, 'version'=> true, 'package'=> true,
  56. 'subpackage'=> true, 'name'=> true, 'global'=> true, 'param'=> true,
  57. 'return'=> true, 'staticvar'=> true, 'category'=> true, 'staticVar'=> true,
  58. 'static'=> true, 'var'=> true, 'throws'=> true, 'inheritdoc'=> true,
  59. 'inheritDoc'=> true, 'license'=> true, 'todo'=> true, 'TODO'=> true,
  60. 'deprec'=> true, 'property' => true, 'method' => true,
  61. 'abstract'=> true, 'exception'=> true, 'magic' => true, 'api' => true,
  62. 'final'=> true, 'filesource'=> true, 'throw' => true, 'uses' => true,
  63. 'usedby'=> true, 'private' => true, 'Annotation' => true, 'override' => true,
  64. 'codeCoverageIgnore' => true, 'codeCoverageIgnoreStart' => true, 'codeCoverageIgnoreEnd' => true,
  65. 'Required' => true, 'Attribute' => true, 'Attributes' => true,
  66. 'Target' => true, 'SuppressWarnings' => true,
  67. 'ingroup' => true, 'code' => true, 'endcode' => true,
  68. 'package_version' => true, 'fixme' => true
  69. );
  70. /**
  71. * Add a new annotation to the globally ignored annotation names with regard to exception handling.
  72. *
  73. * @param string $name
  74. */
  75. static public function addGlobalIgnoredName($name)
  76. {
  77. self::$globalIgnoredNames[$name] = true;
  78. }
  79. /**
  80. * Annotations Parser
  81. *
  82. * @var \Doctrine\Common\Annotations\DocParser
  83. */
  84. private $parser;
  85. /**
  86. * Annotations Parser used to collect parsing metadata
  87. *
  88. * @var \Doctrine\Common\Annotations\DocParser
  89. */
  90. private $preParser;
  91. /**
  92. * PHP Parser used to collect imports.
  93. *
  94. * @var \Doctrine\Common\Annotations\PhpParser
  95. */
  96. private $phpParser;
  97. /**
  98. * In-memory cache mechanism to store imported annotations per class.
  99. *
  100. * @var array
  101. */
  102. private $imports = array();
  103. /**
  104. * In-memory cache mechanism to store ignored annotations per class.
  105. *
  106. * @var array
  107. */
  108. private $ignoredAnnotationNames = array();
  109. /**
  110. * Constructor.
  111. *
  112. * Initializes a new AnnotationReader.
  113. */
  114. public function __construct()
  115. {
  116. if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
  117. throw AnnotationException::optimizerPlusSaveComments();
  118. }
  119. if (extension_loaded('opcache') && ini_get('opcache.save_comments') == 0) {
  120. throw AnnotationException::optimizerPlusSaveComments();
  121. }
  122. AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
  123. $this->parser = new DocParser;
  124. $this->preParser = new DocParser;
  125. $this->preParser->setImports(self::$globalImports);
  126. $this->preParser->setIgnoreNotImportedAnnotations(true);
  127. $this->phpParser = new PhpParser;
  128. }
  129. /**
  130. * Gets the annotations applied to a class.
  131. *
  132. * @param ReflectionClass $class The ReflectionClass of the class from which
  133. * the class annotations should be read.
  134. * @return array An array of Annotations.
  135. */
  136. public function getClassAnnotations(ReflectionClass $class)
  137. {
  138. $this->parser->setTarget(Target::TARGET_CLASS);
  139. $this->parser->setImports($this->getImports($class));
  140. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  141. return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
  142. }
  143. /**
  144. * Gets a class annotation.
  145. *
  146. * @param ReflectionClass $class The ReflectionClass of the class from which
  147. * the class annotations should be read.
  148. * @param string $annotationName The name of the annotation.
  149. * @return mixed The Annotation or NULL, if the requested annotation does not exist.
  150. */
  151. public function getClassAnnotation(ReflectionClass $class, $annotationName)
  152. {
  153. $annotations = $this->getClassAnnotations($class);
  154. foreach ($annotations as $annotation) {
  155. if ($annotation instanceof $annotationName) {
  156. return $annotation;
  157. }
  158. }
  159. return null;
  160. }
  161. /**
  162. * Gets the annotations applied to a property.
  163. *
  164. * @param ReflectionProperty $property The ReflectionProperty of the property
  165. * from which the annotations should be read.
  166. * @return array An array of Annotations.
  167. */
  168. public function getPropertyAnnotations(ReflectionProperty $property)
  169. {
  170. $class = $property->getDeclaringClass();
  171. $context = 'property ' . $class->getName() . "::\$" . $property->getName();
  172. $this->parser->setTarget(Target::TARGET_PROPERTY);
  173. $this->parser->setImports($this->getImports($class));
  174. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  175. return $this->parser->parse($property->getDocComment(), $context);
  176. }
  177. /**
  178. * Gets a property annotation.
  179. *
  180. * @param ReflectionProperty $property
  181. * @param string $annotationName The name of the annotation.
  182. * @return mixed The Annotation or NULL, if the requested annotation does not exist.
  183. */
  184. public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
  185. {
  186. $annotations = $this->getPropertyAnnotations($property);
  187. foreach ($annotations as $annotation) {
  188. if ($annotation instanceof $annotationName) {
  189. return $annotation;
  190. }
  191. }
  192. return null;
  193. }
  194. /**
  195. * Gets the annotations applied to a method.
  196. *
  197. * @param \ReflectionMethod $method The ReflectionMethod of the method from which
  198. * the annotations should be read.
  199. *
  200. * @return array An array of Annotations.
  201. */
  202. public function getMethodAnnotations(ReflectionMethod $method)
  203. {
  204. $class = $method->getDeclaringClass();
  205. $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
  206. $this->parser->setTarget(Target::TARGET_METHOD);
  207. $this->parser->setImports($this->getImports($class));
  208. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  209. return $this->parser->parse($method->getDocComment(), $context);
  210. }
  211. /**
  212. * Gets a method annotation.
  213. *
  214. * @param ReflectionMethod $method
  215. * @param string $annotationName The name of the annotation.
  216. * @return mixed The Annotation or NULL, if the requested annotation does not exist.
  217. */
  218. public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
  219. {
  220. $annotations = $this->getMethodAnnotations($method);
  221. foreach ($annotations as $annotation) {
  222. if ($annotation instanceof $annotationName) {
  223. return $annotation;
  224. }
  225. }
  226. return null;
  227. }
  228. /**
  229. * Returns the ignored annotations for the given class.
  230. *
  231. * @param ReflectionClass $class
  232. * @return array
  233. */
  234. private function getIgnoredAnnotationNames(ReflectionClass $class)
  235. {
  236. if (isset($this->ignoredAnnotationNames[$name = $class->getName()])) {
  237. return $this->ignoredAnnotationNames[$name];
  238. }
  239. $this->collectParsingMetadata($class);
  240. return $this->ignoredAnnotationNames[$name];
  241. }
  242. /**
  243. * Retrieve imports
  244. *
  245. * @param \ReflectionClass $class
  246. * @return array
  247. */
  248. private function getImports(ReflectionClass $class)
  249. {
  250. if (isset($this->imports[$name = $class->getName()])) {
  251. return $this->imports[$name];
  252. }
  253. $this->collectParsingMetadata($class);
  254. return $this->imports[$name];
  255. }
  256. /**
  257. * Collects parsing metadata for a given class
  258. *
  259. * @param ReflectionClass $class
  260. */
  261. private function collectParsingMetadata(ReflectionClass $class)
  262. {
  263. $ignoredAnnotationNames = self::$globalIgnoredNames;
  264. $annotations = $this->preParser->parse($class->getDocComment(), 'class '.$class->name);
  265. foreach ($annotations as $annotation) {
  266. if ($annotation instanceof IgnoreAnnotation) {
  267. foreach ($annotation->names AS $annot) {
  268. $ignoredAnnotationNames[$annot] = true;
  269. }
  270. }
  271. }
  272. $name = $class->getName();
  273. $this->imports[$name] = array_merge(
  274. self::$globalImports,
  275. $this->phpParser->parseClass($class),
  276. array('__NAMESPACE__' => $class->getNamespaceName())
  277. );
  278. $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
  279. }
  280. }