123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 |
- Annotations
- -----------
-
- @Inject
- ~~~~~~~~~
- This marks a property, or parameter for injection:
-
- .. code-block :: php
-
- <?php
-
- use JMS\DiExtraBundle\Annotation\Inject;
-
- class Controller
- {
- /**
- * @Inject("security.context", required = false)
- */
- private $securityContext;
-
- /**
- * @Inject("%kernel.cache_dir%")
- */
- private $cacheDir;
-
- /**
- * @Inject
- */
- private $session;
- }
-
- .. tip ::
-
- If you do not specify the service explicitly, we will try to guess it based on the name
- of the property or the parameter.
-
- @InjectParams
- ~~~~~~~~~~~~~~~
- This marks the parameters of a method for injection:
-
- .. code-block :: php
-
- <?php
-
- use JMS\DiExtraBundle\Annotation\Inject;
- use JMS\DiExtraBundle\Annotation\InjectParams;
- use JMS\DiExtraBundle\Annotation\Service;
-
- /**
- * @Service
- */
- class Listener
- {
- /**
- * @InjectParams({
- * "em" = @Inject("doctrine.entity_manager")
- * })
- */
- public function __construct(EntityManager $em, Session $session)
- {
- // ...
- }
- }
-
- If you don't define all parameters in the param map, we will try to guess which services
- should be injected into the remaining parameters based on their name.
-
- @Service
- ~~~~~~~~
- Marks a class as service:
-
- .. code-block :: php
-
- <?php
-
- use JMS\DiExtraBundle\Annotation\Service;
-
- /**
- * @Service("some.service.id", parent="another.service.id", public=false)
- */
- class Listener
- {
- }
-
- If you do not explicitly define a service id, then we will generated a sensible default
- based on the fully qualified class name for you.
-
- @Tag
- ~~~~
- Adds a tag to the service:
-
- .. code-block :: php
-
- <?php
-
- use JMS\DiExtraBundle\Annotation\Service;
- use JMS\DiExtraBundle\Annotation\Tag;
-
- /**
- * @Service
- * @Tag("doctrine.event_listener", attributes = {"event" = "postGenerateSchema", lazy=true})
- */
- class Listener
- {
- // ...
- }
-
- @Observe
- ~~~~~~~~
- Automatically registers a method as listener to a certain event:
-
- .. code-block :: php
-
- <?php
-
- use JMS\DiExtraBundle\Annotation\Observe;
- use JMS\DiExtraBundle\Annotation\Service;
-
- /**
- * @Service
- */
- class RequestListener
- {
- /**
- * @Observe("kernel.request", priority = 255)
- */
- public function onKernelRequest()
- {
- // ...
- }
- }
-
- @Validator
- ~~~~~~~~~~
- Automatically registers the given class as constraint validator for the Validator component:
-
- .. code-block :: php
-
- <?php
-
- use JMS\DiExtraBundle\Annotation\Validator;
- use Symfony\Component\Validator\Constraint;
- use Symfony\Component\Validator\ConstraintValidator;
-
- /**
- * @Validator("my_alias")
- */
- class MyValidator extends ConstraintValidator
- {
- // ...
- }
-
- class MyConstraint extends Constraint
- {
- // ...
- public function validatedBy()
- {
- return 'my_alias';
- }
- }
-
- The @Validator annotation also implies the @Service annotation if you do not specify it explicitly.
- The alias which is passed to the @Validator annotation must match the string that is returned from
- the ``validatedBy`` method of your constraint.
-
- @FormType
- ~~~~~~~~~
- Automatically, registers the given class as a form type with Symfony2's Form Component.
-
- .. code-block :: php
-
- <?php
-
- use JMS\DiExtraBundle\Annotation\FormType;
- use Symfony\Component\Form\AbstractType;
-
- /**
- * @FormType
- */
- class MyFormType extends AbstractType
- {
- // ...
-
- public function getName()
- {
- return 'my_form';
- }
- }
-
- // Controller.php
- $form = $this->formFactory->create('my_form');
-
- .. note ::
-
- ``@FormType`` implies ``@Service`` if not explicitly defined.
-
- @DoctrineListener
- ~~~~~~~~~~~~~~~~~
- Automatically, registers the given class as a listener with the Doctrine ORM:
-
- .. code-block :: php
-
- <?php
-
- use JMS\DiExtraBundle\Annotation\DoctrineListener;
-
- /**
- * @DoctrineListener(
- * events = {"prePersist", "preUpdate"},
- * connection = "default",
- * lazy = true,
- * priority = 0,
- * )
- class MyListener
- {
- // ...
- }
-
- .. note ::
-
- ``@DoctrineListener`` implies ``@Service`` if not explicitly defined.
-
|