AnnotationReader.php 12KB

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