AnnotationReader.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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 ReflectionClass;
  23. use ReflectionMethod;
  24. use ReflectionProperty;
  25. /**
  26. * A reader for docblock annotations.
  27. *
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  30. * @author Jonathan Wage <jonwage@gmail.com>
  31. * @author Roman Borschel <roman@code-factory.org>
  32. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  33. */
  34. class AnnotationReader implements Reader
  35. {
  36. /**
  37. * Global map for imports.
  38. *
  39. * @var array
  40. */
  41. private static $globalImports = array(
  42. 'ignoreannotation' => 'Doctrine\Common\Annotations\Annotation\IgnoreAnnotation',
  43. );
  44. /**
  45. * A list with annotations that are not causing exceptions when not resolved to an annotation class.
  46. *
  47. * The names are case sensitive.
  48. *
  49. * @var array
  50. */
  51. private static $globalIgnoredNames = array(
  52. // Annotation tags
  53. 'Annotation' => true, 'Attribute' => true, 'Attributes' => true,
  54. /* Can we enable this? 'Enum' => true, */
  55. 'Required' => true,
  56. 'Target' => true,
  57. // Widely used tags (but not existent in phpdoc)
  58. 'fix' => true , 'fixme' => true,
  59. 'override' => true,
  60. // PHPDocumentor 1 tags
  61. 'abstract'=> true, 'access'=> true,
  62. 'code' => true,
  63. 'deprec'=> true,
  64. 'endcode' => true, 'exception'=> true,
  65. 'final'=> true,
  66. 'ingroup' => true, 'inheritdoc'=> true, 'inheritDoc'=> true,
  67. 'magic' => true,
  68. 'name'=> true,
  69. 'toc' => true, 'tutorial'=> true,
  70. 'private' => true,
  71. 'static'=> true, 'staticvar'=> true, 'staticVar'=> true,
  72. 'throw' => true,
  73. // PHPDocumentor 2 tags.
  74. 'api' => true, 'author'=> true,
  75. 'category'=> true, 'copyright'=> true,
  76. 'deprecated'=> true,
  77. 'example'=> true,
  78. 'filesource'=> true,
  79. 'global'=> true,
  80. 'ignore'=> true, /* Can we enable this? 'index' => true, */ 'internal'=> true,
  81. 'license'=> true, 'link'=> true,
  82. 'method' => true,
  83. 'package'=> true, 'param'=> true, 'property' => true, 'property-read' => true, 'property-write' => true,
  84. 'return'=> true,
  85. 'see'=> true, 'since'=> true, 'source' => true, 'subpackage'=> true,
  86. 'throws'=> true, 'todo'=> true, 'TODO'=> true,
  87. 'usedby'=> true, 'uses' => true,
  88. 'var'=> true, 'version'=> true,
  89. // PHPUnit tags
  90. 'codeCoverageIgnore' => true, 'codeCoverageIgnoreStart' => true, 'codeCoverageIgnoreEnd' => true,
  91. // PHPCheckStyle
  92. 'SuppressWarnings' => true,
  93. // PHPStorm
  94. 'noinspection' => true,
  95. // PEAR
  96. 'package_version' => true,
  97. // PlantUML
  98. 'startuml' => true, 'enduml' => true,
  99. );
  100. /**
  101. * Add a new annotation to the globally ignored annotation names with regard to exception handling.
  102. *
  103. * @param string $name
  104. */
  105. static public function addGlobalIgnoredName($name)
  106. {
  107. self::$globalIgnoredNames[$name] = true;
  108. }
  109. /**
  110. * Annotations parser.
  111. *
  112. * @var \Doctrine\Common\Annotations\DocParser
  113. */
  114. private $parser;
  115. /**
  116. * Annotations parser used to collect parsing metadata.
  117. *
  118. * @var \Doctrine\Common\Annotations\DocParser
  119. */
  120. private $preParser;
  121. /**
  122. * PHP parser used to collect imports.
  123. *
  124. * @var \Doctrine\Common\Annotations\PhpParser
  125. */
  126. private $phpParser;
  127. /**
  128. * In-memory cache mechanism to store imported annotations per class.
  129. *
  130. * @var array
  131. */
  132. private $imports = array();
  133. /**
  134. * In-memory cache mechanism to store ignored annotations per class.
  135. *
  136. * @var array
  137. */
  138. private $ignoredAnnotationNames = array();
  139. /**
  140. * Constructor.
  141. *
  142. * Initializes a new AnnotationReader.
  143. */
  144. public function __construct()
  145. {
  146. if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
  147. throw AnnotationException::optimizerPlusSaveComments();
  148. }
  149. if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
  150. throw AnnotationException::optimizerPlusSaveComments();
  151. }
  152. if (PHP_VERSION_ID < 70000) {
  153. if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.load_comments') === "0" || ini_get('opcache.load_comments') === "0")) {
  154. throw AnnotationException::optimizerPlusLoadComments();
  155. }
  156. if (extension_loaded('Zend OPcache') && ini_get('opcache.load_comments') == 0) {
  157. throw AnnotationException::optimizerPlusLoadComments();
  158. }
  159. }
  160. AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
  161. $this->parser = new DocParser;
  162. $this->preParser = new DocParser;
  163. $this->preParser->setImports(self::$globalImports);
  164. $this->preParser->setIgnoreNotImportedAnnotations(true);
  165. $this->phpParser = new PhpParser;
  166. }
  167. /**
  168. * {@inheritDoc}
  169. */
  170. public function getClassAnnotations(ReflectionClass $class)
  171. {
  172. $this->parser->setTarget(Target::TARGET_CLASS);
  173. $this->parser->setImports($this->getClassImports($class));
  174. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  175. return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
  176. }
  177. /**
  178. * {@inheritDoc}
  179. */
  180. public function getClassAnnotation(ReflectionClass $class, $annotationName)
  181. {
  182. $annotations = $this->getClassAnnotations($class);
  183. foreach ($annotations as $annotation) {
  184. if ($annotation instanceof $annotationName) {
  185. return $annotation;
  186. }
  187. }
  188. return null;
  189. }
  190. /**
  191. * {@inheritDoc}
  192. */
  193. public function getPropertyAnnotations(ReflectionProperty $property)
  194. {
  195. $class = $property->getDeclaringClass();
  196. $context = 'property ' . $class->getName() . "::\$" . $property->getName();
  197. $this->parser->setTarget(Target::TARGET_PROPERTY);
  198. $this->parser->setImports($this->getPropertyImports($property));
  199. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  200. return $this->parser->parse($property->getDocComment(), $context);
  201. }
  202. /**
  203. * {@inheritDoc}
  204. */
  205. public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
  206. {
  207. $annotations = $this->getPropertyAnnotations($property);
  208. foreach ($annotations as $annotation) {
  209. if ($annotation instanceof $annotationName) {
  210. return $annotation;
  211. }
  212. }
  213. return null;
  214. }
  215. /**
  216. * {@inheritDoc}
  217. */
  218. public function getMethodAnnotations(ReflectionMethod $method)
  219. {
  220. $class = $method->getDeclaringClass();
  221. $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
  222. $this->parser->setTarget(Target::TARGET_METHOD);
  223. $this->parser->setImports($this->getMethodImports($method));
  224. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  225. return $this->parser->parse($method->getDocComment(), $context);
  226. }
  227. /**
  228. * {@inheritDoc}
  229. */
  230. public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
  231. {
  232. $annotations = $this->getMethodAnnotations($method);
  233. foreach ($annotations as $annotation) {
  234. if ($annotation instanceof $annotationName) {
  235. return $annotation;
  236. }
  237. }
  238. return null;
  239. }
  240. /**
  241. * Returns the ignored annotations for the given class.
  242. *
  243. * @param \ReflectionClass $class
  244. *
  245. * @return array
  246. */
  247. private function getIgnoredAnnotationNames(ReflectionClass $class)
  248. {
  249. if (isset($this->ignoredAnnotationNames[$name = $class->getName()])) {
  250. return $this->ignoredAnnotationNames[$name];
  251. }
  252. $this->collectParsingMetadata($class);
  253. return $this->ignoredAnnotationNames[$name];
  254. }
  255. /**
  256. * Retrieves imports.
  257. *
  258. * @param \ReflectionClass $class
  259. *
  260. * @return array
  261. */
  262. private function getClassImports(ReflectionClass $class)
  263. {
  264. if (isset($this->imports[$name = $class->getName()])) {
  265. return $this->imports[$name];
  266. }
  267. $this->collectParsingMetadata($class);
  268. return $this->imports[$name];
  269. }
  270. /**
  271. * Retrieves imports for methods.
  272. *
  273. * @param \ReflectionMethod $method
  274. *
  275. * @return array
  276. */
  277. private function getMethodImports(ReflectionMethod $method)
  278. {
  279. $class = $method->getDeclaringClass();
  280. $classImports = $this->getClassImports($class);
  281. if (!method_exists($class, 'getTraits')) {
  282. return $classImports;
  283. }
  284. $traitImports = array();
  285. foreach ($class->getTraits() as $trait) {
  286. if ($trait->hasMethod($method->getName())
  287. && $trait->getFileName() === $method->getFileName()
  288. ) {
  289. $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
  290. }
  291. }
  292. return array_merge($classImports, $traitImports);
  293. }
  294. /**
  295. * Retrieves imports for properties.
  296. *
  297. * @param \ReflectionProperty $property
  298. *
  299. * @return array
  300. */
  301. private function getPropertyImports(ReflectionProperty $property)
  302. {
  303. $class = $property->getDeclaringClass();
  304. $classImports = $this->getClassImports($class);
  305. if (!method_exists($class, 'getTraits')) {
  306. return $classImports;
  307. }
  308. $traitImports = array();
  309. foreach ($class->getTraits() as $trait) {
  310. if ($trait->hasProperty($property->getName())) {
  311. $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
  312. }
  313. }
  314. return array_merge($classImports, $traitImports);
  315. }
  316. /**
  317. * Collects parsing metadata for a given class.
  318. *
  319. * @param \ReflectionClass $class
  320. */
  321. private function collectParsingMetadata(ReflectionClass $class)
  322. {
  323. $ignoredAnnotationNames = self::$globalIgnoredNames;
  324. $annotations = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name);
  325. foreach ($annotations as $annotation) {
  326. if ($annotation instanceof IgnoreAnnotation) {
  327. foreach ($annotation->names AS $annot) {
  328. $ignoredAnnotationNames[$annot] = true;
  329. }
  330. }
  331. }
  332. $name = $class->getName();
  333. $this->imports[$name] = array_merge(
  334. self::$globalImports,
  335. $this->phpParser->parseClass($class),
  336. array('__NAMESPACE__' => $class->getNamespaceName())
  337. );
  338. $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
  339. }
  340. }