@@ -744,15 +744,75 @@ Injecting the dependency by the setter method just needs a change of syntax:
744
744
and "setter injection". The Symfony service container also supports
745
745
"property injection".
746
746
747
- Making References optional
747
+ Making References Optional
748
748
--------------------------
749
749
750
750
Sometimes, one of your services may have an optional dependency, meaning
751
751
that the dependency is not required for your service to work properly. In
752
752
the example above, the ``app.mailer `` service *must * exist, otherwise an exception
753
753
will be thrown. By modifying the ``app.newsletter_manager `` service definition,
754
- you can make this reference optional. The container will then inject it if
755
- it exists and do nothing if it doesn't:
754
+ you can make this reference optional, there are two strategies for doing this.
755
+
756
+ Setting Missing Dependencies to null
757
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
758
+
759
+ You can use the ``null `` strategy to explicitly set the argument to ``null ``
760
+ if the service does not exist:
761
+
762
+ .. configuration-block ::
763
+
764
+ .. code-block :: xml
765
+
766
+ <!-- app/config/services.xml -->
767
+ <?xml version =" 1.0" encoding =" UTF-8" ?>
768
+ <container xmlns =" http://symfony.com/schema/dic/services"
769
+ xmlns : xsi =" http://www.w3.org/2001/XMLSchema-instance"
770
+ xsi : schemaLocation =" http://symfony.com/schema/dic/services
771
+ http://symfony.com/schema/dic/services/services-1.0.xsd" >
772
+
773
+ <services >
774
+ <service id =" app.mailer" >
775
+ <!-- ... -->
776
+ </service >
777
+
778
+ <service id =" app.newsletter_manager" class =" AppBundle\Newsletter\NewsletterManager" >
779
+ <argument type =" service" id =" app.mailer" on-invalid =" null" />
780
+ </service >
781
+ </services >
782
+ </container >
783
+
784
+ .. code-block :: php
785
+
786
+ // app/config/services.php
787
+ use Symfony\Component\DependencyInjection\Definition;
788
+ use Symfony\Component\DependencyInjection\Reference;
789
+ use Symfony\Component\DependencyInjection\ContainerInterface;
790
+
791
+ $container->setDefinition('app.mailer', ...);
792
+
793
+ $container->setDefinition('app.newsletter_manager', new Definition(
794
+ 'AppBundle\Newsletter\NewsletterManager',
795
+ array(
796
+ new Reference(
797
+ 'app.mailer',
798
+ ContainerInterface::NULL_ON_INVALID_REFERENCE
799
+ )
800
+ )
801
+ ));
802
+
803
+ .. note ::
804
+
805
+ The "null" strategy is not currently supported by the YAML driver.
806
+
807
+ Ignoring Missing Dependencies
808
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
809
+
810
+ The behavior of ignoring missing dependencies is the same as the "null"
811
+ behavior except when used within a method call, in which case the method call
812
+ itself will be removed.
813
+
814
+ In the following example the container will inject a service using a method
815
+ call if the service exists and remove the method call if it does not:
756
816
757
817
.. configuration-block ::
758
818
@@ -779,7 +839,9 @@ it exists and do nothing if it doesn't:
779
839
</service >
780
840
781
841
<service id =" app.newsletter_manager" class =" AppBundle\Newsletter\NewsletterManager" >
782
- <argument type =" service" id =" app.mailer" on-invalid =" ignore" />
842
+ <call method =" setMailer" >
843
+ <argument type =" service" id =" my_mailer" on-invalid =" ignore" />
844
+ </call >
783
845
</service >
784
846
</services >
785
847
</container >
@@ -794,13 +856,12 @@ it exists and do nothing if it doesn't:
794
856
$container->setDefinition('app.mailer', ...);
795
857
796
858
$container->setDefinition('app.newsletter_manager', new Definition(
797
- 'AppBundle\Newsletter\NewsletterManager',
798
- array(
799
- new Reference(
800
- 'app.mailer',
801
- ContainerInterface::IGNORE_ON_INVALID_REFERENCE
802
- )
803
- )
859
+ 'AppBundle\Newsletter\NewsletterManager'
860
+ ))->addMethodCall('setMailer', array(
861
+ new Reference(
862
+ 'my_mailer',
863
+ ContainerInterface::IGNORE_ON_INVALID_REFERENCE
864
+ ),
804
865
));
805
866
806
867
In YAML, the special ``@? `` syntax tells the service container that the dependency
0 commit comments