annotations.rst 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. Annotations
  2. -----------
  3. @Inject
  4. ~~~~~~~~~
  5. This marks a property, or parameter for injection:
  6. .. code-block :: php
  7. <?php
  8. use JMS\DiExtraBundle\Annotation\Inject;
  9. class Controller
  10. {
  11. /**
  12. * @Inject("security.context", required = false)
  13. */
  14. private $securityContext;
  15. /**
  16. * @Inject("%kernel.cache_dir%")
  17. */
  18. private $cacheDir;
  19. /**
  20. * @Inject
  21. */
  22. private $session;
  23. }
  24. .. tip ::
  25. If you do not specify the service explicitly, we will try to guess it based on the name
  26. of the property or the parameter.
  27. @InjectParams
  28. ~~~~~~~~~~~~~~~
  29. This marks the parameters of a method for injection:
  30. .. code-block :: php
  31. <?php
  32. use JMS\DiExtraBundle\Annotation\Inject;
  33. use JMS\DiExtraBundle\Annotation\InjectParams;
  34. use JMS\DiExtraBundle\Annotation\Service;
  35. /**
  36. * @Service
  37. */
  38. class Listener
  39. {
  40. /**
  41. * @InjectParams({
  42. * "em" = @Inject("doctrine.entity_manager")
  43. * })
  44. */
  45. public function __construct(EntityManager $em, Session $session)
  46. {
  47. // ...
  48. }
  49. }
  50. If you don't define all parameters in the param map, we will try to guess which services
  51. should be injected into the remaining parameters based on their name.
  52. @Service
  53. ~~~~~~~~
  54. Marks a class as service:
  55. .. code-block :: php
  56. <?php
  57. use JMS\DiExtraBundle\Annotation\Service;
  58. /**
  59. * @Service("some.service.id", parent="another.service.id", public=false)
  60. */
  61. class Listener
  62. {
  63. }
  64. If you do not explicitly define a service id, then we will generated a sensible default
  65. based on the fully qualified class name for you.
  66. @Tag
  67. ~~~~
  68. Adds a tag to the service:
  69. .. code-block :: php
  70. <?php
  71. use JMS\DiExtraBundle\Annotation\Service;
  72. use JMS\DiExtraBundle\Annotation\Tag;
  73. /**
  74. * @Service
  75. * @Tag("doctrine.event_listener", attributes = {"event" = "postGenerateSchema", lazy=true})
  76. */
  77. class Listener
  78. {
  79. // ...
  80. }
  81. @Observe
  82. ~~~~~~~~
  83. Automatically registers a method as listener to a certain event:
  84. .. code-block :: php
  85. <?php
  86. use JMS\DiExtraBundle\Annotation\Observe;
  87. use JMS\DiExtraBundle\Annotation\Service;
  88. /**
  89. * @Service
  90. */
  91. class RequestListener
  92. {
  93. /**
  94. * @Observe("kernel.request", priority = 255)
  95. */
  96. public function onKernelRequest()
  97. {
  98. // ...
  99. }
  100. }
  101. @Validator
  102. ~~~~~~~~~~
  103. Automatically registers the given class as constraint validator for the Validator component:
  104. .. code-block :: php
  105. <?php
  106. use JMS\DiExtraBundle\Annotation\Validator;
  107. use Symfony\Component\Validator\Constraint;
  108. use Symfony\Component\Validator\ConstraintValidator;
  109. /**
  110. * @Validator("my_alias")
  111. */
  112. class MyValidator extends ConstraintValidator
  113. {
  114. // ...
  115. }
  116. class MyConstraint extends Constraint
  117. {
  118. // ...
  119. public function validatedBy()
  120. {
  121. return 'my_alias';
  122. }
  123. }
  124. The @Validator annotation also implies the @Service annotation if you do not specify it explicitly.
  125. The alias which is passed to the @Validator annotation must match the string that is returned from
  126. the ``validatedBy`` method of your constraint.
  127. @FormType
  128. ~~~~~~~~~
  129. Automatically, registers the given class as a form type with Symfony2's Form Component.
  130. .. code-block :: php
  131. <?php
  132. use JMS\DiExtraBundle\Annotation\FormType;
  133. use Symfony\Component\Form\AbstractType;
  134. /**
  135. * @FormType
  136. */
  137. class MyFormType extends AbstractType
  138. {
  139. // ...
  140. public function getName()
  141. {
  142. return 'my_form';
  143. }
  144. }
  145. // Controller.php
  146. $form = $this->formFactory->create('my_form');
  147. .. note ::
  148. ``@FormType`` implies ``@Service`` if not explicitly defined.
  149. @DoctrineListener
  150. ~~~~~~~~~~~~~~~~~
  151. Automatically, registers the given class as a listener with the Doctrine ORM:
  152. .. code-block :: php
  153. <?php
  154. use JMS\DiExtraBundle\Annotation\DoctrineListener;
  155. /**
  156. * @DoctrineListener(
  157. * events = {"prePersist", "preUpdate"},
  158. * connection = "default",
  159. * lazy = true,
  160. * priority = 0,
  161. * )
  162. class MyListener
  163. {
  164. // ...
  165. }
  166. .. note ::
  167. ``@DoctrineListener`` implies ``@Service`` if not explicitly defined.