菜谱项目

AnnotationReader.php 13KB

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