AnnotationReader.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 LGPL. 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 Closure;
  22. use ReflectionClass;
  23. use ReflectionMethod;
  24. use ReflectionProperty;
  25. require_once __DIR__ . '/Annotation/IgnoreAnnotation.php';
  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. final 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, 'deprecated'=> true,
  60. 'deprec'=> true, 'author'=> 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. 'codeCoverageIgnoreStart' => true, 'codeCoverageIgnoreEnd' => true,
  65. 'Required' => true, 'Attribute' => true, 'Attributes' => true,
  66. 'Target' => true, 'SuppressWarnings' => true,
  67. );
  68. /**
  69. * Add a new annotation to the globally ignored annotation names with regard to exception handling.
  70. *
  71. * @param string $name
  72. */
  73. static public function addGlobalIgnoredName($name)
  74. {
  75. self::$globalIgnoredNames[$name] = true;
  76. }
  77. /**
  78. * Annotations Parser
  79. *
  80. * @var Doctrine\Common\Annotations\DocParser
  81. */
  82. private $parser;
  83. /**
  84. * Annotations Parser used to collect parsing metadata
  85. *
  86. * @var Doctrine\Common\Annotations\DocParser
  87. */
  88. private $preParser;
  89. /**
  90. * PHP Parser used to collect imports.
  91. *
  92. * @var Doctrine\Common\Annotations\PhpParser
  93. */
  94. private $phpParser;
  95. /**
  96. * In-memory cache mechanism to store imported annotations per class.
  97. *
  98. * @var array
  99. */
  100. private $imports = array();
  101. /**
  102. * In-memory cache mechanism to store ignored annotations per class.
  103. *
  104. * @var array
  105. */
  106. private $ignoredAnnotationNames = array();
  107. /**
  108. * @var string
  109. */
  110. private $defaultAnnotationNamespace = false;
  111. /**
  112. * @var bool
  113. */
  114. private $enablePhpImports = true;
  115. /**
  116. * Constructor. Initializes a new AnnotationReader that uses the given Cache provider.
  117. *
  118. * @param DocParser $parser The parser to use. If none is provided, the default parser is used.
  119. */
  120. public function __construct()
  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. * Ignore not imported annotations and not throw an exception.
  131. *
  132. * @param bool $bool
  133. */
  134. public function setIgnoreNotImportedAnnotations($bool)
  135. {
  136. $this->parser->setIgnoreNotImportedAnnotations($bool);
  137. }
  138. /**
  139. * Detect imports by parsing the use statements of affected files.
  140. *
  141. * @deprecated Will be removed in 3.0, imports will always be enabled.
  142. * @param bool $flag
  143. */
  144. public function setEnableParsePhpImports($flag)
  145. {
  146. $this->enablePhpImports = $flag;
  147. }
  148. /**
  149. * @deprecated Will be removed in 3.0, imports will always be enabled.
  150. * @return bool
  151. */
  152. public function isParsePhpImportsEnabled()
  153. {
  154. return $this->enablePhpImports;
  155. }
  156. /**
  157. * Sets the default namespace that the AnnotationReader should assume for annotations
  158. * with not fully qualified names.
  159. *
  160. * @deprecated This method will be removed in Doctrine Common 3.0
  161. * @param string $defaultNamespace
  162. */
  163. public function setDefaultAnnotationNamespace($defaultNamespace)
  164. {
  165. $this->defaultAnnotationNamespace = $defaultNamespace;
  166. }
  167. /**
  168. * Sets the custom function to use for creating new annotations on the
  169. * underlying parser.
  170. *
  171. * The function is supplied two arguments. The first argument is the name
  172. * of the annotation and the second argument an array of values for this
  173. * annotation. The function is assumed to return an object or NULL.
  174. * Whenever the function returns NULL for an annotation, the implementation falls
  175. * back to the default annotation creation process of the underlying parser.
  176. *
  177. * @deprecated This method will be removed in Doctrine Common 3.0
  178. * @param Closure $func
  179. */
  180. public function setAnnotationCreationFunction(Closure $func)
  181. {
  182. $this->parser->setAnnotationCreationFunction($func);
  183. }
  184. /**
  185. * Sets an alias for an annotation namespace.
  186. *
  187. * @param string $namespace
  188. * @param string $alias
  189. */
  190. public function setAnnotationNamespaceAlias($namespace, $alias)
  191. {
  192. $this->parser->setAnnotationNamespaceAlias($namespace, $alias);
  193. }
  194. /**
  195. * Gets the annotations applied to a class.
  196. *
  197. * @param string|ReflectionClass $class The name or ReflectionClass of the class from which
  198. * the class annotations should be read.
  199. * @return array An array of Annotations.
  200. */
  201. public function getClassAnnotations(ReflectionClass $class)
  202. {
  203. $this->parser->setImports($this->getImports($class));
  204. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  205. return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
  206. }
  207. /**
  208. * Gets a class annotation.
  209. *
  210. * @param ReflectionClass $class The ReflectionClass of the class from which
  211. * the class annotations should be read.
  212. * @param string $annotationName The name of the annotation.
  213. * @return The Annotation or NULL, if the requested annotation does not exist.
  214. */
  215. public function getClassAnnotation(ReflectionClass $class, $annotationName)
  216. {
  217. $annotations = $this->getClassAnnotations($class);
  218. foreach ($annotations as $annotation) {
  219. if ($annotation instanceof $annotationName) {
  220. return $annotation;
  221. }
  222. }
  223. return null;
  224. }
  225. /**
  226. * Gets the annotations applied to a property.
  227. *
  228. * @param string|ReflectionProperty $property The name or ReflectionProperty of the property
  229. * from which the annotations should be read.
  230. * @return array An array of Annotations.
  231. */
  232. public function getPropertyAnnotations(ReflectionProperty $property)
  233. {
  234. $class = $property->getDeclaringClass();
  235. $context = 'property ' . $class->getName() . "::\$" . $property->getName();
  236. $this->parser->setImports($this->getImports($class));
  237. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  238. return $this->parser->parse($property->getDocComment(), $context);
  239. }
  240. /**
  241. * Gets a property annotation.
  242. *
  243. * @param ReflectionProperty $property
  244. * @param string $annotationName The name of the annotation.
  245. * @return The Annotation or NULL, if the requested annotation does not exist.
  246. */
  247. public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
  248. {
  249. $annotations = $this->getPropertyAnnotations($property);
  250. foreach ($annotations as $annotation) {
  251. if ($annotation instanceof $annotationName) {
  252. return $annotation;
  253. }
  254. }
  255. return null;
  256. }
  257. /**
  258. * Gets the annotations applied to a method.
  259. *
  260. * @param ReflectionMethod $property The name or ReflectionMethod of the method from which
  261. * the annotations should be read.
  262. * @return array An array of Annotations.
  263. */
  264. public function getMethodAnnotations(ReflectionMethod $method)
  265. {
  266. $class = $method->getDeclaringClass();
  267. $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
  268. $this->parser->setImports($this->getImports($class));
  269. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  270. return $this->parser->parse($method->getDocComment(), $context);
  271. }
  272. /**
  273. * Gets a method annotation.
  274. *
  275. * @param ReflectionMethod $method
  276. * @param string $annotationName The name of the annotation.
  277. * @return The Annotation or NULL, if the requested annotation does not exist.
  278. */
  279. public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
  280. {
  281. $annotations = $this->getMethodAnnotations($method);
  282. foreach ($annotations as $annotation) {
  283. if ($annotation instanceof $annotationName) {
  284. return $annotation;
  285. }
  286. }
  287. return null;
  288. }
  289. /**
  290. * Returns the ignored annotations for the given class.
  291. *
  292. * @param ReflectionClass $class
  293. * @return array
  294. */
  295. private function getIgnoredAnnotationNames(ReflectionClass $class)
  296. {
  297. if (isset($this->ignoredAnnotationNames[$name = $class->getName()])) {
  298. return $this->ignoredAnnotationNames[$name];
  299. }
  300. $this->collectParsingMetadata($class);
  301. return $this->ignoredAnnotationNames[$name];
  302. }
  303. private function getImports(ReflectionClass $class)
  304. {
  305. if (isset($this->imports[$name = $class->getName()])) {
  306. return $this->imports[$name];
  307. }
  308. $this->collectParsingMetadata($class);
  309. return $this->imports[$name];
  310. }
  311. /**
  312. * Collects parsing metadata for a given class
  313. *
  314. * @param ReflectionClass $class
  315. */
  316. private function collectParsingMetadata(ReflectionClass $class)
  317. {
  318. $imports = self::$globalImports;
  319. $ignoredAnnotationNames = self::$globalIgnoredNames;
  320. if ($this->enablePhpImports) {
  321. $annotations = $this->preParser->parse($class->getDocComment());
  322. foreach ($annotations as $annotation) {
  323. if ($annotation instanceof IgnoreAnnotation) {
  324. foreach ($annotation->names AS $annot) {
  325. $ignoredAnnotationNames[$annot] = true;
  326. }
  327. }
  328. }
  329. }
  330. $name = $class->getName();
  331. $this->imports[$name] = array_merge(
  332. self::$globalImports,
  333. ($this->enablePhpImports) ? $this->phpParser->parseClass($class) : array(),
  334. ($this->enablePhpImports) ? array('__NAMESPACE__' => $class->getNamespaceName()) : array()
  335. );
  336. if ($this->defaultAnnotationNamespace) {
  337. $this->imports[$name]['__DEFAULT__'] = $this->defaultAnnotationNamespace;
  338. }
  339. $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
  340. }
  341. }