AnnotationReader.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. // Symfony 3.3 Cache Adapter
  100. 'experimental' => true
  101. );
  102. /**
  103. * A list with annotations that are not causing exceptions when not resolved to an annotation class.
  104. *
  105. * The names are case sensitive.
  106. *
  107. * @var array
  108. */
  109. private static $globalIgnoredNamespaces = array();
  110. /**
  111. * Add a new annotation to the globally ignored annotation names with regard to exception handling.
  112. *
  113. * @param string $name
  114. */
  115. static public function addGlobalIgnoredName($name)
  116. {
  117. self::$globalIgnoredNames[$name] = true;
  118. }
  119. /**
  120. * Add a new annotation to the globally ignored annotation namespaces with regard to exception handling.
  121. *
  122. * @param string $namespace
  123. */
  124. static public function addGlobalIgnoredNamespace($namespace)
  125. {
  126. self::$globalIgnoredNamespaces[$namespace] = true;
  127. }
  128. /**
  129. * Annotations parser.
  130. *
  131. * @var \Doctrine\Common\Annotations\DocParser
  132. */
  133. private $parser;
  134. /**
  135. * Annotations parser used to collect parsing metadata.
  136. *
  137. * @var \Doctrine\Common\Annotations\DocParser
  138. */
  139. private $preParser;
  140. /**
  141. * PHP parser used to collect imports.
  142. *
  143. * @var \Doctrine\Common\Annotations\PhpParser
  144. */
  145. private $phpParser;
  146. /**
  147. * In-memory cache mechanism to store imported annotations per class.
  148. *
  149. * @var array
  150. */
  151. private $imports = array();
  152. /**
  153. * In-memory cache mechanism to store ignored annotations per class.
  154. *
  155. * @var array
  156. */
  157. private $ignoredAnnotationNames = array();
  158. /**
  159. * Constructor.
  160. *
  161. * Initializes a new AnnotationReader.
  162. *
  163. * @param DocParser $parser
  164. *
  165. * @throws AnnotationException
  166. */
  167. public function __construct(DocParser $parser = null)
  168. {
  169. if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
  170. throw AnnotationException::optimizerPlusSaveComments();
  171. }
  172. if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
  173. throw AnnotationException::optimizerPlusSaveComments();
  174. }
  175. if (PHP_VERSION_ID < 70000) {
  176. if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.load_comments') === "0" || ini_get('opcache.load_comments') === "0")) {
  177. throw AnnotationException::optimizerPlusLoadComments();
  178. }
  179. if (extension_loaded('Zend OPcache') && ini_get('opcache.load_comments') == 0) {
  180. throw AnnotationException::optimizerPlusLoadComments();
  181. }
  182. }
  183. AnnotationRegistry::registerFile(__DIR__ . '/Annotation/IgnoreAnnotation.php');
  184. $this->parser = $parser ?: new DocParser();
  185. $this->preParser = new DocParser;
  186. $this->preParser->setImports(self::$globalImports);
  187. $this->preParser->setIgnoreNotImportedAnnotations(true);
  188. $this->phpParser = new PhpParser;
  189. }
  190. /**
  191. * {@inheritDoc}
  192. */
  193. public function getClassAnnotations(ReflectionClass $class)
  194. {
  195. $this->parser->setTarget(Target::TARGET_CLASS);
  196. $this->parser->setImports($this->getClassImports($class));
  197. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  198. $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
  199. return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
  200. }
  201. /**
  202. * {@inheritDoc}
  203. */
  204. public function getClassAnnotation(ReflectionClass $class, $annotationName)
  205. {
  206. $annotations = $this->getClassAnnotations($class);
  207. foreach ($annotations as $annotation) {
  208. if ($annotation instanceof $annotationName) {
  209. return $annotation;
  210. }
  211. }
  212. return null;
  213. }
  214. /**
  215. * {@inheritDoc}
  216. */
  217. public function getPropertyAnnotations(ReflectionProperty $property)
  218. {
  219. $class = $property->getDeclaringClass();
  220. $context = 'property ' . $class->getName() . "::\$" . $property->getName();
  221. $this->parser->setTarget(Target::TARGET_PROPERTY);
  222. $this->parser->setImports($this->getPropertyImports($property));
  223. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  224. $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
  225. return $this->parser->parse($property->getDocComment(), $context);
  226. }
  227. /**
  228. * {@inheritDoc}
  229. */
  230. public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
  231. {
  232. $annotations = $this->getPropertyAnnotations($property);
  233. foreach ($annotations as $annotation) {
  234. if ($annotation instanceof $annotationName) {
  235. return $annotation;
  236. }
  237. }
  238. return null;
  239. }
  240. /**
  241. * {@inheritDoc}
  242. */
  243. public function getMethodAnnotations(ReflectionMethod $method)
  244. {
  245. $class = $method->getDeclaringClass();
  246. $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
  247. $this->parser->setTarget(Target::TARGET_METHOD);
  248. $this->parser->setImports($this->getMethodImports($method));
  249. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  250. $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
  251. return $this->parser->parse($method->getDocComment(), $context);
  252. }
  253. /**
  254. * {@inheritDoc}
  255. */
  256. public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
  257. {
  258. $annotations = $this->getMethodAnnotations($method);
  259. foreach ($annotations as $annotation) {
  260. if ($annotation instanceof $annotationName) {
  261. return $annotation;
  262. }
  263. }
  264. return null;
  265. }
  266. /**
  267. * Returns the ignored annotations for the given class.
  268. *
  269. * @param \ReflectionClass $class
  270. *
  271. * @return array
  272. */
  273. private function getIgnoredAnnotationNames(ReflectionClass $class)
  274. {
  275. $name = $class->getName();
  276. if (isset($this->ignoredAnnotationNames[$name])) {
  277. return $this->ignoredAnnotationNames[$name];
  278. }
  279. $this->collectParsingMetadata($class);
  280. return $this->ignoredAnnotationNames[$name];
  281. }
  282. /**
  283. * Retrieves imports.
  284. *
  285. * @param \ReflectionClass $class
  286. *
  287. * @return array
  288. */
  289. private function getClassImports(ReflectionClass $class)
  290. {
  291. $name = $class->getName();
  292. if (isset($this->imports[$name])) {
  293. return $this->imports[$name];
  294. }
  295. $this->collectParsingMetadata($class);
  296. return $this->imports[$name];
  297. }
  298. /**
  299. * Retrieves imports for methods.
  300. *
  301. * @param \ReflectionMethod $method
  302. *
  303. * @return array
  304. */
  305. private function getMethodImports(ReflectionMethod $method)
  306. {
  307. $class = $method->getDeclaringClass();
  308. $classImports = $this->getClassImports($class);
  309. if (!method_exists($class, 'getTraits')) {
  310. return $classImports;
  311. }
  312. $traitImports = array();
  313. foreach ($class->getTraits() as $trait) {
  314. if ($trait->hasMethod($method->getName())
  315. && $trait->getFileName() === $method->getFileName()
  316. ) {
  317. $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
  318. }
  319. }
  320. return array_merge($classImports, $traitImports);
  321. }
  322. /**
  323. * Retrieves imports for properties.
  324. *
  325. * @param \ReflectionProperty $property
  326. *
  327. * @return array
  328. */
  329. private function getPropertyImports(ReflectionProperty $property)
  330. {
  331. $class = $property->getDeclaringClass();
  332. $classImports = $this->getClassImports($class);
  333. if (!method_exists($class, 'getTraits')) {
  334. return $classImports;
  335. }
  336. $traitImports = array();
  337. foreach ($class->getTraits() as $trait) {
  338. if ($trait->hasProperty($property->getName())) {
  339. $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
  340. }
  341. }
  342. return array_merge($classImports, $traitImports);
  343. }
  344. /**
  345. * Collects parsing metadata for a given class.
  346. *
  347. * @param \ReflectionClass $class
  348. */
  349. private function collectParsingMetadata(ReflectionClass $class)
  350. {
  351. $ignoredAnnotationNames = self::$globalIgnoredNames;
  352. $annotations = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name);
  353. foreach ($annotations as $annotation) {
  354. if ($annotation instanceof IgnoreAnnotation) {
  355. foreach ($annotation->names AS $annot) {
  356. $ignoredAnnotationNames[$annot] = true;
  357. }
  358. }
  359. }
  360. $name = $class->getName();
  361. $this->imports[$name] = array_merge(
  362. self::$globalImports,
  363. $this->phpParser->parseClass($class),
  364. array('__NAMESPACE__' => $class->getNamespaceName())
  365. );
  366. $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
  367. }
  368. }