annotations.rst 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. Handling Annotations
  2. ====================
  3. There are several different approaches to handling annotations in PHP.
  4. Doctrine Annotations maps docblock annotations to PHP classes. Because
  5. not all docblock annotations are used for metadata purposes a filter is
  6. applied to ignore or skip classes that are not Doctrine annotations.
  7. Take a look at the following code snippet:
  8. .. code-block:: php
  9. namespace MyProject\Entities;
  10. use Doctrine\ORM\Mapping AS ORM;
  11. use Symfony\Component\Validator\Constraints AS Assert;
  12. /**
  13. * @author Benjamin Eberlei
  14. * @ORM\Entity
  15. * @MyProject\Annotations\Foobarable
  16. */
  17. class User
  18. {
  19. /**
  20. * @ORM\Id @ORM\Column @ORM\GeneratedValue
  21. * @dummy
  22. * @var int
  23. */
  24. private $id;
  25. /**
  26. * @ORM\Column(type="string")
  27. * @Assert\NotEmpty
  28. * @Assert\Email
  29. * @var string
  30. */
  31. private $email;
  32. }
  33. In this snippet you can see a variety of different docblock annotations:
  34. - Documentation annotations such as ``@var`` and ``@author``. These
  35. annotations are ignored and never considered for throwing an
  36. exception due to wrongly used annotations.
  37. - Annotations imported through use statements. The statement ``use
  38. Doctrine\ORM\Mapping AS ORM`` makes all classes under that namespace
  39. available as ``@ORM\ClassName``. Same goes for the import of
  40. ``@Assert``.
  41. - The ``@dummy`` annotation. It is not a documentation annotation and
  42. not ignored. For Doctrine Annotations it is not entirely clear how
  43. to handle this annotation. Depending on the configuration an exception
  44. (unknown annotation) will be thrown when parsing this annotation.
  45. - The fully qualified annotation ``@MyProject\Annotations\Foobarable``.
  46. This is transformed directly into the given class name.
  47. How are these annotations loaded? From looking at the code you could
  48. guess that the ORM Mapping, Assert Validation and the fully qualified
  49. annotation can just be loaded using
  50. the defined PHP autoloaders. This is not the case however: For error
  51. handling reasons every check for class existence inside the
  52. ``AnnotationReader`` sets the second parameter $autoload
  53. of ``class_exists($name, $autoload)`` to false. To work flawlessly the
  54. ``AnnotationReader`` requires silent autoloaders which many autoloaders are
  55. not. Silent autoloading is NOT part of the `PSR-0 specification
  56. <https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md>`_
  57. for autoloading.
  58. This is why Doctrine Annotations uses its own autoloading mechanism
  59. through a global registry. If you are wondering about the annotation
  60. registry being global, there is no other way to solve the architectural
  61. problems of autoloading annotation classes in a straightforward fashion.
  62. Additionally if you think about PHP autoloading then you recognize it is
  63. a global as well.
  64. To anticipate the configuration section, making the above PHP class work
  65. with Doctrine Annotations requires this setup:
  66. .. code-block:: php
  67. use Doctrine\Common\Annotations\AnnotationReader;
  68. $reader = new AnnotationReader();
  69. AnnotationReader::addGlobalIgnoredName('dummy');
  70. We create the actual ``AnnotationReader`` instance.
  71. Note that we also add ``dummy`` to the global list of ignored
  72. annotations for which we do not throw exceptions. Setting this is
  73. necessary in our example case, otherwise ``@dummy`` would trigger an
  74. exception to be thrown during the parsing of the docblock of
  75. ``MyProject\Entities\User#id``.
  76. Setup and Configuration
  77. -----------------------
  78. To use the annotations library is simple, you just need to create a new
  79. ``AnnotationReader`` instance:
  80. .. code-block:: php
  81. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  82. This creates a simple annotation reader with no caching other than in
  83. memory (in php arrays). Since parsing docblocks can be expensive you
  84. should cache this process by using a caching reader.
  85. To cache annotations, you can create a ``Doctrine\Common\Annotations\PsrCachedReader``.
  86. This reader decorates the original reader and stores all annotations in a PSR-6
  87. cache:
  88. .. code-block:: php
  89. use Doctrine\Common\Annotations\AnnotationReader;
  90. use Doctrine\Common\Annotations\PsrCachedReader;
  91. $cache = ... // instantiate a PSR-6 Cache pool
  92. $reader = new PsrCachedReader(
  93. new AnnotationReader(),
  94. $cache,
  95. $debug = true
  96. );
  97. The ``debug`` flag is used here as well to invalidate the cache files
  98. when the PHP class with annotations changed and should be used during
  99. development.
  100. .. warning ::
  101. The ``AnnotationReader`` works and caches under the
  102. assumption that all annotations of a doc-block are processed at
  103. once. That means that annotation classes that do not exist and
  104. aren't loaded and cannot be autoloaded (using the
  105. AnnotationRegistry) would never be visible and not accessible if a
  106. cache is used unless the cache is cleared and the annotations
  107. requested again, this time with all annotations defined.
  108. By default the annotation reader returns a list of annotations with
  109. numeric indexes. If you want your annotations to be indexed by their
  110. class name you can wrap the reader in an ``IndexedReader``:
  111. .. code-block:: php
  112. use Doctrine\Common\Annotations\AnnotationReader;
  113. use Doctrine\Common\Annotations\IndexedReader;
  114. $reader = new IndexedReader(new AnnotationReader());
  115. .. warning::
  116. You should never wrap the indexed reader inside a cached reader,
  117. only the other way around. This way you can re-use the cache with
  118. indexed or numeric keys, otherwise your code may experience failures
  119. due to caching in a numerical or indexed format.
  120. Ignoring missing exceptions
  121. ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  122. By default an exception is thrown from the ``AnnotationReader`` if an
  123. annotation was found that:
  124. - is not part of the list of ignored "documentation annotations";
  125. - was not imported through a use statement;
  126. - is not a fully qualified class that exists.
  127. You can disable this behavior for specific names if your docblocks do
  128. not follow strict requirements:
  129. .. code-block:: php
  130. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  131. AnnotationReader::addGlobalIgnoredName('foo');
  132. PHP Imports
  133. ~~~~~~~~~~~
  134. By default the annotation reader parses the use-statement of a php file
  135. to gain access to the import rules and register them for the annotation
  136. processing. Only if you are using PHP Imports can you validate the
  137. correct usage of annotations and throw exceptions if you misspelled an
  138. annotation. This mechanism is enabled by default.
  139. To ease the upgrade path, we still allow you to disable this mechanism.
  140. Note however that we will remove this in future versions:
  141. .. code-block:: php
  142. $reader = new \Doctrine\Common\Annotations\AnnotationReader();
  143. $reader->setEnabledPhpImports(false);